Introduce a new setup method in the users controller
[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: "is_this_correct",        
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 'is_this_correct', created['uuid']
83     assert_not_nil created['email'], 'since email was given, expected non-nil email'
84     assert_nil created['identity_url'], 'even though email isprovided, expected no identity_url since users_controller only creates user' 
85
86     # since no such vm exists, expect only three new links: oid_login_perm, repo link and link add user to 'All users' group
87     verify_num_links @all_links_at_start, 3
88
89     verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
90
91     verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, created['uuid'], 'arvados#repository', true
92
93     verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
94   end
95
96   test "create user with user_param, vm and repo as input" do
97     authorize_with :admin
98
99     post :setup, {
100       user_param: 'not_an_existing_uuid_and_not_email_format',
101       repo_name: 'test_repo',
102       vm_uuid: 'no_such_vm',
103       user: {}
104     }
105     response_body = JSON.parse(@response.body)
106     response_errors = response_body['errors']
107     assert_not_nil response_errors, 'Expected error in response'
108     incorrectly_formatted = response_errors.first.include?('ArgumentError: User param is not of valid email format')
109     assert incorrectly_formatted, 'Expected not valid email format error'
110   end
111
112   test "create user with existing uuid user_param, vm and repo as input" do
113     authorize_with :inactive
114     get :current
115     assert_response :success
116     inactive_user = JSON.parse(@response.body)
117     
118     authorize_with :admin
119
120     post :setup, {
121       user_param: inactive_user['uuid'],
122       repo_name: 'test_repo',
123       vm_uuid: 'no_such_vm',
124       user: {}
125     }
126
127     assert_response :success
128     response_object = JSON.parse(@response.body)
129     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
130     assert_equal inactive_user['uuid'], response_object['uuid']
131     assert_equal inactive_user['email'], response_object['email'], 'expecting inactive user email'
132   end
133
134   test "create user with valid email user_param, vm and repo as input" do
135     authorize_with :admin
136
137     post :setup, {
138       user_param: 'abc@xyz.com',
139       repo_name: 'test_repo',
140       vm_uuid: 'no_such_vm',
141       user: {}
142     }
143
144     assert_response :success
145     response_object = JSON.parse(@response.body)
146     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
147     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
148   end
149
150   test "create user with valid email user_param, no vm and repo as input" do
151     authorize_with :admin
152
153     post :setup, {
154       user_param: 'abc@xyz.com',
155       user: {}
156     }
157
158     assert_response :success    
159     response_object = JSON.parse(@response.body)
160     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
161     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
162   end
163
164   test "create user with user_param and non-empty user which will be ignored" do
165     authorize_with :admin
166
167     post :setup, {
168       user_param: 'abc@xyz.com',
169       repo_name: 'test_repo',
170       vm_uuid: 'no_such_vm',
171       user: {
172         first_name: 'test_first_name',
173         email: 'will_be_ignored@xyz.com'
174       }
175     }
176
177     assert_response :success
178     response_object = JSON.parse(@response.body)
179     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
180     assert_equal response_object['email'], 'abc@xyz.com', 'expecting user_param as email'
181     assert_nil response_object['first_name'], 'expecting no first name since it will be reset when user_param is used'
182   end
183
184   test "create user twice with user param and check links are not recreated" do
185     authorize_with :admin
186
187     post :setup, {
188       user_param: 'abc@xyz.com',
189       repo_name: 'test_repo',
190       vm_uuid: 'no_such_vm',
191       user: {}
192     }
193
194     assert_response :success
195     response_object = JSON.parse(@response.body)
196     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
197     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
198     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
199
200     # create again
201     post :setup, {
202       user_param: 'abc@xyz.com',
203       repo_name: 'test_repo',
204       vm_uuid: 'no_such_vm',
205       user: {}
206     }
207
208     assert_response :success
209     response_object2 = JSON.parse(@response.body)
210     assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
211     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
212     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
213   end
214
215   test "create user twice with user object as input and check links are not recreated" do
216     authorize_with :admin
217
218     post :setup, {
219       repo_name: 'test_repo',
220       user: {
221         email: 'abc@xyz.com'
222       }
223     }
224
225     assert_response :success
226     response_object = JSON.parse(@response.body)
227     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
228     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
229     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
230
231     # create again
232     post :create, user: {
233       email: 'abc@xyz.com'
234     }
235
236     assert_response :success
237     response_object2 = JSON.parse(@response.body)
238     assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
239     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
240     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
241   end
242
243   test "create user with openid prefix" do
244     authorize_with :admin
245
246     post :setup, {
247       repo_name: 'test_repo',
248       vm_uuid: 'no_such_vm',
249       openid_prefix: 'http://www.xyz.com/account',
250       user: {
251         first_name: "in_create_test_first_name",
252         last_name: "test_last_name",
253         email: "test@abc.com"
254       }
255     }
256     assert_response :success
257     created = JSON.parse(@response.body)
258     assert_equal 'in_create_test_first_name', created['first_name']
259     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
260     assert_not_nil created['email'], 'since email was given, expected non-nil email'
261     assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user' 
262
263     # verify links
264     # expect 3 new links: oid_login_perm, repo link, and link add user to 'All users' group. No vm link since the vm_uuid passed in is not in system
265     verify_num_links @all_links_at_start, 3
266
267     verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
268
269     verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
270
271     verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
272   end
273
274   test "create user with user, vm and repo and verify links" do
275     authorize_with :admin
276
277     post :setup, {
278       repo_name: 'test_repo',
279       vm_uuid: @vm_uuid,
280       user: {
281         first_name: "in_create_test_first_name",
282         last_name: "test_last_name",
283         email: "test@abc.com"
284       }
285     }
286     assert_response :success
287     created = JSON.parse(@response.body)
288     assert_equal 'in_create_test_first_name', created['first_name']
289     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
290     assert_not_nil created['email'], 'since email was given, expected non-nil email'
291     assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user' 
292
293     # expect 4 new links: oid_login_perm, repo link, vm link and link add user to 'All users' group. 
294     verify_num_links @all_links_at_start, 4
295
296     verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
297
298     verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
299
300     verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
301
302     verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
303   end
304
305   def verify_num_links (original_links, expected_num_additional_links)
306     links_now = Link.all
307     assert_equal original_links.size+expected_num_additional_links, Link.all.size, 
308               "Expected #{expected_num_additional_links.inspect} more links"
309   end
310
311   def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, head_kind, fetch_object)
312     if fetch_object
313       object = Object.const_get(class_name).where(name: head_uuid)
314       assert [] != object, "expected a #{class_name.inspect} with the name #{head_uuid.inspect}"
315       head_uuid = object.first[:uuid]
316     end
317
318     links = Link.where(link_class: link_class,
319                        name: link_name,
320                        tail_uuid: tail_uuid,
321                        head_uuid: head_uuid,
322                        head_kind: head_kind)
323     assert links.size > 0, "expected one or more links with the given criteria #{class_name} with #{head_uuid}"
324   end
325
326 end