Merge branch 'master' into 3177-collection-choose-files
[arvados.git] / apps / workbench / test / functional / collections_controller_test.rb
1 require 'test_helper'
2
3 class CollectionsControllerTest < ActionController::TestCase
4   NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
5
6   def stub_file_content
7     # For the duration of the current test case, stub file download
8     # content with a randomized (but recognizable) string. Return the
9     # string, the test case can use it in assertions.
10     txt = 'the quick brown fox ' + rand(2**32).to_s
11     @controller.stubs(:file_enumerator).returns([txt])
12     txt
13   end
14
15   def collection_params(collection_name, file_name=nil)
16     uuid = api_fixture('collections')[collection_name.to_s]['uuid']
17     params = {uuid: uuid, id: uuid}
18     params[:file] = file_name if file_name
19     params
20   end
21
22   def assert_hash_includes(actual_hash, expected_hash, msg=nil)
23     expected_hash.each do |key, value|
24       assert_equal(value, actual_hash[key], msg)
25     end
26   end
27
28   def assert_no_session
29     assert_hash_includes(session, {arvados_api_token: nil},
30                          "session includes unexpected API token")
31   end
32
33   def assert_session_for_auth(client_auth)
34     api_token =
35       api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
36     assert_hash_includes(session, {arvados_api_token: api_token},
37                          "session token does not belong to #{client_auth}")
38   end
39
40   def show_collection(params, session={}, response=:success)
41     params = collection_params(params) if not params.is_a? Hash
42     session = session_for(session) if not session.is_a? Hash
43     get(:show, params, session)
44     assert_response response
45   end
46
47   test "viewing a collection" do
48     show_collection(:foo_file, :active)
49     assert_equal([['.', 'foo', 3]], assigns(:object).files)
50   end
51
52   test "viewing a collection fetches related projects" do
53     show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
54     assert_includes(assigns(:same_pdh).map(&:owner_uuid),
55                     api_fixture('groups')['aproject']['uuid'],
56                     "controller did not find linked project")
57   end
58
59   test "viewing a collection fetches related permissions" do
60     show_collection(:bar_file, :active)
61     assert_includes(assigns(:permissions).map(&:uuid),
62                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
63                     "controller did not find permission link")
64   end
65
66   test "viewing a collection fetches jobs that output it" do
67     show_collection(:bar_file, :active)
68     assert_includes(assigns(:output_of).map(&:uuid),
69                     api_fixture('jobs')['foobar']['uuid'],
70                     "controller did not find output job")
71   end
72
73   test "viewing a collection fetches jobs that logged it" do
74     show_collection(:baz_file, :active)
75     assert_includes(assigns(:log_of).map(&:uuid),
76                     api_fixture('jobs')['foobar']['uuid'],
77                     "controller did not find logger job")
78   end
79
80   test "viewing a collection fetches logs about it" do
81     show_collection(:foo_file, :active)
82     assert_includes(assigns(:logs).map(&:uuid),
83                     api_fixture('logs')['log4']['uuid'],
84                     "controller did not find related log")
85   end
86
87   test "viewing collection files with a reader token" do
88     params = collection_params(:foo_file)
89     params[:reader_token] = api_fixture("api_client_authorizations",
90                                         "active_all_collections", "api_token")
91     get(:show_file_links, params)
92     assert_response :success
93     assert_equal([['.', 'foo', 3]], assigns(:object).files)
94     assert_no_session
95   end
96
97   test "fetching collection file with reader token" do
98     expected = stub_file_content
99     params = collection_params(:foo_file, "foo")
100     params[:reader_token] = api_fixture("api_client_authorizations",
101                                         "active_all_collections", "api_token")
102     get(:show_file, params)
103     assert_response :success
104     assert_equal(expected, @response.body,
105                  "failed to fetch a Collection file with a reader token")
106     assert_no_session
107   end
108
109   test "reader token Collection links end with trailing slash" do
110     # Testing the fix for #2937.
111     session = session_for(:active_trustedclient)
112     post(:share, collection_params(:foo_file), session)
113     assert(@controller.download_link.ends_with? '/',
114            "Collection share link does not end with slash for wget")
115   end
116
117   test "getting a file from Keep" do
118     params = collection_params(:foo_file, 'foo')
119     sess = session_for(:active)
120     expect_content = stub_file_content
121     get(:show_file, params, sess)
122     assert_response :success
123     assert_equal(expect_content, @response.body,
124                  "failed to get a correct file from Keep")
125   end
126
127   test "can't get a file from Keep without permission" do
128     params = collection_params(:foo_file, 'foo')
129     sess = session_for(:spectator)
130     get(:show_file, params, sess)
131     assert_response 404
132   end
133
134   test "trying to get a nonexistent file from Keep returns a 404" do
135     params = collection_params(:foo_file, 'gone')
136     sess = session_for(:admin)
137     get(:show_file, params, sess)
138     assert_response 404
139   end
140
141   test "getting a file from Keep with a good reader token" do
142     params = collection_params(:foo_file, 'foo')
143     read_token = api_fixture('api_client_authorizations')['active']['api_token']
144     params[:reader_token] = read_token
145     expect_content = stub_file_content
146     get(:show_file, params)
147     assert_response :success
148     assert_equal(expect_content, @response.body,
149                  "failed to get a correct file from Keep using a reader token")
150     assert_not_equal(read_token, session[:arvados_api_token],
151                      "using a reader token set the session's API token")
152   end
153
154   test "trying to get from Keep with an unscoped reader token prompts login" do
155     params = collection_params(:foo_file, 'foo')
156     params[:reader_token] =
157       api_fixture('api_client_authorizations')['active_noscope']['api_token']
158     get(:show_file, params)
159     assert_response :redirect
160   end
161
162   test "can get a file with an unpermissioned auth but in-scope reader token" do
163     params = collection_params(:foo_file, 'foo')
164     sess = session_for(:expired)
165     read_token = api_fixture('api_client_authorizations')['active']['api_token']
166     params[:reader_token] = read_token
167     expect_content = stub_file_content
168     get(:show_file, params, sess)
169     assert_response :success
170     assert_equal(expect_content, @response.body,
171                  "failed to get a correct file from Keep using a reader token")
172     assert_not_equal(read_token, session[:arvados_api_token],
173                      "using a reader token set the session's API token")
174   end
175
176   test "inactive user can retrieve user agreement" do
177     ua_collection = api_fixture('collections')['user_agreement']
178     # Here we don't test whether the agreement can be retrieved from
179     # Keep. We only test that show_file decides to send file content,
180     # so we use the file content stub.
181     stub_file_content
182     get :show_file, {
183       uuid: ua_collection['uuid'],
184       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
185     }, session_for(:inactive)
186     assert_nil(assigns(:unsigned_user_agreements),
187                "Did not skip check_user_agreements filter " +
188                "when showing the user agreement.")
189     assert_response :success
190   end
191
192   test "requesting nonexistent Collection returns 404" do
193     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
194                     :active, 404)
195   end
196
197   test "use a reasonable read buffer even if client requests a huge range" do
198     fakefiledata = mock
199     IO.expects(:popen).returns(fakefiledata)
200     fakefiledata.expects(:read).twice.with() do |length|
201       # Fail the test if read() is called with length>1MiB:
202       length < 2**20
203       ## Force the ActionController::Live thread to lose the race to
204       ## verify that @response.body.length actually waits for the
205       ## response (see below):
206       # sleep 3
207     end.returns("foo\n", nil)
208     fakefiledata.expects(:close)
209     foo_file = api_fixture('collections')['foo_file']
210     @request.headers['Range'] = 'bytes=0-4294967296/*'
211     get :show_file, {
212       uuid: foo_file['uuid'],
213       file: foo_file['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
214     }, session_for(:active)
215     # Wait for the whole response to arrive before deciding whether
216     # mocks' expectations were met. Otherwise, Mocha will fail the
217     # test depending on how slowly the ActionController::Live thread
218     # runs.
219     @response.body.length
220   end
221
222   test "show file in a subdirectory of a collection" do
223     params = collection_params(:collection_with_files_in_subdir, 'subdir2/subdir3/subdir4/file1_in_subdir4.txt')
224     expect_content = stub_file_content
225     get(:show_file, params, session_for(:user1_with_load))
226     assert_response :success
227     assert_equal(expect_content, @response.body, "failed to get a correct file from Keep")
228   end
229 end