3 class Arvados::V1::UsersControllerTest < ActionController::TestCase
6 @all_links_at_start = Link.all
7 @vm_uuid = virtual_machines(:testvm).uuid
10 test "activate a user after signing UA" do
11 authorize_with :inactive_but_signed_user_agreement
13 assert_response :success
14 me = JSON.parse(@response.body)
15 post :activate, uuid: me['uuid']
16 assert_response :success
17 assert_not_nil assigns(:object)
18 me = JSON.parse(@response.body)
19 assert_equal true, me['is_active']
22 test "refuse to activate a user before signing UA" do
23 authorize_with :inactive
25 assert_response :success
26 me = JSON.parse(@response.body)
27 post :activate, uuid: me['uuid']
30 assert_response :success
31 me = JSON.parse(@response.body)
32 assert_equal false, me['is_active']
35 test "activate an already-active user" do
36 authorize_with :active
38 assert_response :success
39 me = JSON.parse(@response.body)
40 post :activate, uuid: me['uuid']
41 assert_response :success
42 me = JSON.parse(@response.body)
43 assert_equal true, me['is_active']
46 test "create new user with user as input" do
49 first_name: "test_first_name",
50 last_name: "test_last_name",
53 assert_response :success
54 created = JSON.parse(@response.body)
55 assert_equal 'test_first_name', created['first_name']
56 assert_not_nil created['uuid'], 'expected uuid for the newly created user'
57 assert_not_nil created['email'], 'expected non-nil email'
58 assert_nil created['identity_url'], 'expected no identity_url'
61 test "create user with user, vm and repo as input" do
63 repo_name = 'test_repo'
67 openid_prefix: 'https://www.google.com/accounts/o8/id',
69 uuid: "this_is_agreeable",
70 first_name: "in_create_test_first_name",
71 last_name: "test_last_name",
75 assert_response :success
76 response_items = JSON.parse(@response.body)['items']
77 created = find_obj_in_resp response_items, 'User', nil
78 assert_equal 'in_create_test_first_name', created['first_name']
79 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
80 assert_equal 'this_is_agreeable', created['uuid']
81 assert_not_nil created['email'], 'expected non-nil email'
82 assert_nil created['identity_url'], 'expected no identity_url'
84 # invoke setup again with the same data
87 openid_prefix: 'https://www.google.com/accounts/o8/id',
89 uuid: "this_is_agreeable",
90 first_name: "in_create_test_first_name",
91 last_name: "test_last_name",
96 response_items = JSON.parse(@response.body)['items']
97 created = find_obj_in_resp response_items, 'User', nil
98 assert_equal 'in_create_test_first_name', created['first_name']
99 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
100 assert_equal 'this_is_agreeable', created['uuid']
101 assert_not_nil created['email'], 'expected non-nil email'
102 assert_nil created['identity_url'], 'expected no identity_url'
104 # since no such vm exists, expect only three new links:
105 # arvados#user, repo link and link add user to 'All users' group
106 verify_num_links @all_links_at_start, 3
108 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
109 created['uuid'], created['email'], 'arvados#user', false, 'User'
111 verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
112 repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
114 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
115 'All users', created['uuid'], 'arvados#group', true, 'Group'
117 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
118 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
121 test "setup user with bogus uuid and expect error" do
122 authorize_with :admin
126 repo_name: 'test_repo',
129 response_body = JSON.parse(@response.body)
130 response_errors = response_body['errors']
131 assert_not_nil response_errors, 'Expected error in response'
132 assert (response_errors.first.include? 'Path not found'), 'Expected 404'
135 test "setup user with bogus uuid in user and expect error" do
136 authorize_with :admin
139 user: {uuid: 'bogus_uuid'},
140 repo_name: 'test_repo',
142 openid_prefix: 'https://www.google.com/accounts/o8/id'
144 response_body = JSON.parse(@response.body)
145 response_errors = response_body['errors']
146 assert_not_nil response_errors, 'Expected error in response'
147 assert (response_errors.first.include? 'ArgumentError: Require user email'),
148 'Expected RuntimeError'
151 test "setup user with no uuid and user, expect error" do
152 authorize_with :admin
155 repo_name: 'test_repo',
157 openid_prefix: 'https://www.google.com/accounts/o8/id'
159 response_body = JSON.parse(@response.body)
160 response_errors = response_body['errors']
161 assert_not_nil response_errors, 'Expected error in response'
162 assert (response_errors.first.include? 'Required uuid or user'),
163 'Expected ArgumentError'
166 test "setup user with no uuid and email, expect error" do
167 authorize_with :admin
171 repo_name: 'test_repo',
173 openid_prefix: 'https://www.google.com/accounts/o8/id'
175 response_body = JSON.parse(@response.body)
176 response_errors = response_body['errors']
177 assert_not_nil response_errors, 'Expected error in response'
178 assert (response_errors.first.include? '<ArgumentError: Require user email'),
179 'Expected ArgumentError'
182 test "invoke setup with existing uuid, vm and repo and verify links" do
183 authorize_with :inactive
185 assert_response :success
186 inactive_user = JSON.parse(@response.body)
188 authorize_with :admin
191 uuid: inactive_user['uuid'],
192 repo_name: 'test_repo',
196 assert_response :success
198 response_items = JSON.parse(@response.body)['items']
199 resp_obj = find_obj_in_resp response_items, 'User', nil
201 assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
202 assert_equal inactive_user['uuid'], resp_obj['uuid']
203 assert_equal inactive_user['email'], resp_obj['email'],
204 'expecting inactive user email'
206 # expect repo and vm links
207 verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
208 'test_repo', resp_obj['uuid'], 'arvados#repository', true, 'Repository'
210 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
211 @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
214 test "invoke setup with existing uuid in user, verify response" do
215 authorize_with :inactive
217 assert_response :success
218 inactive_user = JSON.parse(@response.body)
220 authorize_with :admin
223 user: {uuid: inactive_user['uuid']},
224 openid_prefix: 'https://www.google.com/accounts/o8/id'
227 assert_response :success
229 response_items = JSON.parse(@response.body)['items']
230 resp_obj = find_obj_in_resp response_items, 'User', nil
232 assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
233 assert_equal inactive_user['uuid'], resp_obj['uuid']
234 assert_equal inactive_user['email'], resp_obj['email'],
235 'expecting inactive user email'
238 test "invoke setup with existing uuid but different email, expect original email" do
239 authorize_with :inactive
241 assert_response :success
242 inactive_user = JSON.parse(@response.body)
244 authorize_with :admin
247 uuid: inactive_user['uuid'],
248 user: {email: 'junk_email'}
251 assert_response :success
253 response_items = JSON.parse(@response.body)['items']
254 resp_obj = find_obj_in_resp response_items, 'User', nil
256 assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
257 assert_equal inactive_user['uuid'], resp_obj['uuid']
258 assert_equal inactive_user['email'], resp_obj['email'],
259 'expecting inactive user email'
262 test "setup user with valid email and repo as input" do
263 authorize_with :admin
266 repo_name: 'test_repo',
267 user: {email: 'abc@xyz.com'},
268 openid_prefix: 'https://www.google.com/accounts/o8/id'
271 assert_response :success
272 response_items = JSON.parse(@response.body)['items']
273 response_object = find_obj_in_resp response_items, 'User', nil
274 assert_not_nil response_object['uuid'], 'expected uuid for the new user'
275 assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
277 # three extra links; login link, group link and repo link
278 verify_num_links @all_links_at_start, 3
281 test "setup user with fake vm and expect error" do
282 authorize_with :admin
285 repo_name: 'test_repo',
286 vm_uuid: 'no_such_vm',
287 user: {email: 'abc@xyz.com'},
288 openid_prefix: 'https://www.google.com/accounts/o8/id'
291 response_body = JSON.parse(@response.body)
292 response_errors = response_body['errors']
293 assert_not_nil response_errors, 'Expected error in response'
294 assert (response_errors.first.include? "No vm found for no_such_vm"),
295 'Expected RuntimeError: No vm found for no_such_vm'
298 test "setup user with valid email, repo and real vm as input" do
299 authorize_with :admin
302 repo_name: 'test_repo',
303 openid_prefix: 'https://www.google.com/accounts/o8/id',
305 user: {email: 'abc@xyz.com'}
308 assert_response :success
309 response_items = JSON.parse(@response.body)['items']
310 response_object = find_obj_in_resp response_items, 'User', nil
311 assert_not_nil response_object['uuid'], 'expected uuid for the new user'
312 assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
314 # three extra links; login link, group link and repo link
315 verify_num_links @all_links_at_start, 4
318 test "setup user with valid email, no vm and repo as input" do
319 authorize_with :admin
322 user: {email: 'abc@xyz.com'},
323 openid_prefix: 'https://www.google.com/accounts/o8/id'
326 assert_response :success
327 response_items = JSON.parse(@response.body)['items']
328 response_object = find_obj_in_resp response_items, 'User', nil
329 assert_not_nil response_object['uuid'], 'expected uuid for new user'
330 assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
332 # two extra links; login link and group link
333 verify_num_links @all_links_at_start, 2
336 test "setup user with email, first name, repo name and vm uuid" do
337 authorize_with :admin
340 openid_prefix: 'https://www.google.com/accounts/o8/id',
341 repo_name: 'test_repo',
344 first_name: 'test_first_name',
349 assert_response :success
350 response_items = JSON.parse(@response.body)['items']
351 response_object = find_obj_in_resp response_items, 'User', nil
352 assert_not_nil response_object['uuid'], 'expected uuid for new user'
353 assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
354 assert_equal 'test_first_name', response_object['first_name'],
355 'expecting first name'
357 # four extra links; login link, group link, repo link and vm link
358 verify_num_links @all_links_at_start, 4
361 test "setup user twice with email and check two different objects created" do
362 authorize_with :admin
365 openid_prefix: 'https://www.google.com/accounts/o8/id',
366 repo_name: 'test_repo',
372 assert_response :success
373 response_items = JSON.parse(@response.body)['items']
374 response_object = find_obj_in_resp response_items, 'User', nil
375 assert_not_nil response_object['uuid'], 'expected uuid for new user'
376 assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
377 verify_num_links @all_links_at_start, 3 # openid, group, and repo. no vm
381 user: {email: 'abc@xyz.com'},
382 openid_prefix: 'https://www.google.com/accounts/o8/id'
385 assert_response :success
386 response_items = JSON.parse(@response.body)['items']
387 response_object2 = find_obj_in_resp response_items, 'User', nil
388 assert_not_equal response_object['uuid'], response_object2['uuid'],
389 'expected same uuid as first create operation'
390 assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
392 # extra login link only
393 verify_num_links @all_links_at_start, 4
396 test "setup user with openid prefix" do
397 authorize_with :admin
400 repo_name: 'test_repo',
401 openid_prefix: 'http://www.xyz.com/account',
403 first_name: "in_create_test_first_name",
404 last_name: "test_last_name",
405 email: "test@abc.com"
409 assert_response :success
411 response_items = JSON.parse(@response.body)['items']
412 created = find_obj_in_resp response_items, 'User', nil
414 assert_equal 'in_create_test_first_name', created['first_name']
415 assert_not_nil created['uuid'], 'expected uuid for new user'
416 assert_not_nil created['email'], 'expected non-nil email'
417 assert_nil created['identity_url'], 'expected no identity_url'
420 # 3 new links: arvados#user, repo, and 'All users' group.
421 verify_num_links @all_links_at_start, 3
423 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
424 created['uuid'], created['email'], 'arvados#user', false, 'User'
426 verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
427 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
429 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
430 'All users', created['uuid'], 'arvados#group', true, 'Group'
432 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
433 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
436 test "invoke setup with no openid prefix, expect error" do
437 authorize_with :admin
440 repo_name: 'test_repo',
442 first_name: "in_create_test_first_name",
443 last_name: "test_last_name",
444 email: "test@abc.com"
448 response_body = JSON.parse(@response.body)
449 response_errors = response_body['errors']
450 assert_not_nil response_errors, 'Expected error in response'
451 assert (response_errors.first.include? 'openid_prefix parameter is missing'),
452 'Expected ArgumentError'
455 test "setup user with user, vm and repo and verify links" do
456 authorize_with :admin
460 first_name: "in_create_test_first_name",
461 last_name: "test_last_name",
462 email: "test@abc.com"
465 repo_name: 'test_repo',
466 openid_prefix: 'https://www.google.com/accounts/o8/id'
469 assert_response :success
471 response_items = JSON.parse(@response.body)['items']
472 created = find_obj_in_resp response_items, 'User', nil
474 assert_equal 'in_create_test_first_name', created['first_name']
475 assert_not_nil created['uuid'], 'expected uuid for new user'
476 assert_not_nil created['email'], 'expected non-nil email'
477 assert_nil created['identity_url'], 'expected no identity_url'
479 # expect 4 new links: arvados#user, repo, vm and 'All users' group link
480 verify_num_links @all_links_at_start, 4
482 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
483 created['uuid'], created['email'], 'arvados#user', false, 'User'
485 verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
486 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
488 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
489 'All users', created['uuid'], 'arvados#group', true, 'Group'
491 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
492 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
495 test "create user as non admin user and expect error" do
496 authorize_with :active
499 user: {email: 'abc@xyz.com'}
502 response_body = JSON.parse(@response.body)
503 response_errors = response_body['errors']
504 assert_not_nil response_errors, 'Expected error in response'
505 assert (response_errors.first.include? 'PermissionDenied'),
506 'Expected PermissionDeniedError'
509 test "setup user as non admin user and expect error" do
510 authorize_with :active
513 openid_prefix: 'https://www.google.com/accounts/o8/id',
514 user: {email: 'abc@xyz.com'}
517 response_body = JSON.parse(@response.body)
518 response_errors = response_body['errors']
519 assert_not_nil response_errors, 'Expected error in response'
520 assert (response_errors.first.include? 'PermissionDenied'),
521 'Expected PermissionDeniedError'
524 test "setup user in multiple steps and verify response" do
525 authorize_with :admin
528 openid_prefix: 'http://www.xyz.com/account',
530 email: "test@abc.com"
534 assert_response :success
536 response_items = JSON.parse(@response.body)['items']
537 created = find_obj_in_resp response_items, 'User', nil
539 assert_not_nil created['uuid'], 'expected uuid for new user'
540 assert_not_nil created['email'], 'expected non-nil email'
541 assert_equal created['email'], 'test@abc.com', 'expected input email'
543 # verify links; 2 new links: arvados#user, and 'All users' group.
544 verify_num_links @all_links_at_start, 2
546 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
547 created['uuid'], created['email'], 'arvados#user', false, 'User'
549 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
550 'All users', created['uuid'], 'arvados#group', true, 'Group'
552 verify_link response_items, 'arvados#repository', false, 'permission', 'can_write',
553 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
555 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
556 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
558 # invoke setup with a repository
560 openid_prefix: 'http://www.xyz.com/account',
561 repo_name: 'new_repo',
562 uuid: created['uuid']
565 assert_response :success
567 response_items = JSON.parse(@response.body)['items']
568 created = find_obj_in_resp response_items, 'User', nil
570 assert_equal 'test@abc.com', created['email'], 'expected input email'
573 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
574 'All users', created['uuid'], 'arvados#group', true, 'Group'
576 verify_link response_items, 'arvados#repository', true, 'permission', 'can_write',
577 'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
579 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
580 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
582 # invoke setup with a vm_uuid
585 openid_prefix: 'http://www.xyz.com/account',
589 uuid: created['uuid']
592 assert_response :success
594 response_items = JSON.parse(@response.body)['items']
595 created = find_obj_in_resp response_items, 'User', nil
597 assert_equal created['email'], 'test@abc.com', 'expected original email'
600 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
601 'All users', created['uuid'], 'arvados#group', true, 'Group'
603 # since no repo name in input, we won't get any; even though user has one
604 verify_link response_items, 'arvados#repository', false, 'permission', 'can_write',
605 'new_repo', created['uuid'], 'arvados#repository', true, 'Repository'
607 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
608 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
611 def verify_num_links (original_links, expected_additional_links)
613 assert_equal original_links.size+expected_additional_links, Link.all.size,
614 "Expected #{expected_additional_links.inspect} more links"
617 def find_obj_in_resp (response_items, object_type, head_kind=nil)
619 response_items.each { |x|
624 if object_type == 'User'
629 else # looking for a link
630 if x['head_kind'] == head_kind
639 def verify_link(response_items, link_object_name, expect_link, link_class,
640 link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
642 link = find_obj_in_resp response_items, 'Link', link_object_name
645 assert_nil link, "Expected no link for #{link_object_name}"
649 assert_not_nil link, "Expected link for #{link_object_name}"
652 object = Object.const_get(class_name).where(name: head_uuid)
653 assert [] != object, "expected #{class_name} with name #{head_uuid}"
654 head_uuid = object.first[:uuid]
656 assert_equal link['link_class'], link_class,
657 "did not find expected link_class for #{link_object_name}"
659 assert_equal link['name'], link_name,
660 "did not find expected link_name for #{link_object_name}"
662 assert_equal link['tail_uuid'], tail_uuid,
663 "did not find expected tail_uuid for #{link_object_name}"
665 assert_equal link['head_kind'], head_kind,
666 "did not find expected head_kind for #{link_object_name}"
668 assert_equal link['head_uuid'], head_uuid,
669 "did not find expected head_uuid for #{link_object_name}"