3153: when deriving unique name, look for all can_login links, not just the vm_uuid...
[arvados.git] / services / api / test / unit / user_test.rb
index c9be478afb607d0fd5c401a19b47cfebde44a01f..c410787a116b30524f0254ee3a7a3c8694cfc3ef 100644 (file)
@@ -154,15 +154,15 @@ class UserTest < ActiveSupport::TestCase
 
     [false, [], [], '^^incorrect_format@example.com', true, true, false],
 
-    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'foo@example.com', true, true, true],  # existing repository name 'foo'
-    [true, 'active-notify@example.com', 'inactive-notify@example.com', 'foo@example.com', true, false, true],  # existing repository name 'foo'
-    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'foo@example.com', false, true, true],  # existing repository name 'foo'
-    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'foo@example.com', false, false, true],  # existing repository name 'foo', but we are not creating repo or login link
+    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', true, true, true],  # existing repository name 'auto_setup_repo'
+    [true, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', true, false, true],  # existing repository name 'auto_setup_repo'
+    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', false, true, true],  # existing repository name 'auto_setup_repo'
+    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', false, false, true],  # existing repository name 'auto_setup_repo', but we are not creating repo or login link
 
-    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'xyz_can_login_to_vm@example.com', true, true, true], # existing vm login name
-    [true, 'active-notify@example.com', 'inactive-notify@example.com', 'xyz_can_login_to_vm@example.com', true, false, true], # existing vm login name
-    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'xyz_can_login_to_vm@example.com', false, true, true], # existing vm login name
-    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'xyz_can_login_to_vm@example.com', false, false, true], # existing vm login name, but we are not creating repo or login link
+    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', true, true, true], # existing vm login name
+    [true, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', true, false, true], # existing vm login name
+    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', false, true, true], # existing vm login name
+    [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', false, false, true], # existing vm login name, but we are not creating repo or login link
 
     [true, 'active-notify@example.com', 'inactive-notify@example.com', '*!*@example.com', true, false, false], # username is invalid format
     [false, 'active-notify@example.com', 'inactive-notify@example.com', '*!*@example.com', false, false, true], # since no repo and vm login, username is ok (not validated)
@@ -479,15 +479,8 @@ class UserTest < ActiveSupport::TestCase
 
       username = user.email.partition('@')[0] if email
 
-      # check vm uuid
-      vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
-      if vm_uuid
-        verify_link_exists true, vm_uuid, user.uuid, 'permission', 'can_login', 'username', username
-      else
-        verify_link_exists false, vm_uuid, user.uuid, 'permission', 'can_login', 'username', username
-      end
-
       # check repo
+      repo_names = []
       if Rails.configuration.auto_setup_new_users_with_repository
         repos = Repository.where('name like ?', "%#{username}%")
         assert_not_nil repos, 'repository not found'
@@ -495,9 +488,33 @@ class UserTest < ActiveSupport::TestCase
         repo_uuids = []
         repos.each do |repo|
           repo_uuids << repo[:uuid]
+          repo_names << repo[:name]
+        end
+        if username == 'auto_setup_repo'
+          begin
+            repo_names.delete('auto_setup_repo')
+          ensure
+            assert_equal true, repo_names.any?, 'Repository name for username foo is not unique'
+          end
         end
         verify_link_exists true, repo_uuids, user.uuid, 'permission', 'can_manage', nil, nil
       end
+
+      # if username is existing vm login name, make sure the username used to generate any repo is unique
+      if username == 'auto_setup_vm_login'
+        if repo_names.any?
+          assert repo_names.first.start_with? 'auto_setup_vm_login'
+          assert_not_nil /\d$/.match(repo_names.first)
+        end
+      end
+
+      # check vm uuid
+      vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
+      if vm_uuid
+        verify_link_exists true, vm_uuid, user.uuid, 'permission', 'can_login', 'username', (username == 'auto_setup_repo' ? repo_names.first : username)
+      else
+        verify_link_exists false, vm_uuid, user.uuid, 'permission', 'can_login', 'username', (username == 'auto_setup_repo' ? repo_names.first : username)
+      end
     end
 
     # check email notifications
@@ -506,7 +523,7 @@ class UserTest < ActiveSupport::TestCase
 
     new_user_email_subject = "#{Rails.configuration.email_subject_prefix}New user created notification"
     if Rails.configuration.auto_setup_new_users
-      new_user_email_subject = valid_username ? "#{Rails.configuration.email_subject_prefix}New user created and setup notification" : 
+      new_user_email_subject = valid_username ? "#{Rails.configuration.email_subject_prefix}New user created and setup notification" :
                                                 "#{Rails.configuration.email_subject_prefix}New user created, but not setup notification"
     end