3 class CollectionsControllerTest < ActionController::TestCase
4 # These tests don't do state-changing API calls. Save some time by
5 # skipping the database reset.
6 reset_api_fixtures :after_each_test, false
7 reset_api_fixtures :after_suite, true
9 include PipelineInstancesHelper
11 NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
13 def config_anonymous enable
14 Rails.configuration.anonymous_user_token =
16 api_fixture('api_client_authorizations')['anonymous']['api_token']
23 # For the duration of the current test case, stub file download
24 # content with a randomized (but recognizable) string. Return the
25 # string, the test case can use it in assertions.
26 txt = 'the quick brown fox ' + rand(2**32).to_s
27 @controller.stubs(:file_enumerator).returns([txt])
31 def collection_params(collection_name, file_name=nil)
32 uuid = api_fixture('collections')[collection_name.to_s]['uuid']
33 params = {uuid: uuid, id: uuid}
34 params[:file] = file_name if file_name
38 def assert_hash_includes(actual_hash, expected_hash, msg=nil)
39 expected_hash.each do |key, value|
40 assert_equal(value, actual_hash[key], msg)
45 assert_hash_includes(session, {arvados_api_token: nil},
46 "session includes unexpected API token")
49 def assert_session_for_auth(client_auth)
51 api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
52 assert_hash_includes(session, {arvados_api_token: api_token},
53 "session token does not belong to #{client_auth}")
56 def show_collection(params, session={}, response=:success)
57 params = collection_params(params) if not params.is_a? Hash
58 session = session_for(session) if not session.is_a? Hash
59 get(:show, params, session)
60 assert_response response
63 test "viewing a collection" do
64 show_collection(:foo_file, :active)
65 assert_equal([['.', 'foo', 3]], assigns(:object).files)
68 test "viewing a collection with spaces in filename" do
69 show_collection(:w_a_z_file, :active)
70 assert_equal([['.', 'w a z', 5]], assigns(:object).files)
73 test "download a file with spaces in filename" do
74 collection = api_fixture('collections')['w_a_z_file']
75 fakepipe = IO.popen(['echo', '-n', 'w a z'], 'rb')
76 IO.expects(:popen).with { |cmd, mode|
77 cmd.include? "#{collection['uuid']}/w a z"
80 uuid: collection['uuid'],
82 }, session_for(:active)
83 assert_response :success
84 assert_equal 'w a z', response.body
87 test "viewing a collection fetches related projects" do
88 show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
89 assert_includes(assigns(:same_pdh).map(&:owner_uuid),
90 api_fixture('groups')['aproject']['uuid'],
91 "controller did not find linked project")
94 test "viewing a collection fetches related permissions" do
95 show_collection(:bar_file, :active)
96 assert_includes(assigns(:permissions).map(&:uuid),
97 api_fixture('links')['bar_file_readable_by_active']['uuid'],
98 "controller did not find permission link")
101 test "viewing a collection fetches jobs that output it" do
102 show_collection(:bar_file, :active)
103 assert_includes(assigns(:output_of).map(&:uuid),
104 api_fixture('jobs')['foobar']['uuid'],
105 "controller did not find output job")
108 test "viewing a collection fetches jobs that logged it" do
109 show_collection(:baz_file, :active)
110 assert_includes(assigns(:log_of).map(&:uuid),
111 api_fixture('jobs')['foobar']['uuid'],
112 "controller did not find logger job")
115 test "sharing auths available to admin" do
116 show_collection("collection_owned_by_active", "admin_trustedclient")
117 assert_not_nil assigns(:search_sharing)
120 test "sharing auths available to owner" do
121 show_collection("collection_owned_by_active", "active_trustedclient")
122 assert_not_nil assigns(:search_sharing)
125 test "sharing auths available to reader" do
126 show_collection("foo_collection_in_aproject",
127 "project_viewer_trustedclient")
128 assert_not_nil assigns(:search_sharing)
131 test "viewing collection files with a reader token" do
132 params = collection_params(:foo_file)
133 params[:reader_token] = api_fixture("api_client_authorizations",
134 "active_all_collections", "api_token")
135 get(:show_file_links, params)
136 assert_response :success
137 assert_equal([['.', 'foo', 3]], assigns(:object).files)
141 test "fetching collection file with reader token" do
142 expected = stub_file_content
143 params = collection_params(:foo_file, "foo")
144 params[:reader_token] = api_fixture("api_client_authorizations",
145 "active_all_collections", "api_token")
146 get(:show_file, params)
147 assert_response :success
148 assert_equal(expected, @response.body,
149 "failed to fetch a Collection file with a reader token")
153 test "reader token Collection links end with trailing slash" do
154 # Testing the fix for #2937.
155 session = session_for(:active_trustedclient)
156 post(:share, collection_params(:foo_file), session)
157 assert(@controller.download_link.ends_with? '/',
158 "Collection share link does not end with slash for wget")
161 test "getting a file from Keep" do
162 params = collection_params(:foo_file, 'foo')
163 sess = session_for(:active)
164 expect_content = stub_file_content
165 get(:show_file, params, sess)
166 assert_response :success
167 assert_equal(expect_content, @response.body,
168 "failed to get a correct file from Keep")
171 test 'anonymous download' do
172 config_anonymous true
173 expect_content = stub_file_content
175 uuid: api_fixture('collections')['user_agreement_in_anonymously_accessible_project']['uuid'],
176 file: 'GNU_General_Public_License,_version_3.pdf',
178 assert_response :success
179 assert_equal expect_content, response.body
182 test "can't get a file from Keep without permission" do
183 params = collection_params(:foo_file, 'foo')
184 sess = session_for(:spectator)
185 get(:show_file, params, sess)
189 test "trying to get a nonexistent file from Keep returns a 404" do
190 params = collection_params(:foo_file, 'gone')
191 sess = session_for(:admin)
192 get(:show_file, params, sess)
196 test "getting a file from Keep with a good reader token" do
197 params = collection_params(:foo_file, 'foo')
198 read_token = api_fixture('api_client_authorizations')['active']['api_token']
199 params[:reader_token] = read_token
200 expect_content = stub_file_content
201 get(:show_file, params)
202 assert_response :success
203 assert_equal(expect_content, @response.body,
204 "failed to get a correct file from Keep using a reader token")
205 assert_not_equal(read_token, session[:arvados_api_token],
206 "using a reader token set the session's API token")
209 [false, true].each do |anon|
210 test "download a file using a reader token with insufficient scope, anon #{anon}" do
211 config_anonymous anon
212 params = collection_params(:foo_file, 'foo')
213 params[:reader_token] =
214 api_fixture('api_client_authorizations')['active_noscope']['api_token']
215 get(:show_file, params)
217 # Some files can be shown without a valid token, but not this one.
220 # No files will ever be shown without a valid token. You
221 # should log in and try again.
222 assert_response :redirect
227 test "can get a file with an unpermissioned auth but in-scope reader token" do
228 params = collection_params(:foo_file, 'foo')
229 sess = session_for(:expired)
230 read_token = api_fixture('api_client_authorizations')['active']['api_token']
231 params[:reader_token] = read_token
232 expect_content = stub_file_content
233 get(:show_file, params, sess)
234 assert_response :success
235 assert_equal(expect_content, @response.body,
236 "failed to get a correct file from Keep using a reader token")
237 assert_not_equal(read_token, session[:arvados_api_token],
238 "using a reader token set the session's API token")
241 test "inactive user can retrieve user agreement" do
242 ua_collection = api_fixture('collections')['user_agreement']
243 # Here we don't test whether the agreement can be retrieved from
244 # Keep. We only test that show_file decides to send file content,
245 # so we use the file content stub.
248 uuid: ua_collection['uuid'],
249 file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
250 }, session_for(:inactive)
251 assert_nil(assigns(:unsigned_user_agreements),
252 "Did not skip check_user_agreements filter " +
253 "when showing the user agreement.")
254 assert_response :success
257 test "requesting nonexistent Collection returns 404" do
258 show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
262 test "use a reasonable read buffer even if client requests a huge range" do
264 IO.expects(:popen).returns(fakefiledata)
265 fakefiledata.expects(:read).twice.with() do |length|
266 # Fail the test if read() is called with length>1MiB:
268 ## Force the ActionController::Live thread to lose the race to
269 ## verify that @response.body.length actually waits for the
270 ## response (see below):
272 end.returns("foo\n", nil)
273 fakefiledata.expects(:close)
274 foo_file = api_fixture('collections')['foo_file']
275 @request.headers['Range'] = 'bytes=0-4294967296/*'
277 uuid: foo_file['uuid'],
278 file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
279 }, session_for(:active)
280 # Wait for the whole response to arrive before deciding whether
281 # mocks' expectations were met. Otherwise, Mocha will fail the
282 # test depending on how slowly the ActionController::Live thread
284 @response.body.length
287 test "show file in a subdirectory of a collection" do
288 params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
289 expect_content = stub_file_content
290 get(:show_file, params, session_for(:user1_with_load))
291 assert_response :success
292 assert_equal(expect_content, @response.body, "failed to get a correct file from Keep")
295 test 'provenance graph' do
298 obj = find_fixture Collection, "graph_test_collection3"
300 provenance = obj.provenance.stringify_keys
302 [obj[:portable_data_hash]].each do |k|
303 assert_not_nil provenance[k], "Expected key #{k} in provenance set"
306 prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
307 {:request => RequestDuck,
308 :direction => :bottom_up,
309 :combine_jobs => :script_only})
311 stage1 = find_fixture Job, "graph_stage1"
312 stage3 = find_fixture Job, "graph_stage3"
313 previous_job_run = find_fixture Job, "previous_job_run"
315 obj_id = obj.portable_data_hash.gsub('+', '\\\+')
316 stage1_out = stage1.output.gsub('+', '\\\+')
317 stage1_id = "#{stage1.script}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
318 stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
320 assert /#{obj_id}->#{stage3_id}/.match(prov_svg)
322 assert /#{stage3_id}->#{stage1_out}/.match(prov_svg)
324 assert /#{stage1_out}->#{stage1_id}/.match(prov_svg)
328 test 'used_by graph' do
330 obj = find_fixture Collection, "graph_test_collection1"
332 used_by = obj.used_by.stringify_keys
334 used_by_svg = ProvenanceHelper::create_provenance_graph(used_by, "used_by_svg",
335 {:request => RequestDuck,
336 :direction => :top_down,
337 :combine_jobs => :script_only,
338 :pdata_only => true})
340 stage2 = find_fixture Job, "graph_stage2"
341 stage3 = find_fixture Job, "graph_stage3"
343 stage2_id = "#{stage2.script}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
344 stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
346 obj_id = obj.portable_data_hash.gsub('+', '\\\+')
347 stage3_out = stage3.output.gsub('+', '\\\+')
349 assert /#{obj_id}->#{stage2_id}/.match(used_by_svg)
351 assert /#{obj_id}->#{stage3_id}/.match(used_by_svg)
353 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)
355 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)
359 test "view collection with empty properties" do
360 fixture_name = :collection_with_empty_properties
361 show_collection(fixture_name, :active)
362 assert_equal(api_fixture('collections')[fixture_name.to_s]['name'], assigns(:object).name)
363 assert_not_nil(assigns(:object).properties)
364 assert_empty(assigns(:object).properties)
367 test "view collection with one property" do
368 fixture_name = :collection_with_one_property
369 show_collection(fixture_name, :active)
370 fixture = api_fixture('collections')[fixture_name.to_s]
371 assert_equal(fixture['name'], assigns(:object).name)
372 assert_equal(fixture['properties'][0], assigns(:object).properties[0])
375 test "create collection with properties" do
378 name: 'collection created with properties',
381 property_1: 'value_1'
385 }, session_for(:active)
386 assert_response :success
387 assert_not_nil assigns(:object).uuid
388 assert_equal 'collection created with properties', assigns(:object).name
389 assert_equal 'value_1', assigns(:object).properties[:property_1]
392 test "update description and check manifest_text is not lost" do
393 collection = api_fixture("collections")["multilevel_collection_1"]
395 id: collection["uuid"],
397 description: 'test description update'
400 }, session_for(:active)
401 assert_response :success
402 assert_not_nil assigns(:object)
403 # Ensure the Workbench response still has the original manifest_text
404 assert_equal 'test description update', assigns(:object).description
405 assert_equal true, strip_signatures_and_compare(collection['manifest_text'], assigns(:object).manifest_text)
406 # Ensure the API server still has the original manifest_text after
407 # we called arvados.v1.collections.update
409 assert_equal true, strip_signatures_and_compare(Collection.find(collection['uuid']).manifest_text,
410 collection['manifest_text'])
414 # Since we got the initial collection from fixture, there are no signatures in manifest_text.
415 # However, after update or find, the collection retrieved will have singed manifest_text.
416 # Hence, let's compare each line after excluding signatures.
417 def strip_signatures_and_compare m1, m2
418 m1_lines = m1.split "\n"
419 m2_lines = m2.split "\n"
421 return false if m1_lines.size != m2_lines.size
423 m1_lines.each_with_index do |line, i|
425 line.split.each do |word|
426 m1_words << word.split('+A')[0]
429 m2_lines[i].split.each do |word|
430 m2_words << word.split('+A')[0]
432 return false if !m1_words.join(' ').eql?(m2_words.join(' '))
438 test "view collection and verify none of the file types listed are disabled" do
439 show_collection(:collection_with_several_supported_file_types, :active)
441 files = assigns(:object).files
442 assert_equal true, files.length>0, "Expected one or more files in collection"
444 disabled = css_select('[disabled="disabled"]').collect do |el|
447 assert_equal 0, disabled.length, "Expected no disabled files in collection viewables list"
450 test "view collection and verify file types listed are all disabled" do
451 show_collection(:collection_with_several_unsupported_file_types, :active)
453 files = assigns(:object).files.collect do |_, file, _|
456 assert_equal true, files.length>0, "Expected one or more files in collection"
458 disabled = css_select('[disabled="disabled"]').collect do |el|
459 el.attributes['title'].split[-1]
462 assert_equal files.sort, disabled.sort, "Expected to see all collection files in disabled list of files"
465 test "anonymous user accesses collection in shared project" do
466 config_anonymous true
467 collection = api_fixture('collections')['public_text_file']
468 get(:show, {id: collection['uuid']})
470 response_object = assigns(:object)
471 assert_equal collection['name'], response_object['name']
472 assert_equal collection['uuid'], response_object['uuid']
473 assert_includes @response.body, 'Hello world'
474 assert_includes @response.body, 'Content address'
475 refute_nil css_select('[href="#Advanced"]')
478 test "can view empty collection" do
479 get :show, {id: 'd41d8cd98f00b204e9800998ecf8427e+0'}, session_for(:active)
480 assert_includes @response.body, 'The following collections have this content'
483 test "collection portable data hash redirect" do
484 di = api_fixture('collections')['docker_image']
485 get :show, {id: di['portable_data_hash']}, session_for(:active)
486 assert_match /\/collections\/#{di['uuid']}/, @response.redirect_url
489 test "collection portable data hash with multiple matches" do
490 pdh = api_fixture('collections')['foo_file']['portable_data_hash']
491 get :show, {id: pdh}, session_for(:admin)
492 matches = api_fixture('collections').select {|k,v| v["portable_data_hash"] == pdh}
493 assert matches.size > 1
495 matches.each do |k,v|
496 assert_match /href="\/collections\/#{v['uuid']}">.*#{v['name']}<\/a>/, @response.body
499 assert_includes @response.body, 'The following collections have this content:'
500 assert_not_includes @response.body, 'more results are not shown'
501 assert_not_includes @response.body, 'Activity'
502 assert_not_includes @response.body, 'Sharing and permissions'
505 test "collection page renders name" do
506 collection = api_fixture('collections')['foo_file']
507 get :show, {id: collection['uuid']}, session_for(:active)
508 assert_includes @response.body, collection['name']
509 assert_match /href="#{collection['uuid']}\/foo" ><\/i> foo</, @response.body
512 test "No Upload tab on non-writable collection" do
513 get :show, {id: api_fixture('collections')['user_agreement']['uuid']}, session_for(:active)
514 assert_not_includes @response.body, '<a href="#Upload"'
517 def setup_for_keep_web cfg='https://%{uuid_or_pdh}.example', dl_cfg=false
518 Rails.configuration.keep_web_url = cfg
519 Rails.configuration.keep_web_download_url = dl_cfg
520 @controller.expects(:file_enumerator).never
523 %w(uuid portable_data_hash).each do |id_type|
524 test "Redirect to keep_web_url via #{id_type}" do
526 tok = api_fixture('api_client_authorizations')['active']['api_token']
527 id = api_fixture('collections')['w_a_z_file'][id_type]
528 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
529 assert_response :redirect
530 assert_equal "https://#{id.sub '+', '-'}.example/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
533 test "Redirect to keep_web_url via #{id_type} with reader token" do
535 tok = api_fixture('api_client_authorizations')['active']['api_token']
536 id = api_fixture('collections')['w_a_z_file'][id_type]
537 get :show_file, {uuid: id, file: "w a z", reader_token: tok}, session_for(:expired)
538 assert_response :redirect
539 assert_equal "https://#{id.sub '+', '-'}.example/t=#{tok}/_/w%20a%20z", @response.redirect_url
542 test "Redirect to keep_web_url via #{id_type} with no token" do
544 config_anonymous true
545 id = api_fixture('collections')['public_text_file'][id_type]
546 get :show_file, {uuid: id, file: "Hello World.txt"}
547 assert_response :redirect
548 assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt", @response.redirect_url
551 test "Redirect to keep_web_url via #{id_type} with disposition param" do
553 config_anonymous true
554 id = api_fixture('collections')['public_text_file'][id_type]
557 file: "Hello World.txt",
558 disposition: 'attachment',
560 assert_response :redirect
561 assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt?disposition=attachment", @response.redirect_url
564 test "Redirect to keep_web_download_url via #{id_type}" do
565 setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
566 'https://download.example/c=%{uuid_or_pdh}')
567 tok = api_fixture('api_client_authorizations')['active']['api_token']
568 id = api_fixture('collections')['w_a_z_file'][id_type]
569 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
570 assert_response :redirect
571 assert_equal "https://download.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
574 test "Redirect to keep_web_url via #{id_type} when trust_all_content enabled" do
575 Rails.configuration.trust_all_content = true
576 setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
577 'https://download.example/c=%{uuid_or_pdh}')
578 tok = api_fixture('api_client_authorizations')['active']['api_token']
579 id = api_fixture('collections')['w_a_z_file'][id_type]
580 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
581 assert_response :redirect
582 assert_equal "https://collections.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
586 [false, true].each do |anon|
587 test "No redirect to keep_web_url if collection not found, anon #{anon}" do
589 config_anonymous anon
590 id = api_fixture('collections')['w_a_z_file']['uuid']
591 get :show_file, {uuid: id, file: "w a z"}, session_for(:spectator)
595 test "Redirect download to keep_web_download_url, anon #{anon}" do
596 config_anonymous anon
597 setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
598 'https://download.example/c=%{uuid_or_pdh}')
599 tok = api_fixture('api_client_authorizations')['active']['api_token']
600 id = api_fixture('collections')['public_text_file']['uuid']
603 file: 'Hello world.txt',
604 disposition: 'attachment',
605 }, session_for(:active)
606 assert_response :redirect
607 expect_url = "https://download.example/c=#{id.sub '+', '-'}/_/Hello%20world.txt"
609 expect_url += "?api_token=#{tok}"
611 assert_equal expect_url, @response.redirect_url
615 test "Error if file is impossible to retrieve from keep_web_url" do
616 # Cannot pass a session token using a single-origin keep-web URL,
617 # cannot read this collection without a session token.
618 setup_for_keep_web 'https://collections.example/c=%{uuid_or_pdh}', false
619 id = api_fixture('collections')['w_a_z_file']['uuid']
620 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
624 [false, true].each do |trust_all_content|
625 test "Redirect preview to keep_web_download_url when preview is disabled and trust_all_content is #{trust_all_content}" do
626 Rails.configuration.trust_all_content = trust_all_content
627 setup_for_keep_web false, 'https://download.example/c=%{uuid_or_pdh}'
628 tok = api_fixture('api_client_authorizations')['active']['api_token']
629 id = api_fixture('collections')['w_a_z_file']['uuid']
630 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
631 assert_response :redirect
632 assert_equal "https://download.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url