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
9 include PipelineInstancesHelper
11 NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
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])
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
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)
36 assert_hash_includes(session, {arvados_api_token: nil},
37 "session includes unexpected API token")
40 def assert_session_for_auth(client_auth)
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}")
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
54 test "viewing a collection" do
55 show_collection(:foo_file, :active)
56 assert_equal([['.', 'foo', 3]], assigns(:object).files)
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")
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")
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")
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")
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")
94 test "sharing auths available to admin" do
95 show_collection("collection_owned_by_active", "admin_trustedclient")
96 assert_not_nil assigns(:search_sharing)
99 test "sharing auths available to owner" do
100 show_collection("collection_owned_by_active", "active_trustedclient")
101 assert_not_nil assigns(:search_sharing)
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)
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)
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")
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")
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")
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)
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)
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")
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
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")
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.
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
215 test "requesting nonexistent Collection returns 404" do
216 show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
220 test "use a reasonable read buffer even if client requests a huge range" do
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:
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):
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/*'
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
242 @response.body.length
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")
253 test 'provenance graph' do
256 obj = find_fixture Collection, "graph_test_collection3"
258 provenance = obj.provenance.stringify_keys
260 [obj[:portable_data_hash]].each do |k|
261 assert_not_nil provenance[k], "Expected key #{k} in provenance set"
264 prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
265 {:request => RequestDuck,
266 :direction => :bottom_up,
267 :combine_jobs => :script_only})
269 stage1 = find_fixture Job, "graph_stage1"
270 stage3 = find_fixture Job, "graph_stage3"
271 previous_job_run = find_fixture Job, "previous_job_run"
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)}"
278 assert /#{obj_id}->#{stage3_id}/.match(prov_svg)
280 assert /#{stage3_id}->#{stage1_out}/.match(prov_svg)
282 assert /#{stage1_out}->#{stage1_id}/.match(prov_svg)
286 test 'used_by graph' do
288 obj = find_fixture Collection, "graph_test_collection1"
290 used_by = obj.used_by.stringify_keys
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})
298 stage2 = find_fixture Job, "graph_stage2"
299 stage3 = find_fixture Job, "graph_stage3"
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)}"
304 obj_id = obj.portable_data_hash.gsub('+', '\\\+')
305 stage3_out = stage3.output.gsub('+', '\\\+')
307 assert /#{obj_id}->#{stage2_id}/.match(used_by_svg)
309 assert /#{obj_id}->#{stage3_id}/.match(used_by_svg)
311 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)
313 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)