Fix exception when valid token points to missing user
[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, uuid: 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     authorize_with :inactive
25     get :current
26     assert_response :success
27     me = JSON.parse(@response.body)
28     post :activate, uuid: me['uuid']
29     assert_response 403
30     get :current
31     assert_response :success
32     me = JSON.parse(@response.body)
33     assert_equal false, me['is_active']
34   end
35
36   test "activate an already-active user" do
37     authorize_with :active
38     get :current
39     assert_response :success
40     me = JSON.parse(@response.body)
41     post :activate, uuid: me['uuid']
42     assert_response :success
43     me = JSON.parse(@response.body)
44     assert_equal true, me['is_active']
45   end
46
47   test "respond 401 if given token exists but user record is missing" do
48     authorize_with :valid_token_deleted_user
49     get :current, {format: :json}
50     assert_response 401
51   end
52
53   test "create new user with user as input" do
54     authorize_with :admin
55     post :create, user: {
56       first_name: "test_first_name",
57       last_name: "test_last_name",
58       email: "foo@example.com"
59     }
60     assert_response :success
61     created = JSON.parse(@response.body)
62     assert_equal 'test_first_name', created['first_name']
63     assert_not_nil created['uuid'], 'expected uuid for the newly created user'
64     assert_not_nil created['email'], 'expected non-nil email'
65     assert_nil created['identity_url'], 'expected no identity_url'
66   end
67
68   test "create user with user, vm and repo as input" do
69     authorize_with :admin
70     repo_name = 'test_repo'
71
72     post :setup, {
73       repo_name: repo_name,
74       openid_prefix: 'https://www.google.com/accounts/o8/id',
75       user: {
76         uuid: "this_is_agreeable",
77         first_name: "in_create_test_first_name",
78         last_name: "test_last_name",
79         email: "foo@example.com"
80       }
81     }
82     assert_response :success
83     response_items = JSON.parse(@response.body)['items']
84
85     created = find_obj_in_resp response_items, 'User', nil
86     assert_equal 'in_create_test_first_name', created['first_name']
87     assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
88     assert_equal 'this_is_agreeable', created['uuid']
89     assert_not_nil created['email'], 'expected non-nil email'
90     assert_nil created['identity_url'], 'expected no identity_url'
91
92     # arvados#user, repo link and link add user to 'All users' group
93     verify_num_links @all_links_at_start, 4
94
95     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
96         created['uuid'], created['email'], 'arvados#user', false, 'User'
97
98     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
99         repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
100
101     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
102         'All users', created['uuid'], 'arvados#group', true, 'Group'
103
104     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
105         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
106
107     verify_system_group_permission_link_for created['uuid']
108
109     # invoke setup again with the same data
110     post :setup, {
111       repo_name: repo_name,
112       vm_uuid: @vm_uuid,
113       openid_prefix: 'https://www.google.com/accounts/o8/id',
114       user: {
115         uuid: "this_is_agreeable",
116         first_name: "in_create_test_first_name",
117         last_name: "test_last_name",
118         email: "foo@example.com"
119       }
120     }
121
122     response_items = JSON.parse(@response.body)['items']
123
124     created = find_obj_in_resp response_items, 'User', nil
125     assert_equal 'in_create_test_first_name', created['first_name']
126     assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
127     assert_equal 'this_is_agreeable', created['uuid']
128     assert_not_nil created['email'], 'expected non-nil email'
129     assert_nil created['identity_url'], 'expected no identity_url'
130
131     # arvados#user, repo link and link add user to 'All users' group
132     verify_num_links @all_links_at_start, 5
133
134     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
135         repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
136
137     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
138         'All users', created['uuid'], 'arvados#group', true, 'Group'
139
140     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
141         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
142
143     verify_system_group_permission_link_for created['uuid']
144   end
145
146   test "setup user with bogus uuid and expect error" do
147     authorize_with :admin
148
149     post :setup, {
150       uuid: 'bogus_uuid',
151       repo_name: 'test_repo',
152       vm_uuid: @vm_uuid
153     }
154     response_body = JSON.parse(@response.body)
155     response_errors = response_body['errors']
156     assert_not_nil response_errors, 'Expected error in response'
157     assert (response_errors.first.include? 'Path not found'), 'Expected 404'
158   end
159
160   test "setup user with bogus uuid in user and expect error" do
161     authorize_with :admin
162
163     post :setup, {
164       user: {uuid: 'bogus_uuid'},
165       repo_name: 'test_repo',
166       vm_uuid: @vm_uuid,
167       openid_prefix: 'https://www.google.com/accounts/o8/id'
168     }
169     response_body = JSON.parse(@response.body)
170     response_errors = response_body['errors']
171     assert_not_nil response_errors, 'Expected error in response'
172     assert (response_errors.first.include? 'ArgumentError: Require user email'),
173       'Expected RuntimeError'
174   end
175
176   test "setup user with no uuid and user, expect error" do
177     authorize_with :admin
178
179     post :setup, {
180       repo_name: 'test_repo',
181       vm_uuid: @vm_uuid,
182       openid_prefix: 'https://www.google.com/accounts/o8/id'
183     }
184     response_body = JSON.parse(@response.body)
185     response_errors = response_body['errors']
186     assert_not_nil response_errors, 'Expected error in response'
187     assert (response_errors.first.include? 'Required uuid or user'),
188         'Expected ArgumentError'
189   end
190
191   test "setup user with no uuid and email, expect error" do
192     authorize_with :admin
193
194     post :setup, {
195       user: {},
196       repo_name: 'test_repo',
197       vm_uuid: @vm_uuid,
198       openid_prefix: 'https://www.google.com/accounts/o8/id'
199     }
200     response_body = JSON.parse(@response.body)
201     response_errors = response_body['errors']
202     assert_not_nil response_errors, 'Expected error in response'
203     assert (response_errors.first.include? '<ArgumentError: Require user email'),
204         'Expected ArgumentError'
205   end
206
207   test "invoke setup with existing uuid, vm and repo and verify links" do
208     authorize_with :inactive
209     get :current
210     assert_response :success
211     inactive_user = JSON.parse(@response.body)
212
213     authorize_with :admin
214
215     post :setup, {
216       uuid: inactive_user['uuid'],
217       repo_name: 'test_repo',
218       vm_uuid: @vm_uuid
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
231     # expect repo and vm links
232     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
233         'test_repo', resp_obj['uuid'], 'arvados#repository', true, 'Repository'
234
235     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
236         @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
237   end
238
239   test "invoke setup with existing uuid in user, verify response" do
240     authorize_with :inactive
241     get :current
242     assert_response :success
243     inactive_user = JSON.parse(@response.body)
244
245     authorize_with :admin
246
247     post :setup, {
248       user: {uuid: inactive_user['uuid']},
249       openid_prefix: 'https://www.google.com/accounts/o8/id'
250     }
251
252     assert_response :success
253
254     response_items = JSON.parse(@response.body)['items']
255     resp_obj = find_obj_in_resp response_items, 'User', nil
256
257     assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
258     assert_equal inactive_user['uuid'], resp_obj['uuid']
259     assert_equal inactive_user['email'], resp_obj['email'],
260         'expecting inactive user email'
261   end
262
263   test "invoke setup with existing uuid but different email, expect original email" do
264     authorize_with :inactive
265     get :current
266     assert_response :success
267     inactive_user = JSON.parse(@response.body)
268
269     authorize_with :admin
270
271     post :setup, {
272       uuid: inactive_user['uuid'],
273       user: {email: 'junk_email'}
274     }
275
276     assert_response :success
277
278     response_items = JSON.parse(@response.body)['items']
279     resp_obj = find_obj_in_resp response_items, 'User', nil
280
281     assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
282     assert_equal inactive_user['uuid'], resp_obj['uuid']
283     assert_equal inactive_user['email'], resp_obj['email'],
284         'expecting inactive user email'
285   end
286
287   test "setup user with valid email and repo as input" do
288     authorize_with :admin
289
290     post :setup, {
291       repo_name: 'test_repo',
292       user: {email: 'foo@example.com'},
293       openid_prefix: 'https://www.google.com/accounts/o8/id'
294     }
295
296     assert_response :success
297     response_items = JSON.parse(@response.body)['items']
298     response_object = find_obj_in_resp response_items, 'User', nil
299     assert_not_nil response_object['uuid'], 'expected uuid for the new user'
300     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
301
302     # four extra links; system_group, login, group and repo perms
303     verify_num_links @all_links_at_start, 4
304   end
305
306   test "setup user with fake vm and expect error" do
307     authorize_with :admin
308
309     post :setup, {
310       repo_name: 'test_repo',
311       vm_uuid: 'no_such_vm',
312       user: {email: 'foo@example.com'},
313       openid_prefix: 'https://www.google.com/accounts/o8/id'
314     }
315
316     response_body = JSON.parse(@response.body)
317     response_errors = response_body['errors']
318     assert_not_nil response_errors, 'Expected error in response'
319     assert (response_errors.first.include? "No vm found for no_such_vm"),
320           'Expected RuntimeError: No vm found for no_such_vm'
321   end
322
323   test "setup user with valid email, repo and real vm as input" do
324     authorize_with :admin
325
326     post :setup, {
327       repo_name: 'test_repo',
328       openid_prefix: 'https://www.google.com/accounts/o8/id',
329       vm_uuid: @vm_uuid,
330       user: {email: 'foo@example.com'}
331     }
332
333     assert_response :success
334     response_items = JSON.parse(@response.body)['items']
335     response_object = find_obj_in_resp response_items, 'User', nil
336     assert_not_nil response_object['uuid'], 'expected uuid for the new user'
337     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
338
339     # five extra links; system_group, login, group, vm, repo
340     verify_num_links @all_links_at_start, 5
341   end
342
343   test "setup user with valid email, no vm and repo as input" do
344     authorize_with :admin
345
346     post :setup, {
347       user: {email: 'foo@example.com'},
348       openid_prefix: 'https://www.google.com/accounts/o8/id'
349     }
350
351     assert_response :success
352     response_items = JSON.parse(@response.body)['items']
353     response_object = find_obj_in_resp response_items, 'User', nil
354     assert_not_nil response_object['uuid'], 'expected uuid for new user'
355     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
356
357     # three extra links; system_group, login, and group
358     verify_num_links @all_links_at_start, 3
359   end
360
361   test "setup user with email, first name, repo name and vm uuid" do
362     authorize_with :admin
363
364     post :setup, {
365       openid_prefix: 'https://www.google.com/accounts/o8/id',
366       repo_name: 'test_repo',
367       vm_uuid: @vm_uuid,
368       user: {
369         first_name: 'test_first_name',
370         email: 'foo@example.com'
371       }
372     }
373
374     assert_response :success
375     response_items = JSON.parse(@response.body)['items']
376     response_object = find_obj_in_resp response_items, 'User', nil
377     assert_not_nil response_object['uuid'], 'expected uuid for new user'
378     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
379     assert_equal 'test_first_name', response_object['first_name'],
380         'expecting first name'
381
382     # five extra links; system_group, login, group, repo and vm
383     verify_num_links @all_links_at_start, 5
384   end
385
386   test "setup user twice with email and check two different objects created" do
387     authorize_with :admin
388
389     post :setup, {
390       openid_prefix: 'https://www.google.com/accounts/o8/id',
391       repo_name: 'test_repo',
392       user: {
393         email: 'foo@example.com'
394       }
395     }
396
397     assert_response :success
398     response_items = JSON.parse(@response.body)['items']
399     response_object = find_obj_in_resp response_items, 'User', nil
400     assert_not_nil response_object['uuid'], 'expected uuid for new user'
401     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
402     # system_group, openid, group, and repo. No vm link.
403     verify_num_links @all_links_at_start, 4
404
405     # create again
406     post :setup, {
407       user: {email: 'foo@example.com'},
408       openid_prefix: 'https://www.google.com/accounts/o8/id'
409     }
410
411     assert_response :success
412     response_items = JSON.parse(@response.body)['items']
413     response_object2 = find_obj_in_resp response_items, 'User', nil
414     assert_not_equal response_object['uuid'], response_object2['uuid'],
415         'expected same uuid as first create operation'
416     assert_equal response_object['email'], 'foo@example.com', 'expected given email'
417
418     # +1 extra login link +1 extra system_group link pointing to the new User
419     verify_num_links @all_links_at_start, 6
420   end
421
422   test "setup user with openid prefix" do
423     authorize_with :admin
424
425     post :setup, {
426       repo_name: 'test_repo',
427       openid_prefix: 'http://www.example.com/account',
428       user: {
429         first_name: "in_create_test_first_name",
430         last_name: "test_last_name",
431         email: "foo@example.com"
432       }
433     }
434
435     assert_response :success
436
437     response_items = JSON.parse(@response.body)['items']
438     created = find_obj_in_resp response_items, 'User', nil
439
440     assert_equal 'in_create_test_first_name', created['first_name']
441     assert_not_nil created['uuid'], 'expected uuid for new user'
442     assert_not_nil created['email'], 'expected non-nil email'
443     assert_nil created['identity_url'], 'expected no identity_url'
444
445     # verify links
446     # four new links: system_group, arvados#user, repo, and 'All users' group.
447     verify_num_links @all_links_at_start, 4
448
449     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
450         created['uuid'], created['email'], 'arvados#user', false, 'User'
451
452     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
453         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
454
455     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
456         'All users', created['uuid'], 'arvados#group', true, 'Group'
457
458     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
459         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
460   end
461
462   test "invoke setup with no openid prefix, expect error" do
463     authorize_with :admin
464
465     post :setup, {
466       repo_name: 'test_repo',
467       user: {
468         first_name: "in_create_test_first_name",
469         last_name: "test_last_name",
470         email: "foo@example.com"
471       }
472     }
473
474     response_body = JSON.parse(@response.body)
475     response_errors = response_body['errors']
476     assert_not_nil response_errors, 'Expected error in response'
477     assert (response_errors.first.include? 'openid_prefix parameter is missing'),
478         'Expected ArgumentError'
479   end
480
481   test "setup user with user, vm and repo and verify links" do
482     authorize_with :admin
483
484     post :setup, {
485       user: {
486         first_name: "in_create_test_first_name",
487         last_name: "test_last_name",
488         email: "foo@example.com"
489       },
490       vm_uuid: @vm_uuid,
491       repo_name: 'test_repo',
492       openid_prefix: 'https://www.google.com/accounts/o8/id'
493     }
494
495     assert_response :success
496
497     response_items = JSON.parse(@response.body)['items']
498     created = find_obj_in_resp response_items, 'User', nil
499
500     assert_equal 'in_create_test_first_name', created['first_name']
501     assert_not_nil created['uuid'], 'expected uuid for new user'
502     assert_not_nil created['email'], 'expected non-nil email'
503     assert_nil created['identity_url'], 'expected no identity_url'
504
505     # five new links: system_group, arvados#user, repo, vm and 'All
506     # users' group link
507     verify_num_links @all_links_at_start, 5
508
509     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
510         created['uuid'], created['email'], 'arvados#user', false, 'User'
511
512     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
513         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
514
515     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
516         'All users', created['uuid'], 'arvados#group', true, 'Group'
517
518     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
519         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
520   end
521
522   test "create user as non admin user and expect error" do
523     authorize_with :active
524
525     post :create, {
526       user: {email: 'foo@example.com'}
527     }
528
529     response_body = JSON.parse(@response.body)
530     response_errors = response_body['errors']
531     assert_not_nil response_errors, 'Expected error in response'
532     assert (response_errors.first.include? 'PermissionDenied'),
533           'Expected PermissionDeniedError'
534   end
535
536   test "setup user as non admin user and expect error" do
537     authorize_with :active
538
539     post :setup, {
540       openid_prefix: 'https://www.google.com/accounts/o8/id',
541       user: {email: 'foo@example.com'}
542     }
543
544     response_body = JSON.parse(@response.body)
545     response_errors = response_body['errors']
546     assert_not_nil response_errors, 'Expected error in response'
547     assert (response_errors.first.include? 'Forbidden'),
548           'Expected Forbidden error'
549   end
550
551   test "setup user in multiple steps and verify response" do
552     authorize_with :admin
553
554     post :setup, {
555       openid_prefix: 'http://www.example.com/account',
556       user: {
557         email: "foo@example.com"
558       }
559     }
560
561     assert_response :success
562     response_items = JSON.parse(@response.body)['items']
563     created = find_obj_in_resp response_items, 'User', nil
564
565     assert_not_nil created['uuid'], 'expected uuid for new user'
566     assert_not_nil created['email'], 'expected non-nil email'
567     assert_equal created['email'], 'foo@example.com', 'expected input email'
568
569     # three new links: system_group, arvados#user, and 'All users' group.
570     verify_num_links @all_links_at_start, 3
571
572     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
573         created['uuid'], created['email'], 'arvados#user', false, 'User'
574
575     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
576         'All users', created['uuid'], 'arvados#group', true, 'Group'
577
578     verify_link response_items, 'arvados#repository', false, 'permission', 'can_write',
579         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
580
581     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
582         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
583
584    # invoke setup with a repository
585     post :setup, {
586       openid_prefix: 'http://www.example.com/account',
587       repo_name: 'new_repo',
588       uuid: created['uuid']
589     }
590
591     assert_response :success
592
593     response_items = JSON.parse(@response.body)['items']
594     created = find_obj_in_resp response_items, 'User', nil
595
596     assert_equal 'foo@example.com', created['email'], 'expected input email'
597
598      # verify links
599     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
600         'All users', created['uuid'], 'arvados#group', true, 'Group'
601
602     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
603         'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
604
605     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
606         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
607
608     # invoke setup with a vm_uuid
609     post :setup, {
610       vm_uuid: @vm_uuid,
611       openid_prefix: 'http://www.example.com/account',
612       user: {
613         email: 'junk_email'
614       },
615       uuid: created['uuid']
616     }
617
618     assert_response :success
619
620     response_items = JSON.parse(@response.body)['items']
621     created = find_obj_in_resp response_items, 'User', nil
622
623     assert_equal created['email'], 'foo@example.com', 'expected original email'
624
625     # verify links
626     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
627         'All users', created['uuid'], 'arvados#group', true, 'Group'
628
629     # since no repo name in input, we won't get any; even though user has one
630     verify_link response_items, 'arvados#repository', false, 'permission', 'can_write',
631         'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
632
633     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
634         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
635   end
636
637   test "setup and unsetup user" do
638     authorize_with :admin
639
640     post :setup, {
641       repo_name: 'test_repo',
642       vm_uuid: @vm_uuid,
643       user: {email: 'foo@example.com'},
644       openid_prefix: 'https://www.google.com/accounts/o8/id'
645     }
646
647     assert_response :success
648     response_items = JSON.parse(@response.body)['items']
649     created = find_obj_in_resp response_items, 'User', nil
650     assert_not_nil created['uuid'], 'expected uuid for the new user'
651     assert_equal created['email'], 'foo@example.com', 'expected given email'
652
653     # five extra links: system_group, login, group, repo and vm
654     verify_num_links @all_links_at_start, 5
655
656     verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
657         created['uuid'], created['email'], 'arvados#user', false, 'User'
658
659     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
660         'All users', created['uuid'], 'arvados#group', true, 'Group'
661
662     verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
663         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
664
665     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
666         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
667
668     verify_link_existence created['uuid'], created['email'], true, true, true, true, false
669
670     # now unsetup this user
671     post :unsetup, uuid: created['uuid']
672     assert_response :success
673
674     created2 = JSON.parse(@response.body)
675     assert_not_nil created2['uuid'], 'expected uuid for the newly created user'
676     assert_equal created['uuid'], created2['uuid'], 'expected uuid not found'
677
678     verify_link_existence created['uuid'], created['email'], false, false, false, false, false
679   end
680
681   test "unsetup active user" do
682     authorize_with :active
683     get :current
684     assert_response :success
685     active_user = JSON.parse(@response.body)
686     assert_not_nil active_user['uuid'], 'expected uuid for the active user'
687     assert active_user['is_active'], 'expected is_active for active user'
688     assert active_user['is_invited'], 'expected is_invited for active user'
689
690     verify_link_existence active_user['uuid'], active_user['email'],
691           false, false, false, true, true
692
693     authorize_with :admin
694
695     # now unsetup this user
696     post :unsetup, uuid: active_user['uuid']
697     assert_response :success
698
699     response_user = JSON.parse(@response.body)
700     assert_not_nil response_user['uuid'], 'expected uuid for the upsetup user'
701     assert_equal active_user['uuid'], response_user['uuid'], 'expected uuid not found'
702     assert !response_user['is_active'], 'expected user to be inactive'
703     assert !response_user['is_invited'], 'expected user to be uninvited'
704
705     verify_link_existence response_user['uuid'], response_user['email'],
706           false, false, false, false, false
707   end
708
709   def verify_num_links (original_links, expected_additional_links)
710     links_now = Link.all
711     assert_equal expected_additional_links, Link.all.size-original_links.size,
712         "Expected #{expected_additional_links.inspect} more links"
713   end
714
715   def find_obj_in_resp (response_items, object_type, head_kind=nil)
716     return_obj = nil
717     response_items.each { |x|
718       if !x
719         next
720       end
721
722       if object_type == 'User'
723         if !x['head_kind']
724           return_obj = x
725           break
726         end
727       else  # looking for a link
728         if x['head_kind'] == head_kind
729           return_obj = x
730           break
731         end
732       end
733     }
734     return return_obj
735   end
736
737   def verify_link(response_items, link_object_name, expect_link, link_class,
738         link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
739
740     link = find_obj_in_resp response_items, 'Link', link_object_name
741
742     if !expect_link
743       assert_nil link, "Expected no link for #{link_object_name}"
744       return
745     end
746
747     assert_not_nil link, "Expected link for #{link_object_name}"
748
749     if fetch_object
750       object = Object.const_get(class_name).where(name: head_uuid)
751       assert [] != object, "expected #{class_name} with name #{head_uuid}"
752       head_uuid = object.first[:uuid]
753     end
754     assert_equal link['link_class'], link_class,
755         "did not find expected link_class for #{link_object_name}"
756
757     assert_equal link['name'], link_name,
758         "did not find expected link_name for #{link_object_name}"
759
760     assert_equal link['tail_uuid'], tail_uuid,
761         "did not find expected tail_uuid for #{link_object_name}"
762
763     assert_equal link['head_kind'], head_kind,
764         "did not find expected head_kind for #{link_object_name}"
765
766     assert_equal link['head_uuid'], head_uuid,
767         "did not find expected head_uuid for #{link_object_name}"
768   end
769
770   def verify_link_existence uuid, email, expect_oid_login_perms,
771       expect_repo_perms, expect_vm_perms, expect_group_perms, expect_signatures
772     # verify that all links are deleted for the user
773     oid_login_perms = Link.where(tail_uuid: email,
774                                  head_kind: 'arvados#user',
775                                  link_class: 'permission',
776                                  name: 'can_login')
777     if expect_oid_login_perms
778       assert oid_login_perms.any?, "expected oid_login_perms"
779     else
780       assert !oid_login_perms.any?, "expected all oid_login_perms deleted"
781     end
782
783     repo_perms = Link.where(tail_uuid: uuid,
784                               head_kind: 'arvados#repository',
785                               link_class: 'permission',
786                               name: 'can_write')
787     if expect_repo_perms
788       assert repo_perms.any?, "expected repo_perms"
789     else
790       assert !repo_perms.any?, "expected all repo_perms deleted"
791     end
792
793     vm_login_perms = Link.where(tail_uuid: uuid,
794                               head_kind: 'arvados#virtualMachine',
795                               link_class: 'permission',
796                               name: 'can_login')
797     if expect_vm_perms
798       assert vm_login_perms.any?, "expected vm_login_perms"
799     else
800       assert !vm_login_perms.any?, "expected all vm_login_perms deleted"
801     end
802
803     group = Group.where(name: 'All users').select do |g|
804       g[:uuid].match /-f+$/
805     end.first
806     group_read_perms = Link.where(tail_uuid: uuid,
807                              head_uuid: group[:uuid],
808                              head_kind: 'arvados#group',
809                              link_class: 'permission',
810                              name: 'can_read')
811     if expect_group_perms
812       assert group_read_perms.any?, "expected all users group read perms"
813     else
814       assert !group_read_perms.any?, "expected all users group perm deleted"
815     end
816
817     signed_uuids = Link.where(link_class: 'signature',
818                                   tail_kind: 'arvados#user',
819                                   tail_uuid: uuid)
820
821     if expect_signatures
822       assert signed_uuids.any?, "expected signatures"
823     else
824       assert !signed_uuids.any?, "expected all signatures deleted"
825     end
826
827   end
828
829   def verify_system_group_permission_link_for user_uuid
830     assert_equal 1, Link.where(link_class: 'permission',
831                                name: 'can_manage',
832                                tail_uuid: system_group_uuid,
833                                head_uuid: user_uuid).count
834   end
835 end