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",
17 openid_prefix: 'https://www.google.com/accounts/o8/id',
19 uuid: 'zzzzz-tpzed-abcdefghijklmno',
20 first_name: "in_create_test_first_name",
21 last_name: "test_last_name",
22 email: "foo@example.com"
27 assert_response :success
29 response_items = json_response['items']
31 created = find_obj_in_resp response_items, 'arvados#user', nil
33 assert_equal 'in_create_test_first_name', created['first_name']
34 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
35 assert_equal 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
36 assert_not_nil created['email'], 'expected non-nil email'
37 assert_nil created['identity_url'], 'expected no identity_url'
39 # arvados#user, repo link and link add user to 'All users' group
40 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
41 created['uuid'], created['email'], 'arvados#user', false, 'arvados#user'
43 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
44 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
46 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
47 'All users', created['uuid'], 'arvados#group', true, 'Group'
49 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
50 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
52 verify_system_group_permission_link_for created['uuid']
54 # invoke setup again with the same data
55 post "/arvados/v1/users/setup",
58 vm_uuid: virtual_machines(:testvm).uuid,
59 openid_prefix: 'https://www.google.com/accounts/o8/id',
61 uuid: 'zzzzz-tpzed-abcdefghijklmno',
62 first_name: "in_create_test_first_name",
63 last_name: "test_last_name",
64 email: "foo@example.com"
68 assert_response 422 # cannot create another user with same UUID
70 # invoke setup on the same user
71 post "/arvados/v1/users/setup",
74 vm_uuid: virtual_machines(:testvm).uuid,
75 openid_prefix: 'https://www.google.com/accounts/o8/id',
76 uuid: 'zzzzz-tpzed-abcdefghijklmno',
80 response_items = json_response['items']
82 created = find_obj_in_resp response_items, 'arvados#user', nil
83 assert_equal 'in_create_test_first_name', created['first_name']
84 assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
85 assert_equal 'zzzzz-tpzed-abcdefghijklmno', created['uuid']
86 assert_not_nil created['email'], 'expected non-nil email'
87 assert_nil created['identity_url'], 'expected no identity_url'
89 # arvados#user, repo link and link add user to 'All users' group
90 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
91 'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
93 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
94 'All users', created['uuid'], 'arvados#group', true, 'Group'
96 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
97 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
99 verify_system_group_permission_link_for created['uuid']
102 test "setup user in multiple steps and verify response" do
103 post "/arvados/v1/users/setup",
105 openid_prefix: 'http://www.example.com/account',
107 email: "foo@example.com"
110 headers: auth(:admin)
112 assert_response :success
113 response_items = json_response['items']
114 created = find_obj_in_resp response_items, 'arvados#user', nil
116 assert_not_nil created['uuid'], 'expected uuid for new user'
117 assert_not_nil created['email'], 'expected non-nil email'
118 assert_equal created['email'], 'foo@example.com', 'expected input email'
120 # three new links: system_group, arvados#user, and 'All users' group.
121 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
122 created['uuid'], created['email'], 'arvados#user', false, 'arvados#user'
124 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
125 'All users', created['uuid'], 'arvados#group', true, 'Group'
127 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
128 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
130 # invoke setup with a repository
131 post "/arvados/v1/users/setup",
133 openid_prefix: 'http://www.example.com/account',
134 repo_name: 'newusertestrepo',
135 uuid: created['uuid']
137 headers: auth(:admin)
139 assert_response :success
141 response_items = json_response['items']
142 created = find_obj_in_resp response_items, 'arvados#user', nil
144 assert_equal 'foo@example.com', created['email'], 'expected input email'
147 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
148 'All users', created['uuid'], 'arvados#group', true, 'Group'
150 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
151 'foo/newusertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
153 verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
154 nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
156 # invoke setup with a vm_uuid
157 post "/arvados/v1/users/setup",
159 vm_uuid: virtual_machines(:testvm).uuid,
160 openid_prefix: 'http://www.example.com/account',
164 uuid: created['uuid']
166 headers: auth(:admin)
168 assert_response :success
170 response_items = json_response['items']
171 created = find_obj_in_resp response_items, 'arvados#user', nil
173 assert_equal created['email'], 'foo@example.com', 'expected original email'
176 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
177 'All users', created['uuid'], 'arvados#group', true, 'Group'
179 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
180 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
183 test "setup and unsetup user" do
184 post "/arvados/v1/users/setup",
186 repo_name: 'newusertestrepo',
187 vm_uuid: virtual_machines(:testvm).uuid,
188 user: {email: 'foo@example.com'},
189 openid_prefix: 'https://www.google.com/accounts/o8/id'
191 headers: auth(:admin)
193 assert_response :success
194 response_items = json_response['items']
195 created = find_obj_in_resp response_items, 'arvados#user', nil
196 assert_not_nil created['uuid'], 'expected uuid for the new user'
197 assert_equal created['email'], 'foo@example.com', 'expected given email'
199 # five extra links: system_group, login, group, repo and vm
200 verify_link response_items, 'arvados#user', true, 'permission', 'can_login',
201 created['uuid'], created['email'], 'arvados#user', false, 'arvados#user'
203 verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
204 'All users', created['uuid'], 'arvados#group', true, 'Group'
206 verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
207 'foo/newusertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
209 verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
210 virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
212 verify_link_existence created['uuid'], created['email'], true, true, true, true, false
214 post "/arvados/v1/users/#{created['uuid']}/unsetup", params: {}, headers: auth(:admin)
216 assert_response :success
218 created2 = json_response
219 assert_not_nil created2['uuid'], 'expected uuid for the newly created user'
220 assert_equal created['uuid'], created2['uuid'], 'expected uuid not found'
222 verify_link_existence created['uuid'], created['email'], false, false, false, false, false
225 def find_obj_in_resp (response_items, kind, head_kind=nil)
226 response_items.each do |x|
228 return x if (x['kind'] == kind && x['head_kind'] == head_kind)
234 test 'merge active into project_viewer account' do
235 post('/arvados/v1/groups',
238 group_class: 'project',
239 name: "active user's stuff",
242 headers: auth(:project_viewer))
243 assert_response(:success)
244 project_uuid = json_response['uuid']
246 post('/arvados/v1/users/merge',
248 new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
249 new_owner_uuid: project_uuid,
250 redirect_to_new_user: true,
252 headers: auth(:active_trustedclient))
253 assert_response(:success)
255 get('/arvados/v1/users/current', params: {}, headers: auth(:active))
256 assert_response(:success)
257 assert_equal(users(:project_viewer).uuid, json_response['uuid'])
259 get('/arvados/v1/authorized_keys/' + authorized_keys(:active).uuid,
261 headers: auth(:active))
262 assert_response(:success)
263 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
264 assert_equal(users(:project_viewer).uuid, json_response['authorized_user_uuid'])
266 get('/arvados/v1/repositories/' + repositories(:foo).uuid,
268 headers: auth(:active))
269 assert_response(:success)
270 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
271 assert_equal("#{users(:project_viewer).username}/foo", json_response['name'])
273 get('/arvados/v1/groups/' + groups(:aproject).uuid,
275 headers: auth(:active))
276 assert_response(:success)
277 assert_equal(project_uuid, json_response['owner_uuid'])
280 test 'pre-activate user' do
281 post '/arvados/v1/users',
284 "email" => 'foo@example.com',
286 "username" => "barney"
289 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"}
290 assert_response :success
292 assert_not_nil rp["uuid"]
293 assert_not_nil rp["is_active"]
294 assert_nil rp["is_admin"]
296 get "/arvados/v1/users/#{rp['uuid']}",
297 params: {format: 'json'},
298 headers: auth(:admin)
299 assert_response :success
300 assert_equal rp["uuid"], json_response['uuid']
301 assert_nil json_response['is_admin']
302 assert_equal true, json_response['is_active']
303 assert_equal 'foo@example.com', json_response['email']
304 assert_equal 'barney', json_response['username']
307 test 'merge with repository name conflict' do
308 post('/arvados/v1/groups',
311 group_class: 'project',
312 name: "active user's stuff",
315 headers: auth(:project_viewer))
316 assert_response(:success)
317 project_uuid = json_response['uuid']
319 post('/arvados/v1/repositories/',
320 params: { :repository => { :name => "#{users(:project_viewer).username}/foo", :owner_uuid => users(:project_viewer).uuid } },
321 headers: auth(:project_viewer))
322 assert_response(:success)
324 post('/arvados/v1/users/merge',
326 new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
327 new_owner_uuid: project_uuid,
328 redirect_to_new_user: true,
330 headers: auth(:active_trustedclient))
331 assert_response(:success)
333 get('/arvados/v1/repositories/' + repositories(:foo).uuid,
335 headers: auth(:active))
336 assert_response(:success)
337 assert_equal(users(:project_viewer).uuid, json_response['owner_uuid'])
338 assert_equal("#{users(:project_viewer).username}/migratedfoo", json_response['name'])
342 test "cannot set is_activate to false directly" do
343 post('/arvados/v1/users',
346 email: "bob@example.com",
350 headers: auth(:admin))
351 assert_response(:success)
353 assert_equal false, user['is_active']
355 post("/arvados/v1/users/#{user['uuid']}/activate",
357 headers: auth(:admin))
358 assert_response(:success)
360 assert_equal true, user['is_active']
362 put("/arvados/v1/users/#{user['uuid']}",
364 user: {is_active: false}
366 headers: auth(:admin))
370 test "cannot self activate when AutoSetupNewUsers is false" do
371 Rails.configuration.Users.NewUsersAreActive = false
372 Rails.configuration.Users.AutoSetupNewUsers = false
376 act_as_system_user do
377 user = User.create!(email: "bob@example.com", username: "bobby")
378 ap = ApiClientAuthorization.create!(user: user, api_client: ApiClient.all.first)
382 get("/arvados/v1/users/#{user['uuid']}",
384 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
385 assert_response(:success)
387 assert_equal false, user['is_active']
389 post("/arvados/v1/users/#{user['uuid']}/activate",
391 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
393 assert_match(/Cannot activate without being invited/, json_response['errors'][0])
397 test "cannot self activate after unsetup" do
398 Rails.configuration.Users.NewUsersAreActive = false
399 Rails.configuration.Users.AutoSetupNewUsers = false
403 act_as_system_user do
404 user = User.create!(email: "bob@example.com", username: "bobby")
405 ap = ApiClientAuthorization.create!(user: user, api_client_id: 0)
409 post("/arvados/v1/users/setup",
410 params: {uuid: user['uuid']},
411 headers: auth(:admin))
412 assert_response :success
414 post("/arvados/v1/users/#{user['uuid']}/activate",
416 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
418 assert_match(/Cannot activate without user agreements/, json_response['errors'][0])
420 post("/arvados/v1/user_agreements/sign",
421 params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
422 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
423 assert_response :success
425 post("/arvados/v1/users/#{user['uuid']}/activate",
427 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
428 assert_response :success
430 get("/arvados/v1/users/#{user['uuid']}",
432 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
433 assert_response(:success)
435 assert_equal true, user['is_active']
437 post("/arvados/v1/users/#{user['uuid']}/unsetup",
439 headers: auth(:admin))
440 assert_response :success
442 get("/arvados/v1/users/#{user['uuid']}",
444 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
445 assert_response(:success)
447 assert_equal false, user['is_active']
449 post("/arvados/v1/users/#{user['uuid']}/activate",
451 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
453 assert_match(/Cannot activate without being invited/, json_response['errors'][0])