1 require 'integration_helper'
2 require 'helpers/download_helper'
4 class DownloadTest < ActionDispatch::IntegrationTest
6 File.read(File.expand_path("../../../../../tmp/#{service}.port", __FILE__))
10 @kwport = getport 'keep-web-ssl'
11 @kwdport = getport 'keep-web-dl-ssl'
12 Rails.configuration.keep_web_url = "https://localhost:#{@kwport}/c=%{uuid_or_pdh}"
13 Rails.configuration.keep_web_download_url = "https://localhost:#{@kwdport}/c=%{uuid_or_pdh}"
14 CollectionsController.any_instance.expects(:file_enumerator).never
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.
22 ['foo', 'w a z', "Hello world\n"].each do |data|
23 md5 = `echo -n #{data.shellescape} | arv-put --no-progress --raw -`
24 assert_match /^#{Digest::MD5.hexdigest(data)}/, md5
25 assert $?.success?, $?
29 ['uuid', 'portable_data_hash'].each do |id_type|
30 test "preview from keep-web by #{id_type} using a reader token" do
31 uuid_or_pdh = api_fixture('collections')['foo_file'][id_type]
32 token = api_fixture('api_client_authorizations')['active_all_collections']['api_token']
33 visit "/collections/download/#{uuid_or_pdh}/#{token}/"
34 within "#collection_files" do
37 assert_no_selector 'a'
41 test "preview anonymous content from keep-web by #{id_type}" do
42 Rails.configuration.anonymous_user_token =
43 api_fixture('api_client_authorizations')['anonymous']['api_token']
45 api_fixture('collections')['public_text_file'][id_type]
46 visit "/collections/#{uuid_or_pdh}"
47 within "#collection_files" do
48 find('[title~=View]').click
50 assert_no_selector 'a'
51 assert_text 'Hello world'
54 test "download anonymous content from keep-web by #{id_type}" do
55 Rails.configuration.anonymous_user_token =
56 api_fixture('api_client_authorizations')['anonymous']['api_token']
58 api_fixture('collections')['public_text_file'][id_type]
59 visit "/collections/#{uuid_or_pdh}"
60 within "#collection_files" do
61 find('[title~=Download]').click
63 wait_for_download 'Hello world.txt', "Hello world\n"
67 test "download from keep-web using a session token" do
68 uuid = api_fixture('collections')['w_a_z_file']['uuid']
69 token = api_fixture('api_client_authorizations')['active']['api_token']
70 visit page_with_token('active', "/collections/#{uuid}")
71 within "#collection_files" do
72 find('[title~=Download]').click
74 wait_for_download 'w a z', 'w a z'
77 def wait_for_download filename, expect_data
83 data = File.read(DownloadHelper.path.join filename) rescue nil
85 assert_equal expect_data, data
88 # TODO(TC): test "view pages hosted by keep-web, using session
89 # token". We might persuade selenium to send
90 # "collection-uuid.dl.example" requests to localhost by configuring
91 # our test nginx server to work as its forward proxy. Until then,
92 # we're relying on the "Redirect to keep_web_url via #{id_type}"
93 # test in CollectionsControllerTest (and keep-web's tests).