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