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