Update the functional tests to use the setup method
authorradhika chippada <radhika@radhika.curoverse>
Thu, 20 Mar 2014 21:03:31 +0000 (17:03 -0400)
committerradhika chippada <radhika@radhika.curoverse>
Thu, 20 Mar 2014 21:03:31 +0000 (17:03 -0400)
services/api/app/controllers/arvados/v1/users_controller.rb
services/api/test/functional/arvados/v1/users_controller_test.rb

index 1233e74e61283fc850f63bfc5af547dd5468a161..92b84f46acd46541711f04fb30ac2f479794f0dd 100644 (file)
@@ -90,8 +90,8 @@ class Arvados::V1::UsersController < ApplicationController
 
   # create user object and all the needed links
   def setup
-puts "\n*************PARAMS = #{params.inspect}"
-    if params[:openid_prefix]   # check if default openid_prefix needs to be overridden
+    # check if default openid_prefix needs to be overridden
+    if params[:openid_prefix]
       openid_prefix = params[:openid_prefix]
     else 
       openid_prefix = Rails.configuration.openid_prefix
@@ -100,93 +100,14 @@ puts "\n*************PARAMS = #{params.inspect}"
 
     @object = model_class.new resource_attrs
 
-    # If user_param is passed, lookup for user. If exists, skip create and only create any missing links. 
-    if params[:user_param]
-      begin
-        @object_found = find_user_from_input params[:user_param], params[:user_param]
-      end
-      if !@object_found
-        @object = User.new    # when user_param is used, it will be used as user object
-        @object[:email] = params[:user_param]       
-        need_to_create = true
-      else
-        @object = @object_found
-      end
-    else    # need to create user for the given user data
-      @object_found = find_user_from_input @object[:uuid], @object[:email]
-      if !@object_found
-        need_to_create = true   # use the user object sent in to create with the user
-      else
-        @object = @object_found
-      end
-    end
+    # Lookup for user. If exists, only create any missing links
+    @object_found = find_user_from_input 
 
-    # create if need be, and then create or update the links as needed 
-    if need_to_create
-      if @object.save
-        oid_login_perm = Link.where(tail_uuid: @object[:email],
-                                    head_kind: 'arvados#user',
-                                    link_class: 'permission',
-                                    name: 'can_login')
-
-        if [] == oid_login_perm
-          # create openid login permission
-          oid_login_perm = Link.create(link_class: 'permission',
-                                       name: 'can_login',
-                                       tail_kind: 'email',
-                                       tail_uuid: @object[:email],
-                                       head_kind: 'arvados#user',
-                                       head_uuid: @object[:uuid],
-                                       properties: login_perm_props
-                                      )
-          logger.info { "openid login permission: " + oid_login_perm[:uuid] }
-        end
-      else
-        raise "Save failed"
+    if !@object_found
+      if !@object[:email]
+        raise "No email found in the input. Aborting user creation."
       end
-    end
 
