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