Merge branch '2770-keep-sigterm' (closes #2770)
[arvados.git] / apps / workbench / test / functional / collections_controller_test.rb
1 require 'test_helper'
2
3 class CollectionsControllerTest < ActionController::TestCase
4   def collection_params(collection_name, file_name=nil)
5     uuid = api_fixture('collections')[collection_name.to_s]['uuid']
6     params = {uuid: uuid, id: uuid}
7     params[:file] = file_name if file_name
8     params
9   end
10
11   def expected_contents(params, token)
12     unless token.is_a? String
13       token = params[:api_token] || token[:arvados_api_token]
14     end
15     [token, params[:uuid], params[:file]].join('/')
16   end
17
18   def assert_hash_includes(actual_hash, expected_hash, msg=nil)
19     expected_hash.each do |key, value|
20       assert_equal(value, actual_hash[key], msg)
21     end
22   end
23
24   def assert_no_session
25     assert_hash_includes(session, {arvados_api_token: nil},
26                          "session includes unexpected API token")
27   end
28
29   def assert_session_for_auth(client_auth)
30     api_token =
31       api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
32     assert_hash_includes(session, {arvados_api_token: api_token},
33                          "session token does not belong to #{client_auth}")
34   end
35
36   def show_collection(params, session={}, response=:success)
37     params = collection_params(params) if not params.is_a? Hash
38     session = session_for(session) if not session.is_a? Hash
39     get(:show, params, session)
40     assert_response response
41   end
42
43   # Mock the collection file reader to avoid external calls and return
44   # a predictable string.
45   CollectionsController.class_eval do
46     def file_enumerator(opts)
47       [[opts[:arvados_api_token], opts[:uuid], opts[:file]].join('/')]
48     end
49   end
50
51   test "viewing a collection" do
52     show_collection(:foo_file, :active)
53     assert_equal([['.', 'foo', 3]], assigns(:object).files)
54   end
55
56   test "viewing a collection fetches related folders" do
57     show_collection(:foo_file, :active)
58     assert_includes(assigns(:folders).map(&:uuid),
59                     api_fixture('groups')['afolder']['uuid'],
60                     "controller did not find linked folder")
61   end
62
63   test "viewing a collection fetches related permissions" do
64     show_collection(:bar_file, :active)
65     assert_includes(assigns(:permissions).map(&:uuid),
66                     api_fixture('links')['bar_file_readable_by_active']['uuid'],
67                     "controller did not find permission link")
68   end
69
70   test "viewing a collection fetches jobs that output it" do
71     show_collection(:bar_file, :active)
72     assert_includes(assigns(:output_of).map(&:uuid),
73                     api_fixture('jobs')['foobar']['uuid'],
74                     "controller did not find output job")
75   end
76
77   test "viewing a collection fetches jobs that logged it" do
78     show_collection(:baz_file, :active)
79     assert_includes(assigns(:log_of).map(&:uuid),
80                     api_fixture('jobs')['foobar']['uuid'],
81                     "controller did not find logger job")
82   end
83
84   test "viewing a collection fetches logs about it" do
85     show_collection(:foo_file, :active)
86     assert_includes(assigns(:logs).map(&:uuid),
87                     api_fixture('logs')['log4']['uuid'],
88                     "controller did not find related log")
89   end
90
91   test "viewing a collection with a reader token" do
92     params = collection_params(:foo_file)
93     params[:reader_tokens] =
94       [api_fixture('api_client_authorizations')['active']['api_token']]
95     show_collection(params)
96     assert_equal([['.', 'foo', 3]], assigns(:object).files)
97     assert_no_session
98   end
99
100   test "viewing the index with a reader token" do
101     params = {reader_tokens:
102       [api_fixture('api_client_authorizations')['spectator']['api_token']]
103     }
104     get(:index, params)
105     assert_response :success
106     assert_no_session
107     listed_collections = assigns(:collections).map { |c| c.uuid }
108     assert_includes(listed_collections,
109                     api_fixture('collections')['bar_file']['uuid'],
110                     "spectator reader token didn't list bar file")
111     refute_includes(listed_collections,
112                     api_fixture('collections')['foo_file']['uuid'],
113                     "spectator reader token listed foo file")
114   end
115
116   test "getting a file from Keep" do
117     params = collection_params(:foo_file, 'foo')
118     sess = session_for(:active)
119     get(:show_file, params, sess)
120     assert_response :success
121     assert_equal(expected_contents(params, sess), @response.body,
122                  "failed to get a correct file from Keep")
123   end
124
125   test "can't get a file from Keep without permission" do
126     params = collection_params(:foo_file, 'foo')
127     sess = session_for(:spectator)
128     get(:show_file, params, sess)
129     assert_includes([403, 404], @response.code.to_i)
130   end
131
132   test "trying to get a nonexistent file from Keep returns a 404" do
133     params = collection_params(:foo_file, 'gone')
134     sess = session_for(:admin)
135     get(:show_file, params, sess)
136     assert_response 404
137   end
138
139   test "getting a file from Keep with a good reader token" do
140     params = collection_params(:foo_file, 'foo')
141     read_token = api_fixture('api_client_authorizations')['active']['api_token']
142     params[:reader_tokens] = [read_token]
143     get(:show_file, params)
144     assert_response :success
145     assert_equal(expected_contents(params, read_token), @response.body,
146                  "failed to get a correct file from Keep using a reader token")
147     assert_not_equal(read_token, session[:arvados_api_token],
148                      "using a reader token set the session's API token")
149   end
150
151   test "trying to get from Keep with an unscoped reader token prompts login" do
152     params = collection_params(:foo_file, 'foo')
153     read_token =
154       api_fixture('api_client_authorizations')['active_noscope']['api_token']
155     params[:reader_tokens] = [read_token]
156     get(:show_file, params)
157     assert_response :redirect
158   end
159
160   test "can get a file with an unpermissioned auth but in-scope reader token" do
161     params = collection_params(:foo_file, 'foo')
162     sess = session_for(:expired)
163     read_token = api_fixture('api_client_authorizations')['active']['api_token']
164     params[:reader_tokens] = [read_token]
165     get(:show_file, params, sess)
166     assert_response :success
167     assert_equal(expected_contents(params, read_token), @response.body,
168                  "failed to get a correct file from Keep using a reader token")
169     assert_not_equal(read_token, session[:arvados_api_token],
170                      "using a reader token set the session's API token")
171   end
172 end