3 class Arvados::V1::UsersControllerTest < ActionController::TestCase
4 include CurrentApiClient
7 @all_links_at_start = Link.all
8 @vm_uuid = virtual_machines(:testvm).uuid
11 test "activate a user after signing UA" do
12 authorize_with :inactive_but_signed_user_agreement
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']
23 test "refuse to activate a user before signing UA" do
25 required_uuids = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
30 Collection.uuid_like_pattern).
33 assert required_uuids.length > 0
35 signed_uuids = Link.where(owner_uuid: system_user_uuid,
36 link_class: 'signature',
38 tail_uuid: users(:inactive).uuid,
39 head_uuid: required_uuids).
42 assert_equal 0, signed_uuids.length
45 authorize_with :inactive
48 assert_response :success
49 me = JSON.parse(@response.body)
50 assert_equal false, me['is_active']
52 post :activate, id: me['uuid']
56 assert_response :success
57 me = JSON.parse(@response.body)
58 assert_equal false, me['is_active']
61 test "activate an already-active user" do
62 authorize_with :active
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']
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}
78 test "create new user with user as input" do
81 first_name: "test_first_name",
82 last_name: "test_last_name",
83 email: "foo@example.com"
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'
93 test "create user with user, vm and repo as input" do
95 repo_name = 'test_repo'
99 openid_prefix: 'https://www.google.com/accounts/o8/id',
101 uuid: 'zzzzz-tpzed-abcdefghijklmno',
102 first_name: "in_create_test_first_name",
103 last_name: "test_last_name",
104 email: "foo@example.com"
107 assert_response :success
108 response_items = JSON.parse(@response.body)['items']
110 created = find_obj_in_resp response_items, 'User', nil
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'
118 # arvados#user, repo link and link add user to 'All users' group
119 verify_num_links @all_links_at_start, 4
121 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
122 created['uuid'], created['email'], 'arvados#user', false, 'User'
124 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
125 repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
127 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
128 'All users', created['uuid'], 'arvados#group', true, 'Group'
130 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
131 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
133 verify_system_group_permission_link_for created['uuid']
135 # invoke setup again with the same data
137 repo_name: repo_name,
139 openid_prefix: 'https://www.google.com/accounts/o8/id',
141 uuid: 'zzzzz-tpzed-abcdefghijklmno',
142 first_name: "in_create_test_first_name",
143 last_name: "test_last_name",
144 email: "foo@example.com"
147 assert_response :success
149 response_items = JSON.parse(@response.body)['items']
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'
158 # arvados#user, repo link and link add user to 'All users' group
159 verify_num_links @all_links_at_start, 5
161 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
162 repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
164 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
165 'All users', created['uuid'], 'arvados#group', true, 'Group'
167 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
168 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
170 verify_system_group_permission_link_for created['uuid']
173 test "setup user with bogus uuid and expect error" do
174 authorize_with :admin
178 repo_name: 'test_repo',
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'
187 test "setup user with bogus uuid in user and expect error" do
188 authorize_with :admin
191 user: {uuid: 'bogus_uuid'},
192 repo_name: 'test_repo',
194 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
203 test "setup user with no uuid and user, expect error" do
204 authorize_with :admin
207 repo_name: 'test_repo',
209 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
218 test "setup user with no uuid and email, expect error" do
219 authorize_with :admin
223 repo_name: 'test_repo',
225 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
234 test "invoke setup with existing uuid, vm and repo and verify links" do
235 authorize_with :inactive
237 assert_response :success
238 inactive_user = JSON.parse(@response.body)
240 authorize_with :admin
243 uuid: inactive_user['uuid'],
244 repo_name: 'test_repo',
248 assert_response :success
250 response_items = JSON.parse(@response.body)['items']
251 resp_obj = find_obj_in_resp response_items, 'User', nil
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'
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'
262 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
263 @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
266 test "invoke setup with existing uuid in user, verify response" do
267 authorize_with :inactive
269 assert_response :success
270 inactive_user = JSON.parse(@response.body)
272 authorize_with :admin
275 user: {uuid: inactive_user['uuid']},
276 openid_prefix: 'https://www.google.com/accounts/o8/id'
279 assert_response :success
281 response_items = JSON.parse(@response.body)['items']
282 resp_obj = find_obj_in_resp response_items, 'User', nil
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'
290 test "invoke setup with existing uuid but different email, expect original email" do
291 authorize_with :inactive
293 assert_response :success
294 inactive_user = JSON.parse(@response.body)
296 authorize_with :admin
299 uuid: inactive_user['uuid'],
300 user: {email: 'junk_email'}
303 assert_response :success
305 response_items = JSON.parse(@response.body)['items']
306 resp_obj = find_obj_in_resp response_items, 'User', nil
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'
314 test "setup user with valid email and repo as input" do
315 authorize_with :admin
318 repo_name: 'test_repo',
319 user: {email: 'foo@example.com'},
320 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
329 # four extra links; system_group, login, group and repo perms
330 verify_num_links @all_links_at_start, 4
333 test "setup user with fake vm and expect error" do
334 authorize_with :admin
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'
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'
350 test "setup user with valid email, repo and real vm as input" do
351 authorize_with :admin
354 repo_name: 'test_repo',
355 openid_prefix: 'https://www.google.com/accounts/o8/id',
357 user: {email: 'foo@example.com'}
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'
366 # five extra links; system_group, login, group, vm, repo
367 verify_num_links @all_links_at_start, 5
370 test "setup user with valid email, no vm and repo as input" do
371 authorize_with :admin
374 user: {email: 'foo@example.com'},
375 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
384 # three extra links; system_group, login, and group
385 verify_num_links @all_links_at_start, 3
388 test "setup user with email, first name, repo name and vm uuid" do
389 authorize_with :admin
392 openid_prefix: 'https://www.google.com/accounts/o8/id',
393 repo_name: 'test_repo',
396 first_name: 'test_first_name',
397 email: 'foo@example.com'
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'
409 # five extra links; system_group, login, group, repo and vm
410 verify_num_links @all_links_at_start, 5
413 test "setup user twice with email and check two different objects created" do
414 authorize_with :admin
417 openid_prefix: 'https://www.google.com/accounts/o8/id',
418 repo_name: 'test_repo',
420 email: 'foo@example.com'
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
434 user: {email: 'foo@example.com'},
435 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
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
452 test "setup user with openid prefix" do
453 authorize_with :admin
456 repo_name: 'test_repo',
457 openid_prefix: 'http://www.example.com/account',
459 first_name: "in_create_test_first_name",
460 last_name: "test_last_name",
461 email: "foo@example.com"
465 assert_response :success
467 response_items = JSON.parse(@response.body)['items']
468 created = find_obj_in_resp response_items, 'User', nil
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'
476 # four new links: system_group, arvados#user, repo, and 'All users' group.
477 verify_num_links @all_links_at_start, 4
479 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
480 created['uuid'], created['email'], 'arvados#user', false, 'User'
482 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
483 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
485 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
486 'All users', created['uuid'], 'arvados#group', true, 'Group'
488 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
489 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
492 test "invoke setup with no openid prefix, expect error" do
493 authorize_with :admin
496 repo_name: 'test_repo',
498 first_name: "in_create_test_first_name",
499 last_name: "test_last_name",
500 email: "foo@example.com"
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'
511 test "setup user with user, vm and repo and verify links" do
512 authorize_with :admin
516 first_name: "in_create_test_first_name",
517 last_name: "test_last_name",
518 email: "foo@example.com"
521 repo_name: 'test_repo',
522 openid_prefix: 'https://www.google.com/accounts/o8/id'
525 assert_response :success
527 response_items = JSON.parse(@response.body)['items']
528 created = find_obj_in_resp response_items, 'User', nil
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'
535 # five new links: system_group, arvados#user, repo, vm and 'All
537 verify_num_links @all_links_at_start, 5
539 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
540 created['uuid'], created['email'], 'arvados#user', false, 'User'
542 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
543 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
545 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
546 'All users', created['uuid'], 'arvados#group', true, 'Group'
548 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
549 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
552 test "create user as non admin user and expect error" do
553 authorize_with :active
556 user: {email: 'foo@example.com'}
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'
566 test "setup user as non admin user and expect error" do
567 authorize_with :active
570 openid_prefix: 'https://www.google.com/accounts/o8/id',
571 user: {email: 'foo@example.com'}
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'
581 test "setup user in multiple steps and verify response" do
582 authorize_with :admin
585 openid_prefix: 'http://www.example.com/account',
587 email: "foo@example.com"
591 assert_response :success
592 response_items = JSON.parse(@response.body)['items']
593 created = find_obj_in_resp response_items, 'User', nil
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'
599 # three new links: system_group, arvados#user, and 'All users' group.
600 verify_num_links @all_links_at_start, 3
602 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
603 created['uuid'], created['email'], 'arvados#user', false, 'User'
605 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
606 'All users', created['uuid'], 'arvados#group', true, 'Group'
608 verify_link response_items, 'arvados#repository', false, 'permission', 'can_manage',
609 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
611 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
612 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
614 # invoke setup with a repository
616 openid_prefix: 'http://www.example.com/account',
617 repo_name: 'new_repo',
618 uuid: created['uuid']
621 assert_response :success
623 response_items = JSON.parse(@response.body)['items']
624 created = find_obj_in_resp response_items, 'User', nil
626 assert_equal 'foo@example.com', created['email'], 'expected input email'
629 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
630 'All users', created['uuid'], 'arvados#group', true, 'Group'
632 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
633 'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
635 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
636 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
638 # invoke setup with a vm_uuid
641 openid_prefix: 'http://www.example.com/account',
645 uuid: created['uuid']
648 assert_response :success
650 response_items = JSON.parse(@response.body)['items']
651 created = find_obj_in_resp response_items, 'User', nil
653 assert_equal created['email'], 'foo@example.com', 'expected original email'
656 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
657 'All users', created['uuid'], 'arvados#group', true, 'Group'
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'
663 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
664 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
667 test "setup and unsetup user" do
668 authorize_with :admin
671 repo_name: 'test_repo',
673 user: {email: 'foo@example.com'},
674 openid_prefix: 'https://www.google.com/accounts/o8/id'
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'
683 # five extra links: system_group, login, group, repo and vm
684 verify_num_links @all_links_at_start, 5
686 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
687 created['uuid'], created['email'], 'arvados#user', false, 'User'
689 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
690 'All users', created['uuid'], 'arvados#group', true, 'Group'
692 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
693 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
695 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
696 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
698 verify_link_existence created['uuid'], created['email'], true, true, true, true, false
700 # now unsetup this user
701 post :unsetup, id: created['uuid']
702 assert_response :success
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'
708 verify_link_existence created['uuid'], created['email'], false, false, false, false, false
711 test "unsetup active user" do
712 authorize_with :active
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'
720 verify_link_existence active_user['uuid'], active_user['email'],
721 false, true, false, true, true
723 authorize_with :admin
725 # now unsetup this user
726 post :unsetup, id: active_user['uuid']
727 assert_response :success
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'
735 verify_link_existence response_user['uuid'], response_user['email'],
736 false, false, false, false, false
738 active_user = User.find_by_uuid(users(:active).uuid)
739 readable_groups = active_user.groups_i_can(:read)
740 all_users_group = Group.all.collect(&:uuid).select { |g| g.match /-f+$/ }
741 refute_includes(readable_groups, all_users_group,
742 "active user can read All Users group after being deactivated")
743 assert_equal(false, active_user.is_invited,
744 "active user is_invited after being deactivated & reloaded")
747 test "setup user with send notification param false and verify no email" do
748 authorize_with :admin
751 openid_prefix: 'http://www.example.com/account',
752 send_notification_email: 'false',
754 email: "foo@example.com"
758 assert_response :success
759 response_items = JSON.parse(@response.body)['items']
760 created = find_obj_in_resp response_items, 'User', nil
761 assert_not_nil created['uuid'], 'expected uuid for the new user'
762 assert_equal created['email'], 'foo@example.com', 'expected given email'
764 setup_email = ActionMailer::Base.deliveries.last
765 assert_nil setup_email, 'expected no setup email'
768 test "setup user with send notification param true and verify email" do
769 authorize_with :admin
772 openid_prefix: 'http://www.example.com/account',
773 send_notification_email: 'true',
775 email: "foo@example.com"
779 assert_response :success
780 response_items = JSON.parse(@response.body)['items']
781 created = find_obj_in_resp response_items, 'User', nil
782 assert_not_nil created['uuid'], 'expected uuid for the new user'
783 assert_equal created['email'], 'foo@example.com', 'expected given email'
785 setup_email = ActionMailer::Base.deliveries.last
786 assert_not_nil setup_email, 'Expected email after setup'
788 assert_equal Rails.configuration.user_notifier_email_from, setup_email.from[0]
789 assert_equal 'foo@example.com', setup_email.to[0]
790 assert_equal 'Welcome to Curoverse', setup_email.subject
791 assert (setup_email.body.to_s.include? 'Your Arvados account has been set up'),
792 'Expected Your Arvados account has been set up in email body'
793 assert (setup_email.body.to_s.include? 'foo@example.com'),
794 'Expected user email in email body'
795 assert (setup_email.body.to_s.include? Rails.configuration.workbench_address),
796 'Expected workbench url in email body'
799 test "non-admin user can get basic information about readable users" do
800 authorize_with :spectator
802 check_non_admin_index
803 check_readable_users_index [:spectator], [:inactive, :active]
806 test "non-admin user gets only safe attributes from users#show" do
807 g = act_as_system_user do
810 users = create_list :active_user, 2, join_groups: [g]
811 token = create :token, user: users[0]
812 authorize_with_token token
813 get :show, id: users[1].uuid
817 test "non-admin user can limit index" do
818 g = act_as_system_user do
821 users = create_list :active_user, 4, join_groups: [g]
822 token = create :token, user: users[0]
824 [2, 4].each do |limit|
825 authorize_with_token token
826 get(:index, limit: limit)
827 check_non_admin_index
828 assert_equal(limit, json_response["items"].size,
829 "non-admin index limit was ineffective")
833 test "admin has full index powers" do
834 authorize_with :admin
835 check_inactive_user_findable
838 test "reader token can grant admin index powers" do
839 authorize_with :spectator
840 check_inactive_user_findable(reader_tokens: [api_token(:admin)])
843 test "admin can filter on user.is_active" do
844 authorize_with :admin
845 get(:index, filters: [["is_active", "=", "true"]])
846 assert_response :success
847 check_readable_users_index [:active, :spectator], [:inactive]
850 test "admin can search where user.is_active" do
851 authorize_with :admin
852 get(:index, where: {is_active: true})
853 assert_response :success
854 check_readable_users_index [:active, :spectator], [:inactive]
857 test "update active_no_prefs user profile and expect notification email" do
858 authorize_with :admin
861 id: users(:active_no_prefs).uuid,
863 prefs: {:profile => {'organization' => 'example.com'}}
866 assert_response :success
869 ActionMailer::Base.deliveries.andand.each do |email|
870 if email.subject == "Profile created by #{users(:active_no_prefs).email}"
875 assert_equal true, found_email, 'Expected email after creating profile'
878 test "update active_no_prefs_profile user profile and expect notification email" do
879 authorize_with :admin
882 user[:prefs] = users(:active_no_prefs_profile).prefs
883 user[:prefs][:profile] = {:profile => {'organization' => 'example.com'}}
885 id: users(:active_no_prefs_profile).uuid,
888 assert_response :success
891 ActionMailer::Base.deliveries.andand.each do |email|
892 if email.subject == "Profile created by #{users(:active_no_prefs_profile).email}"
897 assert_equal true, found_email, 'Expected email after creating profile'
900 test "update active user profile and expect no notification email" do
901 authorize_with :admin
904 id: users(:active).uuid,
906 prefs: {:profile => {'organization' => 'anotherexample.com'}}
909 assert_response :success
912 ActionMailer::Base.deliveries.andand.each do |email|
913 if email.subject == "Profile created by #{users(:active).email}"
918 assert_equal false, found_email, 'Expected no email after updating profile'
922 NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "email", "first_name",
925 def check_non_admin_index
926 assert_response :success
927 response_items = json_response["items"]
928 assert_not_nil response_items
929 response_items.each do |user_data|
930 check_non_admin_item user_data
931 assert(user_data["is_active"], "non-admin index returned inactive user")
935 def check_non_admin_show
936 assert_response :success
937 check_non_admin_item json_response
940 def check_non_admin_item user_data
941 assert_equal(NON_ADMIN_USER_DATA, user_data.keys.sort,
942 "data in response had missing or extra attributes")
943 assert_equal("arvados#user", user_data["kind"])
947 def check_readable_users_index expect_present, expect_missing
948 response_uuids = json_response["items"].map { |u| u["uuid"] }
949 expect_present.each do |user_key|
950 assert_includes(response_uuids, users(user_key).uuid,
951 "#{user_key} missing from index")
953 expect_missing.each do |user_key|
954 refute_includes(response_uuids, users(user_key).uuid,
955 "#{user_key} included in index")
959 def check_inactive_user_findable(params={})
960 inactive_user = users(:inactive)
961 get(:index, params.merge(filters: [["email", "=", inactive_user.email]]))
962 assert_response :success
963 user_list = json_response["items"]
964 assert_equal(1, user_list.andand.count)
965 # This test needs to check a column non-admins have no access to,
966 # to ensure that admins see all user information.
967 assert_equal(inactive_user.identity_url, user_list.first["identity_url"],
968 "admin's filtered index did not return inactive user")
971 def verify_num_links (original_links, expected_additional_links)
973 assert_equal expected_additional_links, Link.all.size-original_links.size,
974 "Expected #{expected_additional_links.inspect} more links"
977 def find_obj_in_resp (response_items, object_type, head_kind=nil)
980 response_items.each { |x|
985 if object_type == 'User'
986 if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
990 else # looking for a link
991 if x['head_uuid'] and ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
1000 def verify_link(response_items, link_object_name, expect_link, link_class,
1001 link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
1003 link = find_obj_in_resp response_items, 'Link', link_object_name
1006 assert_nil link, "Expected no link for #{link_object_name}"
1010 assert_not_nil link, "Expected link for #{link_object_name}"
1013 object = Object.const_get(class_name).where(name: head_uuid)
1014 assert [] != object, "expected #{class_name} with name #{head_uuid}"
1015 head_uuid = object.first[:uuid]
1017 assert_equal link_class, link['link_class'],
1018 "did not find expected link_class for #{link_object_name}"
1020 assert_equal link_name, link['name'],
1021 "did not find expected link_name for #{link_object_name}"
1023 assert_equal tail_uuid, link['tail_uuid'],
1024 "did not find expected tail_uuid for #{link_object_name}"
1026 assert_equal head_kind, link['head_kind'],
1027 "did not find expected head_kind for #{link_object_name}"
1029 assert_equal head_uuid, link['head_uuid'],
1030 "did not find expected head_uuid for #{link_object_name}"
1033 def verify_link_existence uuid, email, expect_oid_login_perms,
1034 expect_repo_perms, expect_vm_perms, expect_group_perms, expect_signatures
1035 # verify that all links are deleted for the user
1036 oid_login_perms = Link.where(tail_uuid: email,
1037 link_class: 'permission',
1038 name: 'can_login').where("head_uuid like ?", User.uuid_like_pattern)
1039 if expect_oid_login_perms
1040 assert oid_login_perms.any?, "expected oid_login_perms"
1042 assert !oid_login_perms.any?, "expected all oid_login_perms deleted"
1045 repo_perms = Link.where(tail_uuid: uuid,
1046 link_class: 'permission',
1047 name: 'can_manage').where("head_uuid like ?", Repository.uuid_like_pattern)
1048 if expect_repo_perms
1049 assert repo_perms.any?, "expected repo_perms"
1051 assert !repo_perms.any?, "expected all repo_perms deleted"
1054 vm_login_perms = Link.
1055 where(tail_uuid: uuid,
1056 link_class: 'permission',
1058 where("head_uuid like ?",
1059 VirtualMachine.uuid_like_pattern).
1061 links(:auto_setup_vm_login_username_can_login_to_test_vm).uuid)
1063 assert vm_login_perms.any?, "expected vm_login_perms"
1065 assert !vm_login_perms.any?, "expected all vm_login_perms deleted"
1068 group = Group.where(name: 'All users').select do |g|
1069 g[:uuid].match /-f+$/
1071 group_read_perms = Link.where(tail_uuid: uuid,
1072 head_uuid: group[:uuid],
1073 link_class: 'permission',
1075 if expect_group_perms
1076 assert group_read_perms.any?, "expected all users group read perms"
1078 assert !group_read_perms.any?, "expected all users group perm deleted"
1081 signed_uuids = Link.where(link_class: 'signature',
1084 if expect_signatures
1085 assert signed_uuids.any?, "expected signatures"
1087 assert !signed_uuids.any?, "expected all signatures deleted"
1092 def verify_system_group_permission_link_for user_uuid
1093 assert_equal 1, Link.where(link_class: 'permission',
1095 tail_uuid: system_group_uuid,
1096 head_uuid: user_uuid).count