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
11 @@wrote_test_data = false
16 # Make sure Capybara can download files.
17 need_selenium 'for downloading', :selenium_with_download
20 # Keep data isn't populated by fixtures, so we have to write any
21 # data we expect to read.
23 ['foo', 'w a z', "Hello world\n"].each do |data|
24 md5 = `echo -n #{data.shellescape} | arv-put --no-progress --raw -`
25 assert_match /^#{Digest::MD5.hexdigest(data)}/, md5
26 assert $?.success?, $?
28 @@wrote_test_data = true
32 ['uuid', 'portable_data_hash'].each do |id_type|
33 test "preview from keep-web by #{id_type} using a reader token" do
34 uuid_or_pdh = api_fixture('collections')['foo_file'][id_type]
35 token = api_fixture('api_client_authorizations')['active_all_collections']['api_token']
36 visit "/collections/download/#{uuid_or_pdh}/#{token}/"
40 assert_no_selector 'a'
44 test "preview anonymous content from keep-web by #{id_type}" do
45 Rails.configuration.anonymous_user_token =
46 api_fixture('api_client_authorizations')['anonymous']['api_token']
48 api_fixture('collections')['public_text_file'][id_type]
49 visit "/collections/#{uuid_or_pdh}"
50 within "#collection_files" do
51 find('[title~=View]').click
53 assert_no_selector 'a'
54 assert_text 'Hello world'
57 test "download anonymous content from keep-web by #{id_type}" do
58 Rails.configuration.anonymous_user_token =
59 api_fixture('api_client_authorizations')['anonymous']['api_token']
61 api_fixture('collections')['public_text_file'][id_type]
62 visit "/collections/#{uuid_or_pdh}"
63 within "#collection_files" do
64 find('[title~=Download]').click
66 wait_for_download 'Hello world.txt', "Hello world\n"
70 test "download from keep-web using a session token" do
71 uuid = api_fixture('collections')['w_a_z_file']['uuid']
72 token = api_fixture('api_client_authorizations')['active']['api_token']
73 visit page_with_token('active', "/collections/#{uuid}")
74 within "#collection_files" do
75 find('[title~=Download]').click
77 wait_for_download 'w a z', 'w a z', timeout: 20
80 def wait_for_download filename, expect_data, timeout: 3
83 while tries < timeout*10 && data != expect_data
86 data = File.read(DownloadHelper.path.join filename) rescue nil
88 assert_equal expect_data, data
91 # TODO(TC): test "view pages hosted by keep-web, using session
92 # token". We might persuade selenium to send
93 # "collection-uuid.dl.example" requests to localhost by configuring
94 # our test nginx server to work as its forward proxy. Until then,
95 # we're relying on the "Redirect to keep_web_url via #{id_type}"
96 # test in CollectionsControllerTest (and keep-web's tests).