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