3889: fix a few tests that were performing multiple actions on a single instance...
[arvados.git] / services / api / test / functional / arvados / v1 / users_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::UsersControllerTest < ActionController::TestCase
4   include CurrentApiClient
5
6   setup do
7     @all_links_at_start = Link.all
8     @vm_uuid = virtual_machines(:testvm).uuid
9   end
10
11   test "activate a user after signing UA" do
12     authorize_with :inactive_but_signed_user_agreement
13     post :activate, id: users(:inactive_but_signed_user_agreement).uuid
14     assert_response :success
15     assert_not_nil assigns(:object)
16     me = JSON.parse(@response.body)
17     assert_equal true, me['is_active']
18   end
19
20   test "refuse to activate a user before signing UA" do
21     act_as_system_user do
22     required_uuids = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
23                                 system_user_uuid,
24                                 'signature',
25                                 'require',
26                                 system_user_uuid,
27                                 Collection.uuid_like_pattern).
28       collect(&:head_uuid)
29
30       assert required_uuids.length > 0
31
32       signed_uuids = Link.where(owner_uuid: system_user_uuid,
33                                 link_class: 'signature',
34                                 name: 'click',
35                                 tail_uuid: users(:inactive).uuid,
36                                 head_uuid: required_uuids).
37         collect(&:head_uuid)
38
39       assert_equal 0, signed_uuids.length
40     end
41
42     authorize_with :inactive
43
44     post :activate, id: users(:inactive).uuid
45     assert_response 403
46
47     response_body = JSON.parse(@response.body)
48     assert response_body['errors'].first.include? 'Cannot activate without user agreements'
49   end
50
51   test "activate an already-active user" do
52     authorize_with :active
53     post :activate, id: users(:active).uuid
54     assert_response :success
55     me = JSON.parse(@response.body)
56     assert_equal true, me['is_active']
57   end
58
59   test "respond 401 if given token exists but user record is missing" do
60     authorize_with :valid_token_deleted_user
61     get :current, {format: :json}
62     assert_response 401
63   end
64
65   test "create new user with user as input" do
66     authorize_with :admin
67     post :create, user: {
68       first_name: "test_first_name",
69       last_name: "test_last_name",
70       email: "foo@example.com"
71     }
72     assert_response :success
73     created = JSON.parse(@response.body)
74     assert_equal 'test_first_name', created['first_name']
75     assert_not_nil created['uuid'], 'expected uuid for the newly created user'
76     assert_not_nil created['email'], 'expected non-nil email'
77     assert_nil created['identity_url'], 'expected no identity_url'
78   end
79
80   test "create user with user, vm and repo as input" do
81     authorize_with :admin
82     repo_name = 'test_repo'
83
84     post :setup, {
85       repo_name: repo_name,
86       openid_prefix: 'https://www.google.com/accounts/o8/id',
87       user: {
88         uuid: 'zzzzz-tpzed-abcdefghijklmno',
89         first_name: "in_create_test_first_name",
90         last_name: "test_last_name",
91         email: "foo@example.com"
92       }
93     }
94     assert_response :success
95     response_items = JSON.parse(@response.body)['items']
96
97     created = find_obj_in_resp response_items, 'User', nil
98
99     assert_equal 'in_create_test_first_name', created['first_name']
100     assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
101     assert_equal 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
102     assert_not_nil created['email'], 'expected non-nil email'
103     assert_nil created['identity_url'], 'expected no identity_url'
104
105     # arvados#user, repo link and link add user to 'All users' group
106     verify_num_links @all_links_at_start, 4
107
108     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
109         created['uuid'], created['email'], 'arvados#user', false, 'User'
110
111     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
112         repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
113
114     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
115         'All users', created['uuid'], 'arvados#group', true, 'Group'
116
117     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
118         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
119
120     verify_system_group_permission_link_for created['uuid']
121   end
122
123   test "setup user with bogus uuid and expect error" do
124     authorize_with :admin
125
126     post :setup, {
127       uuid: 'bogus_uuid',
128       repo_name: 'test_repo',
129       vm_uuid: @vm_uuid
130     }
131     response_body = JSON.parse(@response.body)
132     response_errors = response_body['errors']
133     assert_not_nil response_errors, 'Expected error in response'
134     assert (response_errors.first.include? 'Path not found'), 'Expected 404'
135   end
136
137   test "setup user with bogus uuid in user and expect error" do
138     authorize_with :admin
139
140     post :setup, {
141       user: {uuid: 'bogus_uuid'},
142       repo_name: 'test_repo',
143       vm_uuid: @vm_uuid,
144       openid_prefix: 'https://www.google.com/accounts/o8/id'
145     }
146     response_body = JSON.parse(@response.body)
147     response_errors = response_body['errors']
148     assert_not_nil response_errors, 'Expected error in response'
149     assert (response_errors.first.include? 'ArgumentError: Require user email'),
150       'Expected RuntimeError'
151   end
152
153   test "setup user with no uuid and user, expect error" do
154     authorize_with :admin
155
156     post :setup, {
157       repo_name: 'test_repo',
158       vm_uuid: @vm_uuid,
159       openid_prefix: 'https://www.google.com/accounts/o8/id'
160     }
161     response_body = JSON.parse(@response.body)
162     response_errors = response_body['errors']
163     assert_not_nil response_errors, 'Expected error in response'
164     assert (response_errors.first.include? 'Required uuid or user'),
165         'Expected ArgumentError'
166   end
167
168   test "setup user with no uuid and email, expect error" do
169     authorize_with :admin
170
171     post :setup, {
172       user: {},
173       repo_name: 'test_repo',
174       vm_uuid: @vm_uuid,
175       openid_prefix: 'https://www.google.com/accounts/o8/id'
176     }
177     response_body = JSON.parse(@response.body)
178     response_errors = response_body['errors']
179     assert_not_nil response_errors, 'Expected error in response'
180     assert (response_errors.first.include? '<ArgumentError: Require user email'),
181         'Expected ArgumentError'
182   end
183
184   test "invoke setup with existing uuid, vm and repo and verify links" do
185     authorize_with :admin
186     inactive_user = users(:inactive)
187
188     post :setup, {
189       uuid: users(:inactive).uuid,
190       repo_name: 'test_repo',
191       vm_uuid: @vm_uuid
192     }
193
194     assert_response :success
195
196     response_items = JSON.parse(@response.body)['items']
197     resp_obj = find_obj_in_resp response_items, 'User', nil
198
199     assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
200     assert_equal inactive_user['uuid'], resp_obj['uuid']
201     assert_equal inactive_user['email'], resp_obj['email'],
202         'expecting inactive user email'
203
204     # expect repo and vm links
205     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
206         'test_repo', resp_obj['uuid'], 'arvados#repository', true, 'Repository'
207
208     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
209         @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
210   end
211
212   test "invoke setup with existing uuid in user, verify response" do
213     authorize_with :admin
214     inactive_user = users(:inactive)
215
216     post :setup, {
217       user: {uuid: inactive_user['uuid']},
218       openid_prefix: 'https://www.google.com/accounts/o8/id'
219     }
220
221     assert_response :success
222
223     response_items = JSON.parse(@response.body)['items']
224     resp_obj = find_obj_in_resp response_items, 'User', nil
225
226     assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
227     assert_equal inactive_user['uuid'], resp_obj['uuid']
228     assert_equal inactive_user['email'], resp_obj['email'],
229         'expecting inactive user email'
230   end
231
232   test "invoke setup with existing uuid but different email, expect original email" do
233     authorize_with :admin
234     inactive_user = users(:inactive)
235
236     post :setup, {
237       uuid: inactive_user['uuid'],
238       user: {email: 'junk_email'}
239     }
240
241     assert_response :success
242
243     response_items = JSON.parse(@response.body)['items']
244     resp_obj = find_obj_in_resp response_items, 'User', nil
245
246     assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
247     assert_equal inactive_user['uuid'], resp_obj['uuid']
248     assert_equal inactive_user['email'], resp_obj['email'],
249         'expecting inactive user email'
250   end
251
252   test "setup user with valid email and repo as input" do
253     authorize_with :admin
254
255     post :setup, {
256       repo_name: 'test_repo',
257       user: {email: 'foo@example.com'},
258       openid_prefix: 'https://www.google.com/accounts/o8/id'
259     }
260
261     assert_response :success
262     response_items = JSON.parse(@response.body)['items']
263     response_object = find_obj_in_resp response_items, 'User', nil
264     assert_not_nil response_object['uuid'], 'expected uuid for the new user'
265     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
266
267     # four extra links; system_group, login, group and repo perms
268     verify_num_links @all_links_at_start, 4
269   end
270
271   test "setup user with fake vm and expect error" do
272     authorize_with :admin
273
274     post :setup, {
275       repo_name: 'test_repo',
276       vm_uuid: 'no_such_vm',
277       user: {email: 'foo@example.com'},
278       openid_prefix: 'https://www.google.com/accounts/o8/id'
279     }
280
281     response_body = JSON.parse(@response.body)
282     response_errors = response_body['errors']
283     assert_not_nil response_errors, 'Expected error in response'
284     assert (response_errors.first.include? "No vm found for no_such_vm"),
285           'Expected RuntimeError: No vm found for no_such_vm'
286   end
287
288   test "setup user with valid email, repo and real vm as input" do
289     authorize_with :admin
290
291     post :setup, {
292       repo_name: 'test_repo',
293       openid_prefix: 'https://www.google.com/accounts/o8/id',
294       vm_uuid: @vm_uuid,
295       user: {email: 'foo@example.com'}
296     }
297
298     assert_response :success
299     response_items = JSON.parse(@response.body)['items']
300     response_object = find_obj_in_resp response_items, 'User', nil
301     assert_not_nil response_object['uuid'], 'expected uuid for the new user'
302     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
303
304     # five extra links; system_group, login, group, vm, repo
305     verify_num_links @all_links_at_start, 5
306   end
307
308   test "setup user with valid email, no vm and repo as input" do
309     authorize_with :admin
310
311     post :setup, {
312       user: {email: 'foo@example.com'},
313       openid_prefix: 'https://www.google.com/accounts/o8/id'
314     }
315
316     assert_response :success
317     response_items = JSON.parse(@response.body)['items']
318     response_object = find_obj_in_resp response_items, 'User', nil
319     assert_not_nil response_object['uuid'], 'expected uuid for new user'
320     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
321
322     # three extra links; system_group, login, and group
323     verify_num_links @all_links_at_start, 3
324   end
325
326   test "setup user with email, first name, repo name and vm uuid" do
327     authorize_with :admin
328
329     post :setup, {
330       openid_prefix: 'https://www.google.com/accounts/o8/id',
331       repo_name: 'test_repo',
332       vm_uuid: @vm_uuid,
333       user: {
334         first_name: 'test_first_name',
335         email: 'foo@example.com'
336       }
337     }
338
339     assert_response :success
340     response_items = JSON.parse(@response.body)['items']
341     response_object = find_obj_in_resp response_items, 'User', nil
342     assert_not_nil response_object['uuid'], 'expected uuid for new user'
343     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
344     assert_equal 'test_first_name', response_object['first_name'],
345         'expecting first name'
346
347     # five extra links; system_group, login, group, repo and vm
348     verify_num_links @all_links_at_start, 5
349   end
350
351   test "setup user with an exising user email and check different object is created" do
352     authorize_with :admin
353     inactive_user = users(:inactive)
354
355     post :setup, {
356       openid_prefix: 'https://www.google.com/accounts/o8/id',
357       repo_name: 'test_repo',
358       user: {
359         email: inactive_user['email']
360       }
361     }
362
363     assert_response :success
364     response_items = JSON.parse(@response.body)['items']
365     response_object = find_obj_in_resp response_items, 'User', nil
366     assert_not_nil response_object['uuid'], 'expected uuid for new user'
367     assert_not_equal response_object['uuid'], inactive_user['uuid'],
368         'expected different uuid after create operation'
369     assert_equal inactive_user['email'], response_object['email'], 'expected given email'
370     # system_group, openid, group, and repo. No vm link.
371     verify_num_links @all_links_at_start, 4
372   end
373
374   test "setup user with openid prefix" do
375     authorize_with :admin
376
377     post :setup, {
378       repo_name: 'test_repo',
379       openid_prefix: 'http://www.example.com/account',
380       user: {
381         first_name: "in_create_test_first_name",
382         last_name: "test_last_name",
383         email: "foo@example.com"
384       }
385     }
386
387     assert_response :success
388
389     response_items = JSON.parse(@response.body)['items']
390     created = find_obj_in_resp response_items, 'User', nil
391
392     assert_equal 'in_create_test_first_name', created['first_name']
393     assert_not_nil created['uuid'], 'expected uuid for new user'
394     assert_not_nil created['email'], 'expected non-nil email'
395     assert_nil created['identity_url'], 'expected no identity_url'
396
397     # verify links
398     # four new links: system_group, arvados#user, repo, and 'All users' group.
399     verify_num_links @all_links_at_start, 4
400
401     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
402         created['uuid'], created['email'], 'arvados#user', false, 'User'
403
404     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
405         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
406
407     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
408         'All users', created['uuid'], 'arvados#group', true, 'Group'
409
410     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
411         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
412   end
413
414   test "invoke setup with no openid prefix, expect error" do
415     authorize_with :admin
416
417     post :setup, {
418       repo_name: 'test_repo',
419       user: {
420         first_name: "in_create_test_first_name",
421         last_name: "test_last_name",
422         email: "foo@example.com"
423       }
424     }
425
426     response_body = JSON.parse(@response.body)
427     response_errors = response_body['errors']
428     assert_not_nil response_errors, 'Expected error in response'
429     assert (response_errors.first.include? 'openid_prefix parameter is missing'),
430         'Expected ArgumentError'
431   end
432
433   test "setup user with user, vm and repo and verify links" do
434     authorize_with :admin
435
436     post :setup, {
437       user: {
438         first_name: "in_create_test_first_name",
439         last_name: "test_last_name",
440         email: "foo@example.com"
441       },
442       vm_uuid: @vm_uuid,
443       repo_name: 'test_repo',
444       openid_prefix: 'https://www.google.com/accounts/o8/id'
445     }
446
447     assert_response :success
448
449     response_items = JSON.parse(@response.body)['items']
450     created = find_obj_in_resp response_items, 'User', nil
451
452     assert_equal 'in_create_test_first_name', created['first_name']
453     assert_not_nil created['uuid'], 'expected uuid for new user'
454     assert_not_nil created['email'], 'expected non-nil email'
455     assert_nil created['identity_url'], 'expected no identity_url'
456
457     # five new links: system_group, arvados#user, repo, vm and 'All
458     # users' group link
459     verify_num_links @all_links_at_start, 5
460
461     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
462         created['uuid'], created['email'], 'arvados#user', false, 'User'
463
464     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
465         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
466
467     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
468         'All users', created['uuid'], 'arvados#group', true, 'Group'
469
470     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
471         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
472   end
473
474   test "create user as non admin user and expect error" do
475     authorize_with :active
476
477     post :create, {
478       user: {email: 'foo@example.com'}
479     }
480
481     response_body = JSON.parse(@response.body)
482     response_errors = response_body['errors']
483     assert_not_nil response_errors, 'Expected error in response'
484     assert (response_errors.first.include? 'PermissionDenied'),
485           'Expected PermissionDeniedError'
486   end
487
488   test "setup user as non admin user and expect error" do
489     authorize_with :active
490
491     post :setup, {
492       openid_prefix: 'https://www.google.com/accounts/o8/id',
493       user: {email: 'foo@example.com'}
494     }
495
496     response_body = JSON.parse(@response.body)
497     response_errors = response_body['errors']
498     assert_not_nil response_errors, 'Expected error in response'
499     assert (response_errors.first.include? 'Forbidden'),
500           'Expected Forbidden error'
501   end
502
503   test "setup user in multiple steps and verify response" do
504     authorize_with :admin
505
506     post :setup, {
507       openid_prefix: 'http://www.example.com/account',
508       user: {
509         email: "foo@example.com"
510       }
511     }
512
513     assert_response :success
514     response_items = JSON.parse(@response.body)['items']
515     created = find_obj_in_resp response_items, 'User', nil
516
517     assert_not_nil created['uuid'], 'expected uuid for new user'
518     assert_not_nil created['email'], 'expected non-nil email'
519     assert_equal created['email'], 'foo@example.com', 'expected input email'
520
521     # three new links: system_group, arvados#user, and 'All users' group.
522     verify_num_links @all_links_at_start, 3
523
524     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
525         created['uuid'], created['email'], 'arvados#user', false, 'User'
526
527     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
528         'All users', created['uuid'], 'arvados#group', true, 'Group'
529
530     verify_link response_items, 'arvados#repository', false, 'permission', 'can_manage',
531         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
532
533     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
534         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
535
536    # invoke setup with a repository
537     post :setup, {
538       openid_prefix: 'http://www.example.com/account',
539       repo_name: 'new_repo',
540       uuid: created['uuid']
541     }
542
543     assert_response :success
544
545     response_items = JSON.parse(@response.body)['items']
546     created = find_obj_in_resp response_items, 'User', nil
547
548     assert_equal 'foo@example.com', created['email'], 'expected input email'
549
550      # verify links
551     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
552         'All users', created['uuid'], 'arvados#group', true, 'Group'
553
554     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
555         'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
556
557     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
558         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
559
560     # invoke setup with a vm_uuid
561     post :setup, {
562       vm_uuid: @vm_uuid,
563       openid_prefix: 'http://www.example.com/account',
564       user: {
565         email: 'junk_email'
566       },
567       uuid: created['uuid']
568     }
569
570     assert_response :success
571
572     response_items = JSON.parse(@response.body)['items']
573     created = find_obj_in_resp response_items, 'User', nil
574
575     assert_equal created['email'], 'foo@example.com', 'expected original email'
576
577     # verify links
578     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
579         'All users', created['uuid'], 'arvados#group', true, 'Group'
580
581     # since no repo name in input, we won't get any; even though user has one
582     verify_link response_items, 'arvados#repository', false, 'permission', 'can_manage',
583         'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
584
585     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
586         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
587   end
588
589   test "setup and unsetup user" do
590     authorize_with :admin
591
592     post :setup, {
593       repo_name: 'test_repo',
594       vm_uuid: @vm_uuid,
595       user: {email: 'foo@example.com'},
596       openid_prefix: 'https://www.google.com/accounts/o8/id'
597     }
598
599     assert_response :success
600     response_items = JSON.parse(@response.body)['items']
601     created = find_obj_in_resp response_items, 'User', nil
602     assert_not_nil created['uuid'], 'expected uuid for the new user'
603     assert_equal created['email'], 'foo@example.com', 'expected given email'
604
605     # five extra links: system_group, login, group, repo and vm
606     verify_num_links @all_links_at_start, 5
607
608     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
609         created['uuid'], created['email'], 'arvados#user', false, 'User'
610
611     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
612         'All users', created['uuid'], 'arvados#group', true, 'Group'
613
614     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
615         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
616
617     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
618         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
619
620     verify_link_existence created['uuid'], created['email'], true, true, true, true, false
621
622     # now unsetup this user
623     post :unsetup, id: created['uuid']
624     assert_response :success
625
626     created2 = JSON.parse(@response.body)
627     assert_not_nil created2['uuid'], 'expected uuid for the newly created user'
628     assert_equal created['uuid'], created2['uuid'], 'expected uuid not found'
629
630     verify_link_existence created['uuid'], created['email'], false, false, false, false, false
631   end
632
633   test "unsetup active user" do
634     active_user = users(:active)
635     assert_not_nil active_user['uuid'], 'expected uuid for the active user'
636     assert active_user['is_active'], 'expected is_active for active user'
637
638     verify_link_existence active_user['uuid'], active_user['email'],
639           false, true, false, true, true
640
641     authorize_with :admin
642
643     # now unsetup this user
644     post :unsetup, id: active_user['uuid']
645     assert_response :success
646
647     response_user = JSON.parse(@response.body)
648     assert_not_nil response_user['uuid'], 'expected uuid for the upsetup user'
649     assert_equal active_user['uuid'], response_user['uuid'], 'expected uuid not found'
650     assert !response_user['is_active'], 'expected user to be inactive'
651     assert !response_user['is_invited'], 'expected user to be uninvited'
652
653     verify_link_existence response_user['uuid'], response_user['email'],
654           false, false, false, false, false
655
656     active_user = User.find_by_uuid(users(:active).uuid)
657     readable_groups = active_user.groups_i_can(:read)
658     all_users_group = Group.all.collect(&:uuid).select { |g| g.match /-f+$/ }
659     refute_includes(readable_groups, all_users_group,
660                     "active user can read All Users group after being deactivated")
661     assert_equal(false, active_user.is_invited,
662                  "active user is_invited after being deactivated & reloaded")
663   end
664
665   test "setup user with send notification param false and verify no email" do
666     authorize_with :admin
667
668     post :setup, {
669       openid_prefix: 'http://www.example.com/account',
670       send_notification_email: 'false',
671       user: {
672         email: "foo@example.com"
673       }
674     }
675
676     assert_response :success
677     response_items = JSON.parse(@response.body)['items']
678     created = find_obj_in_resp response_items, 'User', nil
679     assert_not_nil created['uuid'], 'expected uuid for the new user'
680     assert_equal created['email'], 'foo@example.com', 'expected given email'
681
682     setup_email = ActionMailer::Base.deliveries.last
683     assert_nil setup_email, 'expected no setup email'
684   end
685
686   test "setup user with send notification param true and verify email" do
687     authorize_with :admin
688
689     post :setup, {
690       openid_prefix: 'http://www.example.com/account',
691       send_notification_email: 'true',
692       user: {
693         email: "foo@example.com"
694       }
695     }
696
697     assert_response :success
698     response_items = JSON.parse(@response.body)['items']
699     created = find_obj_in_resp response_items, 'User', nil
700     assert_not_nil created['uuid'], 'expected uuid for the new user'
701     assert_equal created['email'], 'foo@example.com', 'expected given email'
702
703     setup_email = ActionMailer::Base.deliveries.last
704     assert_not_nil setup_email, 'Expected email after setup'
705
706     assert_equal Rails.configuration.user_notifier_email_from, setup_email.from[0]
707     assert_equal 'foo@example.com', setup_email.to[0]
708     assert_equal 'Welcome to Curoverse', setup_email.subject
709     assert (setup_email.body.to_s.include? 'Your Arvados account has been set up'),
710         'Expected Your Arvados account has been set up in email body'
711     assert (setup_email.body.to_s.include? 'foo@example.com'),
712         'Expected user email in email body'
713     assert (setup_email.body.to_s.include? Rails.configuration.workbench_address),
714         'Expected workbench url in email body'
715   end
716
717   test "non-admin user can get basic information about readable users" do
718     authorize_with :spectator
719     get(:index)
720     check_non_admin_index
721     check_readable_users_index [:spectator], [:inactive, :active]
722   end
723
724   test "non-admin user gets only safe attributes from users#show" do
725     g = act_as_system_user do
726       create :group
727     end
728     users = create_list :active_user, 2, join_groups: [g]
729     token = create :token, user: users[0]
730     authorize_with_token token
731     get :show, id: users[1].uuid
732     check_non_admin_show
733   end
734
735   test "non-admin user can limit index" do
736     g = act_as_system_user do
737       create :group
738     end
739     users = create_list :active_user, 4, join_groups: [g]
740     token = create :token, user: users[0]
741
742     [2, 4].each do |limit|
743       authorize_with_token token
744       get(:index, limit: limit)
745       check_non_admin_index
746       assert_equal(limit, json_response["items"].size,
747                    "non-admin index limit was ineffective")
748     end
749   end
750
751   test "admin has full index powers" do
752     authorize_with :admin
753     check_inactive_user_findable
754   end
755
756   test "reader token can grant admin index powers" do
757     authorize_with :spectator
758     check_inactive_user_findable(reader_tokens: [api_token(:admin)])
759   end
760
761   test "admin can filter on user.is_active" do
762     authorize_with :admin
763     get(:index, filters: [["is_active", "=", "true"]])
764     assert_response :success
765     check_readable_users_index [:active, :spectator], [:inactive]
766   end
767
768   test "admin can search where user.is_active" do
769     authorize_with :admin
770     get(:index, where: {is_active: true})
771     assert_response :success
772     check_readable_users_index [:active, :spectator], [:inactive]
773   end
774
775   test "update active_no_prefs user profile and expect notification email" do
776     authorize_with :admin
777
778     put :update, {
779       id: users(:active_no_prefs).uuid,
780       user: {
781         prefs: {:profile => {'organization' => 'example.com'}}
782       }
783     }
784     assert_response :success
785
786     found_email = false
787     ActionMailer::Base.deliveries.andand.each do |email|
788       if email.subject == "Profile created by #{users(:active_no_prefs).email}"
789         found_email = true
790         break
791       end
792     end
793     assert_equal true, found_email, 'Expected email after creating profile'
794   end
795
796   test "update active_no_prefs_profile user profile and expect notification email" do
797     authorize_with :admin
798
799     user = {}
800     user[:prefs] = users(:active_no_prefs_profile).prefs
801     user[:prefs][:profile] = {:profile => {'organization' => 'example.com'}}
802     put :update, {
803       id: users(:active_no_prefs_profile).uuid,
804       user: user
805     }
806     assert_response :success
807
808     found_email = false
809     ActionMailer::Base.deliveries.andand.each do |email|
810       if email.subject == "Profile created by #{users(:active_no_prefs_profile).email}"
811         found_email = true
812         break
813       end
814     end
815     assert_equal true, found_email, 'Expected email after creating profile'
816   end
817
818   test "update active user profile and expect no notification email" do
819     authorize_with :admin
820
821     put :update, {
822       id: users(:active).uuid,
823       user: {
824         prefs: {:profile => {'organization' => 'anotherexample.com'}}
825       }
826     }
827     assert_response :success
828
829     found_email = false
830     ActionMailer::Base.deliveries.andand.each do |email|
831       if email.subject == "Profile created by #{users(:active).email}"
832         found_email = true
833         break
834       end
835     end
836     assert_equal false, found_email, 'Expected no email after updating profile'
837   end
838
839
840   NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "email", "first_name",
841                          "last_name"].sort
842
843   def check_non_admin_index
844     assert_response :success
845     response_items = json_response["items"]
846     assert_not_nil response_items
847     response_items.each do |user_data|
848       check_non_admin_item user_data
849       assert(user_data["is_active"], "non-admin index returned inactive user")
850     end
851   end
852
853   def check_non_admin_show
854     assert_response :success
855     check_non_admin_item json_response
856   end
857
858   def check_non_admin_item user_data
859     assert_equal(NON_ADMIN_USER_DATA, user_data.keys.sort,
860                  "data in response had missing or extra attributes")
861     assert_equal("arvados#user", user_data["kind"])
862   end
863
864
865   def check_readable_users_index expect_present, expect_missing
866     response_uuids = json_response["items"].map { |u| u["uuid"] }
867     expect_present.each do |user_key|
868       assert_includes(response_uuids, users(user_key).uuid,
869                       "#{user_key} missing from index")
870     end
871     expect_missing.each do |user_key|
872       refute_includes(response_uuids, users(user_key).uuid,
873                       "#{user_key} included in index")
874     end
875   end
876
877   def check_inactive_user_findable(params={})
878     inactive_user = users(:inactive)
879     get(:index, params.merge(filters: [["email", "=", inactive_user.email]]))
880     assert_response :success
881     user_list = json_response["items"]
882     assert_equal(1, user_list.andand.count)
883     # This test needs to check a column non-admins have no access to,
884     # to ensure that admins see all user information.
885     assert_equal(inactive_user.identity_url, user_list.first["identity_url"],
886                  "admin's filtered index did not return inactive user")
887   end
888
889   def verify_num_links (original_links, expected_additional_links)
890     links_now = Link.all
891     assert_equal expected_additional_links, Link.all.size-original_links.size,
892         "Expected #{expected_additional_links.inspect} more links"
893   end
894
895   def find_obj_in_resp (response_items, object_type, head_kind=nil)
896     return_obj = nil
897     response_items
898     response_items.each { |x|
899       if !x
900         next
901       end
902
903       if object_type == 'User'
904         if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
905           return_obj = x
906           break
907         end
908       else  # looking for a link
909         if x['head_uuid'] and ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
910           return_obj = x
911           break
912         end
913       end
914     }
915     return return_obj
916   end
917
918   def verify_link(response_items, link_object_name, expect_link, link_class,
919         link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
920
921     link = find_obj_in_resp response_items, 'Link', link_object_name
922
923     if !expect_link
924       assert_nil link, "Expected no link for #{link_object_name}"
925       return
926     end
927
928     assert_not_nil link, "Expected link for #{link_object_name}"
929
930     if fetch_object
931       object = Object.const_get(class_name).where(name: head_uuid)
932       assert [] != object, "expected #{class_name} with name #{head_uuid}"
933       head_uuid = object.first[:uuid]
934     end
935     assert_equal link_class, link['link_class'],
936         "did not find expected link_class for #{link_object_name}"
937
938     assert_equal link_name, link['name'],
939         "did not find expected link_name for #{link_object_name}"
940
941     assert_equal tail_uuid, link['tail_uuid'],
942         "did not find expected tail_uuid for #{link_object_name}"
943
944     assert_equal head_kind, link['head_kind'],
945         "did not find expected head_kind for #{link_object_name}"
946
947     assert_equal head_uuid, link['head_uuid'],
948         "did not find expected head_uuid for #{link_object_name}"
949   end
950
951   def verify_link_existence uuid, email, expect_oid_login_perms,
952       expect_repo_perms, expect_vm_perms, expect_group_perms, expect_signatures
953     # verify that all links are deleted for the user
954     oid_login_perms = Link.where(tail_uuid: email,
955                                  link_class: 'permission',
956                                  name: 'can_login').where("head_uuid like ?", User.uuid_like_pattern)
957     if expect_oid_login_perms
958       assert oid_login_perms.any?, "expected oid_login_perms"
959     else
960       assert !oid_login_perms.any?, "expected all oid_login_perms deleted"
961     end
962
963     repo_perms = Link.where(tail_uuid: uuid,
964                             link_class: 'permission',
965                             name: 'can_manage').where("head_uuid like ?", Repository.uuid_like_pattern)
966     if expect_repo_perms
967       assert repo_perms.any?, "expected repo_perms"
968     else
969       assert !repo_perms.any?, "expected all repo_perms deleted"
970     end
971
972     vm_login_perms = Link.
973       where(tail_uuid: uuid,
974             link_class: 'permission',
975             name: 'can_login').
976       where("head_uuid like ?",
977             VirtualMachine.uuid_like_pattern).
978       where('uuid <> ?',
979             links(:auto_setup_vm_login_username_can_login_to_test_vm).uuid)
980     if expect_vm_perms
981       assert vm_login_perms.any?, "expected vm_login_perms"
982     else
983       assert !vm_login_perms.any?, "expected all vm_login_perms deleted"
984     end
985
986     group = Group.where(name: 'All users').select do |g|
987       g[:uuid].match /-f+$/
988     end.first
989     group_read_perms = Link.where(tail_uuid: uuid,
990                                   head_uuid: group[:uuid],
991                                   link_class: 'permission',
992                                   name: 'can_read')
993     if expect_group_perms
994       assert group_read_perms.any?, "expected all users group read perms"
995     else
996       assert !group_read_perms.any?, "expected all users group perm deleted"
997     end
998
999     signed_uuids = Link.where(link_class: 'signature',
1000                               tail_uuid: uuid)
1001
1002     if expect_signatures
1003       assert signed_uuids.any?, "expected signatures"
1004     else
1005       assert !signed_uuids.any?, "expected all signatures deleted"
1006     end
1007
1008   end
1009
1010   def verify_system_group_permission_link_for user_uuid
1011     assert_equal 1, Link.where(link_class: 'permission',
1012                                name: 'can_manage',
1013                                tail_uuid: system_group_uuid,
1014                                head_uuid: user_uuid).count
1015   end
1016 end