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'])
272 get('/arvados/v1/groups/' + groups(:aproject).uuid,
274 headers: auth(:active))
275 assert_response(:success)
276 assert_equal(project_uuid, json_response['owner_uuid'])
279 test 'pre-activate user' do
280 post '/arvados/v1/users',
283 "email" => 'foo@example.com',
285 "username" => "barney"
288 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"}
289 assert_response :success
291 assert_not_nil rp["uuid"]
292 assert_not_nil rp["is_active"]
293 assert_nil rp["is_admin"]
295 get "/arvados/v1/users/#{rp['uuid']}",
296 params: {format: 'json'},
297 headers: auth(:admin)
298 assert_response :success
299 assert_equal rp["uuid"], json_response['uuid']
300 assert_nil json_response['is_admin']
301 assert_equal true, json_response['is_active']
302 assert_equal 'foo@example.com', json_response['email']
303 assert_equal 'barney', json_response['username']