-    # create links
-    create_user_repo_link params[:repo_name]
-    create_vm_login_permission_link params[:vm_uuid], params[:repo_name]
-    create_user_group_link 
-
-    show  end
-
-  # create user object and all the needed links
-  def create
-    if params[:openid_prefix]   # check if default openid_prefix needs to be overridden
-      openid_prefix = params[:openid_prefix]
-    else 
-      openid_prefix = Rails.configuration.openid_prefix
-    end
-    login_perm_props = {identity_url_prefix: openid_prefix}
-
-    @object = model_class.new resource_attrs
-
-    # If user_param is passed, lookup for user. If exists, skip create and only create any missing links. 
-    if params[:user_param]
-      begin
-        @object_found = find_user_from_input params[:user_param], params[:user_param]
-      end
-      if !@object_found
-        @object = User.new    # when user_param is used, it will be used as user object
-        @object[:email] = params[:user_param]       
-        need_to_create = true
-      else
-        @object = @object_found
-      end
-    else    # need to create user for the given user data
-      @object_found = find_user_from_input @object[:uuid], @object[:email]
-      if !@object_found
-        need_to_create = true   # use the user object sent in to create with the user
-      else
-        @object = @object_found
-      end
-    end
-
-    # create if need be, and then create or update the links as needed 
-    if need_to_create
       if @object.save
         oid_login_perm = Link.where(tail_uuid: @object[:email],
                                     head_kind: 'arvados#user',
@@ -208,43 +129,41 @@ puts "\n*************PARAMS = #{params.inspect}"
       else
         raise "Save failed"
       end
+    else
+      @object = @object_found
     end
-
+    
     # create links
     create_user_repo_link params[:repo_name]
     create_vm_login_permission_link params[:vm_uuid], params[:repo_name]
     create_user_group_link 
 
-    show
+    show  
   end
 
   protected 
 
   # find the user from the given user parameters
-  def find_user_from_input(user_uuid, user_email)
-    if user_uuid
-      found_object = User.find_by_uuid user_uuid
+  def find_user_from_input
+    if @object[:uuid]
+      found_object = User.find_by_uuid @object[:uuid]
     end
 
     if !found_object
-      begin
-        if !user_email
-          return
+      if !@object[:email]
+        return
+      end
+=begin  
+        if found_objects.size > 1
+          logger.warn ("Found #{found_objects.size} users with email #{user_email}. Stop.")
+          raise ArgumentError.new "Found #{found_objects.size} users with email #{user_email}. Stop."
+        elsif found_objects.size == 1
+          found_object = found_objects.first
         end
+=end
 
-        if !user_email.match(/\w\@\w+\.\w+/)
-          logger.warn ("Given user param is not valid email format: #{user_email}")
-          raise ArgumentError.new "User param is not of valid email format. Stop"
-        else
-          found_objects = User.where('email=?', user_email)  
-          if found_objects.size > 1
-            logger.warn ("Found #{found_objects.size} users with email #{user_email}. Stop.")
-            raise ArgumentError.new "Found #{found_objects.size} users with email #{user_email}. Stop."
-          elsif found_objects.size == 1
-            found_object = found_objects.first
-          end
-        end
-      end
+      found_objects = User.where('email=?', @object[:email])  
+      found_object = found_objects.first
     end
 
     return found_object
@@ -253,28 +172,30 @@ puts "\n*************PARAMS = #{params.inspect}"
   # link the repo_name passed
   def create_user_repo_link(repo_name)
     if not repo_name
-      logger.warn ("Repository name not given for #{@object[:uuid]}. Skip creating the link")
+      logger.warn ("Repository name not given for #{@object[:uuid]}.")
       return
     end
 
     # Check for an existing repository with the same name we're about to use.
-    repo = (repositories = Repository.where(name: repo_name)) != nil ? repositories.first : nil
+    repo = (repos = Repository.where(name: repo_name)) != nil ? repos.first : nil
     if repo
-      logger.warn "Repository already exists with name #{repo_name}: #{repo[:uuid]}. Will link to user."
+      logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
 
-      # Look for existing repository access (perhaps using a different repository/user name).
+      # Look for existing repository access for this repo
       repo_perms = Link.where(tail_uuid: @object[:uuid],
                               head_kind: 'arvados#repository',
                               head_uuid: repo[:uuid],
                               link_class: 'permission',
                               name: 'can_write')
       if [] != repo_perms
-        logger.warn "User already has repository access " + repo_perms.collect { |p| p[:uuid] }.inspect
+        logger.warn "User already has repository access " + 
+            repo_perms.collect { |p| p[:uuid] }.inspect
         return
       end
     end
 
-    repo ||= Repository.create(name: repo_name)   # create repo, if does not already exist
+    # create repo, if does not already exist
+    repo ||= Repository.create(name: repo_name)
     logger.info { "repo uuid: " + repo[:uuid] }
 
     repo_perm = Link.create(tail_kind: 'arvados#user',
@@ -292,7 +213,7 @@ puts "\n*************PARAMS = #{params.inspect}"
     begin
       vm = (vms = VirtualMachine.where(uuid: vm_uuid)) != nil ? vms.first : nil
       if not vm
-        logger.warn "Could not look up virtual machine with uuid #{vm_uuid.inspect}"
+        logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
         return
       end
 
@@ -324,7 +245,7 @@ puts "\n*************PARAMS = #{params.inspect}"
     end.first
 
     if not group
-      logger.warn "Could not look up the 'All users' group with uuid '*-*-fffffffffffffff'. Skip."
+      logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
       return
     else
       logger.info { "\"All users\" group uuid: " + group[:uuid] }
index 923949acf0aa2cb1c1d948dcd42a67f2512f7af2..d4e25ddbdbf841a58128f5a7e42a39181b95bdd9 100644 (file)
@@ -69,7 +69,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
       repo_name: repo_name,
       vm_uuid: 'no_such_vm',
       user: {
-        uuid: "is_this_correct",        
+        uuid: "this_is_agreeable",        
         first_name: "in_create_test_first_name",
         last_name: "test_last_name",
         email: "test@abc.com"
@@ -79,37 +79,40 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     created = JSON.parse(@response.body)
     assert_equal 'in_create_test_first_name', created['first_name']
     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
-    assert_equal 'is_this_correct', created['uuid']
+    assert_equal 'this_is_agreeable', created['uuid']
     assert_not_nil created['email'], 'since email was given, expected non-nil email'
-    assert_nil created['identity_url'], 'even though email isprovided, expected no identity_url since users_controller only creates user
+    assert_nil created['identity_url'], 'expected no identity_url
 
-    # since no such vm exists, expect only three new links: oid_login_perm, repo link and link add user to 'All users' group
+    # since no such vm exists, expect only three new links: 
+    # oid_login_perm, repo link and link add user to 'All users' group
     verify_num_links @all_links_at_start, 3
 
-    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
+    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
+        created['email'], 'arvados#user', false
 
-    verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, created['uuid'], 'arvados#repository', true
+    verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, 
+        created['uuid'], 'arvados#repository', true
 
-    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
+    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
+        created['uuid'], 'arvados#group', true
   end
 
-  test "create user with user_param, vm and repo as input" do
+  test "create user with bogus uuid, vm and repo as input" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'not_an_existing_uuid_and_not_email_format',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {uuid: 'not_an_existing_uuid_and_not_email_format'}
     }
     response_body = JSON.parse(@response.body)
     response_errors = response_body['errors']
     assert_not_nil response_errors, 'Expected error in response'
-    incorrectly_formatted = response_errors.first.include?('ArgumentError: User param is not of valid email format')
+    incorrectly_formatted = response_errors.first.include?('No email found in the input')
     assert incorrectly_formatted, 'Expected not valid email format error'
   end
 
-  test "create user with existing uuid user_param, vm and repo as input" do
+  test "create user with existing uuid, vm and repo as input" do
     authorize_with :inactive
     get :current
     assert_response :success
@@ -118,10 +121,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
 
     post :setup, {
-      user_param: inactive_user['uuid'],
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {uuid: inactive_user['uuid']}
     }
 
     assert_response :success
@@ -131,14 +133,13 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal inactive_user['email'], response_object['email'], 'expecting inactive user email'
   end
 
-  test "create user with valid email user_param, vm and repo as input" do
+  test "create user with valid email, vm and repo as input" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc@xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {email: 'abc@xyz.com'}
     }
 
     assert_response :success
@@ -147,12 +148,11 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
   end
 
-  test "create user with valid email user_param, no vm and repo as input" do
+  test "create user with valid email, no vm and repo as input" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc@xyz.com',
-      user: {}
+      user: {email: 'abc@xyz.com'}
     }
 
     assert_response :success    
