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: "this_is_agreeable",
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 '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'
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
90 verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'],
91 created['email'], 'arvados#user', false
93 verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name,
94 created['uuid'], 'arvados#repository', true
96 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users',
97 created['uuid'], 'arvados#group', true
100 test "create user with bogus uuid, vm and repo as input" do
101 authorize_with :admin
104 user: {uuid: 'not_an_existing_uuid_and_not_email_format'},
105 repo_name: 'test_repo',
106 vm_uuid: 'no_such_vm'
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'
115 test "create user with existing uuid, vm and repo as input and verify links" do
116 authorize_with :inactive
118 assert_response :success
119 inactive_user = JSON.parse(@response.body)
121 authorize_with :admin
124 user: {uuid: inactive_user['uuid']},
125 repo_name: 'test_repo',
126 vm_uuid: 'no_such_vm'
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'
135 # one extra link for repo
136 verify_num_links @all_links_at_start, 1
139 test "create user with valid email, vm and repo as input" do
140 authorize_with :admin
143 repo_name: 'test_repo',
144 vm_uuid: 'no_such_vm',
145 user: {email: 'abc@xyz.com'}
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'
153 # three extra links; login link, group link and repo link
154 verify_num_links @all_links_at_start, 3
157 test "create user with valid email, no vm and repo as input" do
158 authorize_with :admin
161 user: {email: 'abc@xyz.com'}
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'
169 # two extra links; login link and group link
170 verify_num_links @all_links_at_start, 2
173 test "create user with email, first name, repo name and vm uuid" do
174 authorize_with :admin
177 repo_name: 'test_repo',
180 first_name: 'test_first_name',
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'
191 # four extra links; login link, group link, repo link and vm link
192 verify_num_links @all_links_at_start, 4
195 test "create user twice with user email as input and check two different objects created" do
196 authorize_with :admin
199 repo_name: 'test_repo',
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
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'
222 # extra login link only
223 verify_num_links @all_links_at_start, 4
226 test "create user with openid prefix" do
227 authorize_with :admin
230 repo_name: 'test_repo',
231 vm_uuid: 'no_such_vm',
232 openid_prefix: 'http://www.xyz.com/account',
234 first_name: "in_create_test_first_name",
235 last_name: "test_last_name",
236 email: "test@abc.com"
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'
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
250 verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'],
251 created['email'], 'arvados#user', false
253 verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo',
254 created['uuid'], 'arvados#repository', true
256 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users',
257 created['uuid'], 'arvados#group', true
260 test "create user with user, vm and repo and verify links" do
261 authorize_with :admin
265 first_name: "in_create_test_first_name",
266 last_name: "test_last_name",
267 email: "test@abc.com"
269 repo_name: 'test_repo',
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'
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
282 verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'],
283 created['email'], 'arvados#user', false
285 verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo',
286 created['uuid'], 'arvados#repository', true
288 verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users',
289 created['uuid'], 'arvados#group', true
291 verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login',
292 @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
295 def verify_num_links (original_links, expected_num_additional_links)
297 assert_equal original_links.size+expected_num_additional_links, Link.all.size,
298 "Expected #{expected_num_additional_links.inspect} more links"
301 def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid,
302 head_kind, 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]
309 links = Link.where(link_class: link_class,
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}"