3 class UserTest < ActiveSupport::TestCase
4 include CurrentApiClient
6 # The fixture services/api/test/fixtures/users.yml serves as the input for this test case
8 # Make sure system_user exists before making "pre-test users" list
12 [[false, 'foo@example.com', true, nil],
13 [false, 'bar@example.com', nil, true],
14 [true, 'foo@example.com', true, nil],
15 [true, 'bar@example.com', true, true],
16 [false, false, nil, nil],
17 [true, false, true, nil]
18 ].each do |auto_admin_first_user_config, auto_admin_user_config, foo_should_be_admin, bar_should_be_admin|
19 # In each case, 'foo' is created first, then 'bar', then 'bar2', then 'baz'.
20 test "auto admin with auto_admin_first=#{auto_admin_first_user_config} auto_admin=#{auto_admin_user_config}" do
22 if auto_admin_first_user_config
23 # This test requires no admin users exist (except for the system user)
25 @all_users = User.where("uuid not like '%-000000000000000'").where(:is_admin => true).find(:all)
26 assert_equal 0, @all_users.size, "No admin users should exist (except for the system user)"
29 Rails.configuration.auto_admin_first_user = auto_admin_first_user_config
30 Rails.configuration.auto_admin_user = auto_admin_user_config
32 # See if the foo user has is_admin
34 foo.first_name = 'foo'
35 foo.email = 'foo@example.com'
41 foo = User.find(foo.id) # get the user back
42 assert_equal foo_should_be_admin, foo.is_admin, "is_admin is wrong for user foo"
43 assert_equal 'foo', foo.first_name
45 # See if the bar user has is_admin
47 bar.first_name = 'bar'
48 bar.email = 'bar@example.com'
54 bar = User.find(bar.id) # get the user back
55 assert_equal bar_should_be_admin, bar.is_admin, "is_admin is wrong for user bar"
56 assert_equal 'bar', bar.first_name
58 # A subsequent user with the bar@example.com address should never be
61 bar2.first_name = 'bar2'
62 bar2.email = 'bar@example.com'
68 bar2 = User.find(bar2.id) # get the user back
69 assert !bar2.is_admin, "is_admin is wrong for user bar2"
70 assert_equal 'bar2', bar2.first_name
72 # An ordinary new user should not be elevated to admin
74 baz.first_name = 'baz'
75 baz.email = 'baz@example.com'
81 baz = User.find(baz.id) # get the user back
83 assert_equal 'baz', baz.first_name
88 test "check non-admin active user properties" do
89 @active_user = users(:active) # get the active user
90 assert !@active_user.is_admin, 'is_admin should not be set for a non-admin user'
91 assert @active_user.is_active, 'user should be active'
92 assert @active_user.is_invited, 'is_invited should be set'
93 assert_not_nil @active_user.prefs, "user's preferences should be non-null, but may be size zero"
94 assert (@active_user.can? :read=>"#{@active_user.uuid}"), "user should be able to read own object"
95 assert (@active_user.can? :write=>"#{@active_user.uuid}"), "user should be able to write own object"
96 assert (@active_user.can? :manage=>"#{@active_user.uuid}"), "user should be able to manage own object"
98 assert @active_user.groups_i_can(:read).size > 0, "active user should be able read at least one group"
100 # non-admin user cannot manage or write other user objects
101 @uninvited_user = users(:inactive_uninvited) # get the uninvited user
102 assert !(@active_user.can? :read=>"#{@uninvited_user.uuid}")
103 assert !(@active_user.can? :write=>"#{@uninvited_user.uuid}")
104 assert !(@active_user.can? :manage=>"#{@uninvited_user.uuid}")
107 test "check admin user properties" do
108 @admin_user = users(:admin) # get the admin user
109 assert @admin_user.is_admin, 'is_admin should be set for admin user'
110 assert @admin_user.is_active, 'admin user cannot be inactive'
111 assert @admin_user.is_invited, 'is_invited should be set'
112 assert_not_nil @admin_user.uuid.size, "user's uuid should be non-null"
113 assert_not_nil @admin_user.prefs, "user's preferences should be non-null, but may be size zero"
114 assert @admin_user.identity_url.size > 0, "user's identity url is expected"
115 assert @admin_user.can? :read=>"#{@admin_user.uuid}"
116 assert @admin_user.can? :write=>"#{@admin_user.uuid}"
117 assert @admin_user.can? :manage=>"#{@admin_user.uuid}"
119 assert @admin_user.groups_i_can(:read).size > 0, "admin active user should be able read at least one group"
120 assert @admin_user.groups_i_can(:write).size > 0, "admin active user should be able write to at least one group"
121 assert @admin_user.groups_i_can(:manage).size > 0, "admin active user should be able manage at least one group"
123 # admin user can also write or manage other users
124 @uninvited_user = users(:inactive_uninvited) # get the uninvited user
125 assert @admin_user.can? :read=>"#{@uninvited_user.uuid}"
126 assert @admin_user.can? :write=>"#{@uninvited_user.uuid}"
127 assert @admin_user.can? :manage=>"#{@uninvited_user.uuid}"
130 test "check inactive and uninvited user properties" do
131 @uninvited_user = users(:inactive_uninvited) # get the uninvited user
132 assert !@uninvited_user.is_admin, 'is_admin should not be set for a non-admin user'
133 assert !@uninvited_user.is_active, 'user should be inactive'
134 assert !@uninvited_user.is_invited, 'is_invited should not be set'
135 assert @uninvited_user.can? :read=>"#{@uninvited_user.uuid}"
136 assert @uninvited_user.can? :write=>"#{@uninvited_user.uuid}"
137 assert @uninvited_user.can? :manage=>"#{@uninvited_user.uuid}"
139 assert @uninvited_user.groups_i_can(:read).size == 1, "inactive and uninvited user can only read anonymous user group"
140 assert @uninvited_user.groups_i_can(:read).first.ends_with? 'anonymouspublic' , "inactive and uninvited user can only read anonymous user group"
141 assert @uninvited_user.groups_i_can(:write).size == 0, "inactive and uninvited user should not be able write to any groups"
142 assert @uninvited_user.groups_i_can(:manage).size == 0, "inactive and uninvited user should not be able manage any groups"
145 test "find user method checks" do
146 User.find(:all).each do |user|
147 assert_not_nil user.uuid, "non-null uuid expected for " + user.full_name
150 user = users(:active) # get the active user
152 found_user = User.find(user.id) # find a user by the row id
154 assert_equal found_user.full_name, user.first_name + ' ' + user.last_name
155 assert_equal found_user.identity_url, user.identity_url
158 test "full name should not contain spurious whitespace" do
159 set_user_from_auth :admin
161 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: 'foo@example.com' })
163 assert_equal '', user.full_name
165 user.first_name = 'John'
166 user.last_name = 'Smith'
168 assert_equal user.first_name + ' ' + user.last_name, user.full_name
171 test "create new user" do
172 set_user_from_auth :admin
174 @all_users = User.find(:all)
177 user.first_name = "first_name_for_newly_created_user"
180 # verify there is one extra user in the db now
181 assert_equal @all_users.size+1, User.find(:all).size
183 user = User.find(user.id) # get the user back
184 assert_equal(user.first_name, 'first_name_for_newly_created_user')
185 assert_not_nil user.uuid, 'uuid should be set for newly created user'
186 assert_nil user.email, 'email should be null for newly created user, because it was not passed in'
187 assert_nil user.identity_url, 'identity_url should be null for newly created user, because it was not passed in'
189 user.first_name = 'first_name_for_newly_created_user_updated'
191 user = User.find(user.id) # get the user back
192 assert_equal(user.first_name, 'first_name_for_newly_created_user_updated')
195 test "create new user with notifications" do
196 set_user_from_auth :admin
198 create_user_and_verify_setup_and_notifications true, 'active-notify-address@example.com', 'inactive-notify-address@example.com', nil, false
199 create_user_and_verify_setup_and_notifications true, 'active-notify-address@example.com', [], nil, false
200 create_user_and_verify_setup_and_notifications true, [], [], nil, false
201 create_user_and_verify_setup_and_notifications false, 'active-notify-address@example.com', 'inactive-notify-address@example.com', nil, false
202 create_user_and_verify_setup_and_notifications false, [], 'inactive-notify-address@example.com', nil, false
203 create_user_and_verify_setup_and_notifications false, [], [], nil, false
207 [false, [], [], 'inactive-none@example.com', false, false, true],
208 [false, [], [], 'inactive-vm@example.com', true, false, true],
209 [false, [], [], 'inactive-repo@example.com', false, true, true],
210 [false, [], [], 'inactive-both@example.com', true, true, true],
212 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'active-none@example.com', false, false, true],
213 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'active-vm@example.com', true, false, true],
214 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'active-repo@example.com', false, true, true],
215 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'active-both@example.com', true, true, true],
217 [false, [], [], nil, true, true, false],
219 [false, [], [], 'arvados', true, true, false],
220 [false, [], [], 'arvados', true, false, false], # blacklisted username
221 [false, [], [], 'arvados', false, false, true], # since we are not creating repo and vm login, this blacklisted name is not a problem
223 [false, [], [], 'arvados@example.com', false, false, true], # since we are not creating repo and vm login, this blacklisted name is not a problem
224 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'arvados@example.com', false, false, true], # since we are not creating repo and vm login, this blacklisted name is not a problem
225 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'root@example.com', true, false, false], # blacklisted name
226 [false, 'active-notify@example.com', 'inactive-notify@example.com', 'root@example.com', true, false, false], # blacklisted name
227 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'roo_t@example.com', false, true, true], # not blacklisted name
229 [false, [], [], '@example.com', true, false, false], # incorrect format
230 [false, [], [], '@example.com', false, true, false],
231 [false, [], [], '@example.com', false, false, true], # no repo and vm login, so no issue with email format
233 [false, [], [], '^^incorrect_format@example.com', true, true, false],
235 [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', true, true, true], # existing repository name 'auto_setup_repo'
236 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', true, false, true], # existing repository name 'auto_setup_repo'
237 [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', false, true, true], # existing repository name 'auto_setup_repo'
238 [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
240 [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', true, true, true], # existing vm login name
241 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', true, false, true], # existing vm login name
242 [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', false, true, true], # existing vm login name
243 [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
245 [true, 'active-notify@example.com', 'inactive-notify@example.com', '*!*@example.com', true, false, false], # username is invalid format
246 [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)
247 [true, 'active-notify@example.com', 'inactive-notify@example.com', '*!*@example.com', false, false, true], # since no repo and vm login, username is ok (not validated)
249 [true, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', true, true, false], # username is invalid format
250 [true, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', false, false, true], # no repo or vm login, so format not checked
251 [false, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', true, true, false], # username is invalid format
252 [false, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', false, false, true], # no repo or vm login, so format not checked
254 [true, 'active-notify@example.com', 'inactive-notify@example.com', '4ad@example.com', true, true, false], # username is invalid format
255 [true, 'active-notify@example.com', 'inactive-notify@example.com', '4ad@example.com', false, false, true], # no repo or vm login, so format not checked
256 [false, 'active-notify@example.com', 'inactive-notify@example.com', '4ad@example.com', false, false, true], # no repo or vm login, so format not checked
258 [true, 'active-notify@example.com', 'inactive-notify@example.com', '.foo@example.com', false, false, true], # no repo or vm login, so format not checked
259 [true, 'active-notify@example.com', 'inactive-notify@example.com', '.foo@example.com', true, false, false], # invalid format
261 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'bar.@example.com', false, false, true], # no repo or vm login, so format not checked
262 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'bar.@example.com', true, false, false], # valid format
264 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'ice9@example.com', false, false, true], # no repo or vm login, so format not checked
265 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'ice9@example.com', true, false, true], # valid format
267 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'o_o@example.com', false, false, true], # no repo or vm login, so format not checked
268 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'o_o@example.com', true, false, true], # valid format
270 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'r00t@example.com', false, false, true], # no repo or vm login, so format not checked
271 [true, 'active-notify@example.com', 'inactive-notify@example.com', 'r00t@example.com', true, false, true], # valid format
273 ].each do |active, new_user_recipients, inactive_recipients, email, auto_setup_vm, auto_setup_repo, ok_to_auto_setup|
274 test "create new user with auto setup #{active} #{email} #{auto_setup_vm} #{auto_setup_repo}" do
275 auto_setup_new_users = Rails.configuration.auto_setup_new_users
276 auto_setup_new_users_with_vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
277 auto_setup_new_users_with_repository = Rails.configuration.auto_setup_new_users_with_repository
280 set_user_from_auth :admin
282 Rails.configuration.auto_setup_new_users = true
285 Rails.configuration.auto_setup_new_users_with_vm_uuid = virtual_machines(:testvm)['uuid']
287 Rails.configuration.auto_setup_new_users_with_vm_uuid = false
290 Rails.configuration.auto_setup_new_users_with_repository = auto_setup_repo
292 create_user_and_verify_setup_and_notifications active, new_user_recipients, inactive_recipients, email, ok_to_auto_setup
294 Rails.configuration.auto_setup_new_users = auto_setup_new_users
295 Rails.configuration.auto_setup_new_users_with_vm_uuid = auto_setup_new_users_with_vm_uuid
296 Rails.configuration.auto_setup_new_users_with_repository = auto_setup_new_users_with_repository
301 test "update existing user" do
302 set_user_from_auth :active # set active user as current user
304 @active_user = users(:active) # get the active user
306 @active_user.first_name = "first_name_changed"
309 @active_user = User.find(@active_user.id) # get the user back
310 assert_equal(@active_user.first_name, 'first_name_changed')
312 # admin user also should be able to update the "active" user info
313 set_user_from_auth :admin # set admin user as current user
314 @active_user.first_name = "first_name_changed_by_admin_for_active_user"
317 @active_user = User.find(@active_user.id) # get the user back
318 assert_equal(@active_user.first_name, 'first_name_changed_by_admin_for_active_user')
321 test "delete a user and verify" do
322 @active_user = users(:active) # get the active user
323 active_user_uuid = @active_user.uuid
325 set_user_from_auth :admin
328 found_deleted_user = false
329 User.find(:all).each do |user|
330 if user.uuid == active_user_uuid
331 found_deleted_user = true
335 assert !found_deleted_user, "found deleted user: "+active_user_uuid
339 test "create new user as non-admin user" do
340 set_user_from_auth :active
345 rescue ArvadosModel::PermissionDeniedError => e
347 assert (e.message.include? 'PermissionDeniedError'),
348 'Expected PermissionDeniedError'
351 test "setup new user" do
352 set_user_from_auth :admin
354 email = 'foo@example.com'
355 openid_prefix = 'http://openid/prefix'
357 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
359 vm = VirtualMachine.create
361 response = User.setup user, openid_prefix, 'test_repo', vm.uuid
363 resp_user = find_obj_in_resp response, 'User'
364 verify_user resp_user, email
366 oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
368 verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
371 assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
372 'expected identity_url_prefix not found for oid_login_perm'
374 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
375 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
377 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
378 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
380 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
381 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
384 test "setup new user with junk in database" do
385 set_user_from_auth :admin
387 email = 'foo@example.com'
388 openid_prefix = 'http://openid/prefix'
390 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
392 vm = VirtualMachine.create
394 # Set up the bogus Link
395 bad_uuid = 'zzzzz-tpzed-xyzxyzxyzxyzxyz'
397 resp_link = Link.create ({tail_uuid: email, link_class: 'permission',
398 name: 'can_login', head_uuid: bad_uuid})
399 resp_link.save(validate: false)
401 verify_link resp_link, 'permission', 'can_login', email, bad_uuid
403 response = User.setup user, openid_prefix, 'test_repo', vm.uuid
405 resp_user = find_obj_in_resp response, 'User'
406 verify_user resp_user, email
408 oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
410 verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
413 assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
414 'expected identity_url_prefix not found for oid_login_perm'
416 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
417 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
419 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
420 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
422 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
423 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
426 test "setup new user in multiple steps" do
427 set_user_from_auth :admin
429 email = 'foo@example.com'
430 openid_prefix = 'http://openid/prefix'
432 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
434 response = User.setup user, openid_prefix
436 resp_user = find_obj_in_resp response, 'User'
437 verify_user resp_user, email
439 oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
440 verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
442 assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
443 'expected identity_url_prefix not found for oid_login_perm'
445 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
446 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
448 # invoke setup again with repo_name
449 response = User.setup user, openid_prefix, 'test_repo'
450 resp_user = find_obj_in_resp response, 'User', nil
451 verify_user resp_user, email
452 assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
454 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
455 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
457 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
458 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
460 # invoke setup again with a vm_uuid
461 vm = VirtualMachine.create
463 response = User.setup user, openid_prefix, 'test_repo', vm.uuid
465 resp_user = find_obj_in_resp response, 'User', nil
466 verify_user resp_user, email
467 assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
469 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
470 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
472 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
473 verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
475 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
476 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
479 def find_obj_in_resp (response_items, object_type, head_kind=nil)
481 response_items.each { |x|
486 if object_type == 'User'
487 if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
491 else # looking for a link
492 if ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
501 def verify_user (resp_user, email)
502 assert_not_nil resp_user, 'expected user object'
503 assert_not_nil resp_user['uuid'], 'expected user object'
504 assert_equal email, resp_user['email'], 'expected email not found'
508 def verify_link (link_object, link_class, link_name, tail_uuid, head_uuid)
509 assert_not_nil link_object, "expected link for #{link_class} #{link_name}"
510 assert_not_nil link_object[:uuid],
511 "expected non-nil uuid for link for #{link_class} #{link_name}"
512 assert_equal link_class, link_object[:link_class],
513 "expected link_class not found for #{link_class} #{link_name}"
514 assert_equal link_name, link_object[:name],
515 "expected link_name not found for #{link_class} #{link_name}"
516 assert_equal tail_uuid, link_object[:tail_uuid],
517 "expected tail_uuid not found for #{link_class} #{link_name}"
519 assert_equal head_uuid, link_object[:head_uuid],
520 "expected head_uuid not found for #{link_class} #{link_name}"
524 def create_user_and_verify_setup_and_notifications (active, new_user_recipients, inactive_recipients, email, ok_to_auto_setup)
525 Rails.configuration.new_user_notification_recipients = new_user_recipients
526 Rails.configuration.new_inactive_user_notification_recipients = inactive_recipients
528 assert_equal new_user_recipients, Rails.configuration.new_user_notification_recipients
529 assert_equal inactive_recipients, Rails.configuration.new_inactive_user_notification_recipients
531 ActionMailer::Base.deliveries = []
534 user.first_name = "first_name_for_newly_created_user"
536 user.is_active = active
540 group = Group.where(name: 'All users').select do |g|
541 g[:uuid].match /-f+$/
544 if !Rails.configuration.auto_setup_new_users || !ok_to_auto_setup
545 # verify that the user is not added to "All groups" by auto_setup
546 verify_link_exists false, group[:uuid], user.uuid, 'permission', 'can_read', nil, nil
548 # check oid login link not created by auto_setup
549 verify_link_exists false, user.uuid, user.email, 'permission', 'can_login', nil, nil
551 # verify that auto_setup took place
552 # verify that the user is added to "All groups"
553 verify_link_exists true, group[:uuid], user.uuid, 'permission', 'can_read', nil, nil
555 # check oid login link
556 verify_link_exists true, user.uuid, user.email, 'permission', 'can_login', nil, nil
558 username = user.email.partition('@')[0] if email
562 if Rails.configuration.auto_setup_new_users_with_repository
563 repos = Repository.where('name like ?', "%#{username}%")
564 assert_not_nil repos, 'repository not found'
565 assert_equal true, repos.any?, 'repository not found'
568 repo_uuids << repo[:uuid]
569 repo_names << repo[:name]
571 if username == 'auto_setup_repo'
573 repo_names.delete('auto_setup_repo')
575 assert_equal true, repo_names.any?, 'Repository name for username foo is not unique'
578 verify_link_exists true, repo_uuids, user.uuid, 'permission', 'can_manage', nil, nil
581 # if username is existing vm login name, make sure the username used to generate any repo is unique
582 if username == 'auto_setup_vm_login' || username == 'auto_setup_repo'
584 assert repo_names.first.start_with? username
585 assert_not_nil /\d$/.match(repo_names.first)
590 vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
592 verify_link_exists true, vm_uuid, user.uuid, 'permission', 'can_login', 'username', (username == 'auto_setup_repo' ? repo_names.first : username)
594 verify_link_exists false, vm_uuid, user.uuid, 'permission', 'can_login', 'username', (username == 'auto_setup_repo' ? repo_names.first : username)
598 # check email notifications
600 new_inactive_user_email = nil
602 new_user_email_subject = "#{Rails.configuration.email_subject_prefix}New user created notification"
603 if Rails.configuration.auto_setup_new_users
604 new_user_email_subject = (ok_to_auto_setup || active) ?
605 "#{Rails.configuration.email_subject_prefix}New user created and setup notification" :
606 "#{Rails.configuration.email_subject_prefix}New user created, but not setup notification"
609 ActionMailer::Base.deliveries.each do |d|
610 if d.subject == new_user_email_subject then
612 elsif d.subject == "#{Rails.configuration.email_subject_prefix}New inactive user notification" then
613 new_inactive_user_email = d
617 # both active and inactive user creations should result in new user creation notification mails,
618 # if the new user email recipients config parameter is set
619 if not new_user_recipients.empty? then
620 assert_not_nil new_user_email, 'Expected new user email after setup'
621 assert_equal Rails.configuration.user_notifier_email_from, new_user_email.from[0]
622 assert_equal new_user_recipients, new_user_email.to[0]
623 assert_equal new_user_email_subject, new_user_email.subject
625 assert_nil new_user_email, 'Did not expect new user email after setup'
629 if not inactive_recipients.empty? then
630 assert_not_nil new_inactive_user_email, 'Expected new inactive user email after setup'
631 assert_equal Rails.configuration.user_notifier_email_from, new_inactive_user_email.from[0]
632 assert_equal inactive_recipients, new_inactive_user_email.to[0]
633 assert_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", new_inactive_user_email.subject
635 assert_nil new_inactive_user_email, 'Did not expect new inactive user email after setup'
638 assert_nil new_inactive_user_email, 'Expected no inactive user email after setting up active user'
640 ActionMailer::Base.deliveries = []
644 def verify_link_exists link_exists, head_uuid, tail_uuid, link_class, link_name, property_name, property_value
645 all_links = Link.where(head_uuid: head_uuid,
646 tail_uuid: tail_uuid,
647 link_class: link_class,
649 assert_equal link_exists, all_links.any?, "Link #{'not' if link_exists} found for #{link_name} #{link_class} #{property_value}"
650 if link_exists && property_name && property_value
651 all_links.each do |link|
652 assert_equal true, all_links.first.properties[property_name].start_with?(property_value), 'Property not found in link'