Add 'build/' from commit '555b039609a3c8700c27767c255fdfe00eb42063'
[arvados.git] / services / api / test / unit / user_test.rb
1 require 'test_helper'
2
3 class UserTest < ActiveSupport::TestCase
4   include CurrentApiClient
5
6   # The fixture services/api/test/fixtures/users.yml serves as the input for this test case
7   setup do
8     # Make sure system_user exists before making "pre-test users" list
9     system_user
10   end
11
12   %w(a aa a0 aA Aa AA A0).each do |username|
13     test "#{username.inspect} is a valid username" do
14       user = User.new(username: username)
15       assert(user.valid?)
16     end
17   end
18
19   test "username is not required" do
20     user = User.new(username: nil)
21     assert(user.valid?)
22   end
23
24   test "username beginning with numeral is invalid" do
25     user = User.new(username: "0a")
26     refute(user.valid?)
27   end
28
29   "\\.-_/!@#$%^&*()[]{}".each_char do |bad_char|
30     test "username containing #{bad_char.inspect} is invalid" do
31       user = User.new(username: "bad#{bad_char}username")
32       refute(user.valid?)
33     end
34   end
35
36   test "username must be unique" do
37     user = User.new(username: users(:active).username)
38     refute(user.valid?)
39   end
40
41   test "non-admin can't update username" do
42     set_user_from_auth :rominiadmin
43     user = User.find_by_uuid(users(:rominiadmin).uuid)
44     user.username = "selfupdate"
45     assert_not_allowed { user.save }
46   end
47
48   def check_admin_username_change(fixture_name)
49     set_user_from_auth :admin_trustedclient
50     user = User.find_by_uuid(users(fixture_name).uuid)
51     user.username = "newnamefromtest"
52     assert(user.save)
53   end
54
55   test "admin can set username" do
56     check_admin_username_change(:active_no_prefs)
57   end
58
59   test "admin can update username" do
60     check_admin_username_change(:active)
61   end
62
63   test "admin can update own username" do
64     check_admin_username_change(:admin)
65   end
66
67   def check_new_username_setting(email_name, expect_name)
68     set_user_from_auth :admin
69     user = User.create!(email: "#{email_name}@example.org")
70     assert_equal(expect_name, user.username)
71   end
72
73   test "new username set from e-mail" do
74     check_new_username_setting("dakota", "dakota")
75   end
76
77   test "new username set from e-mail with leading digits" do
78     check_new_username_setting("1dakota9", "dakota9")
79   end
80
81   test "new username set from e-mail with punctuation" do
82     check_new_username_setting("dakota.9", "dakota9")
83   end
84
85   test "new username set from e-mail with leading digits and punctuation" do
86     check_new_username_setting("1.dakota.z", "dakotaz")
87   end
88
89   test "new username set from e-mail with extra part" do
90     check_new_username_setting("dakota+arvados", "dakota")
91   end
92
93   test "new username set with deduplication" do
94     name = users(:active).username
95     check_new_username_setting(name, "#{name}2")
96   end
97
98   test "new username set avoiding blacklist" do
99     Rails.configuration.auto_setup_name_blacklist = ["root"]
100     check_new_username_setting("root", "root2")
101   end
102
103   test "no username set when no base available" do
104     check_new_username_setting("_", nil)
105   end
106
107   test "updating username updates repository names" do
108     set_user_from_auth :admin
109     user = users(:active)
110     user.username = "newtestname"
111     assert(user.save, "username update failed")
112     {foo: "newtestname/foo", repository2: "newtestname/foo2"}.
113         each_pair do |repo_sym, expect_name|
114       assert_equal(expect_name, repositories(repo_sym).name)
115     end
116   end
117
118   test "admin can clear username when user owns no repositories" do
119     set_user_from_auth :admin
120     user = users(:spectator)
121     user.username = nil
122     assert(user.save)
123     assert_nil(user.username)
124   end
125
126   test "admin can't clear username when user owns repositories" do
127     set_user_from_auth :admin
128     user = users(:active)
129     start_username = user.username
130     user.username = nil
131     assert_not_allowed { user.save }
132     refute_empty(user.errors[:username])
133   end
134
135   test "failed username update doesn't change repository names" do
136     set_user_from_auth :admin
137     user = users(:active)
138     user.username = users(:fuse).username
139     assert_not_allowed { user.save }
140     assert_equal("active/foo", repositories(:foo).name)
141   end
142
143   [[false, 'foo@example.com', true, nil],
144    [false, 'bar@example.com', nil, true],
145    [true, 'foo@example.com', true, nil],
146    [true, 'bar@example.com', true, true],
147    [false, false, nil, nil],
148    [true, false, true, nil]
149   ].each do |auto_admin_first_user_config, auto_admin_user_config, foo_should_be_admin, bar_should_be_admin|
150     # In each case, 'foo' is created first, then 'bar', then 'bar2', then 'baz'.
151     test "auto admin with auto_admin_first=#{auto_admin_first_user_config} auto_admin=#{auto_admin_user_config}" do
152
153       if auto_admin_first_user_config
154         # This test requires no admin users exist (except for the system user)
155         users(:admin).delete
156         @all_users = User.where("uuid not like '%-000000000000000'").where(:is_admin => true).find(:all)
157         assert_equal 0, @all_users.size, "No admin users should exist (except for the system user)"
158       end
159
160       Rails.configuration.auto_admin_first_user = auto_admin_first_user_config
161       Rails.configuration.auto_admin_user = auto_admin_user_config
162
163       # See if the foo user has is_admin
164       foo = User.new
165       foo.first_name = 'foo'
166       foo.email = 'foo@example.com'
167
168       act_as_system_user do
169         foo.save!
170       end
171
172       foo = User.find(foo.id)   # get the user back
173       assert_equal foo_should_be_admin, foo.is_admin, "is_admin is wrong for user foo"
174       assert_equal 'foo', foo.first_name
175
176       # See if the bar user has is_admin
177       bar = User.new
178       bar.first_name = 'bar'
179       bar.email = 'bar@example.com'
180
181       act_as_system_user do
182         bar.save!
183       end
184
185       bar = User.find(bar.id)   # get the user back
186       assert_equal bar_should_be_admin, bar.is_admin, "is_admin is wrong for user bar"
187       assert_equal 'bar', bar.first_name
188
189       # A subsequent user with the bar@example.com address should never be
190       # elevated to admin
191       bar2 = User.new
192       bar2.first_name = 'bar2'
193       bar2.email = 'bar@example.com'
194
195       act_as_system_user do
196         bar2.save!
197       end
198
199       bar2 = User.find(bar2.id)   # get the user back
200       assert !bar2.is_admin, "is_admin is wrong for user bar2"
201       assert_equal 'bar2', bar2.first_name
202
203       # An ordinary new user should not be elevated to admin
204       baz = User.new
205       baz.first_name = 'baz'
206       baz.email = 'baz@example.com'
207
208       act_as_system_user do
209         baz.save!
210       end
211
212       baz = User.find(baz.id)   # get the user back
213       assert !baz.is_admin
214       assert_equal 'baz', baz.first_name
215
216     end
217   end
218
219   test "check non-admin active user properties" do
220     @active_user = users(:active)     # get the active user
221     assert !@active_user.is_admin, 'is_admin should not be set for a non-admin user'
222     assert @active_user.is_active, 'user should be active'
223     assert @active_user.is_invited, 'is_invited should be set'
224     assert_not_nil @active_user.prefs, "user's preferences should be non-null, but may be size zero"
225     assert (@active_user.can? :read=>"#{@active_user.uuid}"), "user should be able to read own object"
226     assert (@active_user.can? :write=>"#{@active_user.uuid}"), "user should be able to write own object"
227     assert (@active_user.can? :manage=>"#{@active_user.uuid}"), "user should be able to manage own object"
228
229     assert @active_user.groups_i_can(:read).size > 0, "active user should be able read at least one group"
230
231     # non-admin user cannot manage or write other user objects
232     @uninvited_user = users(:inactive_uninvited)     # get the uninvited user
233     assert !(@active_user.can? :read=>"#{@uninvited_user.uuid}")
234     assert !(@active_user.can? :write=>"#{@uninvited_user.uuid}")
235     assert !(@active_user.can? :manage=>"#{@uninvited_user.uuid}")
236   end
237
238   test "check admin user properties" do
239     @admin_user = users(:admin)     # get the admin user
240     assert @admin_user.is_admin, 'is_admin should be set for admin user'
241     assert @admin_user.is_active, 'admin user cannot be inactive'
242     assert @admin_user.is_invited, 'is_invited should be set'
243     assert_not_nil @admin_user.uuid.size, "user's uuid should be non-null"
244     assert_not_nil @admin_user.prefs, "user's preferences should be non-null, but may be size zero"
245     assert @admin_user.identity_url.size > 0, "user's identity url is expected"
246     assert @admin_user.can? :read=>"#{@admin_user.uuid}"
247     assert @admin_user.can? :write=>"#{@admin_user.uuid}"
248     assert @admin_user.can? :manage=>"#{@admin_user.uuid}"
249
250     assert @admin_user.groups_i_can(:read).size > 0, "admin active user should be able read at least one group"
251     assert @admin_user.groups_i_can(:write).size > 0, "admin active user should be able write to at least one group"
252     assert @admin_user.groups_i_can(:manage).size > 0, "admin active user should be able manage at least one group"
253
254     # admin user can also write or manage other users
255     @uninvited_user = users(:inactive_uninvited)     # get the uninvited user
256     assert @admin_user.can? :read=>"#{@uninvited_user.uuid}"
257     assert @admin_user.can? :write=>"#{@uninvited_user.uuid}"
258     assert @admin_user.can? :manage=>"#{@uninvited_user.uuid}"
259   end
260
261   test "check inactive and uninvited user properties" do
262     @uninvited_user = users(:inactive_uninvited)     # get the uninvited user
263     assert !@uninvited_user.is_admin, 'is_admin should not be set for a non-admin user'
264     assert !@uninvited_user.is_active, 'user should be inactive'
265     assert !@uninvited_user.is_invited, 'is_invited should not be set'
266     assert @uninvited_user.can? :read=>"#{@uninvited_user.uuid}"
267     assert @uninvited_user.can? :write=>"#{@uninvited_user.uuid}"
268     assert @uninvited_user.can? :manage=>"#{@uninvited_user.uuid}"
269
270     assert @uninvited_user.groups_i_can(:read).size == 1, "inactive and uninvited user can only read anonymous user group"
271     assert @uninvited_user.groups_i_can(:read).first.ends_with? 'anonymouspublic' , "inactive and uninvited user can only read anonymous user group"
272     assert @uninvited_user.groups_i_can(:write).size == 0, "inactive and uninvited user should not be able write to any groups"
273     assert @uninvited_user.groups_i_can(:manage).size == 0, "inactive and uninvited user should not be able manage any groups"
274   end
275
276   test "find user method checks" do
277     User.find(:all).each do |user|
278       assert_not_nil user.uuid, "non-null uuid expected for " + user.full_name
279     end
280
281     user = users(:active)     # get the active user
282
283     found_user = User.find(user.id)   # find a user by the row id
284
285     assert_equal found_user.full_name, user.first_name + ' ' + user.last_name
286     assert_equal found_user.identity_url, user.identity_url
287   end
288
289   test "full name should not contain spurious whitespace" do
290     set_user_from_auth :admin
291
292     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: 'foo@example.com' })
293
294     assert_equal '', user.full_name
295
296     user.first_name = 'John'
297     user.last_name = 'Smith'
298
299     assert_equal user.first_name + ' ' + user.last_name, user.full_name
300   end
301
302   test "create new user" do
303     set_user_from_auth :admin
304
305     @all_users = User.find(:all)
306
307     user = User.new
308     user.first_name = "first_name_for_newly_created_user"
309     user.save
310
311     # verify there is one extra user in the db now
312     assert_equal @all_users.size+1, User.find(:all).size
313
314     user = User.find(user.id)   # get the user back
315     assert_equal(user.first_name, 'first_name_for_newly_created_user')
316     assert_not_nil user.uuid, 'uuid should be set for newly created user'
317     assert_nil user.email, 'email should be null for newly created user, because it was not passed in'
318     assert_nil user.identity_url, 'identity_url should be null for newly created user, because it was not passed in'
319
320     user.first_name = 'first_name_for_newly_created_user_updated'
321     user.save
322     user = User.find(user.id)   # get the user back
323     assert_equal(user.first_name, 'first_name_for_newly_created_user_updated')
324   end
325
326   test "create new user with notifications" do
327     set_user_from_auth :admin
328
329     create_user_and_verify_setup_and_notifications true, 'active-notify-address@example.com', 'inactive-notify-address@example.com', nil, nil
330     create_user_and_verify_setup_and_notifications true, 'active-notify-address@example.com', [], nil, nil
331     create_user_and_verify_setup_and_notifications true, [], [], nil, nil
332     create_user_and_verify_setup_and_notifications false, 'active-notify-address@example.com', 'inactive-notify-address@example.com', nil, nil
333     create_user_and_verify_setup_and_notifications false, [], 'inactive-notify-address@example.com', nil, nil
334     create_user_and_verify_setup_and_notifications false, [], [], nil, nil
335   end
336
337   [
338     # Easy inactive user tests.
339     [false, [], [], "inactive-none@example.com", false, false, "inactivenone"],
340     [false, [], [], "inactive-vm@example.com", true, false, "inactivevm"],
341     [false, [], [], "inactive-repo@example.com", false, true, "inactiverepo"],
342     [false, [], [], "inactive-both@example.com", true, true, "inactiveboth"],
343
344     # Easy active user tests.
345     [true, "active-notify@example.com", "inactive-notify@example.com", "active-none@example.com", false, false, "activenone"],
346     [true, "active-notify@example.com", "inactive-notify@example.com", "active-vm@example.com", true, false, "activevm"],
347     [true, "active-notify@example.com", "inactive-notify@example.com", "active-repo@example.com", false, true, "activerepo"],
348     [true, "active-notify@example.com", "inactive-notify@example.com", "active-both@example.com", true, true, "activeboth"],
349
350     # Test users with malformed e-mail addresses.
351     [false, [], [], nil, true, true, nil],
352     [false, [], [], "arvados", true, true, nil],
353     [false, [], [], "@example.com", true, true, nil],
354     [true, "active-notify@example.com", "inactive-notify@example.com", "*!*@example.com", true, false, nil],
355     [true, "active-notify@example.com", "inactive-notify@example.com", "*!*@example.com", false, false, nil],
356
357     # Test users with various username transformations.
358     [false, [], [], "arvados@example.com", false, false, "arvados2"],
359     [true, "active-notify@example.com", "inactive-notify@example.com", "arvados@example.com", false, false, "arvados2"],
360     [true, "active-notify@example.com", "inactive-notify@example.com", "root@example.com", true, false, "root2"],
361     [false, "active-notify@example.com", "inactive-notify@example.com", "root@example.com", true, false, "root2"],
362     [true, "active-notify@example.com", "inactive-notify@example.com", "roo_t@example.com", false, true, "root2"],
363     [false, [], [], "^^incorrect_format@example.com", true, true, "incorrectformat"],
364     [true, "active-notify@example.com", "inactive-notify@example.com", "&4a_d9.@example.com", true, true, "ad9"],
365     [true, "active-notify@example.com", "inactive-notify@example.com", "&4a_d9.@example.com", false, false, "ad9"],
366     [false, "active-notify@example.com", "inactive-notify@example.com", "&4a_d9.@example.com", true, true, "ad9"],
367     [false, "active-notify@example.com", "inactive-notify@example.com", "&4a_d9.@example.com", false, false, "ad9"],
368   ].each do |active, new_user_recipients, inactive_recipients, email, auto_setup_vm, auto_setup_repo, expect_username|
369     test "create new user with auto setup #{active} #{email} #{auto_setup_vm} #{auto_setup_repo}" do
370       set_user_from_auth :admin
371
372       Rails.configuration.auto_setup_new_users = true
373
374       if auto_setup_vm
375         Rails.configuration.auto_setup_new_users_with_vm_uuid = virtual_machines(:testvm)['uuid']
376       else
377         Rails.configuration.auto_setup_new_users_with_vm_uuid = false
378       end
379
380       Rails.configuration.auto_setup_new_users_with_repository = auto_setup_repo
381
382       create_user_and_verify_setup_and_notifications active, new_user_recipients, inactive_recipients, email, expect_username
383     end
384   end
385
386   test "update existing user" do
387     set_user_from_auth :active    # set active user as current user
388
389     @active_user = users(:active)     # get the active user
390
391     @active_user.first_name = "first_name_changed"
392     @active_user.save
393
394     @active_user = User.find(@active_user.id)   # get the user back
395     assert_equal(@active_user.first_name, 'first_name_changed')
396
397     # admin user also should be able to update the "active" user info
398     set_user_from_auth :admin # set admin user as current user
399     @active_user.first_name = "first_name_changed_by_admin_for_active_user"
400     @active_user.save
401
402     @active_user = User.find(@active_user.id)   # get the user back
403     assert_equal(@active_user.first_name, 'first_name_changed_by_admin_for_active_user')
404   end
405
406   test "delete a user and verify" do
407     @active_user = users(:active)     # get the active user
408     active_user_uuid = @active_user.uuid
409
410     set_user_from_auth :admin
411     @active_user.delete
412
413     found_deleted_user = false
414     User.find(:all).each do |user|
415       if user.uuid == active_user_uuid
416         found_deleted_user = true
417         break
418       end
419     end
420     assert !found_deleted_user, "found deleted user: "+active_user_uuid
421
422   end
423
424   test "create new user as non-admin user" do
425     set_user_from_auth :active
426     assert_not_allowed { User.new.save }
427   end
428
429   test "setup new user" do
430     set_user_from_auth :admin
431
432     email = 'foo@example.com'
433     openid_prefix = 'http://openid/prefix'
434
435     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
436
437     vm = VirtualMachine.create
438
439     response = User.setup user, openid_prefix, 'foo/testrepo', vm.uuid
440
441     resp_user = find_obj_in_resp response, 'User'
442     verify_user resp_user, email
443
444     oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
445
446     verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
447         resp_user[:uuid]
448
449     assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
450         'expected identity_url_prefix not found for oid_login_perm'
451
452     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
453     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
454
455     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
456     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
457
458     vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
459     verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
460     assert_equal("foo", vm_perm.properties["username"])
461   end
462
463   test "setup new user with junk in database" do
464     set_user_from_auth :admin
465
466     email = 'foo@example.com'
467     openid_prefix = 'http://openid/prefix'
468
469     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
470
471     vm = VirtualMachine.create
472
473     # Set up the bogus Link
474     bad_uuid = 'zzzzz-tpzed-xyzxyzxyzxyzxyz'
475
476     resp_link = Link.create ({tail_uuid: email, link_class: 'permission',
477         name: 'can_login', head_uuid: bad_uuid})
478     resp_link.save(validate: false)
479
480     verify_link resp_link, 'permission', 'can_login', email, bad_uuid
481
482     response = User.setup user, openid_prefix, 'foo/testrepo', vm.uuid
483
484     resp_user = find_obj_in_resp response, 'User'
485     verify_user resp_user, email
486
487     oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
488
489     verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
490         resp_user[:uuid]
491
492     assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
493         'expected identity_url_prefix not found for oid_login_perm'
494
495     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
496     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
497
498     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
499     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
500
501     vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
502     verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
503     assert_equal("foo", vm_perm.properties["username"])
504   end
505
506   test "setup new user in multiple steps" do
507     set_user_from_auth :admin
508
509     email = 'foo@example.com'
510     openid_prefix = 'http://openid/prefix'
511
512     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
513
514     response = User.setup user, openid_prefix
515
516     resp_user = find_obj_in_resp response, 'User'
517     verify_user resp_user, email
518
519     oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
520     verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
521         resp_user[:uuid]
522     assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
523         'expected identity_url_prefix not found for oid_login_perm'
524
525     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
526     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
527
528     # invoke setup again with repo_name
529     response = User.setup user, openid_prefix, 'foo/testrepo'
530     resp_user = find_obj_in_resp response, 'User', nil
531     verify_user resp_user, email
532     assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
533
534     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
535     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
536
537     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
538     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
539
540     # invoke setup again with a vm_uuid
541     vm = VirtualMachine.create
542
543     response = User.setup user, openid_prefix, 'foo/testrepo', vm.uuid
544
545     resp_user = find_obj_in_resp response, 'User', nil
546     verify_user resp_user, email
547     assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
548
549     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
550     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
551
552     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
553     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
554
555     vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
556     verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
557     assert_equal("foo", vm_perm.properties["username"])
558   end
559
560   def find_obj_in_resp (response_items, object_type, head_kind=nil)
561     return_obj = nil
562     response_items.each { |x|
563       if !x
564         next
565       end
566
567       if object_type == 'User'
568         if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
569           return_obj = x
570           break
571         end
572       else  # looking for a link
573         if ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
574           return_obj = x
575           break
576         end
577       end
578     }
579     return return_obj
580   end
581
582   def verify_user (resp_user, email)
583     assert_not_nil resp_user, 'expected user object'
584     assert_not_nil resp_user['uuid'], 'expected user object'
585     assert_equal email, resp_user['email'], 'expected email not found'
586
587   end
588
589   def verify_link (link_object, link_class, link_name, tail_uuid, head_uuid)
590     assert_not_nil link_object, "expected link for #{link_class} #{link_name}"
591     assert_not_nil link_object[:uuid],
592         "expected non-nil uuid for link for #{link_class} #{link_name}"
593     assert_equal link_class, link_object[:link_class],
594         "expected link_class not found for #{link_class} #{link_name}"
595     assert_equal link_name, link_object[:name],
596         "expected link_name not found for #{link_class} #{link_name}"
597     assert_equal tail_uuid, link_object[:tail_uuid],
598         "expected tail_uuid not found for #{link_class} #{link_name}"
599     if head_uuid
600       assert_equal head_uuid, link_object[:head_uuid],
601           "expected head_uuid not found for #{link_class} #{link_name}"
602     end
603   end
604
605   def create_user_and_verify_setup_and_notifications (active, new_user_recipients, inactive_recipients, email, expect_username)
606     Rails.configuration.new_user_notification_recipients = new_user_recipients
607     Rails.configuration.new_inactive_user_notification_recipients = inactive_recipients
608
609     ActionMailer::Base.deliveries = []
610
611     can_setup = (Rails.configuration.auto_setup_new_users and
612                  (not expect_username.nil?))
613     expect_repo_name = "#{expect_username}/#{expect_username}"
614     prior_repo = Repository.where(name: expect_repo_name).first
615
616     user = User.new
617     user.first_name = "first_name_for_newly_created_user"
618     user.email = email
619     user.is_active = active
620     user.save!
621     assert_equal(expect_username, user.username)
622
623     # check user setup
624     verify_link_exists(Rails.configuration.auto_setup_new_users,
625                        groups(:all_users).uuid, user.uuid,
626                        "permission", "can_read")
627     # Check for OID login link.
628     verify_link_exists(Rails.configuration.auto_setup_new_users,
629                        user.uuid, user.email, "permission", "can_login")
630     # Check for repository.
631     if named_repo = (prior_repo or
632                      Repository.where(name: expect_repo_name).first)
633       verify_link_exists((can_setup and prior_repo.nil? and
634                           Rails.configuration.auto_setup_new_users_with_repository),
635                          named_repo.uuid, user.uuid, "permission", "can_manage")
636     end
637     # Check for VM login.
638     if auto_vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
639       verify_link_exists(can_setup, auto_vm_uuid, user.uuid,
640                          "permission", "can_login", "username", expect_username)
641     end
642
643     # check email notifications
644     new_user_email = nil
645     new_inactive_user_email = nil
646
647     new_user_email_subject = "#{Rails.configuration.email_subject_prefix}New user created notification"
648     if Rails.configuration.auto_setup_new_users
649       new_user_email_subject = (expect_username or active) ?
650                                  "#{Rails.configuration.email_subject_prefix}New user created and setup notification" :
651                                  "#{Rails.configuration.email_subject_prefix}New user created, but not setup notification"
652     end
653
654     ActionMailer::Base.deliveries.each do |d|
655       if d.subject == new_user_email_subject then
656         new_user_email = d
657       elsif d.subject == "#{Rails.configuration.email_subject_prefix}New inactive user notification" then
658         new_inactive_user_email = d
659       end
660     end
661
662     # both active and inactive user creations should result in new user creation notification mails,
663     # if the new user email recipients config parameter is set
664     if not new_user_recipients.empty? then
665       assert_not_nil new_user_email, 'Expected new user email after setup'
666       assert_equal Rails.configuration.user_notifier_email_from, new_user_email.from[0]
667       assert_equal new_user_recipients, new_user_email.to[0]
668       assert_equal new_user_email_subject, new_user_email.subject
669     else
670       assert_nil new_user_email, 'Did not expect new user email after setup'
671     end
672
673     if not active
674       if not inactive_recipients.empty? then
675         assert_not_nil new_inactive_user_email, 'Expected new inactive user email after setup'
676         assert_equal Rails.configuration.user_notifier_email_from, new_inactive_user_email.from[0]
677         assert_equal inactive_recipients, new_inactive_user_email.to[0]
678         assert_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", new_inactive_user_email.subject
679       else
680         assert_nil new_inactive_user_email, 'Did not expect new inactive user email after setup'
681       end
682     else
683       assert_nil new_inactive_user_email, 'Expected no inactive user email after setting up active user'
684     end
685     ActionMailer::Base.deliveries = []
686
687   end
688
689   def verify_link_exists link_exists, head_uuid, tail_uuid, link_class, link_name, property_name=nil, property_value=nil
690     all_links = Link.where(head_uuid: head_uuid,
691                            tail_uuid: tail_uuid,
692                            link_class: link_class,
693                            name: link_name)
694     assert_equal link_exists, all_links.any?, "Link #{'not' if link_exists} found for #{link_name} #{link_class} #{property_value}"
695     if link_exists && property_name && property_value
696       all_links.each do |link|
697         assert_equal true, all_links.first.properties[property_name].start_with?(property_value), 'Property not found in link'
698       end
699     end
700   end
701
702 end