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 "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)
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")
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")
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")
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)
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)
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")
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
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")
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.
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
199 test "requesting nonexistent Collection returns 404" do
200 show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
204 test "use a reasonable read buffer even if client requests a huge range" do
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:
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):
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/*'
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
226 @response.body.length
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")
237 test 'provenance graph' do
240 obj = find_fixture Collection, "graph_test_collection3"
242 provenance = obj.provenance.stringify_keys
244 [obj[:portable_data_hash]].each do |k|
245 assert_not_nil provenance[k], "Expected key #{k} in provenance set"
248 prov_svg = ProvenanceHelper::create_provenance_graph(provenance, "provenance_svg",
249 {:request => RequestDuck,
250 :direction => :bottom_up,
251 :combine_jobs => :script_only})
253 stage1 = find_fixture Job, "graph_stage1"
254 stage3 = find_fixture Job, "graph_stage3"
255 previous_job_run = find_fixture Job, "previous_job_run"
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)}"
262 assert /#{obj_id}->#{stage3_id}/.match(prov_svg)
264 assert /#{stage3_id}->#{stage1_out}/.match(prov_svg)
266 assert /#{stage1_out}->#{stage1_id}/.match(prov_svg)
270 test 'used_by graph' do
272 obj = find_fixture Collection, "graph_test_collection1"
274 used_by = obj.used_by.stringify_keys
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})
282 stage2 = find_fixture Job, "graph_stage2"
283 stage3 = find_fixture Job, "graph_stage3"
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)}"
288 obj_id = obj.portable_data_hash.gsub('+', '\\\+')
289 stage3_out = stage3.output.gsub('+', '\\\+')
291 assert /#{obj_id}->#{stage2_id}/.match(used_by_svg)
293 assert /#{obj_id}->#{stage3_id}/.match(used_by_svg)
295 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)
297 assert /#{stage3_id}->#{stage3_out}/.match(used_by_svg)