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