Merge branch '3781-browser-friendly-servers' refs #3781
[arvados.git] / apps / workbench / test / controllers / collections_controller_test.rb
1 require 'test_helper'
2
3 class CollectionsControllerTest < ActionController::TestCase
4   # These tests don't do state-changing API calls. Save some time by
5   # skipping the database reset.
6   reset_api_fixtures :after_each_test, false
7   reset_api_fixtures :after_suite, true
8
9   include PipelineInstancesHelper
10
11   NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
12
13   def stub_file_content
14     # For the duration of the current test case, stub file download
15     # content with a randomized (but recognizable) string. Return the
16     # string, the test case can use it in assertions.
17     txt = 'the quick brown fox ' + rand(2**32).to_s
18     @controller.stubs(:file_enumerator).returns([txt])
19     txt
20   end
21
22   def collection_params(collection_name, file_name=nil)
23     uuid = api_fixture('collections')[collection_name.to_s]['uuid']
24     params = {uuid: uuid, id: uuid}
25     params[:file] = file_name if file_name
26     params
27   end
28
29   def assert_hash_includes(actual_hash, expected_hash, msg=nil)
30     expected_hash.each do |key, value|
31       assert_equal(value, actual_hash[key], msg)
32     end
33   end
34
35   def assert_no_session
36     assert_hash_includes(session, {arvados_api_token: nil},
37                          "session includes unexpected API token")
38   end
39
40   def assert_session_for_auth(client_auth)
41     api_token =
42       api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
43     assert_hash_includes(session, {arvados_api_token: api_token},
44                          "session token does not belong to #{client_auth}")
45   end
46
47   def show_collection(params, session={}, response=:success)
48     params = collection_params(params) if not params.is_a? Hash
49     session = session_for(session) if not session.is_a? Hash
50     get(:show, params, session)
51     assert_response response
52   end
53
54   test "viewing a collection" do
55     show_collection(:foo_file, :active)
56     assert_equal([['.', 'foo', 3]], assigns(:object).files)
57   end
58
59   test "viewing a collection fetches related projects" do
60     show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
61     assert_includes(assigns(:same_pdh).map(&:owner_uuid),
62                     api_fixture('groups')['aproject']['uuid'],
63                     "controller did not find linked project")
64   end
65
66   test "viewing a collection fetches related permissions" do
67     show_collection(:bar_file, :active)
68     assert_includes(assigns(:permissions).map(&:uuid),
69                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
70                     "controller did not find permission link")
71   end
72
73   test "viewing a collection fetches jobs that output it" do
74     show_collection(:bar_file, :active)
75     assert_includes(assigns(:output_of).map(&:uuid),
76                     api_fixture('jobs')['foobar']['uuid'],
77                     "controller did not find output job")
78   end
79
80   test "viewing a collection fetches jobs that logged it" do
81     show_collection(:baz_file, :active)
82     assert_includes(assigns(:log_of).map(&:uuid),
83                     api_fixture('jobs')['foobar']['uuid'],
84                     "controller did not find logger job")
85   end
86
87   test "viewing a collection fetches logs about it" do
88     show_collection(:foo_file, :active)
89     assert_includes(assigns(:logs).map(&:uuid),
90                     api_fixture('logs')['log4']['uuid'],
91                     "controller did not find related log")
92   end
93
94   test "viewing collection files with a reader token" do
95     params = collection_params(:foo_file)
96     params[:reader_token] = api_fixture("api_client_authorizations",
97                                         "active_all_collections", "api_token")
98     get(:show_file_links, params)
99     assert_response :success
100     assert_equal([['.', 'foo', 3]], assigns(:object).files)
101     assert_no_session
102   end
103
104   test "fetching collection file with reader token" do
105     expected = stub_file_content
106     params = collection_params(:foo_file, "foo")
107     params[:reader_token] = api_fixture("api_client_authorizations",
108                                         "active_all_collections", "api_token")
109     get(:show_file, params)
110     assert_response :success
111     assert_equal(expected, @response.body,
112                  "failed to fetch a Collection file with a reader token")
113     assert_no_session
114   end
115
116   test "reader token Collection links end with trailing slash" do
117     # Testing the fix for #2937.
118     session = session_for(:active_trustedclient)
119     post(:share, collection_params(:foo_file), session)
120     assert(@controller.download_link.ends_with? '/',
121            "Collection share link does not end with slash for wget")
122   end
123
124   test "getting a file from Keep" do
125     params = collection_params(:foo_file, 'foo')
126     sess = session_for(:active)
127     expect_content = stub_file_content
128     get(:show_file, params, sess)
129     assert_response :success
130     assert_equal(expect_content, @response.body,
131                  "failed to get a correct file from Keep")
132   end
133
134   test "can't get a file from Keep without permission" do
135     params = collection_params(:foo_file, 'foo')
136     sess = session_for(:spectator)
137     get(:show_file, params, sess)
138     assert_response 404
139   end
140
141   test "trying to get a nonexistent file from Keep returns a 404" do
142     params = collection_params(:foo_file, 'gone')
143     sess = session_for(:admin)
144     get(:show_file, params, sess)
145     assert_response 404
146   end
147
148   test "getting a file from Keep with a good reader token" do
149     params = collection_params(:foo_file, 'foo')
150     read_token = api_fixture('api_client_authorizations')['active']['api_token']
151     params[:reader_token] = read_token
152     expect_content = stub_file_content
153     get(:show_file, params)
154     assert_response :success
155     assert_equal(expect_content, @response.body,
156                  "failed to get a correct file from Keep using a reader token")
157     assert_not_equal(read_token, session[:arvados_api_token],
158                      "using a reader token set the session's API token")
159   end
160
161   test "trying to get from Keep with an unscoped reader token prompts login" do
162     params = collection_params(:foo_file, 'foo')
163     params[:reader_token] =
164       api_fixture('api_client_authorizations')['active_noscope']['api_token']
165     get(:show_file, params)
166     assert_response :redirect
167   end
168
169   test "can get a file with an unpermissioned auth but in-scope reader token" do
170     params = collection_params(:foo_file, 'foo')
171     sess = session_for(:expired)
172     read_token = api_fixture('api_client_authorizations')['active']['api_token']
173     params[:reader_token] = read_token
174     expect_content = stub_file_content
175     get(:show_file, params, sess)
176     assert_response :success
177     assert_equal(expect_content, @response.body,
178                  "failed to get a correct file from Keep using a reader token")
179     assert_not_equal(read_token, session[:arvados_api_token],
180                      "using a reader token set the session's API token")
181   end
182
183   test "inactive user can retrieve user agreement" do
184     ua_collection = api_fixture('collections')['user_agreement']
185     # Here we don't test whether the agreement can be retrieved from
186     # Keep. We only test that show_file decides to send file content,
187     # so we use the file content stub.
188     stub_file_content
189     get :show_file, {
190       uuid: ua_collection['uuid'],
191       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
192     }, session_for(:inactive)
193     assert_nil(assigns(:unsigned_user_agreements),
194                "Did not skip check_user_agreements filter " +
195                "when showing the user agreement.")
196     assert_response :success
197   end
198
199   test "requesting nonexistent Collection returns 404" do
200     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
201                     :active, 404)
202   end
203
204   test "use a reasonable read buffer even if client requests a huge range" do
205     fakefiledata = mock
206     IO.expects(:popen).returns(fakefiledata)
207     fakefiledata.expects(:read).twice.with() do |length|
208       # Fail the test if read() is called with length>1MiB:
209       length < 2**20
210       ## Force the ActionController::Live thread to lose the race to
211       ## verify that @response.body.length actually waits for the
212       ## response (see below):
213       # sleep 3
214     end.returns("foo\n", nil)
215     fakefiledata.expects(:close)
216     foo_file = api_fixture('collections')['foo_file']
217     @request.headers['Range'] = 'bytes=0-4294967296/*'
218     get :show_file, {
219       uuid: foo_file['uuid'],
220       file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
221     }, session_for(:active)
222     # Wait for the whole response to arrive before deciding whether
223     # mocks' expectations were met. Otherwise, Mocha will fail the
224     # test depending on how slowly the ActionController::Live thread
225     # runs.
226     @response.body.length
227   end
228
229   test "show file in a subdirectory of a collection" do
230     params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
231     expect_content = stub_file_content
232     get(:show_file, params, session_for(:user1_with_load))
233     assert_response :success
234     assert_equal(expect_content, @response.body, "failed to get a correct file from Keep")
235   end
236
237   test 'provenance graph' do
238     use_token 'admin'
239
240     obj = find_fixture Collection, "graph_test_collection3"
241
242     provenance = obj.provenance.stringify_keys
243
244     [obj[:portable_data_hash]].each do |k|
245       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
246     end
247
248     prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
249                                                          {:request => RequestDuck,
250                                                            :direction => :bottom_up,
251                                                            :combine_jobs => :script_only})
252
253     stage1 = find_fixture Job, "graph_stage1"
254     stage3 = find_fixture Job, "graph_stage3"
255     previous_job_run = find_fixture Job, "previous_job_run"
256
257     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
258     stage1_out = stage1.output.gsub('+', '\\\+')
259     stage1_id = "#{stage1.script}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
260     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
261
262     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(prov_svg)
263
264     assert /#{stage3_id}&#45;&gt;#{stage1_out}/.match(prov_svg)
265
266     assert /#{stage1_out}&#45;&gt;#{stage1_id}/.match(prov_svg)
267
268   end
269
270   test 'used_by graph' do
271     use_token 'admin'
272     obj = find_fixture Collection, "graph_test_collection1"
273
274     used_by = obj.used_by.stringify_keys
275
276     used_by_svg = ProvenanceHelper::create_provenance_graph(used_by, "used_by_svg",
277                                                             {:request => RequestDuck,
278                                                               :direction => :top_down,
279                                                               :combine_jobs => :script_only,
280                                                               :pdata_only => true})
281
282     stage2 = find_fixture Job, "graph_stage2"
283     stage3 = find_fixture Job, "graph_stage3"
284
285     stage2_id = "#{stage2.script}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
286     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
287
288     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
289     stage3_out = stage3.output.gsub('+', '\\\+')
290
291     assert /#{obj_id}&#45;&gt;#{stage2_id}/.match(used_by_svg)
292
293     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(used_by_svg)
294
295     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
296
297     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
298
299   end
300 end