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
12 # Make sure Capybara can download files.
13 need_selenium 'for downloading', :selenium_with_download
16 # Keep data isn't populated by fixtures, so we have to write any
17 # data we expect to read.
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?, $?
24 @@wrote_test_data = true
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}/"
36 assert_no_selector 'a'
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']
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
49 assert_no_selector 'a'
50 assert_text 'Hello world'
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']
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
62 wait_for_download 'Hello world.txt', "Hello world\n"
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
73 wait_for_download 'w a z', 'w a z', timeout: 20
76 def wait_for_download filename, expect_data, timeout: 3
79 while tries < timeout*10 && data != expect_data
82 data = File.read(DownloadHelper.path.join filename) rescue nil
84 assert_equal expect_data, data
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).