1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::MarkdownUploads, feature_category: :team_planning do
let_it_be(:group) { create(:group, :private) }
let_it_be(:group_maintainer) { create(:user, maintainer_of: group) }
let_it_be(:project) { create(:project, :private) }
let_it_be(:project_maintainer) { create(:user, maintainer_of: project) }
let_it_be(:user) { create(:user, guest_of: [project, group]) }
describe "POST /projects/:id/uploads/authorize" do
include WorkhorseHelpers
let(:headers) { workhorse_internal_api_request_header.merge({ 'HTTP_GITLAB_WORKHORSE' => 1 }) }
let(:path) { "/projects/#{project.id}/uploads/authorize" }
context 'with authorized user' do
it "returns 200" do
post api(path, user), headers: headers
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['MaximumSize']).to eq(project.max_attachment_size)
end
end
context 'with unauthorized user' do
it "returns 404" do
post api(path, create(:user)), headers: headers
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with no Workhorse headers' do
it "returns 403" do
post api(path, user)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
describe "POST /projects/:id/uploads" do
let(:file) { fixture_file_upload("spec/fixtures/dk.png", "image/png") }
let(:path) { "/projects/#{project.id}/uploads" }
before do
project
end
it "uploads the file through the upload service and returns its info" do
expect(UploadService).to receive(:new).with(project, anything, uploaded_by_user_id: user.id).and_call_original
post api(path, user), params: { file: file }
expect(response).to have_gitlab_http_status(:created)
expect(json_response['id']).to eq(Upload.last.id)
expect(json_response['alt']).to eq("dk")
expect(json_response['url']).to start_with("/uploads/")
expect(json_response['url']).to end_with("/dk.png")
expect(json_response['full_path']).to start_with("/-/project/#{project.id}/uploads")
end
it "does not leave the temporary file in place after uploading, even when the tempfile reaper does not run" do
tempfile = Tempfile.new('foo')
path = tempfile.path
# rubocop: disable RSpec/AnyInstanceOf -- allow_next_instance_of does not work here because TempfileReaper is a middleware that is initialized early
allow_any_instance_of(Rack::TempfileReaper).to receive(:call) do |instance, env|
instance.instance_variable_get(:@app).call(env)
end
# rubocop: enable RSpec/AnyInstanceOf
expect(path).not_to be(nil)
expect(Rack::Multipart::Parser::TEMPFILE_FACTORY).to receive(:call).and_return(tempfile)
post api(path, user), params: { file: fixture_file_upload("spec/fixtures/dk.png", "image/png") }
expect(tempfile.path).to be(nil)
expect(File.exist?(path)).to be(false)
end
end
shared_examples 'an unauthorized request' do
it 'returns 403' do
make_request
expect(response).to have_gitlab_http_status(:forbidden)
end
end
describe "GET /projects/:id/uploads" do
let_it_be(:uploads) { create_list(:upload, 3, :issuable_upload, model: project) }
let_it_be(:other_upload) { create(:upload, :issuable_upload, model: create(:project)) }
let(:path) { "/projects/#{project.id}/uploads" }
it 'returns uploads ordered by created_at' do
get api(path, project_maintainer)
expect_paginated_array_response(uploads.reverse.map(&:id))
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { get api(path, user) }
end
end
describe "GET /projects/:id/uploads/:upload_id" do
let_it_be(:upload) { create(:upload, :issuable_upload, :with_file, model: project, filename: 'test.jpg') }
let(:path) { "/projects/#{project.id}/uploads/#{upload.id}" }
it 'returns the uploaded file' do
get api(path, project_maintainer)
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Disposition'])
.to eq(%(attachment; filename="test.jpg"; filename*=UTF-8''test.jpg))
end
context 'when the upload does not exist' do
let(:path) { "/projects/#{project.id}/uploads/#{non_existing_record_id}" }
it 'returns a 404' do
get api(path, project_maintainer)
expect(response).to have_gitlab_http_status(:not_found)
end
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { get api(path, user) }
end
end
describe "GET /projects/:id/uploads/:secret/:filename" do
let_it_be(:upload) { create(:upload, :issuable_upload, :with_file, model: project, filename: 'test.jpg') }
let(:path) { "/projects/#{project.id}/uploads/#{upload.secret}/#{upload.filename}" }
it 'returns the uploaded file' do
get api(path, project_maintainer)
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Disposition'])
.to eq(%(attachment; filename="test.jpg"; filename*=UTF-8''test.jpg))
end
context 'when the secret does not match' do
let(:path) { "/projects/#{project.id}/uploads/invalid_secret/#{upload.filename}" }
it 'returns a 404' do
get api(path, project_maintainer)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with a user that does not have access to the project' do
it 'returns 404' do
get api(path, create(:user))
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe "DELETE /projects/:id/uploads/:upload_id" do
let_it_be(:upload) { create(:upload, :issuable_upload, model: project) }
let(:path) { "/projects/#{project.id}/uploads/#{upload.id}" }
it 'deletes the given upload' do
expect do
delete api(path, project_maintainer)
end.to change { Upload.count }.by(-1)
expect(response).to have_gitlab_http_status(:no_content)
end
it 'returns an error when deletion fails' do
expect_next_instance_of(Banzai::UploadsFinder) do |finder|
expect(finder).to receive(:find).with(upload.id).and_return(upload)
end
expect(upload).to receive(:destroy).and_return(false)
delete api(path, project_maintainer)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to include(_('Upload could not be deleted.'))
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { delete api(path, user) }
end
end
describe "DELETE /projects/:id/uploads/:secret/:filename" do
let_it_be(:upload) { create(:upload, :issuable_upload, model: project) }
let(:path) { "/projects/#{project.id}/uploads/#{upload.secret}/#{upload.filename}" }
it 'deletes the given upload' do
expect do
delete api(path, project_maintainer)
end.to change { Upload.count }.by(-1)
expect(response).to have_gitlab_http_status(:no_content)
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { delete api(path, user) }
end
end
describe "GET /groups/:id/uploads" do
let_it_be(:uploads) { create_list(:upload, 3, :namespace_upload, model: group) }
let_it_be(:other_upload) { create(:upload, :namespace_upload, model: create(:group)) }
let(:path) { "/groups/#{group.id}/uploads" }
it 'returns uploads ordered by created_at' do
get api(path, group_maintainer)
expect_paginated_array_response(uploads.reverse.map(&:id))
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { get api(path, user) }
end
end
describe "GET /groups/:id/uploads/:upload_id" do
let_it_be(:upload) { create(:upload, :namespace_upload, :with_file, model: group, filename: 'test.jpg') }
let(:path) { "/groups/#{group.id}/uploads/#{upload.id}" }
it 'returns the uploaded file' do
get api(path, group_maintainer)
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Disposition'])
.to eq(%(attachment; filename="test.jpg"; filename*=UTF-8''test.jpg))
end
context 'when the upload does not exist' do
let(:path) { "/groups/#{group.id}/uploads/#{non_existing_record_id}" }
it 'returns a 404' do
get api(path, group_maintainer)
expect(response).to have_gitlab_http_status(:not_found)
end
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { get api(path, user) }
end
end
describe "GET /groups/:id/uploads/:secret/:filename" do
let_it_be(:upload) { create(:upload, :namespace_upload, :with_file, model: group, filename: 'test.jpg') }
let(:path) { "/groups/#{group.id}/uploads/#{upload.secret}/#{upload.filename}" }
it 'returns the uploaded file' do
get api(path, group_maintainer)
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Disposition'])
.to eq(%(attachment; filename="test.jpg"; filename*=UTF-8''test.jpg))
end
context 'when the secret does not match' do
let(:path) { "/groups/#{group.id}/uploads/invalid_secret/#{upload.filename}" }
it 'returns a 404' do
get api(path, group_maintainer)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with a user that does not have access to the group' do
it 'returns 404' do
get api(path, create(:user))
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe "DELETE /groups/:id/uploads/:upload_id" do
let_it_be(:upload) { create(:upload, :namespace_upload, model: group) }
let(:path) { "/groups/#{group.id}/uploads/#{upload.id}" }
it 'deletes the given upload' do
expect do
delete api(path, group_maintainer)
end.to change { Upload.count }.by(-1)
expect(response).to have_gitlab_http_status(:no_content)
end
it 'returns an error when deletion fails' do
expect_next_instance_of(Banzai::UploadsFinder) do |finder|
expect(finder).to receive(:find).with(upload.id).and_return(upload)
end
expect(upload).to receive(:destroy).and_return(false)
delete api(path, group_maintainer)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to include(_('Upload could not be deleted.'))
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { delete api(path, user) }
end
end
describe "DELETE /groups/:id/uploads/:secret/:filename" do
let_it_be(:upload) { create(:upload, :namespace_upload, model: group) }
let(:path) { "/groups/#{group.id}/uploads/#{upload.secret}/#{upload.filename}" }
it 'deletes the given upload' do
expect do
delete api(path, group_maintainer)
end.to change { Upload.count }.by(-1)
expect(response).to have_gitlab_http_status(:no_content)
end
it_behaves_like 'an unauthorized request' do
subject(:make_request) { delete api(path, user) }
end
end
end
|