Merge pull request #1 from arvados/master
[arvados.git] / services / api / test / integration / users_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'helpers/users_test_helper'
7
8 class UsersTest < ActionDispatch::IntegrationTest
9   include UsersTestHelper
10
11   test "setup user multiple times" do
12     repo_name = 'usertestrepo'
13
14     post "/arvados/v1/users/setup",
15       params: {
16         repo_name: repo_name,
17         user: {
18           uuid: 'zzzzz-tpzed-abcdefghijklmno',
19           first_name: "in_create_test_first_name",
20           last_name: "test_last_name",
21           email: "foo@example.com"
22         }
23       },
24       headers: auth(:admin)
25
26     assert_response :success
27
28     response_items = json_response['items']
29
30     created = find_obj_in_resp response_items, 'arvados#user', nil
31
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'
37
38     # repo link and link add user to 'All users' group
39
40     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
41         'foo/usertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
42
43     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
44         'All users', created['uuid'], 'arvados#group', true, 'Group'
45
46     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
47         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
48
49     verify_system_group_permission_link_for created['uuid']
50
51     # invoke setup again with the same data
52     post "/arvados/v1/users/setup",
53       params: {
54         repo_name: repo_name,
55         vm_uuid: virtual_machines(:testvm).uuid,
56         user: {
57           uuid: 'zzzzz-tpzed-abcdefghijklmno',
58           first_name: "in_create_test_first_name",
59           last_name: "test_last_name",
60           email: "foo@example.com"
61         }
62       },
63       headers: auth(:admin)
64     assert_response 422         # cannot create another user with same UUID
65
66     # invoke setup on the same user
67     post "/arvados/v1/users/setup",
68       params: {
69         repo_name: repo_name,
70         vm_uuid: virtual_machines(:testvm).uuid,
71         uuid: 'zzzzz-tpzed-abcdefghijklmno',
72       },
73       headers: auth(:admin)
74
75     response_items = json_response['items']
76
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'
83
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'
87
88     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
89         'All users', created['uuid'], 'arvados#group', true, 'Group'
90
91     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
92         virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
93
94     verify_system_group_permission_link_for created['uuid']
95   end
96
97   test "setup user in multiple steps and verify response" do
98     post "/arvados/v1/users/setup",
99       params: {
100         user: {
101           email: "foo@example.com"
102         }
103       },
104       headers: auth(:admin)
105
106     assert_response :success
107     response_items = json_response['items']
108     created = find_obj_in_resp response_items, 'arvados#user', nil
109
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'
113
114     # two new links: system_group, and 'All users' group.
115
116     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
117         'All users', created['uuid'], 'arvados#group', true, 'Group'
118
119     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
120         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
121
122    # invoke setup with a repository
123     post "/arvados/v1/users/setup",
124       params: {
125         repo_name: 'newusertestrepo',
126         uuid: created['uuid']
127       },
128       headers: auth(:admin)
129
130     assert_response :success
131
132     response_items = json_response['items']
133     created = find_obj_in_resp response_items, 'arvados#user', nil
134
135     assert_equal 'foo@example.com', created['email'], 'expected input email'
136
137      # verify links
138     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
139         'All users', created['uuid'], 'arvados#group', true, 'Group'
140
141     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
142         'foo/newusertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
143
144     verify_link response_items, 'arvados#virtualMachine', false, 'permission', 'can_login',
145         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
146
147     # invoke setup with a vm_uuid
148     post "/arvados/v1/users/setup",
149       params: {
150         vm_uuid: virtual_machines(:testvm).uuid,
151         user: {
152           email: 'junk_email'
153         },
154         uuid: created['uuid']
155       },
156       headers: auth(:admin)
157
158     assert_response :success
159
160     response_items = json_response['items']
161     created = find_obj_in_resp response_items, 'arvados#user', nil
162
163     assert_equal created['email'], 'foo@example.com', 'expected original email'
164
165     # verify links
166     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
167         'All users', created['uuid'], 'arvados#group', true, 'Group'
168
169     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
170         virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
171   end
172
173   test "setup and unsetup user" do
174     post "/arvados/v1/users/setup",
175       params: {
176         repo_name: 'newusertestrepo',
177         vm_uuid: virtual_machines(:testvm).uuid,
178         user: {email: 'foo@example.com'},
179       },
180       headers: auth(:admin)
181
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'
187
188     # four extra links: system_group, login, group, repo and vm
189
190     verify_link response_items, 'arvados#group', true, 'permission', 'can_read',
191         'All users', created['uuid'], 'arvados#group', true, 'Group'
192
193     verify_link response_items, 'arvados#repository', true, 'permission', 'can_manage',
194         'foo/newusertestrepo', created['uuid'], 'arvados#repository', true, 'Repository'
195
196     verify_link response_items, 'arvados#virtualMachine', true, 'permission', 'can_login',
197         virtual_machines(:testvm).uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
198
199     verify_link_existence created['uuid'], created['email'], true, true, true, true, false
200
201     post "/arvados/v1/users/#{created['uuid']}/unsetup", params: {}, headers: auth(:admin)
202
203     assert_response :success
204
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'
208
209     verify_link_existence created['uuid'], created['email'], false, false, false, false, false
210   end
211
212   def find_obj_in_resp (response_items, kind, head_kind=nil)
213     response_items.each do |x|
214       if x && x['kind']
215         return x if (x['kind'] == kind && x['head_kind'] == head_kind)
216       end
217     end
218     nil
219   end
220
221   test 'merge active into project_viewer account' do
222     post('/arvados/v1/groups',
223       params: {
224         group: {
225           group_class: 'project',
226           name: "active user's stuff",
227         },
228       },
229       headers: auth(:project_viewer))
230     assert_response(:success)
231     project_uuid = json_response['uuid']
232
233     post('/arvados/v1/users/merge',
234       params: {
235         new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
236         new_owner_uuid: project_uuid,
237         redirect_to_new_user: true,
238       },
239       headers: auth(:active_trustedclient))
240     assert_response(:success)
241
242     get('/arvados/v1/users/current', params: {}, headers: auth(:active))
243     assert_response(:success)
244     assert_equal(users(:project_viewer).uuid, json_response['uuid'])
245
246     get('/arvados/v1/authorized_keys/' + authorized_keys(:active).uuid,
247       params: {},
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'])
252
253     get('/arvados/v1/repositories/' + repositories(:foo).uuid,
254       params: {},
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'])
259
260     get('/arvados/v1/groups/' + groups(:aproject).uuid,
261       params: {},
262       headers: auth(:active))
263     assert_response(:success)
264     assert_equal(project_uuid, json_response['owner_uuid'])
265   end
266
267   test 'pre-activate user' do
268     post '/arvados/v1/users',
269       params: {
270         "user" => {
271           "email" => 'foo@example.com',
272           "is_active" => true,
273           "username" => "barney"
274         }
275       },
276       headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"}
277     assert_response :success
278     rp = json_response
279     assert_not_nil rp["uuid"]
280     assert_not_nil rp["is_active"]
281     assert_nil rp["is_admin"]
282
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']
292   end
293
294   test 'merge with repository name conflict' do
295     post('/arvados/v1/groups',
296       params: {
297         group: {
298           group_class: 'project',
299           name: "active user's stuff",
300         },
301       },
302       headers: auth(:project_viewer))
303     assert_response(:success)
304     project_uuid = json_response['uuid']
305
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)
310
311     post('/arvados/v1/users/merge',
312       params: {
313         new_user_token: api_client_authorizations(:project_viewer_trustedclient).api_token,
314         new_owner_uuid: project_uuid,
315         redirect_to_new_user: true,
316       },
317       headers: auth(:active_trustedclient))
318     assert_response(:success)
319
320     get('/arvados/v1/repositories/' + repositories(:foo).uuid,
321       params: {},
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'])
326
327   end
328
329   test "cannot set is_active to false directly" do
330     post('/arvados/v1/users',
331       params: {
332         user: {
333           email: "bob@example.com",
334           username: "bobby"
335         },
336       },
337       headers: auth(:admin))
338     assert_response(:success)
339     user = json_response
340     assert_equal false, user['is_active']
341
342     token = act_as_system_user do
343       ApiClientAuthorization.create!(user: User.find_by_uuid(user['uuid']), api_client: ApiClient.all.first).api_token
344     end
345     post("/arvados/v1/user_agreements/sign",
346         params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
347         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
348     assert_response :success
349
350     post("/arvados/v1/users/#{user['uuid']}/activate",
351       params: {},
352       headers: auth(:admin))
353     assert_response(:success)
354     user = json_response
355     assert_equal true, user['is_active']
356
357     put("/arvados/v1/users/#{user['uuid']}",
358          params: {
359            user: {is_active: false}
360          },
361          headers: auth(:admin))
362     assert_response 422
363   end
364
365   test "cannot self activate when AutoSetupNewUsers is false" do
366     Rails.configuration.Users.NewUsersAreActive = false
367     Rails.configuration.Users.AutoSetupNewUsers = false
368
369     user = nil
370     token = nil
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)
374       token = ap.api_token
375     end
376
377     get("/arvados/v1/users/#{user['uuid']}",
378         params: {},
379         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
380     assert_response(:success)
381     user = json_response
382     assert_equal false, user['is_active']
383
384     post("/arvados/v1/users/#{user['uuid']}/activate",
385         params: {},
386         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
387     assert_response 422
388     assert_match(/Cannot activate without being invited/, json_response['errors'][0])
389   end
390
391
392   test "cannot self activate after unsetup" do
393     Rails.configuration.Users.NewUsersAreActive = false
394     Rails.configuration.Users.AutoSetupNewUsers = false
395
396     user = nil
397     token = nil
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)
401       token = ap.api_token
402     end
403
404     post("/arvados/v1/users/setup",
405         params: {uuid: user['uuid']},
406         headers: auth(:admin))
407     assert_response :success
408
409     post("/arvados/v1/users/#{user['uuid']}/activate",
410         params: {},
411         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
412     assert_response 403
413     assert_match(/Cannot activate without user agreements/, json_response['errors'][0])
414
415     post("/arvados/v1/user_agreements/sign",
416         params: {uuid: 'zzzzz-4zz18-t68oksiu9m80s4y'},
417         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
418     assert_response :success
419
420     post("/arvados/v1/users/#{user['uuid']}/activate",
421         params: {},
422         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
423     assert_response :success
424
425     get("/arvados/v1/users/#{user['uuid']}",
426         params: {},
427         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
428     assert_response(:success)
429     user = json_response
430     assert_equal true, user['is_active']
431
432     post("/arvados/v1/users/#{user['uuid']}/unsetup",
433         params: {},
434         headers: auth(:admin))
435     assert_response :success
436
437     get("/arvados/v1/users/#{user['uuid']}",
438         params: {},
439         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
440     assert_response(:success)
441     user = json_response
442     assert_equal false, user['is_active']
443
444     post("/arvados/v1/users/#{user['uuid']}/activate",
445         params: {},
446         headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"})
447     assert_response 422
448     assert_match(/Cannot activate without being invited/, json_response['errors'][0])
449   end
450
451
452 end