1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'helpers/users_test_helper'
8 class UsersTest < ActionDispatch::IntegrationTest
9 include UsersTestHelper
11 test "setup user multiple times" do
12 post "/arvados/v1/users/setup",
15 uuid: 'zzzzz-tpzed-abcdefghijklmno',
16 first_name: "in_create_test_first_name",
17 last_name: "test_last_name",
18 email: "foo@example.com"
23 assert_response :success
25 response_items = json_response['items']
27 created = find_obj_in_resp response_items, 'arvados#user', nil
29 assert_equal 'in_create_test_first_name', created['first_name']
30 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
31 assert_equal 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
32 assert_not_nil created['email'], 'expected non-nil email'
33 assert_nil created['identity_url'], 'expected no identity_url'
35 # link to add user to 'All users' group
37 verify_link response_items, 'arvados#group', true, 'permission', 'can_write',
38 'All users', created['uuid'], 'arvados#group', true, 'Group'
40 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
41 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
43 verify_system_group_permission_link_for created['uuid']
45 # invoke setup again with the same data
46 post "/arvados/v1/users/setup",
48 vm_uuid: virtual_machines(:testvm).uuid,
50 uuid: 'zzzzz-tpzed-abcdefghijklmno',
51 first_name: "in_create_test_first_name",
52 last_name: "test_last_name",
53 email: "foo@example.com"
57 assert_response 422 # cannot create another user with same UUID
59 # invoke setup on the same user
60 post "/arvados/v1/users/setup",
62 vm_uuid: virtual_machines(:testvm).uuid,
63 uuid: 'zzzzz-tpzed-abcdefghijklmno',
67 response_items = json_response['items']
69 created = find_obj_in_resp response_items, 'arvados#user', nil
70 assert_equal 'in_create_test_first_name', created['first_name']
71 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
72 assert_equal 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
73 assert_not_nil created['email'], 'expected non-nil email'
74 assert_nil created['identity_url'], 'expected no identity_url'
76 # arvados#user, and link to add user to 'All users' group
77 verify_link response_items, 'arvados#group', true, 'permission', 'can_write',
78 'All users', created['uuid'], 'arvados#group', true, 'Group'
80 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
81 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
83 verify_system_group_permission_link_for created['uuid']
86 test "setup user in multiple steps and verify response" do
87 post "/arvados/v1/users/setup",
90 email: "foo@example.com"
95 assert_response :success
96 response_items = json_response['items']
97 created = find_obj_in_resp response_items, 'arvados#user', nil
99 assert_not_nil created['uuid'], 'expected uuid for new user'
100 assert_not_nil created['email'], 'expected non-nil email'
101 assert_equal created['email'], 'foo@example.com', 'expected input email'
103 # two new links: system_group, and 'All users' group.
105 verify_link response_items, 'arvados#group', true, 'permission', 'can_write',
106 'All users', created['uuid'], 'arvados#group', true, 'Group'
108 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
109 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
111 # invoke setup with a vm_uuid
112 post "/arvados/v1/users/setup",
114 vm_uuid: virtual_machines(:testvm).uuid,
118 uuid: created['uuid']
120 headers: auth(:admin)
122 assert_response :success
124 response_items = json_response['items']
125 created = find_obj_in_resp response_items, 'arvados#user', nil
127 assert_equal created['email'], 'foo@example.com', 'expected original email'
130 verify_link response_items, 'arvados#group', true, 'permission', 'can_write',
131 'All users', created['uuid'], 'arvados#group', true, 'Group'
133 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
134 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
137 test "setup and unsetup user" do
138 post "/arvados/v1/users/setup",
140 vm_uuid: virtual_machines(:testvm).uuid,
141 user: {email: 'foo@example.com'},
143 headers: auth(:admin)
145 assert_response :success
146 response_items = json_response['items']
147 created = find_obj_in_resp response_items, 'arvados#user', nil
148 assert_not_nil created['uuid'], 'expected uuid for the new user'
149 assert_equal created['email'], 'foo@example.com', 'expected given email'
151 # three extra links: system_group, login, group and vm
153 verify_link response_items, 'arvados#group', true, 'permission', 'can_write',
154 'All users', created['uuid'], 'arvados#group', true, 'Group'
156 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
157 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
159 verify_link_existence created['uuid'], created['email'], true, true, true, true, false
162 token = act_as_system_user do
163 ApiClientAuthorization.create!(user: User.find_by_uuid(created['uuid']), api_client: ApiClient.all.first).api_token
166 # share project and collections with the new user
167 act_as_system_user do
168 Link.create!(tail_uuid: created['uuid'],
169 head_uuid: groups(:aproject).uuid,
170 link_class: 'permission',
172 Link.create!(tail_uuid: created['uuid'],
173 head_uuid: collections(:collection_owned_by_active).uuid,
174 link_class: 'permission',
176 Link.create!(tail_uuid: created['uuid'],
177 head_uuid: collections(:collection_owned_by_active_with_file_stats).uuid,
178 link_class: 'permission',
182 assert_equal 1, ApiClientAuthorization.where(user_id: User.find_by_uuid(created['uuid']).id).size, 'expected token not found'
184 post "/arvados/v1/users/#{created['uuid']}/unsetup", params: {}, headers: auth(:admin)
186 assert_response :success
188 created2 = json_response
189 assert_not_nil created2['uuid'], 'expected uuid for the newly created user'
190 assert_equal created['uuid'], created2['uuid'], 'expected uuid not found'
191 assert_equal 0, ApiClientAuthorization.where(user_id: User.find_by_uuid(created['uuid']).id).size, 'token should have been deleted by user unsetup'
192 # check permissions are deleted
193 assert_empty Link.where(tail_uuid: created['uuid'])
195 verify_link_existence created['uuid'], created['email'], false, false, false, false, false
198 def find_obj_in_resp (response_items, kind, head_kind=nil)
199 response_items.each do |x|
201 return x if (x['kind'] == kind && x['head_kind'] == head_kind)
207 test 'merge active into project_viewer account' do
208 post('/arvados/v1/groups',
211 group_class: 'project',
212 name: "active user's stuff",
215 headers: auth(:project_viewer))
216 assert_response(:success)
217 project_uuid = json_response['uuid']
219 post('/arvados/v1/users/merge',
221 new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
222 new_owner_uuid: project_uuid,
223 redirect_to_new_user: true,
225 headers: auth(:active_trustedclient))
226 assert_response(:success)
228 get('/arvados/v1/users/current', params: {}, headers: auth(:active))
229 assert_response(:success)
230 assert_equal(users(:project_viewer).uuid, json_response['uuid'])
232 get('/arvados/v1/authorized_keys/' + authorized_keys(:active).uuid,
234 headers: auth(:active))
235 assert_response(:success)
236 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
237 assert_equal(users(:project_viewer).uuid, json_response['authorized_user_uuid'])
239 get('/arvados/v1/groups/' + groups(:aproject).uuid,
241 headers: auth(:active))
242 assert_response(:success)
243 assert_equal(project_uuid, json_response['owner_uuid'])
246 test 'pre-activate user' do
247 post '/arvados/v1/users',
250 "email" => 'foo@example.com',
252 "username" => "barney"
255 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"}
256 assert_response :success
258 assert_not_nil rp["uuid"]
259 assert_equal true, rp["is_active"]
260 assert_equal false, rp["is_admin"]
262 get "/arvados/v1/users/#{rp['uuid']}",
263 params: {format: 'json'},
264 headers: auth(:admin)
265 assert_response :success
266 assert_equal rp["uuid"], json_response['uuid']
267 assert_equal false, json_response['is_admin']
268 assert_equal true, json_response['is_active']
269 assert_equal 'foo@example.com', json_response['email']
270 assert_equal 'barney', json_response['username']
273 test "cannot set is_active to false directly" do
274 post('/arvados/v1/users',
277 email: "bob@example.com",
281 headers: auth(:admin))
282 assert_response(:success)
284 assert_equal false, user['is_active']
286 token = act_as_system_user do
287 ApiClientAuthorization.create!(user: User.find_by_uuid(user['uuid']), api_client: ApiClient.all.first).api_token
289 post("/arvados/v1/user_agreements/sign",
290 params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
291 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
292 assert_response :success
294 post("/arvados/v1/users/#{user['uuid']}/activate",
296 headers: auth(:admin))
297 assert_response(:success)
299 assert_equal true, user['is_active']
301 put("/arvados/v1/users/#{user['uuid']}",
303 user: {is_active: false}
305 headers: auth(:admin))
309 test "cannot self activate when AutoSetupNewUsers is false" do
310 Rails.configuration.Users.NewUsersAreActive = false
311 Rails.configuration.Users.AutoSetupNewUsers = false
315 act_as_system_user do
316 user = User.create!(email: "bob@example.com", username: "bobby")
317 ap = ApiClientAuthorization.create!(user: user, api_client: ApiClient.all.first)
321 get("/arvados/v1/users/#{user['uuid']}",
323 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
324 assert_response(:success)
326 assert_equal false, user['is_active']
328 post("/arvados/v1/users/#{user['uuid']}/activate",
330 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
332 assert_match(/Cannot activate without being invited/, json_response['errors'][0])
336 test "cannot self activate after unsetup" do
337 Rails.configuration.Users.NewUsersAreActive = false
338 Rails.configuration.Users.AutoSetupNewUsers = false
342 act_as_system_user do
343 user = User.create!(email: "bob@example.com", username: "bobby")
344 ap = ApiClientAuthorization.create!(user: user, api_client_id: 0)
348 post("/arvados/v1/users/setup",
349 params: {uuid: user['uuid']},
350 headers: auth(:admin))
351 assert_response :success
353 post("/arvados/v1/users/#{user['uuid']}/activate",
355 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
357 assert_match(/Cannot activate without user agreements/, json_response['errors'][0])
359 post("/arvados/v1/user_agreements/sign",
360 params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
361 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
362 assert_response :success
364 post("/arvados/v1/users/#{user['uuid']}/activate",
366 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
367 assert_response :success
369 get("/arvados/v1/users/#{user['uuid']}",
371 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
372 assert_response(:success)
373 userJSON = json_response
374 assert_equal true, userJSON['is_active']
376 post("/arvados/v1/users/#{user['uuid']}/unsetup",
378 headers: auth(:admin))
379 assert_response :success
381 # Need to get a new token, the old one was invalidated by the unsetup call
382 act_as_system_user do
383 ap = ApiClientAuthorization.create!(user: user, api_client_id: 0)
387 get("/arvados/v1/users/#{user['uuid']}",
389 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
390 assert_response(:success)
391 userJSON = json_response
392 assert_equal false, userJSON['is_active']
394 post("/arvados/v1/users/#{user['uuid']}/activate",
396 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
398 assert_match(/Cannot activate without being invited/, json_response['errors'][0])
401 test "bypass_federation only accepted for admins" do
402 get "/arvados/v1/users",
404 bypass_federation: true
406 headers: auth(:admin)
408 assert_response :success
410 get "/arvados/v1/users",
412 bypass_federation: true
414 headers: auth(:active)
419 test "disabling system root user not permitted" do
420 put("/arvados/v1/users/#{users(:system_user).uuid}",
422 user: {is_admin: false}
424 headers: auth(:admin))
427 post("/arvados/v1/users/#{users(:system_user).uuid}/unsetup",
429 headers: auth(:admin))
433 test "creating users only accepted for admins" do
434 assert_equal false, users(:active).is_admin
435 post '/arvados/v1/users',
438 "email" => 'foo@example.com',
439 "username" => "barney"
442 headers: auth(:active)
446 test "create users assigns the system root user as their owner" do
447 post '/arvados/v1/users',
450 "email" => 'foo@example.com',
451 "username" => "barney"
454 headers: auth(:admin)
455 assert_response :success
456 assert_not_nil json_response["uuid"]
457 assert_equal users(:system_user).uuid, json_response["owner_uuid"]
460 test "create users ignores provided owner_uuid field" do
461 assert_equal false, users(:admin).uuid == users(:system_user).uuid
462 post '/arvados/v1/users',
465 "email" => 'foo@example.com',
466 "owner_uuid" => users(:admin).uuid,
467 "username" => "barney"
470 headers: auth(:admin)
471 assert_response :success
472 assert_not_nil json_response["uuid"]
473 assert_equal users(:system_user).uuid, json_response["owner_uuid"]