3 class CollectionsControllerTest < ActionController::TestCase
4 NONEXISTENT_COLLECTION = "ffffffffffffffffffffffffffffffff+0"
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])
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
22 def expected_contents(params, token)
23 unless token.is_a? String
24 token = params[:api_token] || token[:arvados_api_token]
26 [token, params[:uuid], params[:file]].join('/')
29 def assert_hash_includes(actual_hash, expected_hash, msg=nil)
30 expected_hash.each do |key, value|
31 assert_equal(value, actual_hash[key], msg)
36 assert_hash_includes(session, {arvados_api_token: nil},
37 "session includes unexpected API token")
40 def assert_session_for_auth(client_auth)
42 api_fixture('api_client_authorizations')[client_auth.to_s]['api_token']
43 assert_hash_includes(session, {arvados_api_token: api_token},
44 "session token does not belong to #{client_auth}")
47 def show_collection(params, session={}, response=:success)
48 params = collection_params(params) if not params.is_a? Hash
49 session = session_for(session) if not session.is_a? Hash
50 get(:show, params, session)
51 assert_response response
54 test "viewing a collection" do
55 show_collection(:foo_file, :active)
56 assert_equal([['.', 'foo', 3]], assigns(:object).files)
59 test "viewing a collection fetches related projects" do
60 show_collection({id: api_fixture('collections')["foo_file"]['portable_data_hash']}, :active)
61 assert_includes(assigns(:same_pdh).map(&:owner_uuid),
62 api_fixture('groups')['aproject']['uuid'],
63 "controller did not find linked project")
66 test "viewing a collection fetches related permissions" do
67 show_collection(:bar_file, :active)
68 assert_includes(assigns(:permissions).map(&:uuid),
69 api_fixture('links')['bar_file_readable_by_active']['uuid'],
70 "controller did not find permission link")
73 test "viewing a collection fetches jobs that output it" do
74 show_collection(:bar_file, :active)
75 assert_includes(assigns(:output_of).map(&:uuid),
76 api_fixture('jobs')['foobar']['uuid'],
77 "controller did not find output job")
80 test "viewing a collection fetches jobs that logged it" do
81 show_collection(:baz_file, :active)
82 assert_includes(assigns(:log_of).map(&:uuid),
83 api_fixture('jobs')['foobar']['uuid'],
84 "controller did not find logger job")
87 test "viewing a collection fetches logs about it" do
88 show_collection(:foo_file, :active)
89 assert_includes(assigns(:logs).map(&:uuid),
90 api_fixture('logs')['log4']['uuid'],
91 "controller did not find related log")
94 test "viewing collection files with a reader token" do
95 params = collection_params(:foo_file)
96 params[:reader_token] =
97 api_fixture('api_client_authorizations')['active']['api_token']
98 get(:show_file_links, params)
99 assert_response :success
100 assert_equal([['.', 'foo', 3]], assigns(:object).files)
104 test "reader token Collection links end with trailing slash" do
105 # Testing the fix for #2937.
106 session = session_for(:active_trustedclient)
107 post(:share, collection_params(:foo_file), session)
108 assert(@controller.download_link.ends_with? '/',
109 "Collection share link does not end with slash for wget")
112 test "getting a file from Keep" do
113 params = collection_params(:foo_file, 'foo')
114 sess = session_for(:active)
115 expect_content = stub_file_content
116 get(:show_file, params, sess)
117 assert_response :success
118 assert_equal(expect_content, @response.body,
119 "failed to get a correct file from Keep")
122 test "can't get a file from Keep without permission" do
123 params = collection_params(:foo_file, 'foo')
124 sess = session_for(:spectator)
125 get(:show_file, params, sess)
129 test "trying to get a nonexistent file from Keep returns a 404" do
130 params = collection_params(:foo_file, 'gone')
131 sess = session_for(:admin)
132 get(:show_file, params, sess)
136 test "getting a file from Keep with a good reader token" do
137 params = collection_params(:foo_file, 'foo')
138 read_token = api_fixture('api_client_authorizations')['active']['api_token']
139 params[:reader_token] = read_token
140 expect_content = stub_file_content
141 get(:show_file, params)
142 assert_response :success
143 assert_equal(expect_content, @response.body,
144 "failed to get a correct file from Keep using a reader token")
145 assert_not_equal(read_token, session[:arvados_api_token],
146 "using a reader token set the session's API token")
149 test "trying to get from Keep with an unscoped reader token prompts login" do
150 params = collection_params(:foo_file, 'foo')
151 params[:reader_token] =
152 api_fixture('api_client_authorizations')['active_noscope']['api_token']
153 get(:show_file, params)
154 assert_response :redirect
157 test "can get a file with an unpermissioned auth but in-scope reader token" do
158 params = collection_params(:foo_file, 'foo')
159 sess = session_for(:expired)
160 read_token = api_fixture('api_client_authorizations')['active']['api_token']
161 params[:reader_token] = read_token
162 expect_content = stub_file_content
163 get(:show_file, params, sess)
164 assert_response :success
165 assert_equal(expect_content, @response.body,
166 "failed to get a correct file from Keep using a reader token")
167 assert_not_equal(read_token, session[:arvados_api_token],
168 "using a reader token set the session's API token")
171 test "inactive user can retrieve user agreement" do
172 ua_collection = api_fixture('collections')['user_agreement']
174 uuid: ua_collection['uuid'],
175 file: ua_collection['manifest_text'].match(/ \d+:\d+:(\S+)/)[1]
176 }, session_for(:inactive)
177 assert_nil(assigns(:unsigned_user_agreements),
178 "Did not skip check_user_agreements filter " +
179 "when showing the user agreement.")
180 assert_response :success
183 test "requesting nonexistent Collection returns 404" do
184 show_collection({uuid: NONEXISTENT_COLLECTION, id: NONEXISTENT_COLLECTION},