@@ -161,34 +161,32 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
   end
 
-  test "create user with user_param and non-empty user which will be ignored" do
+  test "create user with email, first name, repo name and vm uuid" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc@xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
       user: {
         first_name: 'test_first_name',
-        email: 'will_be_ignored@xyz.com'
+        email: 'abc@xyz.com'
       }
     }
 
     assert_response :success
     response_object = JSON.parse(@response.body)
     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
-    assert_equal response_object['email'], 'abc@xyz.com', 'expecting user_param as email'
-    assert_nil response_object['first_name'], 'expecting no first name since it will be reset when user_param is used'
+    assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
+    assert_equal 'test_first_name', response_object['first_name'], 'expecting first name'
   end
 
   test "create user twice with user param and check links are not recreated" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc@xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {email: 'abc@xyz.com'}
     }
 
     assert_response :success
@@ -207,7 +205,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
 
     assert_response :success
     response_object2 = JSON.parse(@response.body)
-    assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
+    assert_equal response_object['uuid'], response_object2['uuid'], 
+        'expected same uuid as first create operation'
     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
   end
@@ -229,13 +228,14 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
 
     # create again
-    post :create, user: {
+    post :setup, user: {
       email: 'abc@xyz.com'
     }
 
     assert_response :success
     response_object2 = JSON.parse(@response.body)
-    assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
+    assert_equal response_object['uuid'], response_object2['uuid'], 
+        'expected same uuid as first create operation'
     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
   end
@@ -258,17 +258,21 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal 'in_create_test_first_name', created['first_name']
     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
     assert_not_nil created['email'], 'since email was given, expected non-nil email'
-    assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user
+    assert_nil created['identity_url'], 'expected no identity_url
 
     # verify links
-    # expect 3 new links: oid_login_perm, repo link, and link add user to 'All users' group. No vm link since the vm_uuid passed in is not in system
+    # expect 3 new links: oid_login_perm, repo link, and link add user to 'All users' group. 
+    # No vm link since the vm_uuid passed in is not in system
     verify_num_links @all_links_at_start, 3
 
-    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
+    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
+        created['email'], 'arvados#user', false
 
-    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
+    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', 
+        created['uuid'], 'arvados#repository', true
 
-    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
+    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
+      created['uuid'], 'arvados#group', true
   end
 
   test "create user with user, vm and repo and verify links" do
@@ -288,18 +292,22 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal 'in_create_test_first_name', created['first_name']
     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
     assert_not_nil created['email'], 'since email was given, expected non-nil email'
-    assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user
+    assert_nil created['identity_url'], 'expected no identity_url
 
     # expect 4 new links: oid_login_perm, repo link, vm link and link add user to 'All users' group. 
     verify_num_links @all_links_at_start, 4
 
-    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
+    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
+        created['email'], 'arvados#user', false
 
-    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
+    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', 
+        created['uuid'], 'arvados#repository', true
 
-    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
+    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
+        created['uuid'], 'arvados#group', true
 
-    verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
+    verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', 
+        @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
   end
 
   def verify_num_links (original_links, expected_num_additional_links)
@@ -308,7 +316,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
               "Expected #{expected_num_additional_links.inspect} more links"
   end
 
-  def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, head_kind, fetch_object)
+  def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, 
+        head_kind, fetch_object)
     if fetch_object
       object = Object.const_get(class_name).where(name: head_uuid)
       assert [] != object, "expected a #{class_name.inspect} with the name #{head_uuid.inspect}"
@@ -320,7 +329,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
                        tail_uuid: tail_uuid,
                        head_uuid: head_uuid,
                        head_kind: head_kind)
-    assert links.size > 0, "expected one or more links with the given criteria #{class_name} with #{head_uuid}"
+    assert links.size > 0, "expected one or more links for #{class_name} with #{head_uuid}"
   end
 
 end