1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class CollectionsControllerTest < ActionController::TestCase
8 # These tests don't do state-changing API calls. Save some time by
9 # skipping the database reset.
10 reset_api_fixtures :after_each_test, false
11 reset_api_fixtures :after_suite, true
13 include PipelineInstancesHelper
15 NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
17 def config_anonymous enable
18 Rails.configuration.anonymous_user_token =
20 api_fixture('api_client_authorizations')['anonymous']['api_token']
27 # For the duration of the current test case, stub file download
28 # content with a randomized (but recognizable) string. Return the
29 # string, the test case can use it in assertions.
30 txt = 'the quick brown fox ' + rand(2**32).to_s
31 @controller.stubs(:file_enumerator).returns([txt])
35 def collection_params(collection_name, file_name=nil)
36 uuid = api_fixture('collections')[collection_name.to_s]['uuid']
37 params = {uuid: uuid, id: uuid}
38 params[:file] = file_name if file_name
42 def assert_hash_includes(actual_hash, expected_hash, msg=nil)
43 expected_hash.each do |key, value|
44 assert_equal(value, actual_hash[key], msg)
49 assert_hash_includes(session, {arvados_api_token: nil},
50 "session includes unexpected API token")
53 def assert_session_for_auth(client_auth)
55 api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
56 assert_hash_includes(session, {arvados_api_token: api_token},
57 "session token does not belong to #{client_auth}")
60 def show_collection(params, session={}, response=:success)
61 params = collection_params(params) if not params.is_a? Hash
62 session = session_for(session) if not session.is_a? Hash
63 get(:show, params, session)
64 assert_response response
67 test "viewing a collection" do
68 show_collection(:foo_file, :active)
69 assert_equal([['.', 'foo', 3]], assigns(:object).files)
72 test "viewing a collection with spaces in filename" do
73 show_collection(:w_a_z_file, :active)
74 assert_equal([['.', 'w a z', 5]], assigns(:object).files)
77 test "download a file with spaces in filename" do
78 collection = api_fixture('collections')['w_a_z_file']
79 fakepipe = IO.popen(['echo', '-n', 'w a z'], 'rb')
80 IO.expects(:popen).with { |cmd, mode|
81 cmd.include? "#{collection['uuid']}/w a z"
84 uuid: collection['uuid'],
86 }, session_for(:active)
87 assert_response :success
88 assert_equal 'w a z', response.body
91 test "viewing a collection fetches related projects" do
92 show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
93 assert_includes(assigns(:same_pdh).map(&:owner_uuid),
94 api_fixture('groups')['aproject']['uuid'],
95 "controller did not find linked project")
98 test "viewing a collection fetches related permissions" do
99 show_collection(:bar_file, :active)
100 assert_includes(assigns(:permissions).map(&:uuid),
101 api_fixture('links')['bar_file_readable_by_active']['uuid'],
102 "controller did not find permission link")
105 test "viewing a collection fetches jobs that output it" do
106 show_collection(:bar_file, :active)
107 assert_includes(assigns(:output_of).map(&:uuid),
108 api_fixture('jobs')['foobar']['uuid'],
109 "controller did not find output job")
112 test "viewing a collection fetches jobs that logged it" do
113 show_collection(:baz_file, :active)
114 assert_includes(assigns(:log_of).map(&:uuid),
115 api_fixture('jobs')['foobar']['uuid'],
116 "controller did not find logger job")
119 test "sharing auths available to admin" do
120 show_collection("collection_owned_by_active", "admin_trustedclient")
121 assert_not_nil assigns(:search_sharing)
124 test "sharing auths available to owner" do
125 show_collection("collection_owned_by_active", "active_trustedclient")
126 assert_not_nil assigns(:search_sharing)
129 test "sharing auths available to reader" do
130 show_collection("foo_collection_in_aproject",
131 "project_viewer_trustedclient")
132 assert_not_nil assigns(:search_sharing)
135 test "viewing collection files with a reader token" do
136 params = collection_params(:foo_file)
137 params[:reader_token] = api_fixture("api_client_authorizations",
138 "active_all_collections", "api_token")
139 get(:show_file_links, params)
140 assert_response :success
141 assert_equal([['.', 'foo', 3]], assigns(:object).files)
145 test "fetching collection file with reader token" do
146 expected = stub_file_content
147 params = collection_params(:foo_file, "foo")
148 params[:reader_token] = api_fixture("api_client_authorizations",
149 "active_all_collections", "api_token")
150 get(:show_file, params)
151 assert_response :success
152 assert_equal(expected, @response.body,
153 "failed to fetch a Collection file with a reader token")
157 test "reader token Collection links end with trailing slash" do
158 # Testing the fix for #2937.
159 session = session_for(:active_trustedclient)
160 post(:share, collection_params(:foo_file), session)
161 assert(@controller.download_link.ends_with? '/',
162 "Collection share link does not end with slash for wget")
165 test "getting a file from Keep" do
166 params = collection_params(:foo_file, 'foo')
167 sess = session_for(:active)
168 expect_content = stub_file_content
169 get(:show_file, params, sess)
170 assert_response :success
171 assert_equal(expect_content, @response.body,
172 "failed to get a correct file from Keep")
175 test 'anonymous download' do
176 config_anonymous true
177 expect_content = stub_file_content
179 uuid: api_fixture('collections')['user_agreement_in_anonymously_accessible_project']['uuid'],
180 file: 'GNU_General_Public_License,_version_3.pdf',
182 assert_response :success
183 assert_equal expect_content, response.body
186 test "can't get a file from Keep without permission" do
187 params = collection_params(:foo_file, 'foo')
188 sess = session_for(:spectator)
189 get(:show_file, params, sess)
193 test "trying to get a nonexistent file from Keep returns a 404" do
194 params = collection_params(:foo_file, 'gone')
195 sess = session_for(:admin)
196 get(:show_file, params, sess)
200 test "getting a file from Keep with a good reader token" do
201 params = collection_params(:foo_file, 'foo')
202 read_token = api_fixture('api_client_authorizations')['active']['api_token']
203 params[:reader_token] = read_token
204 expect_content = stub_file_content
205 get(:show_file, params)
206 assert_response :success
207 assert_equal(expect_content, @response.body,
208 "failed to get a correct file from Keep using a reader token")
209 assert_not_equal(read_token, session[:arvados_api_token],
210 "using a reader token set the session's API token")
213 [false, true].each do |anon|
214 test "download a file using a reader token with insufficient scope, anon #{anon}" do
215 config_anonymous anon
216 params = collection_params(:foo_file, 'foo')
217 params[:reader_token] =
218 api_fixture('api_client_authorizations')['active_noscope']['api_token']
219 get(:show_file, params)
221 # Some files can be shown without a valid token, but not this one.
224 # No files will ever be shown without a valid token. You
225 # should log in and try again.
226 assert_response :redirect
231 test "can get a file with an unpermissioned auth but in-scope reader token" do
232 params = collection_params(:foo_file, 'foo')
233 sess = session_for(:expired)
234 read_token = api_fixture('api_client_authorizations')['active']['api_token']
235 params[:reader_token] = read_token
236 expect_content = stub_file_content
237 get(:show_file, params, sess)
238 assert_response :success
239 assert_equal(expect_content, @response.body,
240 "failed to get a correct file from Keep using a reader token")
241 assert_not_equal(read_token, session[:arvados_api_token],
242 "using a reader token set the session's API token")
245 test "inactive user can retrieve user agreement" do
246 ua_collection = api_fixture('collections')['user_agreement']
247 # Here we don't test whether the agreement can be retrieved from
248 # Keep. We only test that show_file decides to send file content,
249 # so we use the file content stub.
252 uuid: ua_collection['uuid'],
253 file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
254 }, session_for(:inactive)
255 assert_nil(assigns(:unsigned_user_agreements),
256 "Did not skip check_user_agreements filter " +
257 "when showing the user agreement.")
258 assert_response :success
261 test "requesting nonexistent Collection returns 404" do
262 show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
266 test "use a reasonable read buffer even if client requests a huge range" do
268 IO.expects(:popen).returns(fakefiledata)
269 fakefiledata.expects(:read).twice.with() do |length|
270 # Fail the test if read() is called with length>1MiB:
272 ## Force the ActionController::Live thread to lose the race to
273 ## verify that @response.body.length actually waits for the
274 ## response (see below):
276 end.returns("foo\n", nil)
277 fakefiledata.expects(:close)
278 foo_file = api_fixture('collections')['foo_file']
279 @request.headers['Range'] = 'bytes=0-4294967296/*'
281 uuid: foo_file['uuid'],
282 file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
283 }, session_for(:active)
284 # Wait for the whole response to arrive before deciding whether
285 # mocks' expectations were met. Otherwise, Mocha will fail the
286 # test depending on how slowly the ActionController::Live thread
288 @response.body.length
291 test "show file in a subdirectory of a collection" do
292 params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
293 expect_content = stub_file_content
294 get(:show_file, params, session_for(:user1_with_load))
295 assert_response :success
296 assert_equal(expect_content, @response.body, "failed to get a correct file from Keep")
299 test 'provenance graph' do
302 obj = find_fixture Collection, "graph_test_collection3"
304 provenance = obj.provenance.stringify_keys
306 [obj[:portable_data_hash]].each do |k|
307 assert_not_nil provenance[k], "Expected key #{k} in provenance set"
310 prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
311 {:request => RequestDuck,
312 :direction => :bottom_up,
313 :combine_jobs => :script_only})
315 stage1 = find_fixture Job, "graph_stage1"
316 stage3 = find_fixture Job, "graph_stage3"
317 previous_job_run = find_fixture Job, "previous_job_run"
319 obj_id = obj.portable_data_hash.gsub('+', '\\\+')
320 stage1_out = stage1.output.gsub('+', '\\\+')
321 stage1_id = "#{stage1.script}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
322 stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
324 assert /#{obj_id}->#{stage3_id}/.match(prov_svg)
326 assert /#{stage3_id}->#{stage1_out}/.match(prov_svg)
328 assert /#{stage1_out}->#{stage1_id}/.match(prov_svg)
332 test 'used_by graph' do
334 obj = find_fixture Collection, "graph_test_collection1"
336 used_by = obj.used_by.stringify_keys
338 used_by_svg = ProvenanceHelper::create_provenance_graph(used_by, "used_by_svg",
339 {:request => RequestDuck,
340 :direction => :top_down,
341 :combine_jobs => :script_only,
342 :pdata_only => true})
344 stage2 = find_fixture Job, "graph_stage2"
345 stage3 = find_fixture Job, "graph_stage3"
347 stage2_id = "#{stage2.script}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
348 stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
350 obj_id = obj.portable_data_hash.gsub('+', '\\\+')
351 stage3_out = stage3.output.gsub('+', '\\\+')
353 assert /#{obj_id}->#{stage2_id}/.match(used_by_svg)
355 assert /#{obj_id}->#{stage3_id}/.match(used_by_svg)
357 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)
359 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)
363 test "view collection with empty properties" do
364 fixture_name = :collection_with_empty_properties
365 show_collection(fixture_name, :active)
366 assert_equal(api_fixture('collections')[fixture_name.to_s]['name'], assigns(:object).name)
367 assert_not_nil(assigns(:object).properties)
368 assert_empty(assigns(:object).properties)
371 test "view collection with one property" do
372 fixture_name = :collection_with_one_property
373 show_collection(fixture_name, :active)
374 fixture = api_fixture('collections')[fixture_name.to_s]
375 assert_equal(fixture['name'], assigns(:object).name)
376 assert_equal(fixture['properties'][0], assigns(:object).properties[0])
379 test "create collection with properties" do
382 name: 'collection created with properties',
385 property_1: 'value_1'
389 }, session_for(:active)
390 assert_response :success
391 assert_not_nil assigns(:object).uuid
392 assert_equal 'collection created with properties', assigns(:object).name
393 assert_equal 'value_1', assigns(:object).properties[:property_1]
396 test "update description and check manifest_text is not lost" do
397 collection = api_fixture("collections")["multilevel_collection_1"]
399 id: collection["uuid"],
401 description: 'test description update'
404 }, session_for(:active)
405 assert_response :success
406 assert_not_nil assigns(:object)
407 # Ensure the Workbench response still has the original manifest_text
408 assert_equal 'test description update', assigns(:object).description
409 assert_equal true, strip_signatures_and_compare(collection['manifest_text'], assigns(:object).manifest_text)
410 # Ensure the API server still has the original manifest_text after
411 # we called arvados.v1.collections.update
413 assert_equal true, strip_signatures_and_compare(Collection.find(collection['uuid']).manifest_text,
414 collection['manifest_text'])
418 # Since we got the initial collection from fixture, there are no signatures in manifest_text.
419 # However, after update or find, the collection retrieved will have singed manifest_text.
420 # Hence, let's compare each line after excluding signatures.
421 def strip_signatures_and_compare m1, m2
422 m1_lines = m1.split "\n"
423 m2_lines = m2.split "\n"
425 return false if m1_lines.size != m2_lines.size
427 m1_lines.each_with_index do |line, i|
429 line.split.each do |word|
430 m1_words << word.split('+A')[0]
433 m2_lines[i].split.each do |word|
434 m2_words << word.split('+A')[0]
436 return false if !m1_words.join(' ').eql?(m2_words.join(' '))
442 test "view collection and verify none of the file types listed are disabled" do
443 show_collection(:collection_with_several_supported_file_types, :active)
445 files = assigns(:object).files
446 assert_equal true, files.length>0, "Expected one or more files in collection"
448 disabled = css_select('[disabled="disabled"]').collect do |el|
451 assert_equal 0, disabled.length, "Expected no disabled files in collection viewables list"
454 test "view collection and verify file types listed are all disabled" do
455 show_collection(:collection_with_several_unsupported_file_types, :active)
457 files = assigns(:object).files.collect do |_, file, _|
460 assert_equal true, files.length>0, "Expected one or more files in collection"
462 disabled = css_select('[disabled="disabled"]').collect do |el|
463 el.attributes['title'].split[-1]
466 assert_equal files.sort, disabled.sort, "Expected to see all collection files in disabled list of files"
469 test "anonymous user accesses collection in shared project" do
470 config_anonymous true
471 collection = api_fixture('collections')['public_text_file']
472 get(:show, {id: collection['uuid']})
474 response_object = assigns(:object)
475 assert_equal collection['name'], response_object['name']
476 assert_equal collection['uuid'], response_object['uuid']
477 assert_includes @response.body, 'Hello world'
478 assert_includes @response.body, 'Content address'
479 refute_nil css_select('[href="#Advanced"]')
482 test "can view empty collection" do
483 get :show, {id: 'd41d8cd98f00b204e9800998ecf8427e+0'}, session_for(:active)
484 assert_includes @response.body, 'The following collections have this content'
487 test "collection portable data hash redirect" do
488 di = api_fixture('collections')['docker_image']
489 get :show, {id: di['portable_data_hash']}, session_for(:active)
490 assert_match /\/collections\/#{di['uuid']}/, @response.redirect_url
493 test "collection portable data hash with multiple matches" do
494 pdh = api_fixture('collections')['foo_file']['portable_data_hash']
495 get :show, {id: pdh}, session_for(:admin)
496 matches = api_fixture('collections').select {|k,v| v["portable_data_hash"] == pdh}
497 assert matches.size > 1
499 matches.each do |k,v|
500 assert_match /href="\/collections\/#{v['uuid']}">.*#{v['name']}<\/a>/, @response.body
503 assert_includes @response.body, 'The following collections have this content:'
504 assert_not_includes @response.body, 'more results are not shown'
505 assert_not_includes @response.body, 'Activity'
506 assert_not_includes @response.body, 'Sharing and permissions'
509 test "collection page renders name" do
510 collection = api_fixture('collections')['foo_file']
511 get :show, {id: collection['uuid']}, session_for(:active)
512 assert_includes @response.body, collection['name']
513 assert_match /not authorized to manage collection sharing links/, @response.body
516 test "No Upload tab on non-writable collection" do
517 get :show, {id: api_fixture('collections')['user_agreement']['uuid']}, session_for(:active)
518 assert_not_includes @response.body, '<a href="#Upload"'
521 def setup_for_keep_web cfg='https://%{uuid_or_pdh}.example', dl_cfg=false
522 Rails.configuration.keep_web_url = cfg
523 Rails.configuration.keep_web_download_url = dl_cfg
524 @controller.expects(:file_enumerator).never
527 %w(uuid portable_data_hash).each do |id_type|
528 test "Redirect to keep_web_url via #{id_type}" do
530 tok = api_fixture('api_client_authorizations')['active']['api_token']
531 id = api_fixture('collections')['w_a_z_file'][id_type]
532 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
533 assert_response :redirect
534 assert_equal "https://#{id.sub '+', '-'}.example/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
537 test "Redirect to keep_web_url via #{id_type} with reader token" do
539 tok = api_fixture('api_client_authorizations')['active']['api_token']
540 id = api_fixture('collections')['w_a_z_file'][id_type]
541 get :show_file, {uuid: id, file: "w a z", reader_token: tok}, session_for(:expired)
542 assert_response :redirect
543 assert_equal "https://#{id.sub '+', '-'}.example/t=#{tok}/_/w%20a%20z", @response.redirect_url
546 test "Redirect to keep_web_url via #{id_type} with no token" do
548 config_anonymous true
549 id = api_fixture('collections')['public_text_file'][id_type]
550 get :show_file, {uuid: id, file: "Hello World.txt"}
551 assert_response :redirect
552 assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt", @response.redirect_url
555 test "Redirect to keep_web_url via #{id_type} with disposition param" do
557 config_anonymous true
558 id = api_fixture('collections')['public_text_file'][id_type]
561 file: "Hello World.txt",
562 disposition: 'attachment',
564 assert_response :redirect
565 assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt?disposition=attachment", @response.redirect_url
568 test "Redirect to keep_web_download_url via #{id_type}" do
569 setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
570 'https://download.example/c=%{uuid_or_pdh}')
571 tok = api_fixture('api_client_authorizations')['active']['api_token']
572 id = api_fixture('collections')['w_a_z_file'][id_type]
573 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
574 assert_response :redirect
575 assert_equal "https://download.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
578 test "Redirect to keep_web_url via #{id_type} when trust_all_content enabled" do
579 Rails.configuration.trust_all_content = true
580 setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
581 'https://download.example/c=%{uuid_or_pdh}')
582 tok = api_fixture('api_client_authorizations')['active']['api_token']
583 id = api_fixture('collections')['w_a_z_file'][id_type]
584 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
585 assert_response :redirect
586 assert_equal "https://collections.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
590 [false, true].each do |anon|
591 test "No redirect to keep_web_url if collection not found, anon #{anon}" do
593 config_anonymous anon
594 id = api_fixture('collections')['w_a_z_file']['uuid']
595 get :show_file, {uuid: id, file: "w a z"}, session_for(:spectator)
599 test "Redirect download to keep_web_download_url, anon #{anon}" do
600 config_anonymous anon
601 setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
602 'https://download.example/c=%{uuid_or_pdh}')
603 tok = api_fixture('api_client_authorizations')['active']['api_token']
604 id = api_fixture('collections')['public_text_file']['uuid']
607 file: 'Hello world.txt',
608 disposition: 'attachment',
609 }, session_for(:active)
610 assert_response :redirect
611 expect_url = "https://download.example/c=#{id.sub '+', '-'}/_/Hello%20world.txt"
613 expect_url += "?api_token=#{tok}"
615 assert_equal expect_url, @response.redirect_url
619 test "Error if file is impossible to retrieve from keep_web_url" do
620 # Cannot pass a session token using a single-origin keep-web URL,
621 # cannot read this collection without a session token.
622 setup_for_keep_web 'https://collections.example/c=%{uuid_or_pdh}', false
623 id = api_fixture('collections')['w_a_z_file']['uuid']
624 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
628 [false, true].each do |trust_all_content|
629 test "Redirect preview to keep_web_download_url when preview is disabled and trust_all_content is #{trust_all_content}" do
630 Rails.configuration.trust_all_content = trust_all_content
631 setup_for_keep_web false, 'https://download.example/c=%{uuid_or_pdh}'
632 tok = api_fixture('api_client_authorizations')['active']['api_token']
633 id = api_fixture('collections')['w_a_z_file']['uuid']
634 get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
635 assert_response :redirect
636 assert_equal "https://download.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
640 test "remove selected files from collection" do
643 # create a new collection to test; using existing collections will cause other tests to fail,
644 # and resetting fixtures after each test makes it take almost 4 times to run this test file.
645 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n"
647 collection = Collection.create(manifest_text: manifest_text)
648 assert_includes(collection['manifest_text'], "0:0:file1")
650 # now remove all files named 'file1' from the collection
651 post :remove_selected_files, {
652 id: collection['uuid'],
653 selection: ["#{collection['uuid']}/file1",
654 "#{collection['uuid']}/dir1/file1"],
656 }, session_for(:active)
657 assert_response :success
659 # verify no 'file1' in the updated collection
660 collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
661 assert_not_includes(collection['manifest_text'], "0:0:file1")
662 assert_includes(collection['manifest_text'], "0:0:file2") # but other files still exist
665 test "remove all files from a subdir of a collection" do
668 # create a new collection to test
669 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n"
671 collection = Collection.create(manifest_text: manifest_text)
672 assert_includes(collection['manifest_text'], "0:0:file1")
674 # now remove all files from "dir1" subdir of the collection
675 post :remove_selected_files, {
676 id: collection['uuid'],
677 selection: ["#{collection['uuid']}/dir1/file1",
678 "#{collection['uuid']}/dir1/file2"],
680 }, session_for(:active)
681 assert_response :success
683 # verify that "./dir1" no longer exists in this collection's manifest text
684 collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
685 assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1 0:0:file2\n$/, collection['manifest_text']
686 assert_not_includes(collection['manifest_text'], 'dir1')
689 test "rename file in a collection" do
692 # create a new collection to test
693 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:dir1file1 0:0:dir1file2 0:0:dir1imagefile.png\n"
695 collection = Collection.create(manifest_text: manifest_text)
696 assert_includes(collection['manifest_text'], "0:0:file1")
698 # rename 'file1' as 'file1renamed' and verify
700 id: collection['uuid'],
702 'rename-file-path:file1' => 'file1renamed'
705 }, session_for(:active)
706 assert_response :success
708 collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
709 assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1renamed 0:0:file2\n.\/dir1 d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:dir1file1 0:0:dir1file2 0:0:dir1imagefile.png\n$/, collection['manifest_text']
711 # now rename 'file2' such that it is moved into 'dir1'
714 id: collection['uuid'],
716 'rename-file-path:file2' => 'dir1/file2'
719 }, session_for(:active)
720 assert_response :success
722 collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
723 assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1renamed\n.\/dir1 d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:dir1file1 0:0:dir1file2 0:0:dir1imagefile.png 0:0:file2\n$/, collection['manifest_text']
725 # now rename 'dir1/dir1file1' such that it is moved into a new subdir
728 id: collection['uuid'],
730 'rename-file-path:dir1/dir1file1' => 'dir2/dir3/dir1file1moved'
733 }, session_for(:active)
734 assert_response :success
736 collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
737 assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1renamed\n.\/dir1 d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:dir1file2 0:0:dir1imagefile.png 0:0:file2\n.\/dir2\/dir3 d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:dir1file1moved\n$/, collection['manifest_text']
739 # now rename the image file 'dir1/dir1imagefile.png'
742 id: collection['uuid'],
744 'rename-file-path:dir1/dir1imagefile.png' => 'dir1/dir1imagefilerenamed.png'
747 }, session_for(:active)
748 assert_response :success
750 collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
751 assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1renamed\n.\/dir1 d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:dir1file2 0:0:dir1imagefilerenamed.png 0:0:file2\n.\/dir2\/dir3 d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:dir1file1moved\n$/, collection['manifest_text']
754 test "renaming file with a duplicate name in same stream not allowed" do
757 # rename 'file2' as 'file1' and expect error
759 id: 'zzzzz-4zz18-pyw8yp9g3pr7irn',
761 'rename-file-path:file2' => 'file1'
764 }, session_for(:active)
766 assert_includes json_response['errors'], 'Duplicate file path'
769 test "renaming file with a duplicate name as another stream not allowed" do
772 # rename 'file1' as 'dir1/file1' and expect error
774 id: 'zzzzz-4zz18-pyw8yp9g3pr7irn',
776 'rename-file-path:file1' => 'dir1/file1'
779 }, session_for(:active)
781 assert_includes json_response['errors'], 'Duplicate file path'
787 ].each do |user, editable|
788 test "tags tab #{editable ? 'shows' : 'does not show'} edit button to #{user}" do
792 id: api_fixture('collections')['collection_with_tags_owned_by_active']['uuid'],
796 assert_response :success
799 response.body.scan /<i[^>]+>/ do |remove_icon|
800 remove_icon.scan(/\ collection-tag-remove(.*?)\"/).each do |i,|
806 assert_equal(3, found) # two from the tags + 1 from the hidden "add tag" row
808 assert_equal(0, found)
813 test "save_tags and verify that 'other' properties are retained" do
816 collection = api_fixture('collections')['collection_with_tags_owned_by_active']
818 new_tags = {"new_tag1" => "new_tag1_value",
819 "new_tag2" => "new_tag2_value"}
822 id: collection['uuid'],
825 }, session_for(:active)
827 assert_response :success
828 assert_equal true, response.body.include?("new_tag1")
829 assert_equal true, response.body.include?("new_tag1_value")
830 assert_equal true, response.body.include?("new_tag2")
831 assert_equal true, response.body.include?("new_tag2_value")
832 assert_equal false, response.body.include?("existing tag 1")
833 assert_equal false, response.body.include?("value for existing tag 1")
835 updated_tags = Collection.find(collection['uuid']).properties
836 assert_equal true, updated_tags.keys.include?(:'new_tag1')
837 assert_equal new_tags['new_tag1'], updated_tags[:'new_tag1']
838 assert_equal true, updated_tags.keys.include?(:'new_tag2')
839 assert_equal new_tags['new_tag2'], updated_tags[:'new_tag2']
840 assert_equal false, updated_tags.keys.include?(:'existing tag 1')
841 assert_equal false, updated_tags.keys.include?(:'existing tag 2')