X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/17379ed7eb6e143667e53097f5ada570f051efd1..1fb68cf0a5f6ad058a54d4f822385983b3504987:/services/api/test/functional/arvados/v1/users_controller_test.rb diff --git a/services/api/test/functional/arvados/v1/users_controller_test.rb b/services/api/test/functional/arvados/v1/users_controller_test.rb index 0b5ff722df..ea5d5b1436 100644 --- a/services/api/test/functional/arvados/v1/users_controller_test.rb +++ b/services/api/test/functional/arvados/v1/users_controller_test.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'test_helper' require 'helpers/users_test_helper' @@ -6,13 +10,14 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase include UsersTestHelper setup do - @all_links_at_start = Link.all + @initial_link_count = Link.count @vm_uuid = virtual_machines(:testvm).uuid + ActionMailer::Base.deliveries = [] end test "activate a user after signing UA" do authorize_with :inactive_but_signed_user_agreement - post :activate, id: users(:inactive_but_signed_user_agreement).uuid + post :activate, params: {id: users(:inactive_but_signed_user_agreement).uuid} assert_response :success assert_not_nil assigns(:object) me = JSON.parse(@response.body) @@ -44,7 +49,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase authorize_with :inactive assert_equal false, users(:inactive).is_active - post :activate, id: users(:inactive).uuid + post :activate, params: {id: users(:inactive).uuid} assert_response 403 resp = json_response @@ -54,7 +59,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "activate an already-active user" do authorize_with :active - post :activate, id: users(:active).uuid + post :activate, params: {id: users(:active).uuid} assert_response :success me = JSON.parse(@response.body) assert_equal true, me['is_active'] @@ -68,10 +73,12 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "create new user with user as input" do authorize_with :admin - post :create, user: { - first_name: "test_first_name", - last_name: "test_last_name", - email: "foo@example.com" + post :create, params: { + user: { + first_name: "test_first_name", + last_name: "test_last_name", + email: "foo@example.com" + } } assert_response :success created = JSON.parse(@response.body) @@ -81,13 +88,44 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_nil created['identity_url'], 'expected no identity_url' end + test "create new user with empty username" do + authorize_with :admin + post :create, params: { + user: { + first_name: "test_first_name", + last_name: "test_last_name", + username: "" + } + } + assert_response :success + created = JSON.parse(@response.body) + assert_equal 'test_first_name', created['first_name'] + assert_not_nil created['uuid'], 'expected uuid for the newly created user' + assert_nil created['email'], 'expected no email' + assert_nil created['username'], 'expected no username' + end + + test "update user with empty username" do + authorize_with :admin + user = users('spectator') + assert_not_nil user['username'] + put :update, params: { + id: users('spectator')['uuid'], + user: { + username: "" + } + } + assert_response :success + updated = JSON.parse(@response.body) + assert_nil updated['username'], 'expected no username' + end + test "create user with user, vm and repo as input" do authorize_with :admin - repo_name = 'test_repo' + repo_name = 'usertestrepo' - post :setup, { + post :setup, params: { repo_name: repo_name, - openid_prefix: 'https://www.google.com/accounts/o8/id', user: { uuid: 'zzzzz-tpzed-abcdefghijklmno', first_name: "in_create_test_first_name", @@ -106,14 +144,11 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil created['email'], 'expected non-nil email' assert_nil created['identity_url'], 'expected no identity_url' - # arvados#user, repo link and link add user to 'All users' group - verify_num_links @all_links_at_start, 4 - - verify_link response_items, 'arvados#user', true, 'permission', 'can_login', - created['uuid'], created['email'], 'arvados#user', false, 'User' + # repo link and link add user to 'All users' group + verify_links_added 3 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - repo_name, created['uuid'], 'arvados#repository', true, 'Repository' + "foo/#{repo_name}", created['uuid'], 'arvados#repository', true, 'Repository' verify_link response_items, 'arvados#group', true, 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true, 'Group' @@ -127,9 +162,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with bogus uuid and expect error" do authorize_with :admin - post :setup, { + post :setup, params: { uuid: 'bogus_uuid', - repo_name: 'test_repo', + repo_name: 'usertestrepo', vm_uuid: @vm_uuid } response_body = JSON.parse(@response.body) @@ -141,11 +176,10 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with bogus uuid in user and expect error" do authorize_with :admin - post :setup, { + post :setup, params: { user: {uuid: 'bogus_uuid'}, - repo_name: 'test_repo', + repo_name: 'usertestrepo', vm_uuid: @vm_uuid, - openid_prefix: 'https://www.google.com/accounts/o8/id' } response_body = JSON.parse(@response.body) response_errors = response_body['errors'] @@ -157,10 +191,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with no uuid and user, expect error" do authorize_with :admin - post :setup, { - repo_name: 'test_repo', + post :setup, params: { + repo_name: 'usertestrepo', vm_uuid: @vm_uuid, - openid_prefix: 'https://www.google.com/accounts/o8/id' } response_body = JSON.parse(@response.body) response_errors = response_body['errors'] @@ -172,11 +205,10 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with no uuid and email, expect error" do authorize_with :admin - post :setup, { + post :setup, params: { user: {}, - repo_name: 'test_repo', + repo_name: 'usertestrepo', vm_uuid: @vm_uuid, - openid_prefix: 'https://www.google.com/accounts/o8/id' } response_body = JSON.parse(@response.body) response_errors = response_body['errors'] @@ -189,9 +221,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase authorize_with :admin inactive_user = users(:inactive) - post :setup, { + post :setup, params: { uuid: users(:inactive).uuid, - repo_name: 'test_repo', + repo_name: 'usertestrepo', vm_uuid: @vm_uuid } @@ -207,37 +239,17 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase # expect repo and vm links verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - 'test_repo', resp_obj['uuid'], 'arvados#repository', true, 'Repository' + 'inactiveuser/usertestrepo', resp_obj['uuid'], 'arvados#repository', true, 'Repository' verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login', @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' end - test "invoke setup with existing uuid in user, verify response" do - authorize_with :admin - inactive_user = users(:inactive) - - post :setup, { - user: {uuid: inactive_user['uuid']}, - openid_prefix: 'https://www.google.com/accounts/o8/id' - } - - assert_response :success - - response_items = JSON.parse(@response.body)['items'] - resp_obj = find_obj_in_resp response_items, 'User', nil - - assert_not_nil resp_obj['uuid'], 'expected uuid for the new user' - assert_equal inactive_user['uuid'], resp_obj['uuid'] - assert_equal inactive_user['email'], resp_obj['email'], - 'expecting inactive user email' - end - test "invoke setup with existing uuid but different email, expect original email" do authorize_with :admin inactive_user = users(:inactive) - post :setup, { + post :setup, params: { uuid: inactive_user['uuid'], user: {email: 'junk_email'} } @@ -256,10 +268,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with valid email and repo as input" do authorize_with :admin - post :setup, { - repo_name: 'test_repo', + post :setup, params: { + repo_name: 'usertestrepo', user: {email: 'foo@example.com'}, - openid_prefix: 'https://www.google.com/accounts/o8/id' } assert_response :success @@ -268,18 +279,17 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil response_object['uuid'], 'expected uuid for the new user' assert_equal response_object['email'], 'foo@example.com', 'expected given email' - # four extra links; system_group, login, group and repo perms - verify_num_links @all_links_at_start, 4 + # three extra links; system_group, group and repo perms + verify_links_added 3 end test "setup user with fake vm and expect error" do authorize_with :admin - post :setup, { - repo_name: 'test_repo', + post :setup, params: { + repo_name: 'usertestrepo', vm_uuid: 'no_such_vm', user: {email: 'foo@example.com'}, - openid_prefix: 'https://www.google.com/accounts/o8/id' } response_body = JSON.parse(@response.body) @@ -292,9 +302,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with valid email, repo and real vm as input" do authorize_with :admin - post :setup, { - repo_name: 'test_repo', - openid_prefix: 'https://www.google.com/accounts/o8/id', + post :setup, params: { + repo_name: 'usertestrepo', vm_uuid: @vm_uuid, user: {email: 'foo@example.com'} } @@ -305,16 +314,15 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil response_object['uuid'], 'expected uuid for the new user' assert_equal response_object['email'], 'foo@example.com', 'expected given email' - # five extra links; system_group, login, group, vm, repo - verify_num_links @all_links_at_start, 5 + # four extra links; system_group, group, vm, repo + verify_links_added 4 end test "setup user with valid email, no vm and no repo as input" do authorize_with :admin - post :setup, { + post :setup, params: { user: {email: 'foo@example.com'}, - openid_prefix: 'https://www.google.com/accounts/o8/id' } assert_response :success @@ -323,17 +331,14 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil response_object['uuid'], 'expected uuid for new user' assert_equal response_object['email'], 'foo@example.com', 'expected given email' - # three extra links; system_group, login, and group - verify_num_links @all_links_at_start, 3 - - verify_link response_items, 'arvados#user', true, 'permission', 'can_login', - response_object['uuid'], response_object['email'], 'arvados#user', false, 'User' + # two extra links; system_group, and group + verify_links_added 2 verify_link response_items, 'arvados#group', true, 'permission', 'can_read', 'All users', response_object['uuid'], 'arvados#group', true, 'Group' verify_link response_items, 'arvados#repository', false, 'permission', 'can_manage', - 'test_repo', response_object['uuid'], 'arvados#repository', true, 'Repository' + 'foo/usertestrepo', response_object['uuid'], 'arvados#repository', true, 'Repository' verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login', nil, response_object['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' @@ -342,9 +347,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with email, first name, repo name and vm uuid" do authorize_with :admin - post :setup, { - openid_prefix: 'https://www.google.com/accounts/o8/id', - repo_name: 'test_repo', + post :setup, params: { + repo_name: 'usertestrepo', vm_uuid: @vm_uuid, user: { first_name: 'test_first_name', @@ -360,17 +364,16 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_equal 'test_first_name', response_object['first_name'], 'expecting first name' - # five extra links; system_group, login, group, repo and vm - verify_num_links @all_links_at_start, 5 + # four extra links; system_group, group, repo and vm + verify_links_added 4 end test "setup user with an existing user email and check different object is created" do authorize_with :admin inactive_user = users(:inactive) - post :setup, { - openid_prefix: 'https://www.google.com/accounts/o8/id', - repo_name: 'test_repo', + post :setup, params: { + repo_name: 'usertestrepo', user: { email: inactive_user['email'] } @@ -383,16 +386,15 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_equal response_object['uuid'], inactive_user['uuid'], 'expected different uuid after create operation' assert_equal inactive_user['email'], response_object['email'], 'expected given email' - # system_group, openid, group, and repo. No vm link. - verify_num_links @all_links_at_start, 4 + # system_group, group, and repo. No vm link. + verify_links_added 3 end test "setup user with openid prefix" do authorize_with :admin - post :setup, { - repo_name: 'test_repo', - openid_prefix: 'http://www.example.com/account', + post :setup, params: { + repo_name: 'usertestrepo', user: { first_name: "in_create_test_first_name", last_name: "test_last_name", @@ -411,14 +413,11 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_nil created['identity_url'], 'expected no identity_url' # verify links - # four new links: system_group, arvados#user, repo, and 'All users' group. - verify_num_links @all_links_at_start, 4 - - verify_link response_items, 'arvados#user', true, 'permission', 'can_login', - created['uuid'], created['email'], 'arvados#user', false, 'User' + # three new links: system_group, repo, and 'All users' group. + verify_links_added 3 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository' + 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository' verify_link response_items, 'arvados#group', true, 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true, 'Group' @@ -427,37 +426,17 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' end - test "invoke setup with no openid prefix, expect error" do - authorize_with :admin - - post :setup, { - repo_name: 'test_repo', - user: { - first_name: "in_create_test_first_name", - last_name: "test_last_name", - email: "foo@example.com" - } - } - - response_body = JSON.parse(@response.body) - response_errors = response_body['errors'] - assert_not_nil response_errors, 'Expected error in response' - assert (response_errors.first.include? 'openid_prefix parameter is missing'), - 'Expected ArgumentError' - end - test "setup user with user, vm and repo and verify links" do authorize_with :admin - post :setup, { + post :setup, params: { user: { first_name: "in_create_test_first_name", last_name: "test_last_name", email: "foo@example.com" }, vm_uuid: @vm_uuid, - repo_name: 'test_repo', - openid_prefix: 'https://www.google.com/accounts/o8/id' + repo_name: 'usertestrepo', } assert_response :success @@ -470,15 +449,13 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil created['email'], 'expected non-nil email' assert_nil created['identity_url'], 'expected no identity_url' - # five new links: system_group, arvados#user, repo, vm and 'All - # users' group link - verify_num_links @all_links_at_start, 5 + # four new links: system_group, repo, vm and 'All users' group link + verify_links_added 4 - verify_link response_items, 'arvados#user', true, 'permission', 'can_login', - created['uuid'], created['email'], 'arvados#user', false, 'User' + # system_group isn't part of the response. See User#add_system_group_permission_link verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - 'test_repo', created['uuid'], 'arvados#repository', true, 'Repository' + 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository' verify_link response_items, 'arvados#group', true, 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true, 'Group' @@ -490,7 +467,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "create user as non admin user and expect error" do authorize_with :active - post :create, { + post :create, params: { user: {email: 'foo@example.com'} } @@ -504,8 +481,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user as non admin user and expect error" do authorize_with :active - post :setup, { - openid_prefix: 'https://www.google.com/accounts/o8/id', + post :setup, params: { user: {email: 'foo@example.com'} } @@ -521,8 +497,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase active_user = users(:active) # invoke setup with a repository - post :setup, { - repo_name: 'new_repo', + post :setup, params: { + repo_name: 'usertestrepo', uuid: active_user['uuid'] } @@ -538,7 +514,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 'All users', created['uuid'], 'arvados#group', true, 'Group' verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - 'new_repo', created['uuid'], 'arvados#repository', true, 'Repository' + 'active/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository' verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login', nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' @@ -547,9 +523,14 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup active user with vm and no repo" do authorize_with :admin active_user = users(:active) + repos_query = Repository.where(owner_uuid: active_user.uuid) + repo_link_query = Link.where(tail_uuid: active_user.uuid, + link_class: "permission", name: "can_manage") + repos_count = repos_query.count + repo_link_count = repo_link_query.count # invoke setup with a repository - post :setup, { + post :setup, params: { vm_uuid: @vm_uuid, uuid: active_user['uuid'], email: 'junk_email' @@ -566,8 +547,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase verify_link response_items, 'arvados#group', true, 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true, 'Group' - verify_link response_items, 'arvados#repository', false, 'permission', 'can_manage', - 'new_repo', created['uuid'], 'arvados#repository', true, 'Repository' + assert_equal(repos_count, repos_query.count) + assert_equal(repo_link_count, repo_link_query.count) verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login', @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' @@ -579,12 +560,12 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert active_user['is_active'], 'expected is_active for active user' verify_link_existence active_user['uuid'], active_user['email'], - false, true, false, true, true + false, true, true, true, true authorize_with :admin # now unsetup this user - post :unsetup, id: active_user['uuid'] + post :unsetup, params: {id: active_user['uuid']} assert_response :success response_user = JSON.parse(@response.body) @@ -598,7 +579,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase active_user = User.find_by_uuid(users(:active).uuid) readable_groups = active_user.groups_i_can(:read) - all_users_group = Group.all.collect(&:uuid).select { |g| g.match /-f+$/ } + all_users_group = Group.all.collect(&:uuid).select { |g| g.match(/-f+$/) } refute_includes(readable_groups, all_users_group, "active user can read All Users group after being deactivated") assert_equal(false, active_user.is_invited, @@ -608,8 +589,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with send notification param false and verify no email" do authorize_with :admin - post :setup, { - openid_prefix: 'http://www.example.com/account', + post :setup, params: { send_notification_email: 'false', user: { email: "foo@example.com" @@ -629,8 +609,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with send notification param true and verify email" do authorize_with :admin - post :setup, { - openid_prefix: 'http://www.example.com/account', + post :setup, params: { send_notification_email: 'true', user: { email: "foo@example.com" @@ -646,15 +625,30 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase setup_email = ActionMailer::Base.deliveries.last assert_not_nil setup_email, 'Expected email after setup' - assert_equal Rails.configuration.user_notifier_email_from, setup_email.from[0] + assert_equal Rails.configuration.Users.UserNotifierEmailFrom, setup_email.from[0] assert_equal 'foo@example.com', setup_email.to[0] - assert_equal 'Welcome to Curoverse', setup_email.subject - assert (setup_email.body.to_s.include? 'Your Arvados account has been set up'), - 'Expected Your Arvados account has been set up in email body' - assert (setup_email.body.to_s.include? 'foo@example.com'), - 'Expected user email in email body' - assert (setup_email.body.to_s.include? Rails.configuration.workbench_address), - 'Expected workbench url in email body' + assert_equal 'Welcome to Arvados - account enabled', setup_email.subject + assert (setup_email.body.to_s.include? 'Your Arvados shell account has been set up'), + 'Expected Your Arvados shell account has been set up in email body' + assert (setup_email.body.to_s.include? "#{Rails.configuration.Services.Workbench1.ExternalURL}users/#{created['uuid']}/virtual_machines"), 'Expected virtual machines url in email body' + end + + test "setup inactive user by changing is_active to true" do + authorize_with :admin + active_user = users(:active) + + # invoke setup with a repository + put :update, params: { + id: active_user['uuid'], + user: { + is_active: true, + } + } + assert_response :success + assert_equal active_user['uuid'], json_response['uuid'] + updated = User.where(uuid: active_user['uuid']).first + assert_equal(true, updated.is_active) + assert_equal({read: true}, updated.group_permissions[all_users_group_uuid]) end test "non-admin user can get basic information about readable users" do @@ -666,25 +660,25 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "non-admin user gets only safe attributes from users#show" do g = act_as_system_user do - create :group + create :group, group_class: "role" end users = create_list :active_user, 2, join_groups: [g] token = create :token, user: users[0] authorize_with_token token - get :show, id: users[1].uuid + get :show, params: {id: users[1].uuid} check_non_admin_show end [2, 4].each do |limit| test "non-admin user can limit index to #{limit}" do g = act_as_system_user do - create :group + create :group, group_class: "role" end users = create_list :active_user, 4, join_groups: [g] token = create :token, user: users[0] authorize_with_token token - get(:index, limit: limit) + get(:index, params: {limit: limit}) check_non_admin_index assert_equal(limit, json_response["items"].size, "non-admin index limit was ineffective") @@ -703,14 +697,14 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "admin can filter on user.is_active" do authorize_with :admin - get(:index, filters: [["is_active", "=", "true"]]) + get(:index, params: {filters: [["is_active", "=", "true"]]}) assert_response :success check_readable_users_index [:active, :spectator], [:inactive] end test "admin can search where user.is_active" do authorize_with :admin - get(:index, where: {is_active: true}) + get(:index, params: {where: {is_active: true}}) assert_response :success check_readable_users_index [:active, :spectator], [:inactive] end @@ -718,7 +712,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "update active_no_prefs user profile and expect notification email" do authorize_with :admin - put :update, { + put :update, params: { id: users(:active_no_prefs).uuid, user: { prefs: {:profile => {'organization' => 'example.com'}} @@ -742,7 +736,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase user = {} user[:prefs] = users(:active_no_prefs_profile_no_getting_started_shown).prefs user[:prefs][:profile] = {:profile => {'organization' => 'example.com'}} - put :update, { + put :update, params: { id: users(:active_no_prefs_profile_no_getting_started_shown).uuid, user: user } @@ -761,7 +755,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "update active user profile and expect no notification email" do authorize_with :admin - put :update, { + put :update, params: { id: users(:active).uuid, user: { prefs: {:profile => {'organization' => 'anotherexample.com'}} @@ -789,9 +783,306 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase "user's writable_by should include its owner_uuid") end + [ + [:admin, true], + [:active, false], + ].each do |auth_user, expect_success| + test "update_uuid as #{auth_user}" do + authorize_with auth_user + orig_uuid = users(:active).uuid + post :update_uuid, params: { + id: orig_uuid, + new_uuid: 'zbbbb-tpzed-abcde12345abcde', + } + if expect_success + assert_response :success + assert_empty User.where(uuid: orig_uuid) + else + assert_response 403 + assert_not_empty User.where(uuid: orig_uuid) + end + end + end + + test "merge with redirect_to_user_uuid=false" do + authorize_with :project_viewer_trustedclient + tok = api_client_authorizations(:project_viewer).api_token + post :merge, params: { + new_user_token: api_client_authorizations(:active_trustedclient).api_token, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: false, + } + assert_response(:success) + assert_nil(User.unscoped.find_by_uuid(users(:project_viewer).uuid).redirect_to_user_uuid) + + # because redirect_to_new_user=false, token owned by + # project_viewer should be deleted + auth = ApiClientAuthorization.validate(token: tok) + assert_nil(auth) + end + + test "merge remote to local as admin" do + authorize_with :admin + + remoteuser = User.create!(uuid: "zbbbb-tpzed-remotremotremot") + tok = ApiClientAuthorization.create!(user: remoteuser, api_client: api_clients(:untrusted)).api_token + + auth = ApiClientAuthorization.validate(token: tok) + assert_not_nil(auth) + assert_nil(remoteuser.redirect_to_user_uuid) + + post :merge, params: { + new_user_uuid: users(:active).uuid, + old_user_uuid: remoteuser.uuid, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + } + assert_response(:success) + remoteuser.reload + assert_equal(users(:active).uuid, remoteuser.redirect_to_user_uuid) + + # token owned by remoteuser should be deleted + auth = ApiClientAuthorization.validate(token: tok) + assert_nil(auth) + end + + test "refuse to merge user into self" do + authorize_with(:active_trustedclient) + post(:merge, params: { + new_user_token: api_client_authorizations(:active_trustedclient).api_token, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(422) + end + + [[:active, :project_viewer_trustedclient], + [:active_trustedclient, :project_viewer]].each do |src, dst| + test "refuse to merge with untrusted token (#{src} -> #{dst})" do + authorize_with(src) + post(:merge, params: { + new_user_token: api_client_authorizations(dst).api_token, + new_owner_uuid: api_client_authorizations(dst).user.uuid, + redirect_to_new_user: true, + }) + assert_response(403) + end + end + + [[:expired_trustedclient, :project_viewer_trustedclient], + [:project_viewer_trustedclient, :expired_trustedclient]].each do |src, dst| + test "refuse to merge with expired token (#{src} -> #{dst})" do + authorize_with(src) + post(:merge, params: { + new_user_token: api_client_authorizations(dst).api_token, + new_owner_uuid: api_client_authorizations(dst).user.uuid, + redirect_to_new_user: true, + }) + assert_response(401) + end + end + + [['src', :active_trustedclient], + ['dst', :project_viewer_trustedclient]].each do |which_scoped, auth| + test "refuse to merge with scoped #{which_scoped} token" do + act_as_system_user do + api_client_authorizations(auth).update_attributes(scopes: ["GET /", "POST /", "PUT /"]) + end + authorize_with(:active_trustedclient) + post(:merge, params: { + new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token, + new_owner_uuid: users(:project_viewer).uuid, + redirect_to_new_user: true, + }) + assert_response(403) + end + end + + test "refuse to merge if new_owner_uuid is not writable" do + authorize_with(:project_viewer_trustedclient) + post(:merge, params: { + new_user_token: api_client_authorizations(:active_trustedclient).api_token, + new_owner_uuid: groups(:anonymously_accessible_project).uuid, + redirect_to_new_user: true, + }) + assert_response(403) + end + + test "refuse to merge if new_owner_uuid is empty" do + authorize_with(:project_viewer_trustedclient) + post(:merge, params: { + new_user_token: api_client_authorizations(:active_trustedclient).api_token, + new_owner_uuid: "", + redirect_to_new_user: true, + }) + assert_response(422) + end + + test "refuse to merge if new_owner_uuid is not provided" do + authorize_with(:project_viewer_trustedclient) + post(:merge, params: { + new_user_token: api_client_authorizations(:active_trustedclient).api_token, + redirect_to_new_user: true, + }) + assert_response(422) + end + + test "refuse to update redirect_to_user_uuid directly" do + authorize_with(:active_trustedclient) + patch(:update, params: { + id: users(:active).uuid, + user: { + redirect_to_user_uuid: users(:active).uuid, + }, + }) + assert_response(403) + end + + test "merge 'project_viewer' account into 'active' account" do + authorize_with(:project_viewer_trustedclient) + post(:merge, params: { + new_user_token: api_client_authorizations(:active_trustedclient).api_token, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(:success) + assert_equal(users(:active).uuid, User.unscoped.find_by_uuid(users(:project_viewer).uuid).redirect_to_user_uuid) + + auth = ApiClientAuthorization.validate(token: api_client_authorizations(:project_viewer).api_token) + assert_not_nil(auth) + assert_not_nil(auth.user) + assert_equal(users(:active).uuid, auth.user.uuid) + end + + + test "merge 'project_viewer' account into 'active' account using uuids" do + authorize_with(:admin) + post(:merge, params: { + old_user_uuid: users(:project_viewer).uuid, + new_user_uuid: users(:active).uuid, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(:success) + assert_equal(users(:active).uuid, User.unscoped.find_by_uuid(users(:project_viewer).uuid).redirect_to_user_uuid) + + auth = ApiClientAuthorization.validate(token: api_client_authorizations(:project_viewer).api_token) + assert_not_nil(auth) + assert_not_nil(auth.user) + assert_equal(users(:active).uuid, auth.user.uuid) + end + + test "merge 'project_viewer' account into 'active' account using uuids denied for non-admin" do + authorize_with(:active) + post(:merge, params: { + old_user_uuid: users(:project_viewer).uuid, + new_user_uuid: users(:active).uuid, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(403) + assert_nil(users(:project_viewer).redirect_to_user_uuid) + end + + test "merge 'project_viewer' account into 'active' account using uuids denied missing old_user_uuid" do + authorize_with(:admin) + post(:merge, params: { + new_user_uuid: users(:active).uuid, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(422) + assert_nil(users(:project_viewer).redirect_to_user_uuid) + end + + test "merge 'project_viewer' account into 'active' account using uuids denied missing new_user_uuid" do + authorize_with(:admin) + post(:merge, params: { + old_user_uuid: users(:project_viewer).uuid, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(422) + assert_nil(users(:project_viewer).redirect_to_user_uuid) + end + + test "merge 'project_viewer' account into 'active' account using uuids denied bogus old_user_uuid" do + authorize_with(:admin) + post(:merge, params: { + old_user_uuid: "zzzzz-tpzed-bogusbogusbogus", + new_user_uuid: users(:active).uuid, + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(422) + assert_nil(users(:project_viewer).redirect_to_user_uuid) + end + + test "merge 'project_viewer' account into 'active' account using uuids denied bogus new_user_uuid" do + authorize_with(:admin) + post(:merge, params: { + old_user_uuid: users(:project_viewer).uuid, + new_user_uuid: "zzzzz-tpzed-bogusbogusbogus", + new_owner_uuid: users(:active).uuid, + redirect_to_new_user: true, + }) + assert_response(422) + assert_nil(users(:project_viewer).redirect_to_user_uuid) + end + + test "batch update fails for non-admin" do + authorize_with(:active) + patch(:batch_update, params: {updates: {}}) + assert_response(403) + end + + test "batch update" do + existinguuid = 'remot-tpzed-foobarbazwazqux' + newuuid = 'remot-tpzed-newnarnazwazqux' + unchanginguuid = 'remot-tpzed-nochangingattrs' + act_as_system_user do + User.create!(uuid: existinguuid, email: 'root@existing.example.com') + User.create!(uuid: unchanginguuid, email: 'root@unchanging.example.com', prefs: {'foo' => {'bar' => 'baz'}}) + end + assert_equal(1, Log.where(object_uuid: unchanginguuid).count) + + authorize_with(:admin) + patch(:batch_update, + params: { + updates: { + existinguuid => { + 'first_name' => 'root', + 'email' => 'root@remot.example.com', + 'is_active' => true, + 'is_admin' => true, + 'prefs' => {'foo' => 'bar'}, + }, + newuuid => { + 'first_name' => 'noot', + 'email' => 'root@remot.example.com', + 'username' => '', + }, + unchanginguuid => { + 'email' => 'root@unchanging.example.com', + 'prefs' => {'foo' => {'bar' => 'baz'}}, + }, + }}) + assert_response(:success) + + assert_equal('root', User.find_by_uuid(existinguuid).first_name) + assert_equal('root@remot.example.com', User.find_by_uuid(existinguuid).email) + assert_equal(true, User.find_by_uuid(existinguuid).is_active) + assert_equal(true, User.find_by_uuid(existinguuid).is_admin) + assert_equal({'foo' => 'bar'}, User.find_by_uuid(existinguuid).prefs) + + assert_equal('noot', User.find_by_uuid(newuuid).first_name) + assert_equal('root@remot.example.com', User.find_by_uuid(newuuid).email) + + assert_equal(1, Log.where(object_uuid: unchanginguuid).count) + end NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "email", "first_name", - "last_name"].sort + "last_name", "username"].sort def check_non_admin_index assert_response :success @@ -829,7 +1120,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase def check_inactive_user_findable(params={}) inactive_user = users(:inactive) - get(:index, params.merge(filters: [["email", "=", inactive_user.email]])) + get(:index, params: params.merge(filters: [["email", "=", inactive_user.email]])) assert_response :success user_list = json_response["items"] assert_equal(1, user_list.andand.count) @@ -839,15 +1130,13 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase "admin's filtered index did not return inactive user") end - def verify_num_links (original_links, expected_additional_links) - links_now = Link.all - assert_equal expected_additional_links, Link.all.size-original_links.size, - "Expected #{expected_additional_links.inspect} more links" + def verify_links_added more + assert_equal @initial_link_count+more, Link.count, + "Started with #{@initial_link_count} links, expected #{more} more" end def find_obj_in_resp (response_items, object_type, head_kind=nil) return_obj = nil - response_items response_items.each { |x| if !x next