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