X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7ff8f285eb1ff374091e2d8e8e23f36d8b15a775..a1cc22fad427a935d7747bd6dc2c6b901c8a799a:/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 0ce9f1137f..2b643d4b27 100644 --- a/services/api/test/functional/arvados/v1/users_controller_test.rb +++ b/services/api/test/functional/arvados/v1/users_controller_test.rb @@ -13,6 +13,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase @initial_link_count = Link.count @vm_uuid = virtual_machines(:testvm).uuid ActionMailer::Base.deliveries = [] + Rails.configuration.Users.ActivatedUsersAreVisibleToOthers = false end test "activate a user after signing UA" do @@ -67,7 +68,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "respond 401 if given token exists but user record is missing" do authorize_with :valid_token_deleted_user - get :current, {format: :json} + get :current, format: :json assert_response 401 end @@ -120,12 +121,10 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_nil updated['username'], 'expected no username' end - test "create user with user, vm and repo as input" do + test "create user with user and vm as input" do authorize_with :admin - repo_name = 'usertestrepo' post :setup, params: { - repo_name: repo_name, user: { uuid: 'zzzzz-tpzed-abcdefghijklmno', first_name: "in_create_test_first_name", @@ -144,13 +143,10 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil created['email'], 'expected non-nil email' assert_nil created['identity_url'], 'expected no identity_url' - # repo link and link add user to 'All users' group - verify_links_added 3 - - verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - "foo/#{repo_name}", created['uuid'], 'arvados#repository', true, 'Repository' + # added links: vm permission, 'all users' group + verify_links_added 2 - verify_link response_items, 'arvados#group', true, 'permission', 'can_read', + verify_link response_items, 'arvados#group', true, 'permission', 'can_write', 'All users', created['uuid'], 'arvados#group', true, 'Group' verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login', @@ -164,7 +160,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase post :setup, params: { uuid: 'bogus_uuid', - repo_name: 'usertestrepo', vm_uuid: @vm_uuid } response_body = JSON.parse(@response.body) @@ -178,7 +173,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase post :setup, params: { user: {uuid: 'bogus_uuid'}, - repo_name: 'usertestrepo', vm_uuid: @vm_uuid, } response_body = JSON.parse(@response.body) @@ -192,7 +186,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase authorize_with :admin post :setup, params: { - repo_name: 'usertestrepo', vm_uuid: @vm_uuid, } response_body = JSON.parse(@response.body) @@ -207,7 +200,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase post :setup, params: { user: {}, - repo_name: 'usertestrepo', vm_uuid: @vm_uuid, } response_body = JSON.parse(@response.body) @@ -217,13 +209,12 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 'Expected ArgumentError' end - test "invoke setup with existing uuid, vm and repo and verify links" do + test "invoke setup with existing uuid and vm permission, and verify links" do authorize_with :admin inactive_user = users(:inactive) post :setup, params: { uuid: users(:inactive).uuid, - repo_name: 'usertestrepo', vm_uuid: @vm_uuid } @@ -237,10 +228,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_equal inactive_user['email'], resp_obj['email'], 'expecting inactive user email' - # expect repo and vm links - verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - 'inactiveuser/usertestrepo', resp_obj['uuid'], 'arvados#repository', true, 'Repository' - + # expect vm permission link verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login', @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' end @@ -265,7 +253,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 'expecting inactive user email' end - test "setup user with valid email and repo as input" do + test "setup user with valid email and repo(ignored) as input" do authorize_with :admin post :setup, params: { @@ -279,15 +267,14 @@ 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' - # three extra links; system_group, group and repo perms - verify_links_added 3 + # added links: system_group, 'all users' group. + verify_links_added 2 end test "setup user with fake vm and expect error" do authorize_with :admin post :setup, params: { - repo_name: 'usertestrepo', vm_uuid: 'no_such_vm', user: {email: 'foo@example.com'}, } @@ -299,11 +286,10 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 'Expected RuntimeError: No vm found for no_such_vm' end - test "setup user with valid email, repo and real vm as input" do + test "setup user with valid email and real vm as input" do authorize_with :admin post :setup, params: { - repo_name: 'usertestrepo', vm_uuid: @vm_uuid, user: {email: 'foo@example.com'} } @@ -314,8 +300,8 @@ 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, group, vm, repo - verify_links_added 4 + # added links; system_group, 'all users' group, vm. + verify_links_added 3 end test "setup user with valid email, no vm and no repo as input" do @@ -331,24 +317,20 @@ 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' - # two extra links; system_group, and group + # added links; system_group, 'all users' group. verify_links_added 2 - verify_link response_items, 'arvados#group', true, 'permission', 'can_read', + verify_link response_items, 'arvados#group', true, 'permission', 'can_write', 'All users', response_object['uuid'], 'arvados#group', true, 'Group' - verify_link response_items, 'arvados#repository', false, 'permission', 'can_manage', - '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' end - test "setup user with email, first name, repo name and vm uuid" do + test "setup user with email, first name, and vm uuid" do authorize_with :admin post :setup, params: { - repo_name: 'usertestrepo', vm_uuid: @vm_uuid, user: { first_name: 'test_first_name', @@ -364,8 +346,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_equal 'test_first_name', response_object['first_name'], 'expecting first name' - # four extra links; system_group, group, repo and vm - verify_links_added 4 + # added links: system_group, 'all users' group, vm. + verify_links_added 3 end test "setup user with an existing user email and check different object is created" do @@ -373,7 +355,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase inactive_user = users(:inactive) post :setup, params: { - repo_name: 'usertestrepo', user: { email: inactive_user['email'] } @@ -386,15 +367,14 @@ 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, group, and repo. No vm link. - verify_links_added 3 + # added links: system_group, 'all users' group. + verify_links_added 2 end test "setup user with openid prefix" do authorize_with :admin post :setup, params: { - repo_name: 'usertestrepo', user: { first_name: "in_create_test_first_name", last_name: "test_last_name", @@ -412,21 +392,17 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil created['email'], 'expected non-nil email' assert_nil created['identity_url'], 'expected no identity_url' - # verify links - # three new links: system_group, repo, and 'All users' group. - verify_links_added 3 - - verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository' + # added links: system_group, 'all users' group. + verify_links_added 2 - verify_link response_items, 'arvados#group', true, 'permission', 'can_read', + verify_link response_items, 'arvados#group', true, 'permission', 'can_write', 'All users', created['uuid'], 'arvados#group', true, 'Group' verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login', nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine' end - test "setup user with user, vm and repo and verify links" do + test "setup user with user and vm, and verify links" do authorize_with :admin post :setup, params: { @@ -436,7 +412,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase email: "foo@example.com" }, vm_uuid: @vm_uuid, - repo_name: 'usertestrepo', } assert_response :success @@ -449,15 +424,12 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_not_nil created['email'], 'expected non-nil email' assert_nil created['identity_url'], 'expected no identity_url' - # four new links: system_group, repo, vm and 'All users' group link - verify_links_added 4 + # added links: system_group, 'all users' group, vm + verify_links_added 3 # 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', - 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository' - - verify_link response_items, 'arvados#group', true, 'permission', 'can_read', + verify_link response_items, 'arvados#group', true, 'permission', 'can_write', 'All users', created['uuid'], 'arvados#group', true, 'Group' verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login', @@ -492,13 +464,11 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 'Expected Forbidden error' end - test "setup active user with repo and no vm" do + test "setup active user with no vm" do authorize_with :admin active_user = users(:active) - # invoke setup with a repository post :setup, params: { - repo_name: 'usertestrepo', uuid: active_user['uuid'] } @@ -509,13 +479,10 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_equal active_user[:email], created['email'], 'expected input email' - # verify links - verify_link response_items, 'arvados#group', true, 'permission', 'can_read', + # verify links + verify_link response_items, 'arvados#group', true, 'permission', 'can_write', 'All users', created['uuid'], 'arvados#group', true, 'Group' - verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage', - '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' end @@ -523,13 +490,7 @@ 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, params: { vm_uuid: @vm_uuid, uuid: active_user['uuid'], @@ -544,12 +505,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase assert_equal active_user['email'], created['email'], 'expected original email' # verify links - verify_link response_items, 'arvados#group', true, 'permission', 'can_read', + verify_link response_items, 'arvados#group', true, 'permission', 'can_write', 'All users', created['uuid'], 'arvados#group', true, 'Group' - 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' end @@ -609,6 +567,23 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "setup user with send notification param true and verify email" do authorize_with :admin + Rails.configuration.Users.UserSetupMailText = %{ +<% if not @user.full_name.empty? -%> +<%= @user.full_name %>, +<% else -%> +Hi there, +<% end -%> + +Your Arvados shell account has been set up. Please visit the virtual machines page <% if Rails.configuration.Services.Workbench1.ExternalURL %>at + +<%= Rails.configuration.Services.Workbench1.ExternalURL %><%= "/" if !Rails.configuration.Services.Workbench1.ExternalURL.to_s.end_with?("/") %>users/<%= @user.uuid%>/virtual_machines <% else %><% end %> + +for connection instructions. + +Thanks, +The Arvados team. +} + post :setup, params: { send_notification_email: 'true', user: { @@ -637,7 +612,6 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase authorize_with :admin active_user = users(:active) - # invoke setup with a repository put :update, params: { id: active_user['uuid'], user: { @@ -648,7 +622,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 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]) + assert_equal({read: true, write: true}, updated.group_permissions[all_users_group_uuid]) end test "non-admin user can get basic information about readable users" do @@ -656,6 +630,12 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase get(:index) check_non_admin_index check_readable_users_index [:spectator], [:inactive, :active] + json_response["items"].each do |u| + if u["uuid"] == users(:spectator).uuid + assert_equal true, u["can_write"] + assert_equal true, u["can_manage"] + end + end end test "non-admin user gets only safe attributes from users#show" do @@ -783,27 +763,6 @@ 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 @@ -886,7 +845,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase ['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 /"]) + api_client_authorizations(auth).update(scopes: ["GET /", "POST /", "PUT /"]) end authorize_with(:active_trustedclient) post(:merge, params: { @@ -1039,9 +998,16 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase test "batch update" do existinguuid = 'remot-tpzed-foobarbazwazqux' newuuid = 'remot-tpzed-newnarnazwazqux' + unchanginguuid = 'remot-tpzed-nochangingattrs' + conflictinguuid1 = 'remot-tpzed-conflictingnam1' + conflictinguuid2 = 'remot-tpzed-conflictingnam2' 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) + + Rails.configuration.Login.LoginCluster = 'remot' authorize_with(:admin) patch(:batch_update, @@ -1053,11 +1019,28 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 'is_active' => true, 'is_admin' => true, 'prefs' => {'foo' => 'bar'}, + 'is_invited' => true }, newuuid => { 'first_name' => 'noot', 'email' => 'root@remot.example.com', 'username' => '', + 'is_invited' => true + }, + unchanginguuid => { + 'email' => 'root@unchanging.example.com', + 'prefs' => {'foo' => {'bar' => 'baz'}}, + 'is_invited' => true + }, + conflictinguuid1 => { + 'email' => 'root@conflictingname1.example.com', + 'username' => 'active', + 'is_invited' => true + }, + conflictinguuid2 => { + 'email' => 'root@conflictingname2.example.com', + 'username' => 'federatedactive', + 'is_invited' => true }, }}) assert_response(:success) @@ -1070,10 +1053,43 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase 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 + + test 'batch update does not produce spurious log events' do + # test for bug #21304 + + existinguuid = 'remot-tpzed-foobarbazwazqux' + act_as_system_user do + User.create!(uuid: existinguuid, + first_name: 'root', + is_active: true, + ) + end + assert_equal(1, Log.where(object_uuid: existinguuid).count) + + Rails.configuration.Login.LoginCluster = 'remot' + + authorize_with(:admin) + patch(:batch_update, + params: { + updates: { + existinguuid => { + 'first_name' => 'root', + 'email' => '', + 'username' => '', + 'is_active' => true, + 'is_invited' => true + }, + }}) + assert_response(:success) + + assert_equal(1, Log.where(object_uuid: existinguuid).count) end - NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "email", "first_name", - "last_name", "username"].sort + NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "is_admin", "is_invited", "email", "first_name", + "last_name", "username", "can_write", "can_manage"].sort def check_non_admin_index assert_response :success