remove hard coded uuids from tests
[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 :create, {
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 'Repository', 'permission', 'can_write', repo_name, created['uuid'], 'arvados#repository', true
90
91                 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
92         end
93
94         test "create user with user_param, vm and repo as input" do
95     authorize_with :admin
96
97     post :create, {
98       user_param: 'not_an_existing_uuid_and_not_email_format',
99       repo_name: 'test_repo',
100                         vm_uuid: 'no_such_vm',
101       user: {}
102     }
103
104     response_body = JSON.parse(@response.body)
105     response_errors = response_body['errors']
106                 assert_not_nil response_errors, 'Expected error in response'
107                 incorrectly_formatted = response_errors.first.include?('ArgumentError: User param is not of valid email format')
108                 assert incorrectly_formatted, 'Expected not valid email format error'
109         end
110
111         test "create user with existing uuid user_param, vm and repo as input" do
112                 authorize_with :inactive
113     get :current
114     assert_response :success
115     inactive_user = JSON.parse(@response.body)
116                 
117     authorize_with :admin
118
119     post :create, {
120       user_param: inactive_user['uuid'],
121       repo_name: 'test_repo',
122                         vm_uuid: 'no_such_vm',
123       user: {}
124     }
125
126     assert_response :success
127     response_object = JSON.parse(@response.body)
128     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
129                 assert_equal inactive_user['uuid'], response_object['uuid']
130     assert_equal inactive_user['email'], response_object['email'], 'expecting inactive user email'
131         end
132
133         test "create user with valid email user_param, vm and repo as input" do
134     authorize_with :admin
135
136     post :create, {
137       user_param: 'abc@xyz.com',
138       repo_name: 'test_repo',
139                         vm_uuid: 'no_such_vm',
140       user: {}
141     }
142
143     assert_response :success
144     response_object = JSON.parse(@response.body)
145     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
146     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
147         end
148
149         test "create user with valid email user_param, no vm and repo as input" do
150     authorize_with :admin
151
152     post :create, {
153       user_param: 'abc@xyz.com',
154       user: {}
155     }
156
157     assert_response :success            
158     response_object = JSON.parse(@response.body)
159     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
160     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
161         end
162
163         test "create user with valid email user_param, vm and repo as input with opt.n" do
164     authorize_with :admin
165
166     post :create, {
167       user_param: 'abc@xyz.com',
168       repo_name: 'test_repo',
169                         vm_uuid: 'no_such_vm',
170                         just_probe: 'true',
171       user: {}
172     }
173
174     assert_response :success
175     response_object = JSON.parse(@response.body)
176     assert_nil response_object['uuid'], 'expected null uuid since no object created due to just probe'
177     assert_nil response_object['email'], 'expecting null email'
178         end
179
180         test "create user twice and check links are not recreated" do
181     authorize_with :admin
182
183     post :create, {
184       user_param: 'abc@xyz.com',
185       repo_name: 'test_repo',
186                         vm_uuid: 'no_such_vm',
187       user: {}
188     }
189
190     assert_response :success
191     response_object = JSON.parse(@response.body)
192     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
193     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
194                 verify_num_links @all_links_at_start, 3         # openid, group, and repo links. no vm link
195
196                 # create again
197     post :create, {
198       user_param: 'abc@xyz.com',
199       repo_name: 'test_repo',
200                         vm_uuid: 'no_such_vm',
201       user: {}
202     }
203
204     assert_response :success
205     response_object2 = JSON.parse(@response.body)
206     assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
207     assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
208                 verify_num_links @all_links_at_start, 3         # openid, group, and repo links. no vm link
209         end
210
211         # still in progress
212         test "create user with openid_prefix" do
213     authorize_with :admin
214
215     post :create, {
216       repo_name: 'test_repo',
217                         vm_uuid: 'no_such_vm',
218       user: {
219                                 first_name: "in_create_test_first_name",
220                     last_name: "test_last_name",
221                                 email: "test@abc.com"
222       }
223     }
224     assert_response :success
225     created = JSON.parse(@response.body)
226     assert_equal 'in_create_test_first_name', created['first_name']
227     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
228     assert_not_nil created['email'], 'since email was given, expected non-nil email'
229     assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user' 
230         end
231
232         test "create user with user, vm and repo and verify links" do
233     authorize_with :admin
234
235     post :create, {
236       repo_name: 'test_repo',
237                         vm_uuid: @vm_uuid,
238       user: {
239                                 first_name: "in_create_test_first_name",
240                     last_name: "test_last_name",
241                                 email: "test@abc.com"
242       }
243     }
244     assert_response :success
245     created = JSON.parse(@response.body)
246     assert_equal 'in_create_test_first_name', created['first_name']
247     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
248     assert_not_nil created['email'], 'since email was given, expected non-nil email'
249     assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user' 
250
251                 # expect 4 new links: oid_login_perm, repo link, vm link and link add user to 'All users' group. 
252                 verify_num_links @all_links_at_start, 4
253
254                 verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
255
256                 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
257
258
259                 verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
260         end
261
262         def verify_num_links (original_links, expected_num_additional_links)
263                 links_now = Link.all
264                 assert_equal original_links.size+expected_num_additional_links, Link.all.size, 
265                                                         "Expected #{expected_num_additional_links.inspect} more links"
266         end
267
268         def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, head_kind, fetch_object)
269                 if fetch_object
270                         object = Object.const_get(class_name).where(name: head_uuid)
271                         assert [] != object, "expected a #{class_name.inspect} with the name #{head_uuid.inspect}"
272                         head_uuid = object.first[:uuid]
273                 end
274
275                 links = Link.where(link_class: link_class,
276                                          name: link_name,
277                                                  tail_uuid: tail_uuid,
278                                                  head_uuid: head_uuid,
279                                                head_kind: head_kind)
280                 assert links.size > 0, "expected one or more links with the given criteria #{class_name} with #{head_uuid}"
281         end
282
283 end