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 repo_name = 'usertestrepo'
14 post "/arvados/v1/users/setup",
18 uuid: 'zzzzz-tpzed-abcdefghijklmno',
19 first_name: "in_create_test_first_name",
20 last_name: "test_last_name",
21 email: "foo@example.com"
26 assert_response :success
28 response_items = json_response['items']
30 created = find_obj_in_resp response_items, 'arvados#user', nil
32 assert_equal 'in_create_test_first_name', created['first_name']
33 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
34 assert_equal 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
35 assert_not_nil created['email'], 'expected non-nil email'
36 assert_nil created['identity_url'], 'expected no identity_url'
38 # repo link and link add user to 'All users' group
40 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
41 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
43 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
44 'All users', created['uuid'], 'arvados#group', true, 'Group'
46 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
47 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
49 verify_system_group_permission_link_for created['uuid']
51 # invoke setup again with the same data
52 post "/arvados/v1/users/setup",
55 vm_uuid: virtual_machines(:testvm).uuid,
57 uuid: 'zzzzz-tpzed-abcdefghijklmno',
58 first_name: "in_create_test_first_name",
59 last_name: "test_last_name",
60 email: "foo@example.com"
64 assert_response 422 # cannot create another user with same UUID
66 # invoke setup on the same user
67 post "/arvados/v1/users/setup",
70 vm_uuid: virtual_machines(:testvm).uuid,
71 uuid: 'zzzzz-tpzed-abcdefghijklmno',
75 response_items = json_response['items']
77 created = find_obj_in_resp response_items, 'arvados#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 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
81 assert_not_nil created['email'], 'expected non-nil email'
82 assert_nil created['identity_url'], 'expected no identity_url'
84 # arvados#user, repo link and link add user to 'All users' group
85 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
86 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
88 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
89 'All users', created['uuid'], 'arvados#group', true, 'Group'
91 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
92 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
94 verify_system_group_permission_link_for created['uuid']
97 test "setup user in multiple steps and verify response" do
98 post "/arvados/v1/users/setup",
101 email: "foo@example.com"
104 headers: auth(:admin)
106 assert_response :success
107 response_items = json_response['items']
108 created = find_obj_in_resp response_items, 'arvados#user', nil
110 assert_not_nil created['uuid'], 'expected uuid for new user'
111 assert_not_nil created['email'], 'expected non-nil email'
112 assert_equal created['email'], 'foo@example.com', 'expected input email'
114 # two new links: system_group, and 'All users' group.
116 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
117 'All users', created['uuid'], 'arvados#group', true, 'Group'
119 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
120 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
122 # invoke setup with a repository
123 post "/arvados/v1/users/setup",
125 repo_name: 'newusertestrepo',
126 uuid: created['uuid']
128 headers: auth(:admin)
130 assert_response :success
132 response_items = json_response['items']
133 created = find_obj_in_resp response_items, 'arvados#user', nil
135 assert_equal 'foo@example.com', created['email'], 'expected input email'
138 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
139 'All users', created['uuid'], 'arvados#group', true, 'Group'
141 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
142 'foo/newusertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
144 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
145 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
147 # invoke setup with a vm_uuid
148 post "/arvados/v1/users/setup",
150 vm_uuid: virtual_machines(:testvm).uuid,
154 uuid: created['uuid']
156 headers: auth(:admin)
158 assert_response :success
160 response_items = json_response['items']
161 created = find_obj_in_resp response_items, 'arvados#user', nil
163 assert_equal created['email'], 'foo@example.com', 'expected original email'
166 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
167 'All users', created['uuid'], 'arvados#group', true, 'Group'
169 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
170 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
173 test "setup and unsetup user" do
174 post "/arvados/v1/users/setup",
176 repo_name: 'newusertestrepo',
177 vm_uuid: virtual_machines(:testvm).uuid,
178 user: {email: 'foo@example.com'},
180 headers: auth(:admin)
182 assert_response :success
183 response_items = json_response['items']
184 created = find_obj_in_resp response_items, 'arvados#user', nil
185 assert_not_nil created['uuid'], 'expected uuid for the new user'
186 assert_equal created['email'], 'foo@example.com', 'expected given email'
188 # four extra links: system_group, login, group, repo and vm
190 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
191 'All users', created['uuid'], 'arvados#group', true, 'Group'
193 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
194 'foo/newusertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
196 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
197 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
199 verify_link_existence created['uuid'], created['email'], true, true, true, true, false
201 post "/arvados/v1/users/#{created['uuid']}/unsetup", params: {}, headers: auth(:admin)
203 assert_response :success
205 created2 = json_response
206 assert_not_nil created2['uuid'], 'expected uuid for the newly created user'
207 assert_equal created['uuid'], created2['uuid'], 'expected uuid not found'
209 verify_link_existence created['uuid'], created['email'], false, false, false, false, false
212 def find_obj_in_resp (response_items, kind, head_kind=nil)
213 response_items.each do |x|
215 return x if (x['kind'] == kind && x['head_kind'] == head_kind)
221 test 'merge active into project_viewer account' do
222 post('/arvados/v1/groups',
225 group_class: 'project',
226 name: "active user's stuff",
229 headers: auth(:project_viewer))
230 assert_response(:success)
231 project_uuid = json_response['uuid']
233 post('/arvados/v1/users/merge',
235 new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
236 new_owner_uuid: project_uuid,
237 redirect_to_new_user: true,
239 headers: auth(:active_trustedclient))
240 assert_response(:success)
242 get('/arvados/v1/users/current', params: {}, headers: auth(:active))
243 assert_response(:success)
244 assert_equal(users(:project_viewer).uuid, json_response['uuid'])
246 get('/arvados/v1/authorized_keys/' + authorized_keys(:active).uuid,
248 headers: auth(:active))
249 assert_response(:success)
250 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
251 assert_equal(users(:project_viewer).uuid, json_response['authorized_user_uuid'])
253 get('/arvados/v1/repositories/' + repositories(:foo).uuid,
255 headers: auth(:active))
256 assert_response(:success)
257 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
258 assert_equal("#{users(:project_viewer).username}/foo", json_response['name'])
260 get('/arvados/v1/groups/' + groups(:aproject).uuid,
262 headers: auth(:active))
263 assert_response(:success)
264 assert_equal(project_uuid, json_response['owner_uuid'])
267 test 'pre-activate user' do
268 post '/arvados/v1/users',
271 "email" => 'foo@example.com',
273 "username" => "barney"
276 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"}
277 assert_response :success
279 assert_not_nil rp["uuid"]
280 assert_not_nil rp["is_active"]
281 assert_nil rp["is_admin"]
283 get "/arvados/v1/users/#{rp['uuid']}",
284 params: {format: 'json'},
285 headers: auth(:admin)
286 assert_response :success
287 assert_equal rp["uuid"], json_response['uuid']
288 assert_nil json_response['is_admin']
289 assert_equal true, json_response['is_active']
290 assert_equal 'foo@example.com', json_response['email']
291 assert_equal 'barney', json_response['username']
294 test 'merge with repository name conflict' do
295 post('/arvados/v1/groups',
298 group_class: 'project',
299 name: "active user's stuff",
302 headers: auth(:project_viewer))
303 assert_response(:success)
304 project_uuid = json_response['uuid']
306 post('/arvados/v1/repositories/',
307 params: { :repository => { :name => "#{users(:project_viewer).username}/foo", :owner_uuid => users(:project_viewer).uuid } },
308 headers: auth(:project_viewer))
309 assert_response(:success)
311 post('/arvados/v1/users/merge',
313 new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
314 new_owner_uuid: project_uuid,
315 redirect_to_new_user: true,
317 headers: auth(:active_trustedclient))
318 assert_response(:success)
320 get('/arvados/v1/repositories/' + repositories(:foo).uuid,
322 headers: auth(:active))
323 assert_response(:success)
324 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
325 assert_equal("#{users(:project_viewer).username}/migratedfoo", json_response['name'])
329 test "cannot set is_active to false directly" do
330 post('/arvados/v1/users',
333 email: "bob@example.com",
337 headers: auth(:admin))
338 assert_response(:success)
340 assert_equal false, user['is_active']
342 token = act_as_system_user do
343 ApiClientAuthorization.create!(user: User.find_by_uuid(user['uuid']), api_client: ApiClient.all.first).api_token
345 post("/arvados/v1/user_agreements/sign",
346 params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
347 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
348 assert_response :success
350 post("/arvados/v1/users/#{user['uuid']}/activate",
352 headers: auth(:admin))
353 assert_response(:success)
355 assert_equal true, user['is_active']
357 put("/arvados/v1/users/#{user['uuid']}",
359 user: {is_active: false}
361 headers: auth(:admin))
365 test "cannot self activate when AutoSetupNewUsers is false" do
366 Rails.configuration.Users.NewUsersAreActive = false
367 Rails.configuration.Users.AutoSetupNewUsers = false
371 act_as_system_user do
372 user = User.create!(email: "bob@example.com", username: "bobby")
373 ap = ApiClientAuthorization.create!(user: user, api_client: ApiClient.all.first)
377 get("/arvados/v1/users/#{user['uuid']}",
379 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
380 assert_response(:success)
382 assert_equal false, user['is_active']
384 post("/arvados/v1/users/#{user['uuid']}/activate",
386 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
388 assert_match(/Cannot activate without being invited/, json_response['errors'][0])
392 test "cannot self activate after unsetup" do
393 Rails.configuration.Users.NewUsersAreActive = false
394 Rails.configuration.Users.AutoSetupNewUsers = false
398 act_as_system_user do
399 user = User.create!(email: "bob@example.com", username: "bobby")
400 ap = ApiClientAuthorization.create!(user: user, api_client_id: 0)
404 post("/arvados/v1/users/setup",
405 params: {uuid: user['uuid']},
406 headers: auth(:admin))
407 assert_response :success
409 post("/arvados/v1/users/#{user['uuid']}/activate",
411 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
413 assert_match(/Cannot activate without user agreements/, json_response['errors'][0])
415 post("/arvados/v1/user_agreements/sign",
416 params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
417 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
418 assert_response :success
420 post("/arvados/v1/users/#{user['uuid']}/activate",
422 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
423 assert_response :success
425 get("/arvados/v1/users/#{user['uuid']}",
427 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
428 assert_response(:success)
430 assert_equal true, user['is_active']
432 post("/arvados/v1/users/#{user['uuid']}/unsetup",
434 headers: auth(:admin))
435 assert_response :success
437 get("/arvados/v1/users/#{user['uuid']}",
439 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
440 assert_response(:success)
442 assert_equal false, user['is_active']
444 post("/arvados/v1/users/#{user['uuid']}/activate",
446 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
448 assert_match(/Cannot activate without being invited/, json_response['errors'][0])