9426: save "tags" as "properties" on collection, rather than as a field in properties.
[arvados.git] / apps / workbench / test / controllers / collections_controller_test.rb
1 require 'test_helper'
2
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
8
9   include PipelineInstancesHelper
10
11   NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
12
13   def config_anonymous enable
14     Rails.configuration.anonymous_user_token =
15       if enable
16         api_fixture('api_client_authorizations')['anonymous']['api_token']
17       else
18         false
19       end
20   end
21
22   def stub_file_content
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])
28     txt
29   end
30
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
35     params
36   end
37
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)
41     end
42   end
43
44   def assert_no_session
45     assert_hash_includes(session, {arvados_api_token: nil},
46                          "session includes unexpected API token")
47   end
48
49   def assert_session_for_auth(client_auth)
50     api_token =
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}")
54   end
55
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
61   end
62
63   test "viewing a collection" do
64     show_collection(:foo_file, :active)
65     assert_equal([['.', 'foo', 3]], assigns(:object).files)
66   end
67
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)
71   end
72
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"
78     }.returns(fakepipe)
79     get :show_file, {
80       uuid: collection['uuid'],
81       file: 'w a z'
82     }, session_for(:active)
83     assert_response :success
84     assert_equal 'w a z', response.body
85   end
86
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")
92   end
93
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")
99   end
100
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")
106   end
107
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")
113   end
114
115   test "sharing auths available to admin" do
116     show_collection("collection_owned_by_active", "admin_trustedclient")
117     assert_not_nil assigns(:search_sharing)
118   end
119
120   test "sharing auths available to owner" do
121     show_collection("collection_owned_by_active", "active_trustedclient")
122     assert_not_nil assigns(:search_sharing)
123   end
124
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)
129   end
130
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)
138     assert_no_session
139   end
140
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")
150     assert_no_session
151   end
152
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")
159   end
160
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")
169   end
170
171   test 'anonymous download' do
172     config_anonymous true
173     expect_content = stub_file_content
174     get :show_file, {
175       uuid: api_fixture('collections')['user_agreement_in_anonymously_accessible_project']['uuid'],
176       file: 'GNU_General_Public_License,_version_3.pdf',
177     }
178     assert_response :success
179     assert_equal expect_content, response.body
180   end
181
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)
186     assert_response 404
187   end
188
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)
193     assert_response 404
194   end
195
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")
207   end
208
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)
216       if anon
217         # Some files can be shown without a valid token, but not this one.
218         assert_response 404
219       else
220         # No files will ever be shown without a valid token. You
221         # should log in and try again.
222         assert_response :redirect
223       end
224     end
225   end
226
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")
239   end
240
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.
246     stub_file_content
247     get :show_file, {
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
255   end
256
257   test "requesting nonexistent Collection returns 404" do
258     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
259                     :active, 404)
260   end
261
262   test "use a reasonable read buffer even if client requests a huge range" do
263     fakefiledata = mock
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:
267       length < 2**20
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):
271       # sleep 3
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/*'
276     get :show_file, {
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
283     # runs.
284     @response.body.length
285   end
286
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")
293   end
294
295   test 'provenance graph' do
296     use_token 'admin'
297
298     obj = find_fixture Collection, "graph_test_collection3"
299
300     provenance = obj.provenance.stringify_keys
301
302     [obj[:portable_data_hash]].each do |k|
303       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
304     end
305
306     prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
307                                                          {:request => RequestDuck,
308                                                            :direction => :bottom_up,
309                                                            :combine_jobs => :script_only})
310
311     stage1 = find_fixture Job, "graph_stage1"
312     stage3 = find_fixture Job, "graph_stage3"
313     previous_job_run = find_fixture Job, "previous_job_run"
314
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)}"
319
320     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(prov_svg)
321
322     assert /#{stage3_id}&#45;&gt;#{stage1_out}/.match(prov_svg)
323
324     assert /#{stage1_out}&#45;&gt;#{stage1_id}/.match(prov_svg)
325
326   end
327
328   test 'used_by graph' do
329     use_token 'admin'
330     obj = find_fixture Collection, "graph_test_collection1"
331
332     used_by = obj.used_by.stringify_keys
333
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})
339
340     stage2 = find_fixture Job, "graph_stage2"
341     stage3 = find_fixture Job, "graph_stage3"
342
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)}"
345
346     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
347     stage3_out = stage3.output.gsub('+', '\\\+')
348
349     assert /#{obj_id}&#45;&gt;#{stage2_id}/.match(used_by_svg)
350
351     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(used_by_svg)
352
353     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
354
355     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
356
357   end
358
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)
365   end
366
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])
373   end
374
375   test "create collection with properties" do
376     post :create, {
377       collection: {
378         name: 'collection created with properties',
379         manifest_text: '',
380         properties: {
381           property_1: 'value_1'
382         },
383       },
384       format: :json
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]
390   end
391
392   test "update description and check manifest_text is not lost" do
393     collection = api_fixture("collections")["multilevel_collection_1"]
394     post :update, {
395       id: collection["uuid"],
396       collection: {
397         description: 'test description update'
398       },
399       format: :json
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
408     use_token :active do
409       assert_equal true, strip_signatures_and_compare(Collection.find(collection['uuid']).manifest_text,
410                                                       collection['manifest_text'])
411     end
412   end
413
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"
420
421     return false if m1_lines.size != m2_lines.size
422
423     m1_lines.each_with_index do |line, i|
424       m1_words = []
425       line.split.each do |word|
426         m1_words << word.split('+A')[0]
427       end
428       m2_words = []
429       m2_lines[i].split.each do |word|
430         m2_words << word.split('+A')[0]
431       end
432       return false if !m1_words.join(' ').eql?(m2_words.join(' '))
433     end
434
435     return true
436   end
437
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)
440
441     files = assigns(:object).files
442     assert_equal true, files.length>0, "Expected one or more files in collection"
443
444     disabled = css_select('[disabled="disabled"]').collect do |el|
445       el
446     end
447     assert_equal 0, disabled.length, "Expected no disabled files in collection viewables list"
448   end
449
450   test "view collection and verify file types listed are all disabled" do
451     show_collection(:collection_with_several_unsupported_file_types, :active)
452
453     files = assigns(:object).files.collect do |_, file, _|
454       file
455     end
456     assert_equal true, files.length>0, "Expected one or more files in collection"
457
458     disabled = css_select('[disabled="disabled"]').collect do |el|
459       el.attributes['title'].split[-1]
460     end
461
462     assert_equal files.sort, disabled.sort, "Expected to see all collection files in disabled list of files"
463   end
464
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']})
469
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"]')
476   end
477
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'
481   end
482
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
487   end
488
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
494
495     matches.each do |k,v|
496       assert_match /href="\/collections\/#{v['uuid']}">.*#{v['name']}<\/a>/, @response.body
497     end
498
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'
503   end
504
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 /not authorized to manage collection sharing links/, @response.body
510   end
511
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"'
515   end
516
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
521   end
522
523   %w(uuid portable_data_hash).each do |id_type|
524     test "Redirect to keep_web_url via #{id_type}" do
525       setup_for_keep_web
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
531     end
532
533     test "Redirect to keep_web_url via #{id_type} with reader token" do
534       setup_for_keep_web
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
540     end
541
542     test "Redirect to keep_web_url via #{id_type} with no token" do
543       setup_for_keep_web
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
549     end
550
551     test "Redirect to keep_web_url via #{id_type} with disposition param" do
552       setup_for_keep_web
553       config_anonymous true
554       id = api_fixture('collections')['public_text_file'][id_type]
555       get :show_file, {
556         uuid: id,
557         file: "Hello World.txt",
558         disposition: 'attachment',
559       }
560       assert_response :redirect
561       assert_equal "https://#{id.sub '+', '-'}.example/_/Hello%20World.txt?disposition=attachment", @response.redirect_url
562     end
563
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
572     end
573
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
583     end
584   end
585
586   [false, true].each do |anon|
587     test "No redirect to keep_web_url if collection not found, anon #{anon}" do
588       setup_for_keep_web
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)
592       assert_response 404
593     end
594
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']
601       get :show_file, {
602         uuid: id,
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"
608       if not anon
609         expect_url += "?api_token=#{tok}"
610       end
611       assert_equal expect_url, @response.redirect_url
612     end
613   end
614
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)
621     assert_response 422
622   end
623
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
633     end
634   end
635
636   test "remove selected files from collection" do
637     use_token :active
638
639     # create a new collection to test; using existing collections will cause other tests to fail,
640     # and resetting fixtures after each test makes it take almost 4 times to run this test file.
641     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n"
642
643     collection = Collection.create(manifest_text: manifest_text)
644     assert_includes(collection['manifest_text'], "0:0:file1")
645
646     # now remove all files named 'file1' from the collection
647     post :remove_selected_files, {
648       id: collection['uuid'],
649       selection: ["#{collection['uuid']}/file1",
650                   "#{collection['uuid']}/dir1/file1"],
651       format: :json
652     }, session_for(:active)
653     assert_response :success
654
655     # verify no 'file1' in the updated collection
656     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
657     assert_not_includes(collection['manifest_text'], "0:0:file1")
658     assert_includes(collection['manifest_text'], "0:0:file2") # but other files still exist
659   end
660
661   test "remove all files from a subdir of a collection" do
662     use_token :active
663
664     # create a new collection to test
665     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n./dir1 d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1 0:0:file2\n"
666
667     collection = Collection.create(manifest_text: manifest_text)
668     assert_includes(collection['manifest_text'], "0:0:file1")
669
670     # now remove all files from "dir1" subdir of the collection
671     post :remove_selected_files, {
672       id: collection['uuid'],
673       selection: ["#{collection['uuid']}/dir1/file1",
674                   "#{collection['uuid']}/dir1/file2"],
675       format: :json
676     }, session_for(:active)
677     assert_response :success
678
679     # verify that "./dir1" no longer exists in this collection's manifest text
680     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
681     assert_match /. d41d8cd98f00b204e9800998ecf8427e\+0\+A(.*) 0:0:file1 0:0:file2\n$/, collection['manifest_text']
682     assert_not_includes(collection['manifest_text'], 'dir1')
683   end
684
685   test "rename file in a collection" do
686     use_token :active
687
688     # create a new collection to test
689     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"
690
691     collection = Collection.create(manifest_text: manifest_text)
692     assert_includes(collection['manifest_text'], "0:0:file1")
693
694     # rename 'file1' as 'file1renamed' and verify
695     post :update, {
696       id: collection['uuid'],
697       collection: {
698         'rename-file-path:file1' => 'file1renamed'
699       },
700       format: :json
701     }, session_for(:active)
702     assert_response :success
703
704     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
705     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']
706
707     # now rename 'file2' such that it is moved into 'dir1'
708     @test_counter = 0
709     post :update, {
710       id: collection['uuid'],
711       collection: {
712         'rename-file-path:file2' => 'dir1/file2'
713       },
714       format: :json
715     }, session_for(:active)
716     assert_response :success
717
718     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
719     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']
720
721     # now rename 'dir1/dir1file1' such that it is moved into a new subdir
722     @test_counter = 0
723     post :update, {
724       id: collection['uuid'],
725       collection: {
726         'rename-file-path:dir1/dir1file1' => 'dir2/dir3/dir1file1moved'
727       },
728       format: :json
729     }, session_for(:active)
730     assert_response :success
731
732     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
733     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']
734
735     # now rename the image file 'dir1/dir1imagefile.png'
736     @test_counter = 0
737     post :update, {
738       id: collection['uuid'],
739       collection: {
740         'rename-file-path:dir1/dir1imagefile.png' => 'dir1/dir1imagefilerenamed.png'
741       },
742       format: :json
743     }, session_for(:active)
744     assert_response :success
745
746     collection = Collection.select([:uuid, :manifest_text]).where(uuid: collection['uuid']).first
747     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']
748   end
749
750   test "renaming file with a duplicate name in same stream not allowed" do
751     use_token :active
752
753     # rename 'file2' as 'file1' and expect error
754     post :update, {
755       id: 'zzzzz-4zz18-pyw8yp9g3pr7irn',
756       collection: {
757         'rename-file-path:file2' => 'file1'
758       },
759       format: :json
760     }, session_for(:active)
761     assert_response 422
762     assert_includes json_response['errors'], 'Duplicate file path'
763   end
764
765   test "renaming file with a duplicate name as another stream not allowed" do
766     use_token :active
767
768     # rename 'file1' as 'dir1/file1' and expect error
769     post :update, {
770       id: 'zzzzz-4zz18-pyw8yp9g3pr7irn',
771       collection: {
772         'rename-file-path:file1' => 'dir1/file1'
773       },
774       format: :json
775     }, session_for(:active)
776     assert_response 422
777     assert_includes json_response['errors'], 'Duplicate file path'
778   end
779
780   [
781     [:active, true],
782     [:spectator, false],
783   ].each do |user, editable|
784     test "tags tab #{editable ? 'shows' : 'does not show'} edit button to #{user}" do
785       use_token user
786
787       get :tags, {
788         id: api_fixture('collections')['collection_with_tags_owned_by_active']['uuid'],
789         format: :js,
790       }, session_for(user)
791
792       assert_response :success
793
794       found = 0
795       response.body.scan /<i[^>]+>/ do |remove_icon|
796         remove_icon.scan(/\ collection-tag-remove(.*?)\"/).each do |i,|
797           found += 1
798         end
799       end
800
801       if editable
802         assert_equal(3, found)  # two from the tags + 1 from the hidden "add tag" row
803       else
804         assert_equal(0, found)
805       end
806     end
807   end
808
809   test "save_tags and verify that 'other' properties are retained" do
810     use_token :active
811
812     collection = api_fixture('collections')['collection_with_tags_owned_by_active']
813
814     new_tags = {"new_tag1" => "new_tag1_value",
815                 "new_tag2" => "new_tag2_value"}
816
817     post :save_tags, {
818       id: collection['uuid'],
819       tag_data: new_tags,
820       format: :js,
821     }, session_for(:active)
822
823     assert_response :success
824     assert_equal true, response.body.include?("new_tag1")
825     assert_equal true, response.body.include?("new_tag1_value")
826     assert_equal true, response.body.include?("new_tag2")
827     assert_equal true, response.body.include?("new_tag2_value")
828     assert_equal false, response.body.include?("existing tag 1")
829     assert_equal false, response.body.include?("value for existing tag 1")
830
831     updated_tags = Collection.find(collection['uuid']).properties
832     assert_equal true, updated_tags.keys.include?(:'new_tag1')
833     assert_equal new_tags['new_tag1'], updated_tags[:'new_tag1']
834     assert_equal true, updated_tags.keys.include?(:'new_tag2')
835     assert_equal new_tags['new_tag2'], updated_tags[:'new_tag2']
836     assert_equal false, updated_tags.keys.include?(:'existing tag 1')
837     assert_equal false, updated_tags.keys.include?(:'existing tag 2')
838   end
839 end