2872: Merge branch 'master' into 2872-folder-nav
[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 projects" do
57     show_collection(:foo_file, :active)
58     assert_includes(assigns(:projects).map(&:uuid),
59                     api_fixture('groups')['aproject']['uuid'],
60                     "controller did not find linked project")
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 collection files with a reader token" do
92     params = collection_params(:foo_file)
93     params[:reader_token] =
94       api_fixture('api_client_authorizations')['active']['api_token']
95     get(:show_file_links, params)
96     assert_response :success
97     assert_equal([['.', 'foo', 3]], assigns(:object).files)
98     assert_no_session
99   end
100
101   test "getting a file from Keep" do
102     params = collection_params(:foo_file, 'foo')
103     sess = session_for(:active)
104     get(:show_file, params, sess)
105     assert_response :success
106     assert_equal(expected_contents(params, sess), @response.body,
107                  "failed to get a correct file from Keep")
108   end
109
110   test "can't get a file from Keep without permission" do
111     params = collection_params(:foo_file, 'foo')
112     sess = session_for(:spectator)
113     get(:show_file, params, sess)
114     assert_response 404
115   end
116
117   test "trying to get a nonexistent file from Keep returns a 404" do
118     params = collection_params(:foo_file, 'gone')
119     sess = session_for(:admin)
120     get(:show_file, params, sess)
121     assert_response 404
122   end
123
124   test "getting a file from Keep with a good reader token" do
125     params = collection_params(:foo_file, 'foo')
126     read_token = api_fixture('api_client_authorizations')['active']['api_token']
127     params[:reader_token] = read_token
128     get(:show_file, params)
129     assert_response :success
130     assert_equal(expected_contents(params, read_token), @response.body,
131                  "failed to get a correct file from Keep using a reader token")
132     assert_not_equal(read_token, session[:arvados_api_token],
133                      "using a reader token set the session's API token")
134   end
135
136   test "trying to get from Keep with an unscoped reader token prompts login" do
137     params = collection_params(:foo_file, 'foo')
138     params[:reader_token] =
139       api_fixture('api_client_authorizations')['active_noscope']['api_token']
140     get(:show_file, params)
141     assert_response :redirect
142   end
143
144   test "can get a file with an unpermissioned auth but in-scope reader token" do
145     params = collection_params(:foo_file, 'foo')
146     sess = session_for(:expired)
147     read_token = api_fixture('api_client_authorizations')['active']['api_token']
148     params[:reader_token] = read_token
149     get(:show_file, params, sess)
150     assert_response :success
151     assert_equal(expected_contents(params, read_token), @response.body,
152                  "failed to get a correct file from Keep using a reader token")
153     assert_not_equal(read_token, session[:arvados_api_token],
154                      "using a reader token set the session's API token")
155   end
156
157   test "inactive user can retrieve user agreement" do
158     ua_collection = api_fixture('collections')['user_agreement']
159     get :show_file, {
160       uuid: ua_collection['uuid'],
161       file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
162     }, session_for(:inactive)
163     assert_nil(assigns(:required_user_agreements),
164                "Did not skip check_user_agreements filter " +
165                "when showing the user agreement.")
166     assert_response :success
167   end
168 end