13822: Don't call list_sizes() in cloud client constructor.
[arvados.git] / apps / workbench / test / controllers / collections_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
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
12
13   include PipelineInstancesHelper
14
15   NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
16
17   def config_anonymous enable
18     Rails.configuration.anonymous_user_token =
19       if enable
20         api_fixture('api_client_authorizations')['anonymous']['api_token']
21       else
22         false
23       end
24   end
25
26   def collection_params(collection_name, file_name=nil)
27     uuid = api_fixture('collections')[collection_name.to_s]['uuid']
28     params = {uuid: uuid, id: uuid}
29     params[:file] = file_name if file_name
30     params
31   end
32
33   def assert_hash_includes(actual_hash, expected_hash, msg=nil)
34     expected_hash.each do |key, value|
35       assert_equal(value, actual_hash[key], msg)
36     end
37   end
38
39   def assert_no_session
40     assert_hash_includes(session, {arvados_api_token: nil},
41                          "session includes unexpected API token")
42   end
43
44   def assert_session_for_auth(client_auth)
45     api_token =
46       api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
47     assert_hash_includes(session, {arvados_api_token: api_token},
48                          "session token does not belong to #{client_auth}")
49   end
50
51   def show_collection(params, session={}, response=:success)
52     params = collection_params(params) if not params.is_a? Hash
53     session = session_for(session) if not session.is_a? Hash
54     get(:show, params, session)
55     assert_response response
56   end
57
58   test "viewing a collection" do
59     show_collection(:foo_file, :active)
60     assert_equal([['.', 'foo', 3]], assigns(:object).files)
61   end
62
63   test "viewing a collection with spaces in filename" do
64     show_collection(:w_a_z_file, :active)
65     assert_equal([['.', 'w a z', 5]], assigns(:object).files)
66   end
67
68   test "download a file with spaces in filename" do
69     setup_for_keep_web
70     collection = api_fixture('collections')['w_a_z_file']
71     get :show_file, {
72       uuid: collection['uuid'],
73       file: 'w a z'
74     }, session_for(:active)
75     assert_response :redirect
76     assert_match /w%20a%20z/, response.redirect_url
77   end
78
79   test "viewing a collection fetches related projects" do
80     show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
81     assert_includes(assigns(:same_pdh).map(&:owner_uuid),
82                     api_fixture('groups')['aproject']['uuid'],
83                     "controller did not find linked project")
84   end
85
86   test "viewing a collection fetches related permissions" do
87     show_collection(:bar_file, :active)
88     assert_includes(assigns(:permissions).map(&:uuid),
89                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
90                     "controller did not find permission link")
91   end
92
93   test "viewing a collection fetches jobs that output it" do
94     show_collection(:bar_file, :active)
95     assert_includes(assigns(:output_of).map(&:uuid),
96                     api_fixture('jobs')['foobar']['uuid'],
97                     "controller did not find output job")
98   end
99
100   test "viewing a collection fetches jobs that logged it" do
101     show_collection(:baz_file, :active)
102     assert_includes(assigns(:log_of).map(&:uuid),
103                     api_fixture('jobs')['foobar']['uuid'],
104                     "controller did not find logger job")
105   end
106
107   test "sharing auths available to admin" do
108     show_collection("collection_owned_by_active", "admin_trustedclient")
109     assert_not_nil assigns(:search_sharing)
110   end
111
112   test "sharing auths available to owner" do
113     show_collection("collection_owned_by_active", "active_trustedclient")
114     assert_not_nil assigns(:search_sharing)
115   end
116
117   test "sharing auths available to reader" do
118     show_collection("foo_collection_in_aproject",
119                     "project_viewer_trustedclient")
120     assert_not_nil assigns(:search_sharing)
121   end
122
123   test "viewing collection files with a reader token" do
124     params = collection_params(:foo_file)
125     params[:reader_token] = api_fixture("api_client_authorizations",
126                                         "active_all_collections", "api_token")
127     get(:show_file_links, params)
128     assert_response :redirect
129     assert_no_session
130   end
131
132   test "fetching collection file with reader token" do
133     setup_for_keep_web
134     params = collection_params(:foo_file, "foo")
135     params[:reader_token] = api_fixture("api_client_authorizations",
136                                         "active_all_collections", "api_token")
137     get(:show_file, params)
138     assert_response :redirect
139     assert_match /foo/, response.redirect_url
140     assert_no_session
141   end
142
143   test "reader token Collection links end with trailing slash" do
144     # Testing the fix for #2937.
145     session = session_for(:active_trustedclient)
146     post(:share, collection_params(:foo_file), session)
147     assert(@controller.download_link.ends_with? '/',
148            "Collection share link does not end with slash for wget")
149   end
150
151   test "getting a file from Keep" do
152     setup_for_keep_web
153     params = collection_params(:foo_file, 'foo')
154     sess = session_for(:active)
155     get(:show_file, params, sess)
156     assert_response :redirect
157     assert_match /foo/, response.redirect_url
158   end
159
160   test 'anonymous download' do
161     setup_for_keep_web
162     config_anonymous true
163     get :show_file, {
164       uuid: api_fixture('collections')['user_agreement_in_anonymously_accessible_project']['uuid'],
165       file: 'GNU_General_Public_License,_version_3.pdf',
166     }
167     assert_response :redirect
168     assert_match /GNU_General_Public_License/, response.redirect_url
169   end
170
171   test "can't get a file from Keep without permission" do
172     params = collection_params(:foo_file, 'foo')
173     sess = session_for(:spectator)
174     get(:show_file, params, sess)
175     assert_response 404
176   end
177
178   test "getting a file from Keep with a good reader token" do
179     setup_for_keep_web
180     params = collection_params(:foo_file, 'foo')
181     read_token = api_fixture('api_client_authorizations')['active']['api_token']
182     params[:reader_token] = read_token
183     get(:show_file, params)
184     assert_response :redirect
185     assert_match /foo/, response.redirect_url
186     assert_not_equal(read_token, session[:arvados_api_token],
187                      "using a reader token set the session's API token")
188   end
189
190   [false, true].each do |anon|
191     test "download a file using a reader token with insufficient scope, anon #{anon}" do
192       config_anonymous anon
193       params = collection_params(:foo_file, 'foo')
194       params[:reader_token] =
195         api_fixture('api_client_authorizations')['active_noscope']['api_token']
196       get(:show_file, params)
197       if anon
198         # Some files can be shown without a valid token, but not this one.
199         assert_response 404
200       else
201         # No files will ever be shown without a valid token. You
202         # should log in and try again.
203         assert_response :redirect
204       end
205     end
206   end
207
208   test "can get a file with an unpermissioned auth but in-scope reader token" do
209     setup_for_keep_web
210     params = collection_params(:foo_file, 'foo')
211     sess = session_for(:expired)
212     read_token = api_fixture('api_client_authorizations')['active']['api_token']
213     params[:reader_token] = read_token
214     get(:show_file, params, sess)
215     assert_response :redirect
216     assert_not_equal(read_token, session[:arvados_api_token],
217                      "using a reader token set the session's API token")
218   end
219
220   test "inactive user can retrieve user agreement" do
221     setup_for_keep_web
222     ua_collection = api_fixture('collections')['user_agreement']
223     # Here we don't test whether the agreement can be retrieved from
224     # Keep. We only test that show_file decides to send file content.
225     get :show_file, {
226       uuid: ua_collection['uuid'],
227       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
228     }, session_for(:inactive)
229     assert_nil(assigns(:unsigned_user_agreements),
230                "Did not skip check_user_agreements filter " +
231                "when showing the user agreement.")
232     assert_response :redirect
233   end
234
235   test "requesting nonexistent Collection returns 404" do
236     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
237                     :active, 404)
238   end
239
240   test "show file in a subdirectory of a collection" do
241     setup_for_keep_web
242     params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
243     get(:show_file, params, session_for(:user1_with_load))
244     assert_response :redirect
245     assert_match /subdir2\/subdir3\/subdir4\/file1_in_subdir4\.txt/, response.redirect_url
246   end
247
248   test 'provenance graph' do
249     use_token 'admin'
250
251     obj = find_fixture Collection, "graph_test_collection3"
252
253     provenance = obj.provenance.stringify_keys
254
255     [obj[:portable_data_hash]].each do |k|
256       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
257     end
258
259     prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
260                                                          {:request => RequestDuck,
261                                                            :direction => :bottom_up,
262                                                            :combine_jobs => :script_only})
263
264     stage1 = find_fixture Job, "graph_stage1"
265     stage3 = find_fixture Job, "graph_stage3"
266     previous_job_run = find_fixture Job, "previous_job_run"
267
268     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
269     stage1_out = stage1.output.gsub('+', '\\\+')
270     stage1_id = "#{stage1.script}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
271     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
272
273     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(prov_svg)
274
275     assert /#{stage3_id}&#45;&gt;#{stage1_out}/.match(prov_svg)
276
277     assert /#{stage1_out}&#45;&gt;#{stage1_id}/.match(prov_svg)
278
279   end
280
281   test 'used_by graph' do
282     use_token 'admin'
283     obj = find_fixture Collection, "graph_test_collection1"
284
285     used_by = obj.used_by.stringify_keys
286
287     used_by_svg = ProvenanceHelper::create_provenance_graph(used_by, "used_by_svg",
288                                                             {:request => RequestDuck,
289                                                               :direction => :top_down,
290                                                               :combine_jobs => :script_only,
291                                                               :pdata_only => true})
292
293     stage2 = find_fixture Job, "graph_stage2"
294     stage3 = find_fixture Job, "graph_stage3"
295
296     stage2_id = "#{stage2.script}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
297     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
298
299     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
300     stage3_out = stage3.output.gsub('+', '\\\+')
301
302     assert /#{obj_id}&#45;&gt;#{stage2_id}/.match(used_by_svg)
303
304     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(used_by_svg)
305
306     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
307
308     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
309
310   end
311
312   test "view collection with empty properties" do
313     fixture_name = :collection_with_empty_properties
314     show_collection(fixture_name, :active)
315     assert_equal(api_fixture('collections')[fixture_name.to_s]['name'], assigns(:object).name)
316     assert_not_nil(assigns(:object).properties)
317     assert_empty(assigns(:object).properties)
318   end
319
320   test "view collection with one property" do
321     fixture_name = :collection_with_one_property
322     show_collection(fixture_name, :active)
323     fixture = api_fixture('collections')[fixture_name.to_s]
324     assert_equal(fixture['name'], assigns(:object).name)
325     assert_equal(fixture['properties'][0], assigns(:object).properties[0])
326   end
327
328   test "create collection with properties" do
329     post :create, {
330       collection: {
331         name: 'collection created with properties',
332         manifest_text: '',
333         properties: {
334           property_1: 'value_1'
335         },
336       },
337       format: :json
338     }, session_for(:active)
339     assert_response :success
340     assert_not_nil assigns(:object).uuid
341     assert_equal 'collection created with properties', assigns(:object).name
342     assert_equal 'value_1', assigns(:object).properties[:property_1]
343   end
344
345   test "update description and check manifest_text is not lost" do
346     collection = api_fixture("collections")["multilevel_collection_1"]
347     post :update, {
348       id: collection["uuid"],
349       collection: {
350         description: 'test description update'
351       },
352       format: :json
353     }, session_for(:active)
354     assert_response :success
355     assert_not_nil assigns(:object)
356     # Ensure the Workbench response still has the original manifest_text
357     assert_equal 'test description update', assigns(:object).description
358     assert_equal true, strip_signatures_and_compare(collection['manifest_text'], assigns(:object).manifest_text)
359     # Ensure the API server still has the original manifest_text after
360     # we called arvados.v1.collections.update
361     use_token :active do
362       assert_equal true, strip_signatures_and_compare(Collection.find(collection['uuid']).manifest_text,
363                                                       collection['manifest_text'])
364     end
365   end
366
367   # Since we got the initial collection from fixture, there are no signatures in manifest_text.
368   # However, after update or find, the collection retrieved will have singed manifest_text.
369   # Hence, let's compare each line after excluding signatures.
370   def strip_signatures_and_compare m1, m2
371     m1_lines = m1.split "\n"
372     m2_lines = m2.split "\n"
373
374     return false if m1_lines.size != m2_lines.size
375
376     m1_lines.each_with_index do |line, i|
377       m1_words = []
378       line.split.each do |word|
379         m1_words << word.split('+A')[0]
380       end
381       m2_words = []
382       m2_lines[i].split.each do |word|
383         m2_words << word.split('+A')[0]
384       end
385       return false if !m1_words.join(' ').eql?(m2_words.join(' '))
386     end
387
388     return true
389   end
390
391   test "view collection and verify none of the file types listed are disabled" do
392     show_collection(:collection_with_several_supported_file_types, :active)
393
394     files = assigns(:object).files
395     assert_equal true, files.length>0, "Expected one or more files in collection"
396
397     disabled = css_select('[disabled="disabled"]').collect do |el|
398       el
399     end
400     assert_equal 0, disabled.length, "Expected no disabled files in collection viewables list"
401   end
402
403   test "view collection and verify file types listed are all disabled" do
404     show_collection(:collection_with_several_unsupported_file_types, :active)
405
406     files = assigns(:object).files.collect do |_, file, _|
407       file
408     end
409     assert_equal true, files.length>0, "Expected one or more files in collection"
410
411     disabled = css_select('[disabled="disabled"]').collect do |el|
412       el.attributes['title'].split[-1]
413     end
414
415     assert_equal files.sort, disabled.sort, "Expected to see all collection files in disabled list of files"
416   end
417
418   test "anonymous user accesses collection in shared project" do
419     config_anonymous true
420     collection = api_fixture('collections')['public_text_file']
421     get(:show, {id: collection['uuid']})
422
423     response_object = assigns(:object)
424     assert_equal collection['name'], response_object['name']
425     assert_equal collection['uuid'], response_object['uuid']
426     assert_includes @response.body, 'Hello world'
427     assert_includes @response.body, 'Content address'
428     refute_nil css_select('[href="#Advanced"]')
429   end
430
431   test "can view empty collection" do
432     get :show, {id: 'd41d8cd98f00b204e9800998ecf8427e+0'}, session_for(:active)
433     assert_includes @response.body, 'The following collections have this content'
434   end
435
436   test "collection portable data hash redirect" do
437     di = api_fixture('collections')['docker_image']
438     get :show, {id: di['portable_data_hash']}, session_for(:active)
439     assert_match /\/collections\/#{di['uuid']}/, @response.redirect_url
440   end
441
442   test "collection portable data hash with multiple matches" do
443     pdh = api_fixture('collections')['foo_file']['portable_data_hash']
444     get :show, {id: pdh}, session_for(:admin)
445     matches = api_fixture('collections').select {|k,v| v["portable_data_hash"] == pdh}
446     assert matches.size > 1
447
448     matches.each do |k,v|
449       assert_match /href="\/collections\/#{v['uuid']}">.*#{v['name']}<\/a>/, @response.body
450     end
451
452     assert_includes @response.body, 'The following collections have this content:'
453     assert_not_includes @response.body, 'more results are not shown'
454     assert_not_includes @response.body, 'Activity'
455     assert_not_includes @response.body, 'Sharing and permissions'
456   end
457
458   test "collection page renders name" do
459     collection = api_fixture('collections')['foo_file']
460     get :show, {id: collection['uuid']}, session_for(:active)
461     assert_includes @response.body, collection['name']
462     assert_match /not authorized to manage collection sharing links/, @response.body
463   end
464
465   test "No Upload tab on non-writable collection" do
466     get :show, {id: api_fixture('collections')['user_agreement']['uuid']}, session_for(:active)
467     assert_not_includes @response.body, '<a href="#Upload"'
468   end
469
470   def setup_for_keep_web cfg='https://%{uuid_or_pdh}.example', dl_cfg=false
471     Rails.configuration.keep_web_url = cfg
472     Rails.configuration.keep_web_download_url = dl_cfg
473   end
474
475   %w(uuid portable_data_hash).each do |id_type|
476     test "Redirect to keep_web_url via #{id_type}" do
477       setup_for_keep_web
478       tok = api_fixture('api_client_authorizations')['active']['api_token']
479       id = api_fixture('collections')['w_a_z_file'][id_type]
480       get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
481       assert_response :redirect
482       assert_equal "https://#{id.sub '+', '-'}.example/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
483     end
484
485     test "Redirect to keep_web_url via #{id_type} with reader token" do
486       setup_for_keep_web
487       tok = api_fixture('api_client_authorizations')['active']['api_token']
488       id = api_fixture('collections')['w_a_z_file'][id_type]
489       get :show_file, {uuid: id, file: "w a z", reader_token: tok}, session_for(:expired)
490       assert_response :redirect
491       assert_equal "https://#{id.sub '+', '-'}.example/t=#{tok}/_/w%20a%20z", @response.redirect_url
492     end
493
494     test "Redirect to keep_web_url via #{id_type} with no token" do
495       setup_for_keep_web
496       config_anonymous true
497       id = api_fixture('collections')['public_text_file'][id_type]
498       get :show_file, {uuid: id, file: "Hello World.txt"}
499       assert_response :redirect
500       assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt", @response.redirect_url
501     end
502
503     test "Redirect to keep_web_url via #{id_type} with disposition param" do
504       setup_for_keep_web
505       config_anonymous true
506       id = api_fixture('collections')['public_text_file'][id_type]
507       get :show_file, {
508         uuid: id,
509         file: "Hello World.txt",
510         disposition: 'attachment',
511       }
512       assert_response :redirect
513       assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt?disposition=attachment", @response.redirect_url
514     end
515
516     test "Redirect to keep_web_download_url via #{id_type}" do
517       setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
518                          'https://download.example/c=%{uuid_or_pdh}')
519       tok = api_fixture('api_client_authorizations')['active']['api_token']
520       id = api_fixture('collections')['w_a_z_file'][id_type]
521       get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
522       assert_response :redirect
523       assert_equal "https://download.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
524     end
525
526     test "Redirect to keep_web_url via #{id_type} when trust_all_content enabled" do
527       Rails.configuration.trust_all_content = true
528       setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
529                          'https://download.example/c=%{uuid_or_pdh}')
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://collections.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
535     end
536   end
537
538   [false, true].each do |anon|
539     test "No redirect to keep_web_url if collection not found, anon #{anon}" do
540       setup_for_keep_web
541       config_anonymous anon
542       id = api_fixture('collections')['w_a_z_file']['uuid']
543       get :show_file, {uuid: id, file: "w a z"}, session_for(:spectator)
544       assert_response 404
545     end
546
547     test "Redirect download to keep_web_download_url, anon #{anon}" do
548       config_anonymous anon
549       setup_for_keep_web('https://collections.example/c=%{uuid_or_pdh}',
550                          'https://download.example/c=%{uuid_or_pdh}')
551       tok = api_fixture('api_client_authorizations')['active']['api_token']
552       id = api_fixture('collections')['public_text_file']['uuid']
553       get :show_file, {
554         uuid: id,
555         file: 'Hello world.txt',
556         disposition: 'attachment',
557       }, session_for(:active)
558       assert_response :redirect
559       expect_url = "https://download.example/c=#{id.sub '+', '-'}/_/Hello%20world.txt"
560       if not anon
561         expect_url += "?api_token=#{tok}"
562       end
563       assert_equal expect_url, @response.redirect_url
564     end
565   end
566
567   test "Error if file is impossible to retrieve from keep_web_url" do
568     # Cannot pass a session token using a single-origin keep-web URL,
569     # cannot read this collection without a session token.
570     setup_for_keep_web 'https://collections.example/c=%{uuid_or_pdh}', false
571     id = api_fixture('collections')['w_a_z_file']['uuid']
572     get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
573     assert_response 422
574   end
575
576   [false, true].each do |trust_all_content|
577     test "Redirect preview to keep_web_download_url when preview is disabled and trust_all_content is #{trust_all_content}" do
578       Rails.configuration.trust_all_content = trust_all_content
579       setup_for_keep_web false, 'https://download.example/c=%{uuid_or_pdh}'
580       tok = api_fixture('api_client_authorizations')['active']['api_token']
581       id = api_fixture('collections')['w_a_z_file']['uuid']
582       get :show_file, {uuid: id, file: "w a z"}, session_for(:active)
583       assert_response :redirect
584       assert_equal "https://download.example/c=#{id.sub '+', '-'}/_/w%20a%20z?api_token=#{tok}", @response.redirect_url
585     end
586   end
587
588   test "remove selected files from collection" do
589     use_token :active
590
591     # create a new collection to test; using existing collections will cause other tests to fail,
592     # and resetting fixtures after each test makes it take almost 4 times to run this test file.
593     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n"
594
595     collection = Collection.create(manifest_text: manifest_text)
596     assert_includes(collection['manifest_text'], "0:0:file1")
597
598     # now remove all files named 'file1' from the collection
599     post :remove_selected_files, {
600       id: collection['uuid'],
601       selection: ["#{collection['uuid']}/file1",
602                   "#{collection['uuid']}/dir1/file1"],
603       format: :json
604     }, session_for(:active)
605     assert_response :success
606
607     # verify no 'file1' in the updated collection
608     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
609     assert_not_includes(collection['manifest_text'], "0:0:file1")
610     assert_includes(collection['manifest_text'], "0:0:file2") # but other files still exist
611   end
612
613   test "remove all files from a subdir of a collection" do
614     use_token :active
615
616     # create a new collection to test
617     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n"
618
619     collection = Collection.create(manifest_text: manifest_text)
620     assert_includes(collection['manifest_text'], "0:0:file1")
621
622     # now remove all files from "dir1" subdir of the collection
623     post :remove_selected_files, {
624       id: collection['uuid'],
625       selection: ["#{collection['uuid']}/dir1/file1",
626                   "#{collection['uuid']}/dir1/file2"],
627       format: :json
628     }, session_for(:active)
629     assert_response :success
630
631     # verify that "./dir1" no longer exists in this collection's manifest text
632     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
633     assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1 0:0:file2\n$/, collection['manifest_text']
634     assert_not_includes(collection['manifest_text'], 'dir1')
635   end
636
637   test "rename file in a collection" do
638     use_token :active
639
640     # create a new collection to test
641     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"
642
643     collection = Collection.create(manifest_text: manifest_text)
644     assert_includes(collection['manifest_text'], "0:0:file1")
645
646     # rename 'file1' as 'file1renamed' and verify
647     post :update, {
648       id: collection['uuid'],
649       collection: {
650         'rename-file-path:file1' => 'file1renamed'
651       },
652       format: :json
653     }, session_for(:active)
654     assert_response :success
655
656     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
657     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']
658
659     # now rename 'file2' such that it is moved into 'dir1'
660     @test_counter = 0
661     post :update, {
662       id: collection['uuid'],
663       collection: {
664         'rename-file-path:file2' => 'dir1/file2'
665       },
666       format: :json
667     }, session_for(:active)
668     assert_response :success
669
670     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
671     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']
672
673     # now rename 'dir1/dir1file1' such that it is moved into a new subdir
674     @test_counter = 0
675     post :update, {
676       id: collection['uuid'],
677       collection: {
678         'rename-file-path:dir1/dir1file1' => 'dir2/dir3/dir1file1moved'
679       },
680       format: :json
681     }, session_for(:active)
682     assert_response :success
683
684     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
685     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']
686
687     # now rename the image file 'dir1/dir1imagefile.png'
688     @test_counter = 0
689     post :update, {
690       id: collection['uuid'],
691       collection: {
692         'rename-file-path:dir1/dir1imagefile.png' => 'dir1/dir1imagefilerenamed.png'
693       },
694       format: :json
695     }, session_for(:active)
696     assert_response :success
697
698     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
699     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']
700   end
701
702   test "renaming file with a duplicate name in same stream not allowed" do
703     use_token :active
704
705     # rename 'file2' as 'file1' and expect error
706     post :update, {
707       id: 'zzzzz-4zz18-pyw8yp9g3pr7irn',
708       collection: {
709         'rename-file-path:file2' => 'file1'
710       },
711       format: :json
712     }, session_for(:active)
713     assert_response 422
714     assert_includes json_response['errors'], 'Duplicate file path'
715   end
716
717   test "renaming file with a duplicate name as another stream not allowed" do
718     use_token :active
719
720     # rename 'file1' as 'dir1/file1' and expect error
721     post :update, {
722       id: 'zzzzz-4zz18-pyw8yp9g3pr7irn',
723       collection: {
724         'rename-file-path:file1' => 'dir1/file1'
725       },
726       format: :json
727     }, session_for(:active)
728     assert_response 422
729     assert_includes json_response['errors'], 'Duplicate file path'
730   end
731 end