All tests passing
[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_users_at_start = User.all
7     @all_groups_at_start = Group.all
8     @all_links_at_start = Link.all
9
10     @vm_uuid = virtual_machines(:testvm).uuid
11   end
12
13   test "activate a user after signing UA" do
14     authorize_with :inactive_but_signed_user_agreement
15     get :current
16     assert_response :success
17     me = JSON.parse(@response.body)
18     post :activate, uuid: me['uuid']
19     assert_response :success
20     assert_not_nil assigns(:object)
21     me = JSON.parse(@response.body)
22     assert_equal true, me['is_active']
23   end
24
25   test "refuse to activate a user before signing UA" do
26     authorize_with :inactive
27     get :current
28     assert_response :success
29     me = JSON.parse(@response.body)
30     post :activate, uuid: me['uuid']
31     assert_response 403
32     get :current
33     assert_response :success
34     me = JSON.parse(@response.body)
35     assert_equal false, me['is_active']
36   end
37
38   test "activate an already-active user" do
39     authorize_with :active
40     get :current
41     assert_response :success
42     me = JSON.parse(@response.body)
43     post :activate, uuid: me['uuid']
44     assert_response :success
45     me = JSON.parse(@response.body)
46     assert_equal true, me['is_active']
47   end
48
49   test "create new user with user as input" do
50     authorize_with :admin
51     post :create, user: {
52       first_name: "test_first_name",
53       last_name: "test_last_name",
54       email: "test@abc.com"
55     }
56     assert_response :success
57     created = JSON.parse(@response.body)
58     assert_equal 'test_first_name', created['first_name']
59     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
60     assert_not_nil created['email'], 'since email was given, expected non-nil email'
61     assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user at this time'
62   end
63
64   test "create user with user, vm and repo as input" do
65     authorize_with :admin
66     repo_name = 'test_repo'
67
68     post :setup, {
69       repo_name: repo_name,
70       vm_uuid: 'no_such_vm',
71       user: {
72         uuid: "this_is_agreeable",        
73         first_name: "in_create_test_first_name",
74         last_name: "test_last_name",
75         email: "test@abc.com"
76       }
77     }
78     assert_response :success
79     created = JSON.parse(@response.body)
80     assert_equal 'in_create_test_first_name', created['first_name']
81     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
82     assert_equal 'this_is_agreeable', created['uuid']
83     assert_not_nil created['email'], 'since email was given, 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_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
91         created['email'], 'arvados#user', false
92
93     verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, 
94         created['uuid'], 'arvados#repository', true
95
96     verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
97         created['uuid'], 'arvados#group', true
98   end
99
100   test "create user with bogus uuid, vm and repo as input" do
101     authorize_with :admin
102
103     post :setup, {
104       user: {uuid: 'not_an_existing_uuid_and_not_email_format'},
105       repo_name: 'test_repo',
106       vm_uuid: 'no_such_vm'
107     }
108     response_body = JSON.parse(@response.body)
109     response_errors = response_body['errors']
110     assert_not_nil response_errors, 'Expected error in response'
111     incorrectly_formatted = response_errors.first.include?('No email found in the input')
112     assert incorrectly_formatted, 'Expected not valid email format error'
113   end
114
115   test "create user with existing uuid, vm and repo as input and verify links" do
116     authorize_with :inactive
117     get :current
118     assert_response :success
119     inactive_user = JSON.parse(@response.body)
120     
121     authorize_with :admin
122
123     post :setup, {
124       user: {uuid: inactive_user['uuid']},
125       repo_name: 'test_repo',
126       vm_uuid: 'no_such_vm'
127     }
128
129     assert_response :success
130     response_object = JSON.parse(@response.body)
131     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
132     assert_equal inactive_user['uuid'], response_object['uuid']
133     assert_equal inactive_user['email'], response_object['email'], 'expecting inactive user email'
134
135     # one extra link for repo
136     verify_num_links @all_links_at_start, 1
137   end
138
139   test "create user with valid email, vm and repo as input" do
140     authorize_with :admin
141
142     post :setup, {
143       repo_name: 'test_repo',
144       vm_uuid: 'no_such_vm',
145       user: {email: 'abc@xyz.com'}
146     }
147
148     assert_response :success
149     response_object = JSON.parse(@response.body)
150     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
151     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
152
153     # three extra links; login link, group link and repo link
154     verify_num_links @all_links_at_start, 3
155   end
156
157   test "create user with valid email, no vm and repo as input" do
158     authorize_with :admin
159
160     post :setup, {
161       user: {email: 'abc@xyz.com'}
162     }
163
164     assert_response :success    
165     response_object = JSON.parse(@response.body)
166     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
167     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
168
169     # two extra links; login link and group link
170     verify_num_links @all_links_at_start, 2
171   end
172
173   test "create user with email, first name, repo name and vm uuid" do
174     authorize_with :admin
175
176     post :setup, {
177       repo_name: 'test_repo',
178       vm_uuid: @vm_uuid,
179       user: {
180         first_name: 'test_first_name',
181         email: 'abc@xyz.com'
182       }
183     }
184
185     assert_response :success
186     response_object = JSON.parse(@response.body)
187     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
188     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
189     assert_equal 'test_first_name', response_object['first_name'], 'expecting first name'
190
191     # four extra links; login link, group link, repo link and vm link
192     verify_num_links @all_links_at_start, 4
193   end
194
195   test "create user twice with user email as input and check two different objects created" do
196     authorize_with :admin
197
198     post :setup, {
199       repo_name: 'test_repo',
200       user: {
201         email: 'abc@xyz.com'
202       }
203     }
204
205     assert_response :success
206     response_object = JSON.parse(@response.body)
207     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
208     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
209     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
210
211     # create again
212     post :setup, user: {
213       email: 'abc@xyz.com'
214     }
215
216     assert_response :success
217     response_object2 = JSON.parse(@response.body)
218     assert_not_equal response_object['uuid'], response_object2['uuid'], 
219         'expected same uuid as first create operation'
220     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
221
222     # extra login link only
223     verify_num_links @all_links_at_start, 4
224   end
225
226   test "create user with openid prefix" do
227     authorize_with :admin
228
229     post :setup, {
230       repo_name: 'test_repo',
231       vm_uuid: 'no_such_vm',
232       openid_prefix: 'http://www.xyz.com/account',
233       user: {
234         first_name: "in_create_test_first_name",
235         last_name: "test_last_name",
236         email: "test@abc.com"
237       }
238     }
239     assert_response :success
240     created = JSON.parse(@response.body)
241     assert_equal 'in_create_test_first_name', created['first_name']
242     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
243     assert_not_nil created['email'], 'since email was given, expected non-nil email'
244     assert_nil created['identity_url'], 'expected no identity_url' 
245
246     # verify links
247     # 3 new links: oid_login_perm, repo link, and link add user to 'All users' group. 
248     verify_num_links @all_links_at_start, 3
249
250     verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
251         created['email'], 'arvados#user', false
252
253     verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', 
254         created['uuid'], 'arvados#repository', true
255
256     verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
257       created['uuid'], 'arvados#group', true
258   end
259
260   test "create user with user, vm and repo and verify links" do
261     authorize_with :admin
262
263     post :setup, {
264       user: {
265         first_name: "in_create_test_first_name",
266         last_name: "test_last_name",
267         email: "test@abc.com"
268       },
269       repo_name: 'test_repo',
270       vm_uuid: @vm_uuid
271     }
272     assert_response :success
273     created = JSON.parse(@response.body)
274     assert_equal 'in_create_test_first_name', created['first_name']
275     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
276     assert_not_nil created['email'], 'since email was given, expected non-nil email'
277     assert_nil created['identity_url'], 'expected no identity_url' 
278
279     # expect 4 new links: oid_login_perm, repo link, vm link and link add user to 'All users' group. 
280     verify_num_links @all_links_at_start, 4
281
282     verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
283         created['email'], 'arvados#user', false
284
285     verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', 
286         created['uuid'], 'arvados#repository', true
287
288     verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
289         created['uuid'], 'arvados#group', true
290
291     verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', 
292         @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
293   end
294
295   def verify_num_links (original_links, expected_num_additional_links)
296     links_now = Link.all
297     assert_equal original_links.size+expected_num_additional_links, Link.all.size, 
298               "Expected #{expected_num_additional_links.inspect} more links"
299   end
300
301   def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, 
302         head_kind, fetch_object)
303     if fetch_object
304       object = Object.const_get(class_name).where(name: head_uuid)
305       assert [] != object, "expected a #{class_name.inspect} with the name #{head_uuid.inspect}"
306       head_uuid = object.first[:uuid]
307     end
308
309     links = Link.where(link_class: link_class,
310                        name: link_name,
311                        tail_uuid: tail_uuid,
312                        head_uuid: head_uuid,
313                        head_kind: head_kind)
314     assert links.size > 0, "expected one or more links for #{class_name} with #{head_uuid}"
315   end
316
317 end