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