Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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   test "trying to get from Keep with an unscoped reader token prompts login" do
178     params = collection_params(:foo_file, 'foo')
179     params[:reader_token] =
180       api_fixture('api_client_authorizations')['active_noscope']['api_token']
181     get(:show_file, params)
182     assert_response :redirect
183   end
184
185   test "can get a file with an unpermissioned auth but in-scope reader token" do
186     params = collection_params(:foo_file, 'foo')
187     sess = session_for(:expired)
188     read_token = api_fixture('api_client_authorizations')['active']['api_token']
189     params[:reader_token] = read_token
190     expect_content = stub_file_content
191     get(:show_file, params, sess)
192     assert_response :success
193     assert_equal(expect_content, @response.body,
194                  "failed to get a correct file from Keep using a reader token")
195     assert_not_equal(read_token, session[:arvados_api_token],
196                      "using a reader token set the session's API token")
197   end
198
199   test "inactive user can retrieve user agreement" do
200     ua_collection = api_fixture('collections')['user_agreement']
201     # Here we don't test whether the agreement can be retrieved from
202     # Keep. We only test that show_file decides to send file content,
203     # so we use the file content stub.
204     stub_file_content
205     get :show_file, {
206       uuid: ua_collection['uuid'],
207       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
208     }, session_for(:inactive)
209     assert_nil(assigns(:unsigned_user_agreements),
210                "Did not skip check_user_agreements filter " +
211                "when showing the user agreement.")
212     assert_response :success
213   end
214
215   test "requesting nonexistent Collection returns 404" do
216     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
217                     :active, 404)
218   end
219
220   test "use a reasonable read buffer even if client requests a huge range" do
221     fakefiledata = mock
222     IO.expects(:popen).returns(fakefiledata)
223     fakefiledata.expects(:read).twice.with() do |length|
224       # Fail the test if read() is called with length>1MiB:
225       length < 2**20
226       ## Force the ActionController::Live thread to lose the race to
227       ## verify that @response.body.length actually waits for the
228       ## response (see below):
229       # sleep 3
230     end.returns("foo\n", nil)
231     fakefiledata.expects(:close)
232     foo_file = api_fixture('collections')['foo_file']
233     @request.headers['Range'] = 'bytes=0-4294967296/*'
234     get :show_file, {
235       uuid: foo_file['uuid'],
236       file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
237     }, session_for(:active)
238     # Wait for the whole response to arrive before deciding whether
239     # mocks' expectations were met. Otherwise, Mocha will fail the
240     # test depending on how slowly the ActionController::Live thread
241     # runs.
242     @response.body.length
243   end
244
245   test "show file in a subdirectory of a collection" do
246     params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
247     expect_content = stub_file_content
248     get(:show_file, params, session_for(:user1_with_load))
249     assert_response :success
250     assert_equal(expect_content, @response.body, "failed to get a correct file from Keep")
251   end
252
253   test 'provenance graph' do
254     use_token 'admin'
255
256     obj = find_fixture Collection, "graph_test_collection3"
257
258     provenance = obj.provenance.stringify_keys
259
260     [obj[:portable_data_hash]].each do |k|
261       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
262     end
263
264     prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
265                                                          {:request => RequestDuck,
266                                                            :direction => :bottom_up,
267                                                            :combine_jobs => :script_only})
268
269     stage1 = find_fixture Job, "graph_stage1"
270     stage3 = find_fixture Job, "graph_stage3"
271     previous_job_run = find_fixture Job, "previous_job_run"
272
273     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
274     stage1_out = stage1.output.gsub('+', '\\\+')
275     stage1_id = "#{stage1.script}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
276     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
277
278     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(prov_svg)
279
280     assert /#{stage3_id}&#45;&gt;#{stage1_out}/.match(prov_svg)
281
282     assert /#{stage1_out}&#45;&gt;#{stage1_id}/.match(prov_svg)
283
284   end
285
286   test 'used_by graph' do
287     use_token 'admin'
288     obj = find_fixture Collection, "graph_test_collection1"
289
290     used_by = obj.used_by.stringify_keys
291
292     used_by_svg = ProvenanceHelper::create_provenance_graph(used_by, "used_by_svg",
293                                                             {:request => RequestDuck,
294                                                               :direction => :top_down,
295                                                               :combine_jobs => :script_only,
296                                                               :pdata_only => true})
297
298     stage2 = find_fixture Job, "graph_stage2"
299     stage3 = find_fixture Job, "graph_stage3"
300
301     stage2_id = "#{stage2.script}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
302     stage3_id = "#{stage3.script}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
303
304     obj_id = obj.portable_data_hash.gsub('+', '\\\+')
305     stage3_out = stage3.output.gsub('+', '\\\+')
306
307     assert /#{obj_id}&#45;&gt;#{stage2_id}/.match(used_by_svg)
308
309     assert /#{obj_id}&#45;&gt;#{stage3_id}/.match(used_by_svg)
310
311     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
312
313     assert /#{stage3_id}&#45;&gt;#{stage3_out}/.match(used_by_svg)
314
315   end
316 end