Merge branch '11917-dont-clear-cache'
[arvados.git] / apps / workbench / test / integration / download_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'integration_helper'
6 require 'helpers/download_helper'
7
8 class DownloadTest < ActionDispatch::IntegrationTest
9   include KeepWebConfig
10
11   @@wrote_test_data = false
12
13   setup do
14     use_keep_web_config
15
16     # Make sure Capybara can download files.
17     need_selenium 'for downloading', :selenium_with_download
18     DownloadHelper.clear
19
20     # Keep data isn't populated by fixtures, so we have to write any
21     # data we expect to read.
22     if !@@wrote_test_data
23       ['foo', 'w a z', "Hello world\n"].each do |data|
24         md5 = `echo -n #{data.shellescape} | arv-put --no-progress --raw -`
25         assert_match /^#{Digest::MD5.hexdigest(data)}/, md5
26         assert $?.success?, $?
27       end
28       @@wrote_test_data = true
29     end
30   end
31
32   ['uuid', 'portable_data_hash'].each do |id_type|
33     test "preview from keep-web by #{id_type} using a reader token" do
34       uuid_or_pdh = api_fixture('collections')['foo_file'][id_type]
35       token = api_fixture('api_client_authorizations')['active_all_collections']['api_token']
36       visit "/collections/download/#{uuid_or_pdh}/#{token}/"
37       within 'ul' do
38         click_link 'foo'
39       end
40       assert_no_selector 'a'
41       assert_text 'foo'
42     end
43
44     test "preview anonymous content from keep-web by #{id_type}" do
45       Rails.configuration.anonymous_user_token =
46         api_fixture('api_client_authorizations')['anonymous']['api_token']
47       uuid_or_pdh =
48         api_fixture('collections')['public_text_file'][id_type]
49       visit "/collections/#{uuid_or_pdh}"
50       within "#collection_files" do
51         find('[title~=View]').click
52       end
53       assert_no_selector 'a'
54       assert_text 'Hello world'
55     end
56
57     test "download anonymous content from keep-web by #{id_type}" do
58       Rails.configuration.anonymous_user_token =
59         api_fixture('api_client_authorizations')['anonymous']['api_token']
60       uuid_or_pdh =
61         api_fixture('collections')['public_text_file'][id_type]
62       visit "/collections/#{uuid_or_pdh}"
63       within "#collection_files" do
64         find('[title~=Download]').click
65       end
66       wait_for_download 'Hello world.txt', "Hello world\n"
67     end
68   end
69
70   test "download from keep-web using a session token" do
71     uuid = api_fixture('collections')['w_a_z_file']['uuid']
72     token = api_fixture('api_client_authorizations')['active']['api_token']
73     visit page_with_token('active', "/collections/#{uuid}")
74     within "#collection_files" do
75       find('[title~=Download]').click
76     end
77     wait_for_download 'w a z', 'w a z', timeout: 20
78   end
79
80   def wait_for_download filename, expect_data, timeout: 3
81     data = nil
82     tries = 0
83     while tries < timeout*10 && data != expect_data
84       sleep 0.1
85       tries += 1
86       data = File.read(DownloadHelper.path.join filename) rescue nil
87     end
88     assert_equal expect_data, data
89   end
90
91   # TODO(TC): test "view pages hosted by keep-web, using session
92   # token". We might persuade selenium to send
93   # "collection-uuid.dl.example" requests to localhost by configuring
94   # our test nginx server to work as its forward proxy. Until then,
95   # we're relying on the "Redirect to keep_web_url via #{id_type}"
96   # test in CollectionsControllerTest (and keep-web's tests).
97 end