1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class UserTest < ActiveSupport::TestCase
8 include CurrentApiClient
10 # The fixture services/api/test/fixtures/users.yml serves as the input for this test case
12 # Make sure system_user exists before making "pre-test users" list
16 %w(a aa a0 aA Aa AA A0).each do |username|
17 test "#{username.inspect} is a valid username" do
18 user = User.new(username: username)
23 test "username is not required" do
24 user = User.new(username: nil)
28 test "username beginning with numeral is invalid" do
29 user = User.new(username: "0a")
33 "\\.-_/!@#$%^&*()[]{}".each_char do |bad_char|
34 test "username containing #{bad_char.inspect} is invalid" do
35 user = User.new(username: "bad#{bad_char}username")
40 test "username must be unique" do
41 user = User.new(username: users(:active).username)
45 test "non-admin can't update username" do
46 set_user_from_auth :rominiadmin
47 user = User.find_by_uuid(users(:rominiadmin).uuid)
48 user.username = "selfupdate"
49 assert_not_allowed { user.save }
52 def check_admin_username_change(fixture_name)
53 set_user_from_auth :admin_trustedclient
54 user = User.find_by_uuid(users(fixture_name).uuid)
55 user.username = "newnamefromtest"
59 test "admin can set username" do
60 check_admin_username_change(:active_no_prefs)
63 test "admin can update username" do
64 check_admin_username_change(:active)
67 test "admin can update own username" do
68 check_admin_username_change(:admin)
71 def check_new_username_setting(email_name, expect_name)
72 set_user_from_auth :admin
73 user = User.create!(email: "#{email_name}@example.org")
74 assert_equal(expect_name, user.username)
77 test "new username set from e-mail" do
78 check_new_username_setting("dakota", "dakota")
81 test "new username set from e-mail with leading digits" do
82 check_new_username_setting("1dakota9", "dakota9")
85 test "new username set from e-mail with punctuation" do
86 check_new_username_setting("dakota.9", "dakota9")
89 test "new username set from e-mail with leading digits and punctuation" do
90 check_new_username_setting("1.dakota.z", "dakotaz")
93 test "new username set from e-mail with extra part" do
94 check_new_username_setting("dakota+arvados", "dakota")
97 test "new username set with deduplication" do
98 name = users(:active).username
99 check_new_username_setting(name, "#{name}2")
100 check_new_username_setting(name, "#{name}3")
101 # Insert some out-of-order conflicts, to ensure our "sort by
102 # username, stop when we see a hole" strategy doesn't depend on
104 check_new_username_setting("#{name}13", "#{name}13")
105 check_new_username_setting("#{name}5", "#{name}5")
106 check_new_username_setting(name, "#{name}4")
107 6.upto(12).each do |n|
108 check_new_username_setting(name, "#{name}#{n}")
112 test "new username set avoiding blacklist" do
113 Rails.configuration.Users.AutoSetupUsernameBlacklist = ConfigLoader.to_OrderedOptions({"root"=>{}})
114 check_new_username_setting("root", "root2")
117 test "no username set when no base available" do
118 check_new_username_setting("_", nil)
121 test "updating username updates repository names" do
122 set_user_from_auth :admin
123 user = users(:active)
124 user.username = "newtestname"
125 assert(user.save, "username update failed")
126 {foo: "newtestname/foo", repository2: "newtestname/foo2"}.
127 each_pair do |repo_sym, expect_name|
128 assert_equal(expect_name, repositories(repo_sym).name)
132 test "admin can clear username when user owns no repositories" do
133 set_user_from_auth :admin
134 user = users(:spectator)
137 assert_nil(user.username)
140 test "admin can't clear username when user owns repositories" do
141 set_user_from_auth :admin
142 user = users(:active)
144 assert_not_allowed { user.save }
145 refute_empty(user.errors[:username])
148 test "failed username update doesn't change repository names" do
149 set_user_from_auth :admin
150 user = users(:active)
151 user.username = users(:fuse).username
152 assert_not_allowed { user.save }
153 assert_equal("active/foo", repositories(:foo).name)
156 [[false, 'foo@example.com', true, false],
157 [false, 'bar@example.com', false, true],
158 [true, 'foo@example.com', true, false],
159 [true, 'bar@example.com', true, true],
160 [false, '', false, false],
161 [true, '', true, false]
162 ].each do |auto_admin_first_user_config, auto_admin_user_config, foo_should_be_admin, bar_should_be_admin|
163 # In each case, 'foo' is created first, then 'bar', then 'bar2', then 'baz'.
164 test "auto admin with auto_admin_first=#{auto_admin_first_user_config} auto_admin=#{auto_admin_user_config}" do
166 if auto_admin_first_user_config
167 # This test requires no admin users exist (except for the system user)
168 act_as_system_user do
169 users(:admin).update!(is_admin: false)
171 @all_users = User.where("uuid not like '%-000000000000000'").where(:is_admin => true)
172 assert_equal 0, @all_users.count, "No admin users should exist (except for the system user)"
175 Rails.configuration.Users.AutoAdminFirstUser = auto_admin_first_user_config
176 Rails.configuration.Users.AutoAdminUserWithEmail = auto_admin_user_config
178 # See if the foo user has is_admin
180 foo.first_name = 'foo'
181 foo.email = 'foo@example.com'
183 act_as_system_user do
187 foo = User.find(foo.id) # get the user back
188 assert_equal foo_should_be_admin, foo.is_admin, "is_admin is wrong for user foo"
189 assert_equal 'foo', foo.first_name
191 # See if the bar user has is_admin
193 bar.first_name = 'bar'
194 bar.email = 'bar@example.com'
196 act_as_system_user do
200 bar = User.find(bar.id) # get the user back
201 assert_equal bar_should_be_admin, bar.is_admin, "is_admin is wrong for user bar"
202 assert_equal 'bar', bar.first_name
204 # A subsequent user with the bar@example.com address should never be
207 bar2.first_name = 'bar2'
208 bar2.email = 'bar@example.com'
210 act_as_system_user do
214 bar2 = User.find(bar2.id) # get the user back
215 assert !bar2.is_admin, "is_admin is wrong for user bar2"
216 assert_equal 'bar2', bar2.first_name
218 # An ordinary new user should not be elevated to admin
220 baz.first_name = 'baz'
221 baz.email = 'baz@example.com'
223 act_as_system_user do
227 baz = User.find(baz.id) # get the user back
229 assert_equal 'baz', baz.first_name
234 test "check non-admin active user properties" do
235 @active_user = users(:active) # get the active user
236 assert !@active_user.is_admin, 'is_admin should not be set for a non-admin user'
237 assert @active_user.is_active, 'user should be active'
238 assert @active_user.is_invited, 'is_invited should be set'
239 assert_not_nil @active_user.prefs, "user's preferences should be non-null, but may be size zero"
240 assert (@active_user.can? :read=>"#{@active_user.uuid}"), "user should be able to read own object"
241 assert (@active_user.can? :write=>"#{@active_user.uuid}"), "user should be able to write own object"
242 assert (@active_user.can? :manage=>"#{@active_user.uuid}"), "user should be able to manage own object"
244 assert @active_user.groups_i_can(:read).size > 0, "active user should be able read at least one group"
246 # non-admin user cannot manage or write other user objects
247 @uninvited_user = users(:inactive_uninvited) # get the uninvited user
248 assert !(@active_user.can? :read=>"#{@uninvited_user.uuid}")
249 assert !(@active_user.can? :write=>"#{@uninvited_user.uuid}")
250 assert !(@active_user.can? :manage=>"#{@uninvited_user.uuid}")
253 test "check admin user properties" do
254 @admin_user = users(:admin) # get the admin user
255 assert @admin_user.is_admin, 'is_admin should be set for admin user'
256 assert @admin_user.is_active, 'admin user cannot be inactive'
257 assert @admin_user.is_invited, 'is_invited should be set'
258 assert_not_nil @admin_user.uuid.size, "user's uuid should be non-null"
259 assert_not_nil @admin_user.prefs, "user's preferences should be non-null, but may be size zero"
260 assert @admin_user.identity_url.size > 0, "user's identity url is expected"
261 assert @admin_user.can? :read=>"#{@admin_user.uuid}"
262 assert @admin_user.can? :write=>"#{@admin_user.uuid}"
263 assert @admin_user.can? :manage=>"#{@admin_user.uuid}"
265 assert @admin_user.groups_i_can(:read).size > 0, "admin active user should be able read at least one group"
266 assert @admin_user.groups_i_can(:write).size > 0, "admin active user should be able write to at least one group"
267 assert @admin_user.groups_i_can(:manage).size > 0, "admin active user should be able manage at least one group"
269 # admin user can also write or manage other users
270 @uninvited_user = users(:inactive_uninvited) # get the uninvited user
271 assert @admin_user.can? :read=>"#{@uninvited_user.uuid}"
272 assert @admin_user.can? :write=>"#{@uninvited_user.uuid}"
273 assert @admin_user.can? :manage=>"#{@uninvited_user.uuid}"
276 test "check inactive and uninvited user properties" do
277 @uninvited_user = users(:inactive_uninvited) # get the uninvited user
278 assert !@uninvited_user.is_admin, 'is_admin should not be set for a non-admin user'
279 assert !@uninvited_user.is_active, 'user should be inactive'
280 assert !@uninvited_user.is_invited, 'is_invited should not be set'
281 assert @uninvited_user.can? :read=>"#{@uninvited_user.uuid}"
282 assert @uninvited_user.can? :write=>"#{@uninvited_user.uuid}"
283 assert @uninvited_user.can? :manage=>"#{@uninvited_user.uuid}"
285 assert_equal(@uninvited_user.groups_i_can(:read).sort,
286 [@uninvited_user.uuid, groups(:anonymous_group).uuid].sort)
287 assert_equal(@uninvited_user.groups_i_can(:write),
288 [@uninvited_user.uuid])
289 assert_equal(@uninvited_user.groups_i_can(:manage),
290 [@uninvited_user.uuid])
293 test "find user method checks" do
294 User.all.each do |user|
295 assert_not_nil user.uuid, "non-null uuid expected for " + user.full_name
298 user = users(:active) # get the active user
300 found_user = User.find(user.id) # find a user by the row id
302 assert_equal found_user.full_name, user.first_name + ' ' + user.last_name
303 assert_equal found_user.identity_url, user.identity_url
306 test "full name should not contain spurious whitespace" do
307 set_user_from_auth :admin
309 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: 'foo@example.com' })
311 assert_equal '', user.full_name
313 user.first_name = 'John'
314 user.last_name = 'Smith'
316 assert_equal user.first_name + ' ' + user.last_name, user.full_name
319 test "create new user" do
320 set_user_from_auth :admin
322 @all_users = User.all.to_a
325 user.first_name = "first_name_for_newly_created_user"
328 # verify there is one extra user in the db now
329 assert_equal @all_users.size+1, User.all.count
331 user = User.find(user.id) # get the user back
332 assert_equal(user.first_name, 'first_name_for_newly_created_user')
333 assert_not_nil user.uuid, 'uuid should be set for newly created user'
334 assert_nil user.email, 'email should be null for newly created user, because it was not passed in'
335 assert_nil user.identity_url, 'identity_url should be null for newly created user, because it was not passed in'
337 user.first_name = 'first_name_for_newly_created_user_updated'
339 user = User.find(user.id) # get the user back
340 assert_equal(user.first_name, 'first_name_for_newly_created_user_updated')
343 active_notify_list = ConfigLoader.to_OrderedOptions({"active-notify@example.com"=>{}})
344 inactive_notify_list = ConfigLoader.to_OrderedOptions({"inactive-notify@example.com"=>{}})
345 empty_notify_list = ConfigLoader.to_OrderedOptions({})
347 test "create new user with notifications" do
348 set_user_from_auth :admin
350 Rails.configuration.Users.AutoSetupNewUsers = false
352 create_user_and_verify_setup_and_notifications true, active_notify_list, inactive_notify_list, nil, nil
353 create_user_and_verify_setup_and_notifications true, active_notify_list, empty_notify_list, nil, nil
354 create_user_and_verify_setup_and_notifications true, empty_notify_list, empty_notify_list, nil, nil
355 create_user_and_verify_setup_and_notifications false, empty_notify_list, inactive_notify_list, nil, nil
356 create_user_and_verify_setup_and_notifications false, empty_notify_list, inactive_notify_list, nil, nil
357 create_user_and_verify_setup_and_notifications false, empty_notify_list, empty_notify_list, nil, nil
361 # Easy inactive user tests.
362 [false, empty_notify_list, empty_notify_list, "inactive-none@example.com", false, false, "inactivenone"],
363 [false, empty_notify_list, empty_notify_list, "inactive-vm@example.com", true, false, "inactivevm"],
364 [false, empty_notify_list, empty_notify_list, "inactive-repo@example.com", false, true, "inactiverepo"],
365 [false, empty_notify_list, empty_notify_list, "inactive-both@example.com", true, true, "inactiveboth"],
367 # Easy active user tests.
368 [true, active_notify_list, inactive_notify_list, "active-none@example.com", false, false, "activenone"],
369 [true, active_notify_list, inactive_notify_list, "active-vm@example.com", true, false, "activevm"],
370 [true, active_notify_list, inactive_notify_list, "active-repo@example.com", false, true, "activerepo"],
371 [true, active_notify_list, inactive_notify_list, "active-both@example.com", true, true, "activeboth"],
373 # Test users with malformed e-mail addresses.
374 [false, empty_notify_list, empty_notify_list, nil, true, true, nil],
375 [false, empty_notify_list, empty_notify_list, "arvados", true, true, nil],
376 [false, empty_notify_list, empty_notify_list, "@example.com", true, true, nil],
377 [true, active_notify_list, inactive_notify_list, "*!*@example.com", true, false, nil],
378 [true, active_notify_list, inactive_notify_list, "*!*@example.com", false, false, nil],
380 # Test users with various username transformations.
381 [false, empty_notify_list, empty_notify_list, "arvados@example.com", false, false, "arvados2"],
382 [true, active_notify_list, inactive_notify_list, "arvados@example.com", false, false, "arvados2"],
383 [true, active_notify_list, inactive_notify_list, "root@example.com", true, false, "root2"],
384 [false, active_notify_list, empty_notify_list, "root@example.com", true, false, "root2"],
385 [true, active_notify_list, inactive_notify_list, "roo_t@example.com", false, true, "root2"],
386 [false, empty_notify_list, empty_notify_list, "^^incorrect_format@example.com", true, true, "incorrectformat"],
387 [true, active_notify_list, inactive_notify_list, "&4a_d9.@example.com", true, true, "ad9"],
388 [true, active_notify_list, inactive_notify_list, "&4a_d9.@example.com", false, false, "ad9"],
389 [false, active_notify_list, empty_notify_list, "&4a_d9.@example.com", true, true, "ad9"],
390 [false, active_notify_list, empty_notify_list, "&4a_d9.@example.com", false, false, "ad9"],
391 ].each do |active, new_user_recipients, inactive_recipients, email, auto_setup_vm, auto_setup_repo, expect_username|
392 test "create new user with auto setup active=#{active} email=#{email} vm=#{auto_setup_vm} repo=#{auto_setup_repo}" do
393 set_user_from_auth :admin
395 Rails.configuration.Users.AutoSetupNewUsers = true
398 Rails.configuration.Users.AutoSetupNewUsersWithVmUUID = virtual_machines(:testvm)['uuid']
400 Rails.configuration.Users.AutoSetupNewUsersWithVmUUID = ""
403 Rails.configuration.Users.AutoSetupNewUsersWithRepository = auto_setup_repo
405 create_user_and_verify_setup_and_notifications active, new_user_recipients, inactive_recipients, email, expect_username
409 test "update existing user" do
410 set_user_from_auth :active # set active user as current user
412 @active_user = users(:active) # get the active user
414 @active_user.first_name = "first_name_changed"
417 @active_user = User.find(@active_user.id) # get the user back
418 assert_equal(@active_user.first_name, 'first_name_changed')
420 # admin user also should be able to update the "active" user info
421 set_user_from_auth :admin # set admin user as current user
422 @active_user.first_name = "first_name_changed_by_admin_for_active_user"
425 @active_user = User.find(@active_user.id) # get the user back
426 assert_equal(@active_user.first_name, 'first_name_changed_by_admin_for_active_user')
429 test "delete a user and verify" do
430 @active_user = users(:active) # get the active user
431 active_user_uuid = @active_user.uuid
433 set_user_from_auth :admin
436 found_deleted_user = false
437 User.all.each do |user|
438 if user.uuid == active_user_uuid
439 found_deleted_user = true
443 assert !found_deleted_user, "found deleted user: "+active_user_uuid
447 test "create new user as non-admin user" do
448 set_user_from_auth :active
449 assert_not_allowed { User.new.save }
452 [true, false].each do |visible|
453 test "setup new user with ActivatedUsersAreVisibleToOthers=#{visible}" do
454 Rails.configuration.Users.ActivatedUsersAreVisibleToOthers = visible
455 set_user_from_auth :admin
457 email = 'foo@example.com'
459 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
461 vm = VirtualMachine.create
463 response = user.setup(repo_name: 'foo/testrepo',
466 resp_user = find_obj_in_resp response, 'User'
467 verify_user resp_user, email
469 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
470 verify_link group_perm, 'permission', 'can_write', resp_user[:uuid], groups(:all_users).uuid
472 group_perm2 = find_obj_in_resp response, 'Link', 'arvados#user'
474 verify_link group_perm2, 'permission', 'can_read', groups(:all_users).uuid, nil
476 assert_nil group_perm2
479 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
480 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
482 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
483 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
484 assert_equal("foo", vm_perm.properties["username"])
488 test "setup new user with junk in database" do
489 set_user_from_auth :admin
491 email = 'foo@example.com'
493 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
495 vm = VirtualMachine.create
497 response = user.setup(repo_name: 'foo/testrepo',
500 resp_user = find_obj_in_resp response, 'User'
501 verify_user resp_user, email
503 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
504 verify_link group_perm, 'permission', 'can_write', resp_user[:uuid], groups(:all_users).uuid
506 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
507 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
509 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
510 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
511 assert_equal("foo", vm_perm.properties["username"])
514 test "setup new user in multiple steps" do
515 set_user_from_auth :admin
517 email = 'foo@example.com'
519 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
521 response = user.setup()
523 resp_user = find_obj_in_resp response, 'User'
524 verify_user resp_user, email
526 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
527 verify_link group_perm, 'permission', 'can_write', resp_user[:uuid], groups(:all_users).uuid
529 group_perm2 = find_obj_in_resp response, 'Link', 'arvados#user'
530 verify_link group_perm2, 'permission', 'can_read', groups(:all_users).uuid, nil
532 # invoke setup again with repo_name
533 response = user.setup(repo_name: 'foo/testrepo')
534 resp_user = find_obj_in_resp response, 'User', nil
535 verify_user resp_user, email
536 assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
538 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
539 verify_link group_perm, 'permission', 'can_write', resp_user[:uuid], groups(:all_users).uuid
541 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
542 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
544 # invoke setup again with a vm_uuid
545 vm = VirtualMachine.create
547 response = user.setup(repo_name: 'foo/testrepo',
550 resp_user = find_obj_in_resp response, 'User', nil
551 verify_user resp_user, email
552 assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
554 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
555 verify_link group_perm, 'permission', 'can_write', resp_user[:uuid], groups(:all_users).uuid
557 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
558 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
560 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
561 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
562 assert_equal("foo", vm_perm.properties["username"])
565 def find_obj_in_resp (response_items, object_type, head_kind=nil)
567 response_items.each { |x|
572 if object_type == 'User'
573 if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
577 else # looking for a link
578 if ArvadosModel::resource_class_for_uuid(x['head_uuid']).andand.kind == head_kind
587 def verify_user (resp_user, email)
588 assert_not_nil resp_user, 'expected user object'
589 assert_not_nil resp_user['uuid'], 'expected user object'
590 assert_equal email, resp_user['email'], 'expected email not found'
593 def verify_link (link_object, link_class, link_name, tail_uuid, head_uuid)
594 assert_not_nil link_object, "expected link for #{link_class} #{link_name}"
595 assert_not_nil link_object[:uuid],
596 "expected non-nil uuid for link for #{link_class} #{link_name}"
597 assert_equal link_class, link_object[:link_class],
598 "expected link_class not found for #{link_class} #{link_name}"
599 assert_equal link_name, link_object[:name],
600 "expected link_name not found for #{link_class} #{link_name}"
601 assert_equal tail_uuid, link_object[:tail_uuid],
602 "expected tail_uuid not found for #{link_class} #{link_name}"
604 assert_equal head_uuid, link_object[:head_uuid],
605 "expected head_uuid not found for #{link_class} #{link_name}"
609 def create_user_and_verify_setup_and_notifications (active, new_user_recipients, inactive_recipients, email, expect_username)
610 Rails.configuration.Users.NewUserNotificationRecipients = new_user_recipients
611 Rails.configuration.Users.NewInactiveUserNotificationRecipients = inactive_recipients
613 ActionMailer::Base.deliveries = []
615 can_setup = (Rails.configuration.Users.AutoSetupNewUsers and
616 (not expect_username.nil?))
617 expect_repo_name = "#{expect_username}/#{expect_username}"
618 prior_repo = Repository.where(name: expect_repo_name).first
621 user.first_name = "first_name_for_newly_created_user"
623 user.is_active = active
625 assert_equal(expect_username, user.username)
628 verify_link_exists(Rails.configuration.Users.AutoSetupNewUsers || active,
629 groups(:all_users).uuid, user.uuid,
630 "permission", "can_write")
632 # Check for repository.
633 if named_repo = (prior_repo or
634 Repository.where(name: expect_repo_name).first)
635 verify_link_exists((can_setup and prior_repo.nil? and
636 Rails.configuration.Users.AutoSetupNewUsersWithRepository),
637 named_repo.uuid, user.uuid, "permission", "can_manage")
640 # Check for VM login.
641 if (auto_vm_uuid = Rails.configuration.Users.AutoSetupNewUsersWithVmUUID) != ""
642 verify_link_exists(can_setup, auto_vm_uuid, user.uuid,
643 "permission", "can_login", "username", expect_username)
646 # check email notifications
648 new_inactive_user_email = nil
650 new_user_email_subject = "#{Rails.configuration.Users.EmailSubjectPrefix}New user created notification"
651 if Rails.configuration.Users.AutoSetupNewUsers
652 new_user_email_subject = (expect_username or active) ?
653 "#{Rails.configuration.Users.EmailSubjectPrefix}New user created and setup notification" :
654 "#{Rails.configuration.Users.EmailSubjectPrefix}New user created, but not setup notification"
657 ActionMailer::Base.deliveries.each do |d|
658 if d.subject == new_user_email_subject then
660 elsif d.subject == "#{Rails.configuration.Users.EmailSubjectPrefix}New inactive user notification" then
661 new_inactive_user_email = d
665 # both active and inactive user creations should result in new user creation notification mails,
666 # if the new user email recipients config parameter is set
667 if not new_user_recipients.empty? then
668 assert_not_nil new_user_email, 'Expected new user email after setup'
669 assert_equal Rails.configuration.Users.UserNotifierEmailFrom, new_user_email.from[0]
670 assert_equal new_user_recipients.stringify_keys.keys.first, new_user_email.to[0]
671 assert_equal new_user_email_subject, new_user_email.subject
673 assert_nil new_user_email, 'Did not expect new user email after setup'
677 if not inactive_recipients.empty? then
678 assert_not_nil new_inactive_user_email, 'Expected new inactive user email after setup'
679 assert_equal Rails.configuration.Users.UserNotifierEmailFrom, new_inactive_user_email.from[0]
680 assert_equal inactive_recipients.stringify_keys.keys.first, new_inactive_user_email.to[0]
681 assert_equal "#{Rails.configuration.Users.EmailSubjectPrefix}New inactive user notification", new_inactive_user_email.subject
683 assert_nil new_inactive_user_email, 'Did not expect new inactive user email after setup'
686 assert_nil new_inactive_user_email, 'Expected no inactive user email after setting up active user'
688 ActionMailer::Base.deliveries = []
691 def verify_link_exists link_exists, head_uuid, tail_uuid, link_class, link_name, property_name=nil, property_value=nil
692 all_links = Link.where(head_uuid: head_uuid,
693 tail_uuid: tail_uuid,
694 link_class: link_class,
696 assert_equal link_exists, all_links.any?, "Link#{' not' if link_exists} found for #{link_name} #{link_class} #{property_value}"
697 if link_exists && property_name && property_value
698 all_links.each do |link|
699 assert_equal true, all_links.first.properties[property_name].start_with?(property_value), 'Property not found in link'
704 def assert_update_success(old_uuid:, new_uuid:, expect_owned_objects: true)
708 [Group, :owner_uuid],
709 [Collection, :owner_uuid],
710 ].each do |klass, attr|
711 assert_empty klass.where(attr => old_uuid)
712 if klass == User || expect_owned_objects
713 assert_not_empty klass.where(attr => new_uuid)
718 test "lookup user by email" do
719 u = User.register({"email" => "active-user@arvados.local", "identity_url" => "different-identity-url"})
720 active = User.find_by_uuid(users(:active).uuid)
721 assert_equal active.uuid, u.uuid
722 assert_equal "active-user@arvados.local", active.email
723 # identity_url is not updated
724 assert_equal "https://active-user.openid.local", active.identity_url
727 test "lookup user by alternate email" do
728 # register method will find that active-user@arvados.local already
729 # exists and return existing 'active' user.
730 u = User.register({"email" => "user@parent-company.com",
731 "alternate_emails" => ["active-user@arvados.local"],
732 "identity_url" => "different-identity-url"})
733 active = User.find_by_uuid(users(:active).uuid)
734 assert_equal active.uuid, u.uuid
736 # email should be updated
737 assert_equal "user@parent-company.com", active.email
739 # identity_url is not updated
740 assert_equal "https://active-user.openid.local", active.identity_url
743 test "register new user" do
744 u = User.register({"email" => "never-before-seen-user@arvados.local",
745 "identity_url" => "different-identity-url",
746 "first_name" => "Robert",
747 "last_name" => "Baratheon",
748 "username" => "bobby"})
749 nbs = User.find_by_uuid(u.uuid)
750 assert_equal nbs.uuid, u.uuid
751 assert_equal "different-identity-url", nbs.identity_url
752 assert_equal "never-before-seen-user@arvados.local", nbs.email
753 assert_equal false, nbs.is_admin
754 assert_equal false , nbs.is_active
755 assert_equal "bobby", nbs.username
756 assert_equal "Robert", nbs.first_name
757 assert_equal "Baratheon", nbs.last_name
760 test "fail when email address is ambiguous" do
761 User.register({"email" => "active-user@arvados.local"})
762 u = User.register({"email" => "never-before-seen-user@arvados.local"})
763 u.email = "active-user@arvados.local"
764 act_as_system_user do
768 User.register({"email" => "active-user@arvados.local"})
772 test "fail lookup without identifiers" do
774 User.register({"first_name" => "Robert", "last_name" => "Baratheon"})
777 User.register({"first_name" => "Robert", "last_name" => "Baratheon", "identity_url" => "", "email" => ""})
781 test "user can update name" do
782 set_user_from_auth :active
783 user = users(:active)
784 user.first_name = "MyNewName"
788 test "user cannot update email" do
789 set_user_from_auth :active
790 user = users(:active)
791 user.email = "new-name@example.com"
792 assert_not_allowed { user.save }
795 test "admin can update email" do
796 set_user_from_auth :admin
797 user = users(:active)
798 user.email = "new-name@example.com"
802 test "empty identity_url saves as null" do
803 set_user_from_auth :admin
804 user = users(:active)
805 assert user.update(identity_url: '')
807 assert_nil user.identity_url
810 test "id overflows int32" do
811 uuid = users(:active).uuid
812 ActiveRecord::Base.connection.execute "update users set id=333222111000 where uuid='#{uuid}'"
813 u = User.find_by_uuid(uuid)
814 assert_equal 333222111000, u.id