Merge branch 'master' into 3618-column-ordering
[arvados.git] / apps / workbench / test / functional / collections_controller_test.rb
1 require 'test_helper'
2
3 class CollectionsControllerTest < ActionController::TestCase
4   NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
5
6   def stub_file_content
7     # For the duration of the current test case, stub file download
8     # content with a randomized (but recognizable) string. Return the
9     # string, the test case can use it in assertions.
10     txt = 'the quick brown fox ' + rand(2**32).to_s
11     @controller.stubs(:file_enumerator).returns([txt])
12     txt
13   end
14
15   def collection_params(collection_name, file_name=nil)
16     uuid = api_fixture('collections')[collection_name.to_s]['uuid']
17     params = {uuid: uuid, id: uuid}
18     params[:file] = file_name if file_name
19     params
20   end
21
22   def assert_hash_includes(actual_hash, expected_hash, msg=nil)
23     expected_hash.each do |key, value|
24       assert_equal(value, actual_hash[key], msg)
25     end
26   end
27
28   def assert_no_session
29     assert_hash_includes(session, {arvados_api_token: nil},
30                          "session includes unexpected API token")
31   end
32
33   def assert_session_for_auth(client_auth)
34     api_token =
35       api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
36     assert_hash_includes(session, {arvados_api_token: api_token},
37                          "session token does not belong to #{client_auth}")
38   end
39
40   def show_collection(params, session={}, response=:success)
41     params = collection_params(params) if not params.is_a? Hash
42     session = session_for(session) if not session.is_a? Hash
43     get(:show, params, session)
44     assert_response response
45   end
46
47   test "viewing a collection" do
48     show_collection(:foo_file, :active)
49     assert_equal([['.', 'foo', 3]], assigns(:object).files)
50   end
51
52   test "viewing a collection fetches related projects" do
53     show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
54     assert_includes(assigns(:same_pdh).map(&:owner_uuid),
55                     api_fixture('groups')['aproject']['uuid'],
56                     "controller did not find linked project")
57   end
58
59   test "viewing a collection fetches related permissions" do
60     show_collection(:bar_file, :active)
61     assert_includes(assigns(:permissions).map(&:uuid),
62                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
63                     "controller did not find permission link")
64   end
65
66   test "viewing a collection fetches jobs that output it" do
67     show_collection(:bar_file, :active)
68     assert_includes(assigns(:output_of).map(&:uuid),
69                     api_fixture('jobs')['foobar']['uuid'],
70                     "controller did not find output job")
71   end
72
73   test "viewing a collection fetches jobs that logged it" do
74     show_collection(:baz_file, :active)
75     assert_includes(assigns(:log_of).map(&:uuid),
76                     api_fixture('jobs')['foobar']['uuid'],
77                     "controller did not find logger job")
78   end
79
80   test "viewing a collection fetches logs about it" do
81     show_collection(:foo_file, :active)
82     assert_includes(assigns(:logs).map(&:uuid),
83                     api_fixture('logs')['log4']['uuid'],
84                     "controller did not find related log")
85   end
86
87   test "viewing collection files with a reader token" do
88     params = collection_params(:foo_file)
89     params[:reader_token] =
90       api_fixture('api_client_authorizations')['active']['api_token']
91     get(:show_file_links, params)
92     assert_response :success
93     assert_equal([['.', 'foo', 3]], assigns(:object).files)
94     assert_no_session
95   end
96
97   test "reader token Collection links end with trailing slash" do
98     # Testing the fix for #2937.
99     session = session_for(:active_trustedclient)
100     post(:share, collection_params(:foo_file), session)
101     assert(@controller.download_link.ends_with? '/',
102            "Collection share link does not end with slash for wget")
103   end
104
105   test "getting a file from Keep" do
106     params = collection_params(:foo_file, 'foo')
107     sess = session_for(:active)
108     expect_content = stub_file_content
109     get(:show_file, params, sess)
110     assert_response :success
111     assert_equal(expect_content, @response.body,
112                  "failed to get a correct file from Keep")
113   end
114
115   test "can't get a file from Keep without permission" do
116     params = collection_params(:foo_file, 'foo')
117     sess = session_for(:spectator)
118     get(:show_file, params, sess)
119     assert_response 404
120   end
121
122   test "trying to get a nonexistent file from Keep returns a 404" do
123     params = collection_params(:foo_file, 'gone')
124     sess = session_for(:admin)
125     get(:show_file, params, sess)
126     assert_response 404
127   end
128
129   test "getting a file from Keep with a good reader token" do
130     params = collection_params(:foo_file, 'foo')
131     read_token = api_fixture('api_client_authorizations')['active']['api_token']
132     params[:reader_token] = read_token
133     expect_content = stub_file_content
134     get(:show_file, params)
135     assert_response :success
136     assert_equal(expect_content, @response.body,
137                  "failed to get a correct file from Keep using a reader token")
138     assert_not_equal(read_token, session[:arvados_api_token],
139                      "using a reader token set the session's API token")
140   end
141
142   test "trying to get from Keep with an unscoped reader token prompts login" do
143     params = collection_params(:foo_file, 'foo')
144     params[:reader_token] =
145       api_fixture('api_client_authorizations')['active_noscope']['api_token']
146     get(:show_file, params)
147     assert_response :redirect
148   end
149
150   test "can get a file with an unpermissioned auth but in-scope reader token" do
151     params = collection_params(:foo_file, 'foo')
152     sess = session_for(:expired)
153     read_token = api_fixture('api_client_authorizations')['active']['api_token']
154     params[:reader_token] = read_token
155     expect_content = stub_file_content
156     get(:show_file, params, sess)
157     assert_response :success
158     assert_equal(expect_content, @response.body,
159                  "failed to get a correct file from Keep using a reader token")
160     assert_not_equal(read_token, session[:arvados_api_token],
161                      "using a reader token set the session's API token")
162   end
163
164   test "inactive user can retrieve user agreement" do
165     ua_collection = api_fixture('collections')['user_agreement']
166     # Here we don't test whether the agreement can be retrieved from
167     # Keep. We only test that show_file decides to send file content,
168     # so we use the file content stub.
169     stub_file_content
170     get :show_file, {
171       uuid: ua_collection['uuid'],
172       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
173     }, session_for(:inactive)
174     assert_nil(assigns(:unsigned_user_agreements),
175                "Did not skip check_user_agreements filter " +
176                "when showing the user agreement.")
177     assert_response :success
178   end
179
180   test "requesting nonexistent Collection returns 404" do
181     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
182                     :active, 404)
183   end
184
185   test "use a reasonable read buffer even if client requests a huge range" do
186     fakefiledata = mock
187     IO.expects(:popen).returns(fakefiledata)
188     fakefiledata.expects(:read).twice.with() do |length|
189       # Fail the test if read() is called with length>1MiB:
190       length < 2**20
191       ## Force the ActionController::Live thread to lose the race to
192       ## verify that @response.body.length actually waits for the
193       ## response (see below):
194       # sleep 3
195     end.returns("foo\n", nil)
196     fakefiledata.expects(:close)
197     foo_file = api_fixture('collections')['foo_file']
198     @request.headers['Range'] = 'bytes=0-4294967296/*'
199     get :show_file, {
200       uuid: foo_file['uuid'],
201       file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
202     }, session_for(:active)
203     # Wait for the whole response to arrive before deciding whether
204     # mocks' expectations were met. Otherwise, Mocha will fail the
205     # test depending on how slowly the ActionController::Live thread
206     # runs.
207     @response.body.length
208   end
209 end