1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require 'integration_helper'
6 require 'helpers/download_helper'
8 class DownloadTest < ActionDispatch::IntegrationTest
9 @@wrote_test_data = false
14 # Make sure Capybara can download files.
15 need_selenium 'for downloading', :selenium_with_download
18 # Keep data isn't populated by fixtures, so we have to write any
19 # data we expect to read.
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?, $?
26 @@wrote_test_data = true
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}/"
38 assert_no_selector 'a'
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']
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
51 assert_no_selector 'a'
52 assert_text 'Hello world'
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']
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
64 wait_for_download 'Hello world.txt', "Hello world\n"
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
75 wait_for_download 'w a z', 'w a z', timeout: 20
78 def wait_for_download filename, expect_data, timeout: 3
81 while tries < timeout*10 && data != expect_data
84 data = File.read(DownloadHelper.path.join filename) rescue nil
86 assert_equal expect_data, data
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).