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
11 def expected_contents(params, token)
12 unless token.is_a? String
13 token = params[:api_token] || token[:arvados_api_token]
15 [token, params[:uuid], params[:file]].join('/')
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)
25 assert_hash_includes(session, {arvados_api_token: nil},
26 "session includes unexpected API token")
29 def assert_session_for_auth(client_auth)
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}")
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
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('/')]
51 test "viewing a collection" do
52 show_collection(:foo_file, :active)
53 assert_equal([['.', 'foo', 3]], assigns(:object).files)
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")
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")
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")
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")
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")
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)
101 test "reader token Collection links end with trailing slash" do
102 # Testing the fix for #2937.
103 show_collection(:foo_file, :active_trustedclient)
104 post(:share, collection_params(:foo_file))
105 assert(@controller.download_link.ends_with? '/',
106 "Collection share link does not end with slash for wget")
109 test "getting a file from Keep" do
110 params = collection_params(:foo_file, 'foo')
111 sess = session_for(:active)
112 get(:show_file, params, sess)
113 assert_response :success
114 assert_equal(expected_contents(params, sess), @response.body,
115 "failed to get a correct file from Keep")
118 test "can't get a file from Keep without permission" do
119 params = collection_params(:foo_file, 'foo')
120 sess = session_for(:spectator)
121 get(:show_file, params, sess)
125 test "trying to get a nonexistent file from Keep returns a 404" do
126 params = collection_params(:foo_file, 'gone')
127 sess = session_for(:admin)
128 get(:show_file, params, sess)
132 test "getting a file from Keep with a good reader token" do
133 params = collection_params(:foo_file, 'foo')
134 read_token = api_fixture('api_client_authorizations')['active']['api_token']
135 params[:reader_token] = read_token
136 get(:show_file, params)
137 assert_response :success
138 assert_equal(expected_contents(params, read_token), @response.body,
139 "failed to get a correct file from Keep using a reader token")
140 assert_not_equal(read_token, session[:arvados_api_token],
141 "using a reader token set the session's API token")
144 test "trying to get from Keep with an unscoped reader token prompts login" do
145 params = collection_params(:foo_file, 'foo')
146 params[:reader_token] =
147 api_fixture('api_client_authorizations')['active_noscope']['api_token']
148 get(:show_file, params)
149 assert_response :redirect
152 test "can get a file with an unpermissioned auth but in-scope reader token" do
153 params = collection_params(:foo_file, 'foo')
154 sess = session_for(:expired)
155 read_token = api_fixture('api_client_authorizations')['active']['api_token']
156 params[:reader_token] = read_token
157 get(:show_file, params, sess)
158 assert_response :success
159 assert_equal(expected_contents(params, read_token), @response.body,
160 "failed to get a correct file from Keep using a reader token")
161 assert_not_equal(read_token, session[:arvados_api_token],
162 "using a reader token set the session's API token")
165 test "inactive user can retrieve user agreement" do
166 ua_collection = api_fixture('collections')['user_agreement']
168 uuid: ua_collection['uuid'],
169 file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
170 }, session_for(:inactive)
171 assert_nil(assigns(:required_user_agreements),
172 "Did not skip check_user_agreements filter " +
173 "when showing the user agreement.")
174 assert_response :success