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