3 class Arvados::V1::LinksControllerTest < ActionController::TestCase
5 ['link', 'link_json'].each do |formatted_link|
6 test "no symbol keys in serialized hash #{formatted_link}" do
8 properties: {username: 'testusername'},
11 tail_uuid: users(:admin).uuid,
12 head_uuid: virtual_machines(:testvm).uuid
15 if formatted_link == 'link_json'
16 post :create, link: link.to_json
18 post :create, link: link
20 assert_response :success
21 assert_not_nil assigns(:object)
22 assert_equal 'testusername', assigns(:object).properties['username']
23 assert_equal false, assigns(:object).properties.has_key?(:username)
27 %w(created_at modified_at).each do |attr|
28 {nil: nil, bogus: 2.days.ago}.each do |bogustype, bogusvalue|
29 test "cannot set #{bogustype} #{attr} in create" do
30 authorize_with :active
36 }.merge(attr => bogusvalue)
38 assert_response :success
39 resp = JSON.parse @response.body
40 assert_in_delta Time.now, Time.parse(resp[attr]), 3.0
42 test "cannot set #{bogustype} #{attr} in update" do
43 really_created_at = links(:test_timestamps).created_at
44 authorize_with :active
46 id: links(:test_timestamps).uuid,
48 :properties => {test: 'test'},
52 assert_response :success
53 resp = JSON.parse @response.body
56 assert_in_delta really_created_at, Time.parse(resp[attr]), 0.001
58 assert_in_delta Time.now, Time.parse(resp[attr]), 3.0
64 test "head must exist" do
68 tail_uuid: users(:active).uuid,
69 head_uuid: 'zzzzz-tpzed-xyzxyzxerrrorxx'
72 post :create, link: link
76 test "tail must exist" do
80 head_uuid: users(:active).uuid,
81 tail_uuid: 'zzzzz-tpzed-xyzxyzxerrrorxx'
84 post :create, link: link
88 test "head and tail exist, head_kind and tail_kind are returned" do
92 head_uuid: users(:active).uuid,
93 tail_uuid: users(:spectator).uuid,
96 post :create, link: link
97 assert_response :success
98 l = JSON.parse(@response.body)
99 assert 'arvados#user', l['head_kind']
100 assert 'arvados#user', l['tail_kind']
103 test "can supply head_kind and tail_kind without error" do
107 head_uuid: users(:active).uuid,
108 tail_uuid: users(:spectator).uuid,
109 head_kind: "arvados#user",
110 tail_kind: "arvados#user",
112 authorize_with :admin
113 post :create, link: link
114 assert_response :success
115 l = JSON.parse(@response.body)
116 assert 'arvados#user', l['head_kind']
117 assert 'arvados#user', l['tail_kind']
120 test "tail must be visible by user" do
124 head_uuid: users(:active).uuid,
125 tail_uuid: virtual_machines(:testvm2).uuid
127 authorize_with :active
128 post :create, link: link
132 test "filter links with 'is_a' operator" do
133 authorize_with :admin
135 filters: [ ['tail_uuid', 'is_a', 'arvados#user'] ]
137 assert_response :success
138 found = assigns(:objects)
139 assert_not_equal 0, found.count
140 assert_equal found.count, (found.select { |f| f.tail_uuid.match /[a-z0-9]{5}-tpzed-[a-z0-9]{15}/}).count
143 test "filter links with 'is_a' operator with more than one" do
144 authorize_with :admin
146 filters: [ ['tail_uuid', 'is_a', ['arvados#user', 'arvados#group'] ] ],
148 assert_response :success
149 found = assigns(:objects)
150 assert_not_equal 0, found.count
151 assert_equal found.count, (found.select { |f| f.tail_uuid.match /[a-z0-9]{5}-(tpzed|j7d0g)-[a-z0-9]{15}/}).count
154 test "filter links with 'is_a' operator with bogus type" do
155 authorize_with :admin
157 filters: [ ['tail_uuid', 'is_a', ['arvados#bogus'] ] ],
159 assert_response :success
160 found = assigns(:objects)
161 assert_equal 0, found.count
164 test "filter links with 'is_a' operator with collection" do
165 authorize_with :admin
167 filters: [ ['head_uuid', 'is_a', ['arvados#collection'] ] ],
169 assert_response :success
170 found = assigns(:objects)
171 assert_not_equal 0, found.count
172 assert_equal found.count, (found.select { |f| f.head_uuid.match /.....-4zz18-.............../}).count
175 test "test can still use where tail_kind" do
176 authorize_with :admin
178 where: { tail_kind: 'arvados#user' }
180 assert_response :success
181 found = assigns(:objects)
182 assert_not_equal 0, found.count
183 assert_equal found.count, (found.select { |f| f.tail_uuid.match /[a-z0-9]{5}-tpzed-[a-z0-9]{15}/}).count
186 test "test can still use where head_kind" do
187 authorize_with :admin
189 where: { head_kind: 'arvados#user' }
191 assert_response :success
192 found = assigns(:objects)
193 assert_not_equal 0, found.count
194 assert_equal found.count, (found.select { |f| f.head_uuid.match /[a-z0-9]{5}-tpzed-[a-z0-9]{15}/}).count
197 test "test can still use filter tail_kind" do
198 authorize_with :admin
200 filters: [ ['tail_kind', '=', 'arvados#user'] ]
202 assert_response :success
203 found = assigns(:objects)
204 assert_not_equal 0, found.count
205 assert_equal found.count, (found.select { |f| f.tail_uuid.match /[a-z0-9]{5}-tpzed-[a-z0-9]{15}/}).count
208 test "test can still use filter head_kind" do
209 authorize_with :admin
211 filters: [ ['head_kind', '=', 'arvados#user'] ]
213 assert_response :success
214 found = assigns(:objects)
215 assert_not_equal 0, found.count
216 assert_equal found.count, (found.select { |f| f.head_uuid.match /[a-z0-9]{5}-tpzed-[a-z0-9]{15}/}).count
219 test "head_kind matches head_uuid" do
223 head_uuid: groups(:public).uuid,
224 head_kind: "arvados#user",
225 tail_uuid: users(:spectator).uuid,
226 tail_kind: "arvados#user",
228 authorize_with :admin
229 post :create, link: link
233 test "tail_kind matches tail_uuid" do
237 head_uuid: users(:active).uuid,
238 head_kind: "arvados#user",
239 tail_uuid: groups(:public).uuid,
240 tail_kind: "arvados#user",
242 authorize_with :admin
243 post :create, link: link
247 test "test with virtual_machine" do
249 tail_kind: "arvados#user",
250 tail_uuid: users(:active).uuid,
251 head_kind: "arvados#virtual_machine",
252 head_uuid: virtual_machines(:testvm).uuid,
253 link_class: "permission",
255 properties: {username: "repo_and_user_name"}
257 authorize_with :admin
258 post :create, link: link
262 test "test with virtualMachine" do
264 tail_kind: "arvados#user",
265 tail_uuid: users(:active).uuid,
266 head_kind: "arvados#virtualMachine",
267 head_uuid: virtual_machines(:testvm).uuid,
268 link_class: "permission",
270 properties: {username: "repo_and_user_name"}
272 authorize_with :admin
273 post :create, link: link
274 assert_response :success
277 test "project owner can show a project permission" do
278 uuid = links(:project_viewer_can_read_project).uuid
279 authorize_with :active
281 assert_response :success
282 assert_equal(uuid, assigns(:object).andand.uuid)
285 test "admin can show a project permission" do
286 uuid = links(:project_viewer_can_read_project).uuid
287 authorize_with :admin
289 assert_response :success
290 assert_equal(uuid, assigns(:object).andand.uuid)
293 test "project viewer can't show others' project permissions" do
294 authorize_with :project_viewer
295 get :show, id: links(:admin_can_write_aproject).uuid
299 test "requesting a nonexistent link returns 404" do
300 authorize_with :active
301 get :show, id: 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
305 test "retrieve all permissions using generic links index api" do
306 skip "(not implemented)"
307 # Links.readable_by() does not return the full set of permission
308 # links that are visible to a user (i.e., all permission links
309 # whose head_uuid references an object for which the user has
310 # ownership or can_manage permission). Therefore, neither does
313 # It is possible to retrieve the full set of permissions for a
314 # single object via /arvados/v1/permissions.
315 authorize_with :active
316 get :index, filters: [['link_class', '=', 'permission'],
317 ['head_uuid', '=', groups(:aproject).uuid]]
318 assert_response :success
319 assert_not_nil assigns(:objects)
320 assert_includes(assigns(:objects).map(&:uuid),
321 links(:project_viewer_can_read_project).uuid)
324 test "admin can index project permissions" do
325 authorize_with :admin
326 get :index, filters: [['link_class', '=', 'permission'],
327 ['head_uuid', '=', groups(:aproject).uuid]]
328 assert_response :success
329 assert_not_nil assigns(:objects)
330 assert_includes(assigns(:objects).map(&:uuid),
331 links(:project_viewer_can_read_project).uuid)
334 test "project viewer can't index others' project permissions" do
335 authorize_with :project_viewer
336 get :index, filters: [['link_class', '=', 'permission'],
337 ['head_uuid', '=', groups(:aproject).uuid],
338 ['tail_uuid', '!=', users(:project_viewer).uuid]]
339 assert_response :success
340 assert_not_nil assigns(:objects)
341 assert_empty assigns(:objects)