Merge branch '5030-hide-graph-until-data' closes #5030
[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 with spaces in filename" do
60     show_collection(:w_a_z_file, :active)
61     assert_equal([['.', 'w a z', 5]], assigns(:object).files)
62   end
63
64   test "download a file with spaces in filename" do
65     collection = api_fixture('collections')['w_a_z_file']
66     fakepipe = IO.popen(['echo', '-n', 'w a z'], 'rb')
67     IO.expects(:popen).with { |cmd, mode|
68       cmd.include? "#{collection['uuid']}/w a z"
69     }.returns(fakepipe)
70     get :show_file, {
71       uuid: collection['uuid'],
72       file: 'w a z'
73     }, session_for(:active)
74     assert_response :success
75     assert_equal 'w a z', response.body
76   end
77
78   test "viewing a collection fetches related projects" do
79     show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
80     assert_includes(assigns(:same_pdh).map(&:owner_uuid),
81                     api_fixture('groups')['aproject']['uuid'],
82                     "controller did not find linked project")
83   end
84
85   test "viewing a collection fetches related permissions" do
86     show_collection(:bar_file, :active)
87     assert_includes(assigns(:permissions).map(&:uuid),
88                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
89                     "controller did not find permission link")
90   end
91
92   test "viewing a collection fetches jobs that output it" do
93     show_collection(:bar_file, :active)
94     assert_includes(assigns(:output_of).map(&:uuid),
95                     api_fixture('jobs')['foobar']['uuid'],
96                     "controller did not find output job")
97   end
98
99   test "viewing a collection fetches jobs that logged it" do
100     show_collection(:baz_file, :active)
101     assert_includes(assigns(:log_of).map(&:uuid),
102                     api_fixture('jobs')['foobar']['uuid'],
103                     "controller did not find logger job")
104   end
105
106   test "viewing a collection fetches logs about it" do
107     show_collection(:foo_file, :active)
108     assert_includes(assigns(:logs).map(&:uuid),
109                     api_fixture('logs')['log4']['uuid'],
110                     "controller did not find related log")
111   end
112
113   test "sharing auths available to admin" do
114     show_collection("collection_owned_by_active", "admin_trustedclient")
115     assert_not_nil assigns(:search_sharing)
116   end
117
118   test "sharing auths available to owner" do
119     show_collection("collection_owned_by_active", "active_trustedclient")
120     assert_not_nil assigns(:search_sharing)
121   end
122
123   test "sharing auths available to reader" do
124     show_collection("foo_collection_in_aproject",
125                     "project_viewer_trustedclient")
126     assert_not_nil assigns(:search_sharing)
127   end
128
129   test "viewing collection files with a reader token" do
130     params = collection_params(:foo_file)
131     params[:reader_token] = api_fixture("api_client_authorizations",
132                                         "active_all_collections", "api_token")
133     get(:show_file_links, params)
134     assert_response :success
135     assert_equal([['.', 'foo', 3]], assigns(:object).files)
136     assert_no_session
137   end
138
139   test "fetching collection file with reader token" do
140     expected = stub_file_content
141     params = collection_params(:foo_file, "foo")
142     params[:reader_token] = api_fixture("api_client_authorizations",
143                                         "active_all_collections", "api_token")
144     get(:show_file, params)
145     assert_response :success
146     assert_equal(expected, @response.body,
147                  "failed to fetch a Collection file with a reader token")
148     assert_no_session
149   end
150
151   test "reader token Collection links end with trailing slash" do
152     # Testing the fix for #2937.
153     session = session_for(:active_trustedclient)
154     post(:share, collection_params(:foo_file), session)
155     assert(@controller.download_link.ends_with? '/',
156            "Collection share link does not end with slash for wget")
157   end
158
159   test "getting a file from Keep" do
160     params = collection_params(:foo_file, 'foo')
161     sess = session_for(:active)
162     expect_content = stub_file_content
163     get(:show_file, params, sess)
164     assert_response :success
165     assert_equal(expect_content, @response.body,
166                  "failed to get a correct file from Keep")
167   end
168
169   test "can't get a file from Keep without permission" do
170     params = collection_params(:foo_file, 'foo')
171     sess = session_for(:spectator)
172     get(:show_file, params, sess)
173     assert_response 404
174   end
175
176   test "trying to get a nonexistent file from Keep returns a 404" do
177     params = collection_params(:foo_file, 'gone')
178     sess = session_for(:admin)
179     get(:show_file, params, sess)
180     assert_response 404
181   end
182
183   test "getting a file from Keep with a good reader token" do
184     params = collection_params(:foo_file, 'foo')
185     read_token = api_fixture('api_client_authorizations')['active']['api_token']
186     params[:reader_token] = read_token
187     expect_content = stub_file_content
188     get(:show_file, params)
189     assert_response :success
190     assert_equal(expect_content, @response.body,
191                  "failed to get a correct file from Keep using a reader token")
192     assert_not_equal(read_token, session[:arvados_api_token],
193                      "using a reader token set the session's API token")
194   end
195
196   [false, api_fixture('api_client_authorizations')['anonymous']['api_token']].
197     each do |anon_conf|
198     test "download a file using a reader token with insufficient scope (anon_conf=#{!!anon_conf})" do
199       Rails.configuration.anonymous_user_token = anon_conf
200       params = collection_params(:foo_file, 'foo')
201       params[:reader_token] =
202         api_fixture('api_client_authorizations')['active_noscope']['api_token']
203       get(:show_file, params)
204       if anon_conf
205         # Some files can be shown without a valid token, but not this one.
206         assert_response 404
207       else
208         # No files will ever be shown without a valid token. You
209         # should log in and try again.
210         assert_response :redirect
211       end
212     end
213   end
214
215   test "can get a file with an unpermissioned auth but in-scope reader token" do
216     params = collection_params(:foo_file, 'foo')
217     sess = session_for(:expired)
218     read_token = api_fixture('api_client_authorizations')['active']['api_token']
219     params[:reader_token] = read_token
220     expect_content = stub_file_content
221     get(:show_file, params, sess)
222     assert_response :success
223     assert_equal(expect_content, @response.body,
224                  "failed to get a correct file from Keep using a reader token")
225     assert_not_equal(read_token, session[:arvados_api_token],
226                      "using a reader token set the session's API token")
227   end
228
229   test "inactive user can retrieve user agreement" do
230     ua_collection = api_fixture('collections')['user_agreement']
231     # Here we don't test whether the agreement can be retrieved from
232     # Keep. We only test that show_file decides to send file content,
233     # so we use the file content stub.
234     stub_file_content
235     get :show_file, {
236       uuid: ua_collection['uuid'],
237       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
238     }, session_for(:inactive)
239     assert_nil(assigns(:unsigned_user_agreements),
240                "Did not skip check_user_agreements filter " +
241                "when showing the user agreement.")
242     assert_response :success
243   end
244
245   test "requesting nonexistent Collection returns 404" do
246     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
247                     :active, 404)
248   end
249
250   test "use a reasonable read buffer even if client requests a huge range" do
251     fakefiledata = mock
252     IO.expects(:popen).returns(fakefiledata)
253     fakefiledata.expects(:read).twice.with() do |length|
254       # Fail the test if read() is called with length>1MiB:
255       length < 2**20
256       ## Force the ActionController::Live thread to lose the race to
257       ## verify that @response.body.length actually waits for the
258       ## response (see below):
259       # sleep 3
260     end.returns("foo\n", nil)
261     fakefiledata.expects(:close)
262     foo_file = api_fixture('collections')['foo_file']
263     @request.headers['Range'] = 'bytes=0-4294967296/*'
264     get :show_file, {
265       uuid: foo_file['uuid'],
266       file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
267     }, session_for(:active)
268     # Wait for the whole response to arrive before deciding whether
269     # mocks' expectations were met. Otherwise, Mocha will fail the
270     # test depending on how slowly the ActionController::Live thread
271     # runs.
272     @response.body.length
273   end
274
275   test "show file in a subdirectory of a collection" do
276     params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
277     expect_content = stub_file_content
278     get(:show_file, params, session_for(:user1_with_load))
279     assert_response :success
280     assert_equal(expect_content, @response.body, "failed to get a correct file from Keep")
281   end
282
283   test 'provenance graph' do
284     use_token 'admin'
285
286     obj = find_fixture Collection, "graph_test_collection3"
287
288     provenance = obj.provenance.stringify_keys
289
290     [obj[:portable_data_hash]].each do |k|
291       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
292     end
293
294     prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
295                                                          {:request => RequestDuck,
296                                                            :direction => :bottom_up,
297                                                            :combine_jobs => :script_only})
298
299     stage1 = find_fixture Job, "graph_stage1"
300     stage3 = find_fixture Job, "graph_stage3"
301     previous_job_run = find_fixture Job, "previous_job_run"
302
303     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
304     stage1_out = stage1.output.gsub('+', '\\\+')
305     stage1_id = "#{stage1.script}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
306     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
307
308     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(prov_svg)
309
310     assert /#{stage3_id}&#45;&gt;#{stage1_out}/.match(prov_svg)
311
312     assert /#{stage1_out}&#45;&gt;#{stage1_id}/.match(prov_svg)
313
314   end
315
316   test 'used_by graph' do
317     use_token 'admin'
318     obj = find_fixture Collection, "graph_test_collection1"
319
320     used_by = obj.used_by.stringify_keys
321
322     used_by_svg = ProvenanceHelper::create_provenance_graph(used_by, "used_by_svg",
323                                                             {:request => RequestDuck,
324                                                               :direction => :top_down,
325                                                               :combine_jobs => :script_only,
326                                                               :pdata_only => true})
327
328     stage2 = find_fixture Job, "graph_stage2"
329     stage3 = find_fixture Job, "graph_stage3"
330
331     stage2_id = "#{stage2.script}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
332     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
333
334     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
335     stage3_out = stage3.output.gsub('+', '\\\+')
336
337     assert /#{obj_id}&#45;&gt;#{stage2_id}/.match(used_by_svg)
338
339     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(used_by_svg)
340
341     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
342
343     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
344
345   end
346 end