Merge branch 'master' into 7454-azure-custom-data
[arvados.git] / apps / workbench / test / integration / download_test.rb
1 require 'integration_helper'
2 require 'helpers/download_helper'
3
4 class DownloadTest < ActionDispatch::IntegrationTest
5   def getport service
6     File.read(File.expand_path("../../../../../tmp/#{service}.port", __FILE__))
7   end
8
9   setup do
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
15
16     # Make sure Capybara can download files.
17     need_selenium 'for downloading', :selenium_with_download
18     DownloadHelper.clear
19
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?, $?
26     end
27   end
28
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
35         click_link 'foo'
36       end
37       assert_no_selector 'a'
38       assert_text 'foo'
39     end
40
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']
44       uuid_or_pdh =
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
49       end
50       assert_no_selector 'a'
51       assert_text 'Hello world'
52     end
53
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']
57       uuid_or_pdh =
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
62       end
63       wait_for_download 'Hello world.txt', "Hello world\n"
64     end
65   end
66
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
73     end
74     wait_for_download 'w a z', 'w a z'
75   end
76
77   def wait_for_download filename, expect_data
78     data = nil
79     tries = 0
80     while tries < 20
81       sleep 0.1
82       tries += 1
83       data = File.read(DownloadHelper.path.join filename) rescue nil
84     end
85     assert_equal expect_data, data
86   end
87
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).
94 end