3 class Arvados::V1::UsersControllerTest < ActionController::TestCase
6 @all_users_at_start = User.all
7 @all_groups_at_start = Group.all
8 @all_links_at_start = Link.all
10 @vm_uuid = virtual_machines(:testvm).uuid
13 test "activate a user after signing UA" do
14 authorize_with :inactive_but_signed_user_agreement
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']
25 test "refuse to activate a user before signing UA" do
26 authorize_with :inactive
28 assert_response :success
29 me = JSON.parse(@response.body)
30 post :activate, uuid: me['uuid']
33 assert_response :success
34 me = JSON.parse(@response.body)
35 assert_equal false, me['is_active']
38 test "activate an already-active user" do
39 authorize_with :active
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']
49 test "create new user with user as input" do
52 first_name: "test_first_name",
53 last_name: "test_last_name",
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'
64 test "create user with user, vm and repo as input" do
66 repo_name = 'test_repo'
70 vm_uuid: 'no_such_vm',
72 uuid: "is_this_correct",
73 first_name: "in_create_test_first_name",
74 last_name: "test_last_name",
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'
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
89 verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
91 verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, created['uuid'], 'arvados#repository', true
93 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
96 test "create user with user_param, vm and repo as input" do
100 user_param: 'not_an_existing_uuid_and_not_email_format',
101 repo_name: 'test_repo',
102 vm_uuid: 'no_such_vm',
106 response_body = JSON.parse(@response.body)
107 response_errors = response_body['errors']
108 assert_not_nil response_errors, 'Expected error in response'
109 incorrectly_formatted = response_errors.first.include?('ArgumentError: User param is not of valid email format')
110 assert incorrectly_formatted, 'Expected not valid email format error'
113 test "create user with existing uuid user_param, vm and repo as input" do
114 authorize_with :inactive
116 assert_response :success
117 inactive_user = JSON.parse(@response.body)
119 authorize_with :admin
122 user_param: inactive_user['uuid'],
123 repo_name: 'test_repo',
124 vm_uuid: 'no_such_vm',
128 assert_response :success
129 response_object = JSON.parse(@response.body)
130 assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
131 assert_equal inactive_user['uuid'], response_object['uuid']
132 assert_equal inactive_user['email'], response_object['email'], 'expecting inactive user email'
135 test "create user with valid email user_param, vm and repo as input" do
136 authorize_with :admin
139 user_param: 'abc@xyz.com',
140 repo_name: 'test_repo',
141 vm_uuid: 'no_such_vm',
145 assert_response :success
146 response_object = JSON.parse(@response.body)
147 assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
148 assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
151 test "create user with valid email user_param, no vm and repo as input" do
152 authorize_with :admin
155 user_param: 'abc@xyz.com',
159 assert_response :success
160 response_object = JSON.parse(@response.body)
161 assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
162 assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
165 test "create user with user_param and user which will be ignored" do
166 authorize_with :admin
169 user_param: 'abc@xyz.com',
170 repo_name: 'test_repo',
171 vm_uuid: 'no_such_vm',
173 email: 'will_be_ignored@xyz.om'
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'
183 test "create user with valid email user_param, vm and repo as input with opt.n" do
184 authorize_with :admin
187 user_param: 'abc@xyz.com',
188 repo_name: 'test_repo',
189 vm_uuid: 'no_such_vm',
194 assert_response :success
195 response_object = JSON.parse(@response.body)
196 assert_nil response_object['uuid'], 'expected null uuid since no object created due to just probe'
197 assert_nil response_object['email'], 'expecting null email'
200 test "create user twice and check links are not recreated" do
201 authorize_with :admin
204 user_param: 'abc@xyz.com',
205 repo_name: 'test_repo',
206 vm_uuid: 'no_such_vm',
210 assert_response :success
211 response_object = JSON.parse(@response.body)
212 assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
213 assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
214 verify_num_links @all_links_at_start, 3 # openid, group, and repo links. no vm link
218 user_param: 'abc@xyz.com',
219 repo_name: 'test_repo',
220 vm_uuid: 'no_such_vm',
224 assert_response :success
225 response_object2 = JSON.parse(@response.body)
226 assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
227 assert_equal response_object['email'], 'abc@xyz.com', 'expecting given email'
228 verify_num_links @all_links_at_start, 3 # openid, group, and repo links. no vm link
231 test "create user with openid prefix" do
232 authorize_with :admin
235 repo_name: 'test_repo',
236 vm_uuid: 'no_such_vm',
237 openid_prefix: 'http://www.xyz.com/account',
239 first_name: "in_create_test_first_name",
240 last_name: "test_last_name",
241 email: "test@abc.com"
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'
252 # 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
253 verify_num_links @all_links_at_start, 3
255 verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
257 verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
259 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
262 test "create user with user, vm and repo and verify links" do
263 authorize_with :admin
266 repo_name: 'test_repo',
269 first_name: "in_create_test_first_name",
270 last_name: "test_last_name",
271 email: "test@abc.com"
274 assert_response :success
275 created = JSON.parse(@response.body)
276 assert_equal 'in_create_test_first_name', created['first_name']
277 assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
278 assert_not_nil created['email'], 'since email was given, expected non-nil email'
279 assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user'
281 # expect 4 new links: oid_login_perm, repo link, vm link and link add user to 'All users' group.
282 verify_num_links @all_links_at_start, 4
284 verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
286 verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
288 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
290 verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
293 def verify_num_links (original_links, expected_num_additional_links)
295 assert_equal original_links.size+expected_num_additional_links, Link.all.size,
296 "Expected #{expected_num_additional_links.inspect} more links"
299 def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, head_kind, fetch_object)
301 object = Object.const_get(class_name).where(name: head_uuid)
302 assert [] != object, "expected a #{class_name.inspect} with the name #{head_uuid.inspect}"
303 head_uuid = object.first[:uuid]
306 links = Link.where(link_class: link_class,
308 tail_uuid: tail_uuid,
309 head_uuid: head_uuid,
310 head_kind: head_kind)
311 assert links.size > 0, "expected one or more links with the given criteria #{class_name} with #{head_uuid}"