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