1 require 'integration_helper'
2 require 'helpers/download_helper'
4 class DownloadTest < ActionDispatch::IntegrationTest
10 # Make sure Capybara can download files.
11 need_selenium 'for downloading', :selenium_with_download
14 # Keep data isn't populated by fixtures, so we have to write any
15 # data we expect to read.
16 ['foo', 'w a z', "Hello world\n"].each do |data|
17 md5 = `echo -n #{data.shellescape} | arv-put --no-progress --raw -`
18 assert_match /^#{Digest::MD5.hexdigest(data)}/, md5
19 assert $?.success?, $?
23 ['uuid', 'portable_data_hash'].each do |id_type|
24 test "preview from keep-web by #{id_type} using a reader token" do
25 uuid_or_pdh = api_fixture('collections')['foo_file'][id_type]
26 token = api_fixture('api_client_authorizations')['active_all_collections']['api_token']
27 visit "/collections/download/#{uuid_or_pdh}/#{token}/"
28 within "#collection_files" do
31 assert_no_selector 'a'
35 test "preview anonymous content from keep-web by #{id_type}" do
36 Rails.configuration.anonymous_user_token =
37 api_fixture('api_client_authorizations')['anonymous']['api_token']
39 api_fixture('collections')['public_text_file'][id_type]
40 visit "/collections/#{uuid_or_pdh}"
41 within "#collection_files" do
42 find('[title~=View]').click
44 assert_no_selector 'a'
45 assert_text 'Hello world'
48 test "download anonymous content from keep-web by #{id_type}" do
49 Rails.configuration.anonymous_user_token =
50 api_fixture('api_client_authorizations')['anonymous']['api_token']
52 api_fixture('collections')['public_text_file'][id_type]
53 visit "/collections/#{uuid_or_pdh}"
54 within "#collection_files" do
55 find('[title~=Download]').click
57 wait_for_download 'Hello world.txt', "Hello world\n"
61 test "download from keep-web using a session token" do
62 uuid = api_fixture('collections')['w_a_z_file']['uuid']
63 token = api_fixture('api_client_authorizations')['active']['api_token']
64 visit page_with_token('active', "/collections/#{uuid}")
65 within "#collection_files" do
66 find('[title~=Download]').click
68 wait_for_download 'w a z', 'w a z'
71 def wait_for_download filename, expect_data
77 data = File.read(DownloadHelper.path.join filename) rescue nil
79 assert_equal expect_data, data
82 # TODO(TC): test "view pages hosted by keep-web, using session
83 # token". We might persuade selenium to send
84 # "collection-uuid.dl.example" requests to localhost by configuring
85 # our test nginx server to work as its forward proxy. Until then,
86 # we're relying on the "Redirect to keep_web_url via #{id_type}"
87 # test in CollectionsControllerTest (and keep-web's tests).