3036: Updated migration to set modified_at instead of updated_at, added modified_by.
[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 collection_params(collection_name, file_name=nil)
7     uuid = api_fixture('collections')[collection_name.to_s]['uuid']
8     params = {uuid: uuid, id: uuid}
9     params[:file] = file_name if file_name
10     params
11   end
12
13   def expected_contents(params, token)
14     unless token.is_a? String
15       token = params[:api_token] || token[:arvados_api_token]
16     end
17     [token, params[:uuid], params[:file]].join('/')
18   end
19
20   def assert_hash_includes(actual_hash, expected_hash, msg=nil)
21     expected_hash.each do |key, value|
22       assert_equal(value, actual_hash[key], msg)
23     end
24   end
25
26   def assert_no_session
27     assert_hash_includes(session, {arvados_api_token: nil},
28                          "session includes unexpected API token")
29   end
30
31   def assert_session_for_auth(client_auth)
32     api_token =
33       api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
34     assert_hash_includes(session, {arvados_api_token: api_token},
35                          "session token does not belong to #{client_auth}")
36   end
37
38   def show_collection(params, session={}, response=:success)
39     params = collection_params(params) if not params.is_a? Hash
40     session = session_for(session) if not session.is_a? Hash
41     get(:show, params, session)
42     assert_response response
43   end
44
45   # Mock the collection file reader to avoid external calls and return
46   # a predictable string.
47   CollectionsController.class_eval do
48     def file_enumerator(opts)
49       [[opts[:arvados_api_token], opts[:uuid], opts[:file]].join('/')]
50     end
51   end
52
53   test "viewing a collection" do
54     show_collection(:foo_file, :active)
55     assert_equal([['.', 'foo', 3]], assigns(:object).files)
56   end
57
58   test "viewing a collection fetches related projects" do
59     show_collection(:foo_file, :active)
60     assert_includes(assigns(:projects).map(&:uuid),
61                     api_fixture('groups')['aproject']['uuid'],
62                     "controller did not find linked project")
63   end
64
65   test "viewing a collection fetches related permissions" do
66     show_collection(:bar_file, :active)
67     assert_includes(assigns(:permissions).map(&:uuid),
68                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
69                     "controller did not find permission link")
70   end
71
72   test "viewing a collection fetches jobs that output it" do
73     show_collection(:bar_file, :active)
74     assert_includes(assigns(:output_of).map(&:uuid),
75                     api_fixture('jobs')['foobar']['uuid'],
76                     "controller did not find output job")
77   end
78
79   test "viewing a collection fetches jobs that logged it" do
80     show_collection(:baz_file, :active)
81     assert_includes(assigns(:log_of).map(&:uuid),
82                     api_fixture('jobs')['foobar']['uuid'],
83                     "controller did not find logger job")
84   end
85
86   test "viewing a collection fetches logs about it" do
87     show_collection(:foo_file, :active)
88     assert_includes(assigns(:logs).map(&:uuid),
89                     api_fixture('logs')['log4']['uuid'],
90                     "controller did not find related log")
91   end
92
93   test "viewing collection files with a reader token" do
94     params = collection_params(:foo_file)
95     params[:reader_token] =
96       api_fixture('api_client_authorizations')['active']['api_token']
97     get(:show_file_links, params)
98     assert_response :success
99     assert_equal([['.', 'foo', 3]], assigns(:object).files)
100     assert_no_session
101   end
102
103   test "reader token Collection links end with trailing slash" do
104     # Testing the fix for #2937.
105     show_collection(:foo_file, :active_trustedclient)
106     post(:share, collection_params(:foo_file))
107     assert(@controller.download_link.ends_with? '/',
108            "Collection share link does not end with slash for wget")
109   end
110
111   test "getting a file from Keep" do
112     params = collection_params(:foo_file, 'foo')
113     sess = session_for(:active)
114     get(:show_file, params, sess)
115     assert_response :success
116     assert_equal(expected_contents(params, sess), @response.body,
117                  "failed to get a correct file from Keep")
118   end
119
120   test "can't get a file from Keep without permission" do
121     params = collection_params(:foo_file, 'foo')
122     sess = session_for(:spectator)
123     get(:show_file, params, sess)
124     assert_response 404
125   end
126
127   test "trying to get a nonexistent file from Keep returns a 404" do
128     params = collection_params(:foo_file, 'gone')
129     sess = session_for(:admin)
130     get(:show_file, params, sess)
131     assert_response 404
132   end
133
134   test "getting a file from Keep with a good reader token" do
135     params = collection_params(:foo_file, 'foo')
136     read_token = api_fixture('api_client_authorizations')['active']['api_token']
137     params[:reader_token] = read_token
138     get(:show_file, params)
139     assert_response :success
140     assert_equal(expected_contents(params, read_token), @response.body,
141                  "failed to get a correct file from Keep using a reader token")
142     assert_not_equal(read_token, session[:arvados_api_token],
143                      "using a reader token set the session's API token")
144   end
145
146   test "trying to get from Keep with an unscoped reader token prompts login" do
147     params = collection_params(:foo_file, 'foo')
148     params[:reader_token] =
149       api_fixture('api_client_authorizations')['active_noscope']['api_token']
150     get(:show_file, params)
151     assert_response :redirect
152   end
153
154   test "can get a file with an unpermissioned auth but in-scope reader token" do
155     params = collection_params(:foo_file, 'foo')
156     sess = session_for(:expired)
157     read_token = api_fixture('api_client_authorizations')['active']['api_token']
158     params[:reader_token] = read_token
159     get(:show_file, params, sess)
160     assert_response :success
161     assert_equal(expected_contents(params, read_token), @response.body,
162                  "failed to get a correct file from Keep using a reader token")
163     assert_not_equal(read_token, session[:arvados_api_token],
164                      "using a reader token set the session's API token")
165   end
166
167   test "inactive user can retrieve user agreement" do
168     ua_collection = api_fixture('collections')['user_agreement']
169     get :show_file, {
170       uuid: ua_collection['uuid'],
171       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
172     }, session_for(:inactive)
173     assert_nil(assigns(:required_user_agreements),
174                "Did not skip check_user_agreements filter " +
175                "when showing the user agreement.")
176     assert_response :success
177   end
178
179   test "requesting nonexistent Collection returns 404" do
180     show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},
181                     :active, 404)
182   end
183 end