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