f1ba9090e45f6df3ef775f493ca6a2a566942d89
[arvados.git] / services / api / test / functional / arvados / v1 / users_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::UsersControllerTest < ActionController::TestCase
4
5   setup do
6     @all_links_at_start = Link.all
7     @vm_uuid = virtual_machines(:testvm).uuid
8   end
9
10   test "activate a user after signing UA" do
11     authorize_with :inactive_but_signed_user_agreement
12     get :current
13     assert_response :success
14     me = JSON.parse(@response.body)
15     post :activate, uuid: me['uuid']
16     assert_response :success
17     assert_not_nil assigns(:object)
18     me = JSON.parse(@response.body)
19     assert_equal true, me['is_active']
20   end
21
22   test "refuse to activate a user before signing UA" do
23     authorize_with :inactive
24     get :current
25     assert_response :success
26     me = JSON.parse(@response.body)
27     post :activate, uuid: me['uuid']
28     assert_response 403
29     get :current
30     assert_response :success
31     me = JSON.parse(@response.body)
32     assert_equal false, me['is_active']
33   end
34
35   test "activate an already-active user" do
36     authorize_with :active
37     get :current
38     assert_response :success
39     me = JSON.parse(@response.body)
40     post :activate, uuid: me['uuid']
41     assert_response :success
42     me = JSON.parse(@response.body)
43     assert_equal true, me['is_active']
44   end
45
46   test "create new user with user as input" do
47     authorize_with :admin
48     post :create, user: {
49       first_name: "test_first_name",
50       last_name: "test_last_name",
51       email: "test@abc.com"
52     }
53     assert_response :success
54     created = JSON.parse(@response.body)
55     assert_equal 'test_first_name', created['first_name']
56     assert_not_nil created['uuid'], 'expected uuid for the newly created user'
57     assert_not_nil created['email'], 'expected non-nil email'
58     assert_nil created['identity_url'], 'expected no identity_url'
59   end
60
61   test "create user with user, vm and repo as input" do
62     authorize_with :admin
63     repo_name = 'test_repo'
64
65     post :setup, {
66       repo_name: repo_name,
67       openid_prefix: 'https://www.google.com/accounts/o8/id',
68       user: {
69         uuid: "this_is_agreeable",        
70         first_name: "in_create_test_first_name",
71         last_name: "test_last_name",
72         email: "test@abc.com"
73       }
74     }
75     assert_response :success
76
77     response_items = JSON.parse(@response.body)['items']
78     created = response_items['user']
79
80     assert_equal 'in_create_test_first_name', created['first_name']
81     assert_not_nil created['uuid'], 'expected non-null uuid for the new user'
82     assert_equal 'this_is_agreeable', created['uuid']
83     assert_not_nil created['email'], 'expected non-nil email'
84     assert_nil created['identity_url'], 'expected no identity_url' 
85
86     # since no such vm exists, expect only three new links: 
87     # oid_login_perm, repo link and link add user to 'All users' group
88     verify_num_links @all_links_at_start, 3
89
90     verify_link response_items, 'oid_login_perm', true, 'permission', 'can_login',
91         created['uuid'], created['email'], 'arvados#user', false, 'User'
92
93     verify_link response_items, 'repo_perm', true, 'permission', 'can_write',
94         repo_name, created['uuid'], 'arvados#repository', true, 'Repository'
95
96     verify_link response_items, 'group_perm', true, 'permission', 'can_read',
97         'All users', created['uuid'], 'arvados#group', true, 'Group'
98
99     verify_link response_items, 'vm_login_perm', false, 'permission', 'can_login',
100         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
101   end
102
103   test "create user with bogus uuid, vm and repo as input" do
104     authorize_with :admin
105
106     post :setup, {
107       uuid: 'not_an_existing_uuid_and_not_email_format',
108       repo_name: 'test_repo',
109       vm_uuid: @vm_uuid,
110       openid_prefix: 'https://www.google.com/accounts/o8/id'
111     }
112     response_body = JSON.parse(@response.body)
113     response_errors = response_body['errors']
114     assert_not_nil response_errors, 'Expected error in response'
115     assert (response_errors.first.include? 'Path not found'), 'Expected 404'
116   end
117
118   test "invoke setup with existing uuid, vm and repo and verify links" do
119     authorize_with :inactive
120     get :current
121     assert_response :success
122     inactive_user = JSON.parse(@response.body)
123     
124     authorize_with :admin
125
126     post :setup, {
127       user: {uuid: inactive_user['uuid']},
128       repo_name: 'test_repo',
129       vm_uuid: @vm_uuid,
130       openid_prefix: 'https://www.google.com/accounts/o8/id'
131     }
132
133     assert_response :success
134
135     response_items = JSON.parse(@response.body)['items']
136     resp_obj = response_items['user']
137
138     assert_not_nil resp_obj['uuid'], 'expected uuid for the new user'
139     assert_equal inactive_user['uuid'], resp_obj['uuid']
140     assert_equal inactive_user['email'], resp_obj['email'], 
141         'expecting inactive user email'
142
143     # expect repo and vm links
144     verify_link response_items, 'repo_perm', true, 'permission', 'can_write',
145         'test_repo', resp_obj['uuid'], 'arvados#repository', true, 'Repository'
146
147     verify_link response_items, 'vm_login_perm', true, 'permission', 'can_login',
148         @vm_uuid, resp_obj['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
149   end
150
151   test "create user with valid email and repo as input" do
152     authorize_with :admin
153
154     post :setup, {
155       repo_name: 'test_repo',
156       user: {email: 'abc@xyz.com'},
157       openid_prefix: 'https://www.google.com/accounts/o8/id'
158     }
159
160     assert_response :success
161     response_object = JSON.parse(@response.body)['items']['user']
162     assert_not_nil response_object['uuid'], 'expected uuid for the new user'
163     assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
164
165     # three extra links; login link, group link and repo link
166     verify_num_links @all_links_at_start, 3
167   end
168
169   test "create user with valid email, repo and fake vm as input" do
170     authorize_with :admin
171
172     post :setup, {
173       repo_name: 'test_repo',
174       vm_uuid: 'no_such_vm',
175       user: {email: 'abc@xyz.com'},
176       openid_prefix: 'https://www.google.com/accounts/o8/id'
177     }
178
179     response_body = JSON.parse(@response.body)
180     response_errors = response_body['errors']
181     assert_not_nil response_errors, 'Expected error in response'
182     assert (response_errors.first.include? "No vm found for no_such_vm"), 
183           'Expected RuntimeError: No vm found for no_such_vm'
184   end
185
186   test "create user with valid email, repo and real vm as input" do
187     authorize_with :admin
188
189     post :setup, {
190       repo_name: 'test_repo',
191       openid_prefix: 'https://www.google.com/accounts/o8/id',
192       vm_uuid: @vm_uuid,
193       user: {email: 'abc@xyz.com'}
194     }
195
196     assert_response :success
197     response_object = JSON.parse(@response.body)['items']['user']
198     assert_not_nil response_object['uuid'], 'expected uuid for the new user'
199     assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
200
201     # three extra links; login link, group link and repo link
202     verify_num_links @all_links_at_start, 4
203   end
204
205   test "create user with valid email, no vm and repo as input" do
206     authorize_with :admin
207
208     post :setup, {
209       user: {email: 'abc@xyz.com'},
210       openid_prefix: 'https://www.google.com/accounts/o8/id'
211     }
212
213     assert_response :success    
214     response_object = JSON.parse(@response.body)['items']['user']
215     assert_not_nil response_object['uuid'], 'expected uuid for new user'
216     assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
217
218     # two extra links; login link and group link
219     verify_num_links @all_links_at_start, 2
220   end
221
222   test "create user with email, first name, repo name and vm uuid" do
223     authorize_with :admin
224
225     post :setup, {
226       openid_prefix: 'https://www.google.com/accounts/o8/id',
227       repo_name: 'test_repo',
228       vm_uuid: @vm_uuid,
229       user: {
230         first_name: 'test_first_name',
231         email: 'abc@xyz.com'
232       }
233     }
234
235     assert_response :success
236     response_object = JSON.parse(@response.body)['items']['user']
237     assert_not_nil response_object['uuid'], 'expected uuid for new user'
238     assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
239     assert_equal 'test_first_name', response_object['first_name'], 
240         'expecting first name'
241
242     # four extra links; login link, group link, repo link and vm link
243     verify_num_links @all_links_at_start, 4
244   end
245
246   test "create user twice with email and check two different objects created" do
247     authorize_with :admin
248
249     post :setup, {
250       openid_prefix: 'https://www.google.com/accounts/o8/id',
251       repo_name: 'test_repo',
252       user: {
253         email: 'abc@xyz.com'
254       }
255     }
256
257     assert_response :success
258     response_object = JSON.parse(@response.body)['items']['user']
259     assert_not_nil response_object['uuid'], 'expected uuid for new user'
260     assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
261     verify_num_links @all_links_at_start, 3   # openid, group, and repo. no vm
262
263     # create again
264     post :setup, {
265       user: {email: 'abc@xyz.com'},
266       openid_prefix: 'https://www.google.com/accounts/o8/id'
267     }
268
269     assert_response :success
270     response_object2 = JSON.parse(@response.body)['items']['user']
271     assert_not_equal response_object['uuid'], response_object2['uuid'], 
272         'expected same uuid as first create operation'
273     assert_equal response_object['email'], 'abc@xyz.com', 'expected given email'
274
275     # extra login link only
276     verify_num_links @all_links_at_start, 4
277   end
278
279   test "create user with openid prefix" do
280     authorize_with :admin
281
282     post :setup, {
283       repo_name: 'test_repo',
284       openid_prefix: 'http://www.xyz.com/account',
285       user: {
286         first_name: "in_create_test_first_name",
287         last_name: "test_last_name",
288         email: "test@abc.com"
289       }
290     }
291
292     assert_response :success
293
294     response_items = JSON.parse(@response.body)['items']
295     created = response_items['user']
296
297     assert_equal 'in_create_test_first_name', created['first_name']
298     assert_not_nil created['uuid'], 'expected uuid for new user'
299     assert_not_nil created['email'], 'expected non-nil email'
300     assert_nil created['identity_url'], 'expected no identity_url' 
301
302     # verify links
303     # 3 new links: oid_login_perm, repo, and 'All users' group.
304     verify_num_links @all_links_at_start, 3
305
306     verify_link response_items, 'oid_login_perm', true, 'permission', 'can_login',
307         created['uuid'], created['email'], 'arvados#user', false, 'User'
308
309     verify_link response_items, 'repo_perm', true, 'permission', 'can_write',
310         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
311
312     verify_link response_items, 'group_perm', true, 'permission', 'can_read',
313         'All users', created['uuid'], 'arvados#group', true, 'Group'
314
315     verify_link response_items, 'vm_login_perm', false, 'permission', 'can_login',
316         nil, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
317   end
318
319   test "invoke setup with no openid prefix, expect error" do
320     authorize_with :admin
321
322     post :setup, {
323       repo_name: 'test_repo',
324       user: {
325         first_name: "in_create_test_first_name",
326         last_name: "test_last_name",
327         email: "test@abc.com"
328       }
329     }
330
331     response_body = JSON.parse(@response.body)
332     response_errors = response_body['errors']
333     assert_not_nil response_errors, 'Expected error in response'
334     assert (response_errors.first.include? 'openid_prefix parameter is missing'),
335         'Expected ArgumentError'
336   end
337
338   test "create user with user, vm and repo and verify links" do
339     authorize_with :admin
340
341     post :setup, {
342       user: {
343         first_name: "in_create_test_first_name",
344         last_name: "test_last_name",
345         email: "test@abc.com"
346       },
347       vm_uuid: @vm_uuid,
348       repo_name: 'test_repo',
349       openid_prefix: 'https://www.google.com/accounts/o8/id'
350     }
351
352     assert_response :success
353
354     response_items = JSON.parse(@response.body)['items']
355     created = response_items['user']
356
357     assert_equal 'in_create_test_first_name', created['first_name']
358     assert_not_nil created['uuid'], 'expected uuid for new user'
359     assert_not_nil created['email'], 'expected non-nil email'
360     assert_nil created['identity_url'], 'expected no identity_url' 
361
362     # expect 4 new links: oid_login_perm, repo, vm and 'All users' group link
363     verify_num_links @all_links_at_start, 4
364
365     verify_link response_items, 'oid_login_perm', true, 'permission', 'can_login',
366         created['uuid'], created['email'], 'arvados#user', false, 'User'
367
368     verify_link response_items, 'repo_perm', true, 'permission', 'can_write',
369         'test_repo', created['uuid'], 'arvados#repository', true, 'Repository'
370
371     verify_link response_items, 'group_perm', true, 'permission', 'can_read',
372         'All users', created['uuid'], 'arvados#group', true, 'Group'
373
374     verify_link response_items, 'vm_login_perm', true, 'permission', 'can_login', 
375         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false, 'VirtualMachine'
376   end
377
378   test "try to create user as non admin user" do
379     authorize_with :active
380
381     post :create, {
382       user: {email: 'abc@xyz.com'}
383     }
384
385     response_body = JSON.parse(@response.body)
386     response_errors = response_body['errors']
387     assert_not_nil response_errors, 'Expected error in response'
388     assert (response_errors.first.include? 'PermissionDenied'), 
389           'Expected PermissionDeniedError'
390   end
391
392   test "try to setup user as non admin user" do
393     authorize_with :active
394
395     post :setup, {
396       openid_prefix: 'https://www.google.com/accounts/o8/id',
397       user: {email: 'abc@xyz.com'}
398     }
399
400     response_body = JSON.parse(@response.body)
401     response_errors = response_body['errors']
402     assert_not_nil response_errors, 'Expected error in response'
403     assert (response_errors.first.include? 'PermissionDenied'), 
404           'Expected PermissionDeniedError'
405   end
406
407   def verify_num_links (original_links, expected_additional_links)
408     links_now = Link.all
409     assert_equal original_links.size+expected_additional_links, Link.all.size,
410         "Expected #{expected_additional_links.inspect} more links"
411   end
412
413   def verify_link(response_items, link_object_name, expect_link, link_class,
414         link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
415     link = response_items[link_object_name]
416
417     if !expect_link 
418       assert_nil link
419       return
420     end
421
422     assert_not_nil link
423
424     if fetch_object
425       object = Object.const_get(class_name).where(name: head_uuid)
426       assert [] != object, "expected #{class_name} with name #{head_uuid}"
427       head_uuid = object.first[:uuid]
428     end
429
430     assert_equal link['link_class'], link_class,
431         "did not find expected link_class for #{link_object_name}"
432  
433     assert_equal link['name'], link_name,
434         "did not find expected link_name for #{link_object_name}"
435  
436     assert_equal link['tail_uuid'], tail_uuid,
437         "did not find expected tail_uuid for #{link_object_name}"
438  
439     assert_equal link['head_kind'], head_kind,
440         "did not find expected head_kind for #{link_object_name}"
441  
442     assert_equal link['head_uuid'], head_uuid,
443         "did not find expected head_uuid for #{link_object_name}"
444   end
445
446 end