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
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.
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?, $?
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
35 assert_no_selector 'a'
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']
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
48 assert_no_selector 'a'
49 assert_text 'Hello world'
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']
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
61 wait_for_download 'Hello world.txt', "Hello world\n"
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
72 wait_for_download 'w a z', 'w a z', timeout: 20
75 def wait_for_download filename, expect_data, timeout: 3
78 while tries < timeout*10 && data != expect_data
81 data = File.read(DownloadHelper.path.join filename) rescue nil
83 assert_equal expect_data, data
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).