3153: directly invoke setup method during auto-setup
[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@ex.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 after removing -._ characters
150     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'roo_t@example.com', false, true, true], # not blacklisted name
151
152     [false, [], [], '@example.com', true, false, false],  # incorrect format
153     [false, [], [], '@example.com', false, true, false],
154     [false, [], [], '@example.com', false, false, true],  # no repo and vm login, so no issue with email format
155
156     [false, [], [], '^^incorrect_format@example.com', true, true, false],
157
158     [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', true, true, true],  # existing repository name 'auto_setup_repo'
159     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', true, false, true],  # existing repository name 'auto_setup_repo'
160     [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_repo@example.com', false, true, true],  # existing repository name 'auto_setup_repo'
161     [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
162
163     [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', true, true, true], # existing vm login name
164     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', true, false, true], # existing vm login name
165     [false, 'active-notify@example.com', 'inactive-notify@example.com', 'auto_setup_vm_login@example.com', false, true, true], # existing vm login name
166     [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
167
168     [true, 'active-notify@example.com', 'inactive-notify@example.com', '*!*@example.com', true, false, false], # username is invalid format
169     [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)
170     [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)
171
172     [true, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', true, true, false], # username is invalid format
173     [true, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', false, false, true], # no repo or vm login, so format not checked
174     [false, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', true, true, false], # username is invalid format
175     [false, 'active-notify@example.com', 'inactive-notify@example.com', '&4ad@example.com', false, false, true], # no repo or vm login, so format not checked
176
177     [true, 'active-notify@example.com', 'inactive-notify@example.com', '4ad@example.com', true, true, false], # username is invalid format
178     [true, 'active-notify@example.com', 'inactive-notify@example.com', '4ad@example.com', false, false, true], # no repo or vm login, so format not checked
179     [false, 'active-notify@example.com', 'inactive-notify@example.com', '4ad@example.com', false, false, true], # no repo or vm login, so format not checked
180
181     [true, 'active-notify@example.com', 'inactive-notify@example.com', '.foo@example.com', false, false, true], # no repo or vm login, so format not checked
182     [true, 'active-notify@example.com', 'inactive-notify@example.com', '.foo@example.com', true, false, false], # invalid format
183
184     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'bar.@example.com', false, false, true], # no repo or vm login, so format not checked
185     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'bar.@example.com', true, false, false], # valid format
186
187     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'ice9@example.com', false, false, true], # no repo or vm login, so format not checked
188     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'ice9@example.com', true, false, true], # valid format
189
190     [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
191     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'o_o@example.com', true, false, true], # valid format
192
193     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'r00t@example.com', false, false, true], # no repo or vm login, so format not checked
194     [true, 'active-notify@example.com', 'inactive-notify@example.com', 'r00t@example.com', true, false, true], # valid format
195
196   ].each do |active, active_recipients, inactive_recipients, email, auto_setup_vm, auto_setup_repo, valid_username|
197     test "create new user with auto setup #{active} #{email} #{auto_setup_vm} #{auto_setup_repo}" do
198       auto_setup_new_users = Rails.configuration.auto_setup_new_users
199       auto_setup_new_users_with_vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
200       auto_setup_new_users_with_repository = Rails.configuration.auto_setup_new_users_with_repository
201
202       begin
203         set_user_from_auth :admin
204
205         Rails.configuration.auto_setup_new_users = true
206
207         if auto_setup_vm
208           Rails.configuration.auto_setup_new_users_with_vm_uuid = virtual_machines(:testvm)['uuid']
209         else
210           Rails.configuration.auto_setup_new_users_with_vm_uuid = false
211         end
212
213         Rails.configuration.auto_setup_new_users_with_repository = auto_setup_repo
214
215         create_user_and_verify_setup_and_notifications active, active_recipients, inactive_recipients, email, valid_username
216       ensure
217         Rails.configuration.auto_setup_new_users = auto_setup_new_users
218         Rails.configuration.auto_setup_new_users_with_vm_uuid = auto_setup_new_users_with_vm_uuid
219         Rails.configuration.auto_setup_new_users_with_repository = auto_setup_new_users_with_repository
220       end
221     end
222   end
223
224   test "update existing user" do
225     set_user_from_auth :active    # set active user as current user
226
227     @active_user = users(:active)     # get the active user
228
229     @active_user.first_name = "first_name_changed"
230     @active_user.save
231
232     @active_user = User.find(@active_user.id)   # get the user back
233     assert_equal(@active_user.first_name, 'first_name_changed')
234
235     # admin user also should be able to update the "active" user info
236     set_user_from_auth :admin # set admin user as current user
237     @active_user.first_name = "first_name_changed_by_admin_for_active_user"
238     @active_user.save
239
240     @active_user = User.find(@active_user.id)   # get the user back
241     assert_equal(@active_user.first_name, 'first_name_changed_by_admin_for_active_user')
242   end
243
244   test "delete a user and verify" do
245     @active_user = users(:active)     # get the active user
246     active_user_uuid = @active_user.uuid
247
248     set_user_from_auth :admin
249     @active_user.delete
250
251     found_deleted_user = false
252     User.find(:all).each do |user|
253       if user.uuid == active_user_uuid
254         found_deleted_user = true
255         break
256       end
257     end
258     assert !found_deleted_user, "found deleted user: "+active_user_uuid
259
260   end
261
262   test "create new user as non-admin user" do
263     set_user_from_auth :active
264
265     begin
266       user = User.new
267       user.save
268     rescue ArvadosModel::PermissionDeniedError => e
269     end
270     assert (e.message.include? 'PermissionDeniedError'),
271         'Expected PermissionDeniedError'
272   end
273
274   test "setup new user" do
275     set_user_from_auth :admin
276
277     email = 'foo@example.com'
278     openid_prefix = 'http://openid/prefix'
279
280     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
281
282     vm = VirtualMachine.create
283
284     response = User.setup user, openid_prefix, 'test_repo', vm.uuid
285
286     resp_user = find_obj_in_resp response, 'User'
287     verify_user resp_user, email
288
289     oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
290
291     verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
292         resp_user[:uuid]
293
294     assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
295         'expected identity_url_prefix not found for oid_login_perm'
296
297     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
298     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
299
300     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
301     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
302
303     vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
304     verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
305   end
306
307   test "setup new user with junk in database" do
308     set_user_from_auth :admin
309
310     email = 'foo@example.com'
311     openid_prefix = 'http://openid/prefix'
312
313     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
314
315     vm = VirtualMachine.create
316
317     # Set up the bogus Link
318     bad_uuid = 'zzzzz-tpzed-xyzxyzxyzxyzxyz'
319
320     resp_link = Link.create ({tail_uuid: email, link_class: 'permission',
321         name: 'can_login', head_uuid: bad_uuid})
322     resp_link.save(validate: false)
323
324     verify_link resp_link, 'permission', 'can_login', email, bad_uuid
325
326     response = User.setup user, openid_prefix, 'test_repo', vm.uuid
327
328     resp_user = find_obj_in_resp response, 'User'
329     verify_user resp_user, email
330
331     oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
332
333     verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
334         resp_user[:uuid]
335
336     assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
337         'expected identity_url_prefix not found for oid_login_perm'
338
339     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
340     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
341
342     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
343     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
344
345     vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
346     verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
347   end
348
349   test "setup new user in multiple steps" do
350     set_user_from_auth :admin
351
352     email = 'foo@example.com'
353     openid_prefix = 'http://openid/prefix'
354
355     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
356
357     response = User.setup user, openid_prefix
358
359     resp_user = find_obj_in_resp response, 'User'
360     verify_user resp_user, email
361
362     oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
363     verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
364         resp_user[:uuid]
365     assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
366         'expected identity_url_prefix not found for oid_login_perm'
367
368     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
369     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
370
371     # invoke setup again with repo_name
372     response = User.setup user, openid_prefix, 'test_repo'
373     resp_user = find_obj_in_resp response, 'User', nil
374     verify_user resp_user, email
375     assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
376
377     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
378     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
379
380     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
381     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
382
383     # invoke setup again with a vm_uuid
384     vm = VirtualMachine.create
385
386     response = User.setup user, openid_prefix, 'test_repo', vm.uuid
387
388     resp_user = find_obj_in_resp response, 'User', nil
389     verify_user resp_user, email
390     assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
391
392     group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
393     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
394
395     repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
396     verify_link repo_perm, 'permission', 'can_manage', resp_user[:uuid], nil
397
398     vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
399     verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
400   end
401
402   def find_obj_in_resp (response_items, object_type, head_kind=nil)
403     return_obj = nil
404     response_items.each { |x|
405       if !x
406         next
407       end
408
409       if object_type == 'User'
410         if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
411           return_obj = x
412           break
413         end
414       else  # looking for a link
415         if ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
416           return_obj = x
417           break
418         end
419       end
420     }
421     return return_obj
422   end
423
424   def verify_user (resp_user, email)
425     assert_not_nil resp_user, 'expected user object'
426     assert_not_nil resp_user['uuid'], 'expected user object'
427     assert_equal email, resp_user['email'], 'expected email not found'
428
429   end
430
431   def verify_link (link_object, link_class, link_name, tail_uuid, head_uuid)
432     assert_not_nil link_object, "expected link for #{link_class} #{link_name}"
433     assert_not_nil link_object[:uuid],
434         "expected non-nil uuid for link for #{link_class} #{link_name}"
435     assert_equal link_class, link_object[:link_class],
436         "expected link_class not found for #{link_class} #{link_name}"
437     assert_equal link_name, link_object[:name],
438         "expected link_name not found for #{link_class} #{link_name}"
439     assert_equal tail_uuid, link_object[:tail_uuid],
440         "expected tail_uuid not found for #{link_class} #{link_name}"
441     if head_uuid
442       assert_equal head_uuid, link_object[:head_uuid],
443           "expected head_uuid not found for #{link_class} #{link_name}"
444     end
445   end
446
447   def create_user_and_verify_setup_and_notifications (active, active_recipients, inactive_recipients, email, valid_username)
448     Rails.configuration.new_user_notification_recipients = active_recipients
449     Rails.configuration.new_inactive_user_notification_recipients = inactive_recipients
450
451     assert_equal active_recipients, Rails.configuration.new_user_notification_recipients
452     assert_equal inactive_recipients, Rails.configuration.new_inactive_user_notification_recipients
453
454     ActionMailer::Base.deliveries = []
455
456     user = User.new
457     user.first_name = "first_name_for_newly_created_user"
458     user.email = email
459     user.is_active = active
460     user.save!
461
462     # check user setup
463     group = Group.where(name: 'All users').select do |g|
464       g[:uuid].match /-f+$/
465     end.first
466
467     if !Rails.configuration.auto_setup_new_users || !valid_username
468       # verify that the user is not added to "All groups" by auto_setup
469       verify_link_exists false, group[:uuid], user.uuid, 'permission', 'can_read', nil, nil
470
471       # check oid login link not created by auto_setup
472       verify_link_exists false, user.uuid, user.email, 'permission', 'can_login', nil, nil
473     else
474       # verify that auto_setup took place
475       # verify that the user is added to "All groups"
476       verify_link_exists true, group[:uuid], user.uuid, 'permission', 'can_read', nil, nil
477
478       # check oid login link
479       verify_link_exists true, user.uuid, user.email, 'permission', 'can_login', nil, nil
480
481       username = user.email.partition('@')[0] if email
482
483       # check repo
484       repo_names = []
485       if Rails.configuration.auto_setup_new_users_with_repository
486         repos = Repository.where('name like ?', "%#{username}%")
487         assert_not_nil repos, 'repository not found'
488         assert_equal true, repos.any?, 'repository not found'
489         repo_uuids = []
490         repos.each do |repo|
491           repo_uuids << repo[:uuid]
492           repo_names << repo[:name]
493         end
494         if username == 'auto_setup_repo'
495           begin
496             repo_names.delete('auto_setup_repo')
497           ensure
498             assert_equal true, repo_names.any?, 'Repository name for username foo is not unique'
499           end
500         end
501         verify_link_exists true, repo_uuids, user.uuid, 'permission', 'can_manage', nil, nil
502       end
503
504       # if username is existing vm login name, make sure the username used to generate any repo is unique
505       if username == 'auto_setup_vm_login' || username == 'auto_setup_repo'
506         if repo_names.any?
507           assert repo_names.first.start_with? username
508           assert_not_nil /\d$/.match(repo_names.first)
509         end
510       end
511
512       # check vm uuid
513       vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
514       if vm_uuid
515         verify_link_exists true, vm_uuid, user.uuid, 'permission', 'can_login', 'username', (username == 'auto_setup_repo' ? repo_names.first : username)
516       else
517         verify_link_exists false, vm_uuid, user.uuid, 'permission', 'can_login', 'username', (username == 'auto_setup_repo' ? repo_names.first : username)
518       end
519     end
520
521     # check email notifications
522     new_user_email = nil
523     new_inactive_user_email = nil
524
525     new_user_email_subject = "#{Rails.configuration.email_subject_prefix}New user created notification"
526     if Rails.configuration.auto_setup_new_users
527       new_user_email_subject = valid_username ? "#{Rails.configuration.email_subject_prefix}New user created and setup notification" :
528                                                 "#{Rails.configuration.email_subject_prefix}New user created, but not setup notification"
529     end
530
531     ActionMailer::Base.deliveries.each do |d|
532       if d.subject == new_user_email_subject then
533         new_user_email = d
534       elsif d.subject == "#{Rails.configuration.email_subject_prefix}New inactive user notification" then
535         new_inactive_user_email = d
536       end
537     end
538
539     if not active
540       if not inactive_recipients.empty? then
541         assert_not_nil new_inactive_user_email, 'Expected new inactive user email after setup'
542         assert_equal Rails.configuration.user_notifier_email_from, new_inactive_user_email.from[0]
543         assert_equal inactive_recipients, new_inactive_user_email.to[0]
544         assert_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", new_inactive_user_email.subject
545       else
546         assert_nil new_inactive_user_email, 'Did not expect new inactive user email after setup'
547       end
548     end
549
550     if active
551       assert_nil new_inactive_user_email, 'Expected no inactive user email after setting up active user'
552       if (not active_recipients.empty?) && valid_username then
553         assert_not_nil new_user_email, 'Expected new user email after setup'
554         assert_equal Rails.configuration.user_notifier_email_from, new_user_email.from[0]
555         assert_equal active_recipients, new_user_email.to[0]
556         assert_equal new_user_email_subject, new_user_email.subject
557       else
558         assert_nil new_user_email, 'Did not expect new user email after setup'
559       end
560     end
561     ActionMailer::Base.deliveries = []
562
563   end
564
565   def verify_link_exists link_exists, head_uuid, tail_uuid, link_class, link_name, property_name, property_value
566     all_links = Link.where(head_uuid: head_uuid,
567                            tail_uuid: tail_uuid,
568                            link_class: link_class,
569                            name: link_name)
570     assert_equal link_exists, all_links.any?, "Link #{'not' if link_exists} found for #{link_name} #{link_class} #{property_value}"
571     if link_exists && property_name && property_value
572       all_links.each do |link|
573         assert_equal true, all_links.first.properties[property_name].start_with?(property_value), 'Property not found in link'
574       end
575     end
576   end
577
578 end