3889: remove the functional test "setup and unsetup user" that is performing two...
[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 "unsetup active user" do
590     active_user = users(:active)
591     assert_not_nil active_user['uuid'], 'expected uuid for the active user'
592     assert active_user['is_active'], 'expected is_active for active user'
593
594     verify_link_existence active_user['uuid'], active_user['email'],
595           false, true, false, true, true
596
597     authorize_with :admin
598
599     # now unsetup this user
600     post :unsetup, id: active_user['uuid']
601     assert_response :success
602
603     response_user = JSON.parse(@response.body)
604     assert_not_nil response_user['uuid'], 'expected uuid for the upsetup user'
605     assert_equal active_user['uuid'], response_user['uuid'], 'expected uuid not found'
606     assert !response_user['is_active'], 'expected user to be inactive'
607     assert !response_user['is_invited'], 'expected user to be uninvited'
608
609     verify_link_existence response_user['uuid'], response_user['email'],
610           false, false, false, false, false
611
612     active_user = User.find_by_uuid(users(:active).uuid)
613     readable_groups = active_user.groups_i_can(:read)
614     all_users_group = Group.all.collect(&:uuid).select { |g| g.match /-f+$/ }
615     refute_includes(readable_groups, all_users_group,
616                     "active user can read All Users group after being deactivated")
617     assert_equal(false, active_user.is_invited,
618                  "active user is_invited after being deactivated & reloaded")
619   end
620
621   test "setup user with send notification param false and verify no email" do
622     authorize_with :admin
623
624     post :setup, {
625       openid_prefix: 'http://www.example.com/account',
626       send_notification_email: 'false',
627       user: {
628         email: "foo@example.com"
629       }
630     }
631
632     assert_response :success
633     response_items = JSON.parse(@response.body)['items']
634     created = find_obj_in_resp response_items, 'User', nil
635     assert_not_nil created['uuid'], 'expected uuid for the new user'
636     assert_equal created['email'], 'foo@example.com', 'expected given email'
637
638     setup_email = ActionMailer::Base.deliveries.last
639     assert_nil setup_email, 'expected no setup email'
640   end
641
642   test "setup user with send notification param true and verify email" do
643     authorize_with :admin
644
645     post :setup, {
646       openid_prefix: 'http://www.example.com/account',
647       send_notification_email: 'true',
648       user: {
649         email: "foo@example.com"
650       }
651     }
652
653     assert_response :success
654     response_items = JSON.parse(@response.body)['items']
655     created = find_obj_in_resp response_items, 'User', nil
656     assert_not_nil created['uuid'], 'expected uuid for the new user'
657     assert_equal created['email'], 'foo@example.com', 'expected given email'
658
659     setup_email = ActionMailer::Base.deliveries.last
660     assert_not_nil setup_email, 'Expected email after setup'
661
662     assert_equal Rails.configuration.user_notifier_email_from, setup_email.from[0]
663     assert_equal 'foo@example.com', setup_email.to[0]
664     assert_equal 'Welcome to Curoverse', setup_email.subject
665     assert (setup_email.body.to_s.include? 'Your Arvados account has been set up'),
666         'Expected Your Arvados account has been set up in email body'
667     assert (setup_email.body.to_s.include? 'foo@example.com'),
668         'Expected user email in email body'
669     assert (setup_email.body.to_s.include? Rails.configuration.workbench_address),
670         'Expected workbench url in email body'
671   end
672
673   test "non-admin user can get basic information about readable users" do
674     authorize_with :spectator
675     get(:index)
676     check_non_admin_index
677     check_readable_users_index [:spectator], [:inactive, :active]
678   end
679
680   test "non-admin user gets only safe attributes from users#show" do
681     g = act_as_system_user do
682       create :group
683     end
684     users = create_list :active_user, 2, join_groups: [g]
685     token = create :token, user: users[0]
686     authorize_with_token token
687     get :show, id: users[1].uuid
688     check_non_admin_show
689   end
690
691   test "non-admin user can limit index" do
692     g = act_as_system_user do
693       create :group
694     end
695     users = create_list :active_user, 4, join_groups: [g]
696     token = create :token, user: users[0]
697
698     [2, 4].each do |limit|
699       authorize_with_token token
700       get(:index, limit: limit)
701       check_non_admin_index
702       assert_equal(limit, json_response["items"].size,
703                    "non-admin index limit was ineffective")
704     end
705   end
706
707   test "admin has full index powers" do
708     authorize_with :admin
709     check_inactive_user_findable
710   end
711
712   test "reader token can grant admin index powers" do
713     authorize_with :spectator
714     check_inactive_user_findable(reader_tokens: [api_token(:admin)])
715   end
716
717   test "admin can filter on user.is_active" do
718     authorize_with :admin
719     get(:index, filters: [["is_active", "=", "true"]])
720     assert_response :success
721     check_readable_users_index [:active, :spectator], [:inactive]
722   end
723
724   test "admin can search where user.is_active" do
725     authorize_with :admin
726     get(:index, where: {is_active: true})
727     assert_response :success
728     check_readable_users_index [:active, :spectator], [:inactive]
729   end
730
731   test "update active_no_prefs user profile and expect notification email" do
732     authorize_with :admin
733
734     put :update, {
735       id: users(:active_no_prefs).uuid,
736       user: {
737         prefs: {:profile => {'organization' => 'example.com'}}
738       }
739     }
740     assert_response :success
741
742     found_email = false
743     ActionMailer::Base.deliveries.andand.each do |email|
744       if email.subject == "Profile created by #{users(:active_no_prefs).email}"
745         found_email = true
746         break
747       end
748     end
749     assert_equal true, found_email, 'Expected email after creating profile'
750   end
751
752   test "update active_no_prefs_profile user profile and expect notification email" do
753     authorize_with :admin
754
755     user = {}
756     user[:prefs] = users(:active_no_prefs_profile).prefs
757     user[:prefs][:profile] = {:profile => {'organization' => 'example.com'}}
758     put :update, {
759       id: users(:active_no_prefs_profile).uuid,
760       user: user
761     }
762     assert_response :success
763
764     found_email = false
765     ActionMailer::Base.deliveries.andand.each do |email|
766       if email.subject == "Profile created by #{users(:active_no_prefs_profile).email}"
767         found_email = true
768         break
769       end
770     end
771     assert_equal true, found_email, 'Expected email after creating profile'
772   end
773
774   test "update active user profile and expect no notification email" do
775     authorize_with :admin
776
777     put :update, {
778       id: users(:active).uuid,
779       user: {
780         prefs: {:profile => {'organization' => 'anotherexample.com'}}
781       }
782     }
783     assert_response :success
784
785     found_email = false
786     ActionMailer::Base.deliveries.andand.each do |email|
787       if email.subject == "Profile created by #{users(:active).email}"
788         found_email = true
789         break
790       end
791     end
792     assert_equal false, found_email, 'Expected no email after updating profile'
793   end
794
795
796   NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "email", "first_name",
797                          "last_name"].sort
798
799   def check_non_admin_index
800     assert_response :success
801     response_items = json_response["items"]
802     assert_not_nil response_items
803     response_items.each do |user_data|
804       check_non_admin_item user_data
805       assert(user_data["is_active"], "non-admin index returned inactive user")
806     end
807   end
808
809   def check_non_admin_show
810     assert_response :success
811     check_non_admin_item json_response
812   end
813
814   def check_non_admin_item user_data
815     assert_equal(NON_ADMIN_USER_DATA, user_data.keys.sort,
816                  "data in response had missing or extra attributes")
817     assert_equal("arvados#user", user_data["kind"])
818   end
819
820
821   def check_readable_users_index expect_present, expect_missing
822     response_uuids = json_response["items"].map { |u| u["uuid"] }
823     expect_present.each do |user_key|
824       assert_includes(response_uuids, users(user_key).uuid,
825                       "#{user_key} missing from index")
826     end
827     expect_missing.each do |user_key|
828       refute_includes(response_uuids, users(user_key).uuid,
829                       "#{user_key} included in index")
830     end
831   end
832
833   def check_inactive_user_findable(params={})
834     inactive_user = users(:inactive)
835     get(:index, params.merge(filters: [["email", "=", inactive_user.email]]))
836     assert_response :success
837     user_list = json_response["items"]
838     assert_equal(1, user_list.andand.count)
839     # This test needs to check a column non-admins have no access to,
840     # to ensure that admins see all user information.
841     assert_equal(inactive_user.identity_url, user_list.first["identity_url"],
842                  "admin's filtered index did not return inactive user")
843   end
844
845   def verify_num_links (original_links, expected_additional_links)
846     links_now = Link.all
847     assert_equal expected_additional_links, Link.all.size-original_links.size,
848         "Expected #{expected_additional_links.inspect} more links"
849   end
850
851   def find_obj_in_resp (response_items, object_type, head_kind=nil)
852     return_obj = nil
853     response_items
854     response_items.each { |x|
855       if !x
856         next
857       end
858
859       if object_type == 'User'
860         if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
861           return_obj = x
862           break
863         end
864       else  # looking for a link
865         if x['head_uuid'] and ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
866           return_obj = x
867           break
868         end
869       end
870     }
871     return return_obj
872   end
873
874   def verify_link(response_items, link_object_name, expect_link, link_class,
875         link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
876
877     link = find_obj_in_resp response_items, 'Link', link_object_name
878
879     if !expect_link
880       assert_nil link, "Expected no link for #{link_object_name}"
881       return
882     end
883
884     assert_not_nil link, "Expected link for #{link_object_name}"
885
886     if fetch_object
887       object = Object.const_get(class_name).where(name: head_uuid)
888       assert [] != object, "expected #{class_name} with name #{head_uuid}"
889       head_uuid = object.first[:uuid]
890     end
891     assert_equal link_class, link['link_class'],
892         "did not find expected link_class for #{link_object_name}"
893
894     assert_equal link_name, link['name'],
895         "did not find expected link_name for #{link_object_name}"
896
897     assert_equal tail_uuid, link['tail_uuid'],
898         "did not find expected tail_uuid for #{link_object_name}"
899
900     assert_equal head_kind, link['head_kind'],
901         "did not find expected head_kind for #{link_object_name}"
902
903     assert_equal head_uuid, link['head_uuid'],
904         "did not find expected head_uuid for #{link_object_name}"
905   end
906
907   def verify_link_existence uuid, email, expect_oid_login_perms,
908       expect_repo_perms, expect_vm_perms, expect_group_perms, expect_signatures
909     # verify that all links are deleted for the user
910     oid_login_perms = Link.where(tail_uuid: email,
911                                  link_class: 'permission',
912                                  name: 'can_login').where("head_uuid like ?", User.uuid_like_pattern)
913     if expect_oid_login_perms
914       assert oid_login_perms.any?, "expected oid_login_perms"
915     else
916       assert !oid_login_perms.any?, "expected all oid_login_perms deleted"
917     end
918
919     repo_perms = Link.where(tail_uuid: uuid,
920                             link_class: 'permission',
921                             name: 'can_manage').where("head_uuid like ?", Repository.uuid_like_pattern)
922     if expect_repo_perms
923       assert repo_perms.any?, "expected repo_perms"
924     else
925       assert !repo_perms.any?, "expected all repo_perms deleted"
926     end
927
928     vm_login_perms = Link.
929       where(tail_uuid: uuid,
930             link_class: 'permission',
931             name: 'can_login').
932       where("head_uuid like ?",
933             VirtualMachine.uuid_like_pattern).
934       where('uuid <> ?',
935             links(:auto_setup_vm_login_username_can_login_to_test_vm).uuid)
936     if expect_vm_perms
937       assert vm_login_perms.any?, "expected vm_login_perms"
938     else
939       assert !vm_login_perms.any?, "expected all vm_login_perms deleted"
940     end
941
942     group = Group.where(name: 'All users').select do |g|
943       g[:uuid].match /-f+$/
944     end.first
945     group_read_perms = Link.where(tail_uuid: uuid,
946                                   head_uuid: group[:uuid],
947                                   link_class: 'permission',
948                                   name: 'can_read')
949     if expect_group_perms
950       assert group_read_perms.any?, "expected all users group read perms"
951     else
952       assert !group_read_perms.any?, "expected all users group perm deleted"
953     end
954
955     signed_uuids = Link.where(link_class: 'signature',
956                               tail_uuid: uuid)
957
958     if expect_signatures
959       assert signed_uuids.any?, "expected signatures"
960     else
961       assert !signed_uuids.any?, "expected all signatures deleted"
962     end
963
964   end
965
966   def verify_system_group_permission_link_for user_uuid
967     assert_equal 1, Link.where(link_class: 'permission',
968                                name: 'can_manage',
969                                tail_uuid: system_group_uuid,
970                                head_uuid: user_uuid).count
971   end
972 end