2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-04-02 19:17:17 -04:00
|
|
|
|
|
|
|
describe UploadsController do
|
2018-06-03 23:13:52 -04:00
|
|
|
describe '#create' do
|
2013-09-06 13:18:42 -04:00
|
|
|
it 'requires you to be logged in' do
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json"
|
2018-01-11 22:15:10 -05:00
|
|
|
expect(response.status).to eq(403)
|
2013-04-02 19:17:17 -04:00
|
|
|
end
|
|
|
|
|
2013-09-06 13:18:42 -04:00
|
|
|
context 'logged in' do
|
2018-06-03 23:13:52 -04:00
|
|
|
let!(:user) { sign_in(Fabricate(:user)) }
|
2013-04-02 19:17:17 -04:00
|
|
|
|
2013-06-15 03:54:49 -04:00
|
|
|
let(:logo) do
|
2017-08-31 00:06:56 -04:00
|
|
|
Rack::Test::UploadedFile.new(file_from_fixtures("logo.png"))
|
2013-04-02 19:17:17 -04:00
|
|
|
end
|
|
|
|
|
2015-12-21 10:08:14 -05:00
|
|
|
let(:fake_jpg) do
|
2017-08-31 00:06:56 -04:00
|
|
|
Rack::Test::UploadedFile.new(file_from_fixtures("fake.jpg"))
|
2015-12-21 10:08:14 -05:00
|
|
|
end
|
|
|
|
|
2013-06-15 03:54:49 -04:00
|
|
|
let(:text_file) do
|
2017-08-31 00:06:56 -04:00
|
|
|
Rack::Test::UploadedFile.new(File.new("#{Rails.root}/LICENSE.txt"))
|
2013-06-15 03:54:49 -04:00
|
|
|
end
|
2013-04-02 19:17:17 -04:00
|
|
|
|
2017-05-18 06:13:13 -04:00
|
|
|
it 'expects a type' do
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: logo }
|
|
|
|
expect(response.status).to eq(400)
|
2017-08-22 16:40:01 -04:00
|
|
|
end
|
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
it 'is successful with an image' do
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: logo, type: "avatar" }
|
2015-05-19 19:39:58 -04:00
|
|
|
expect(response.status).to eq 200
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(JSON.parse(response.body)["id"]).to be_present
|
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(1)
|
2015-05-19 19:39:58 -04:00
|
|
|
end
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
it 'is successful with an attachment' do
|
2017-06-12 16:41:29 -04:00
|
|
|
SiteSetting.authorized_extensions = "*"
|
2015-05-25 11:59:00 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: text_file, type: "composer" }
|
2015-05-19 19:39:58 -04:00
|
|
|
expect(response.status).to eq 200
|
2018-06-03 23:13:52 -04:00
|
|
|
|
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(0)
|
2017-11-26 20:43:18 -05:00
|
|
|
id = JSON.parse(response.body)["id"]
|
|
|
|
expect(id).to be
|
2015-06-21 07:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 06:35:57 -05:00
|
|
|
it 'is successful with api' do
|
2017-04-15 00:11:02 -04:00
|
|
|
SiteSetting.authorized_extensions = "*"
|
2018-06-03 23:13:52 -04:00
|
|
|
api_key = Fabricate(:api_key, user: user).key
|
2017-05-26 03:19:09 -04:00
|
|
|
|
2018-02-24 06:35:57 -05:00
|
|
|
url = "http://example.com/image.png"
|
|
|
|
png = File.read(Rails.root + "spec/fixtures/images/logo.png")
|
|
|
|
|
|
|
|
stub_request(:get, url).to_return(status: 200, body: png)
|
2015-06-21 07:52:52 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { url: url, type: "avatar", api_key: api_key, api_username: user.username }
|
2015-06-21 07:52:52 -04:00
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(1)
|
|
|
|
expect(json["id"]).to be_present
|
2017-08-22 16:40:01 -04:00
|
|
|
expect(json["short_url"]).to eq("upload://qUm0DGR49PAZshIi7HxMd3cAlzn.png")
|
2015-05-19 19:39:58 -04:00
|
|
|
end
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
it 'correctly sets retain_hours for admins' do
|
2018-06-03 23:13:52 -04:00
|
|
|
sign_in(Fabricate(:admin))
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: {
|
2017-11-26 20:43:18 -05:00
|
|
|
file: logo,
|
|
|
|
retain_hours: 100,
|
|
|
|
type: "profile_background",
|
|
|
|
}
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2017-11-26 20:43:18 -05:00
|
|
|
id = JSON.parse(response.body)["id"]
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(0)
|
2015-05-19 19:39:58 -04:00
|
|
|
expect(Upload.find(id).retain_hours).to eq(100)
|
2013-06-15 03:54:49 -04:00
|
|
|
end
|
2013-04-02 19:17:17 -04:00
|
|
|
|
2015-08-18 05:39:51 -04:00
|
|
|
it 'requires a file' do
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { type: "composer" }
|
2015-08-18 05:39:51 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(0)
|
2017-11-26 20:43:18 -05:00
|
|
|
message = JSON.parse(response.body)
|
|
|
|
expect(response.status).to eq 422
|
|
|
|
expect(message["errors"]).to contain_exactly(I18n.t("upload.file_missing"))
|
2015-08-18 05:39:51 -04:00
|
|
|
end
|
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
it 'properly returns errors' do
|
2018-06-03 23:13:52 -04:00
|
|
|
SiteSetting.authorized_extensions = "*"
|
2017-06-12 16:41:29 -04:00
|
|
|
SiteSetting.max_attachment_size_kb = 1
|
2013-04-02 19:17:17 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: text_file, type: "avatar" }
|
2015-05-25 11:59:00 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(0)
|
2017-11-26 20:43:18 -05:00
|
|
|
errors = JSON.parse(response.body)["errors"]
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(errors.first).to eq(I18n.t("upload.attachments.too_large", max_size_kb: 1))
|
2013-04-02 19:17:17 -04:00
|
|
|
end
|
|
|
|
|
2015-11-12 04:26:45 -05:00
|
|
|
it 'ensures allow_uploaded_avatars is enabled when uploading an avatar' do
|
2017-06-12 16:41:29 -04:00
|
|
|
SiteSetting.allow_uploaded_avatars = false
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: logo, type: "avatar" }
|
|
|
|
expect(response.status).to eq(422)
|
2015-11-12 04:26:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures sso_overrides_avatar is not enabled when uploading an avatar' do
|
2017-06-12 16:41:29 -04:00
|
|
|
SiteSetting.sso_overrides_avatar = true
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: logo, type: "avatar" }
|
|
|
|
expect(response.status).to eq(422)
|
2015-11-12 04:26:45 -05:00
|
|
|
end
|
|
|
|
|
2018-12-05 07:35:59 -05:00
|
|
|
it 'always allows admins to upload avatars' do
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
SiteSetting.allow_uploaded_avatars = false
|
|
|
|
|
|
|
|
post "/uploads.json", params: { file: logo, type: "avatar" }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2017-06-12 16:41:29 -04:00
|
|
|
it 'allows staff to upload any file in PM' do
|
|
|
|
SiteSetting.authorized_extensions = "jpg"
|
|
|
|
SiteSetting.allow_staff_to_upload_any_file_in_pm = true
|
2018-06-03 23:13:52 -04:00
|
|
|
user.update_columns(moderator: true)
|
2017-06-12 16:41:29 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: {
|
2017-11-26 20:43:18 -05:00
|
|
|
file: text_file,
|
|
|
|
type: "composer",
|
|
|
|
for_private_message: "true",
|
|
|
|
}
|
2017-06-12 16:41:29 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-26 20:43:18 -05:00
|
|
|
id = JSON.parse(response.body)["id"]
|
2018-11-14 02:03:02 -05:00
|
|
|
expect(Upload.last.id).to eq(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows staff to upload supported images for site settings' do
|
|
|
|
SiteSetting.authorized_extensions = ''
|
|
|
|
user.update!(admin: true)
|
|
|
|
|
|
|
|
post "/uploads.json", params: {
|
|
|
|
file: logo,
|
|
|
|
type: "site_setting",
|
|
|
|
for_site_setting: "true",
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
id = JSON.parse(response.body)["id"]
|
|
|
|
|
|
|
|
upload = Upload.last
|
|
|
|
|
|
|
|
expect(upload.id).to eq(id)
|
|
|
|
expect(upload.original_filename).to eq('logo.png')
|
2017-06-12 16:41:29 -04:00
|
|
|
end
|
|
|
|
|
2018-02-19 04:44:24 -05:00
|
|
|
it 'respects `authorized_extensions_for_staff` setting when staff upload file' do
|
|
|
|
SiteSetting.authorized_extensions = ""
|
|
|
|
SiteSetting.authorized_extensions_for_staff = "*"
|
2018-06-03 23:13:52 -04:00
|
|
|
user.update_columns(moderator: true)
|
2018-02-19 04:44:24 -05:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: {
|
2018-02-19 04:44:24 -05:00
|
|
|
file: text_file,
|
|
|
|
type: "composer",
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-02-19 04:44:24 -05:00
|
|
|
data = JSON.parse(response.body)
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(data["id"]).to be_present
|
2018-02-19 04:44:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores `authorized_extensions_for_staff` setting when non-staff upload file' do
|
|
|
|
SiteSetting.authorized_extensions = ""
|
|
|
|
SiteSetting.authorized_extensions_for_staff = "*"
|
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: {
|
2018-02-19 04:44:24 -05:00
|
|
|
file: text_file,
|
|
|
|
type: "composer",
|
|
|
|
}
|
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
expect(data["errors"].first).to eq(I18n.t("upload.unauthorized", authorized_extensions: ''))
|
|
|
|
end
|
|
|
|
|
2015-12-21 10:08:14 -05:00
|
|
|
it 'returns an error when it could not determine the dimensions of an image' do
|
2018-06-03 23:13:52 -04:00
|
|
|
post "/uploads.json", params: { file: fake_jpg, type: "composer" }
|
2015-12-21 10:08:14 -05:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(Jobs::CreateAvatarThumbnails.jobs.size).to eq(0)
|
2017-11-26 20:43:18 -05:00
|
|
|
message = JSON.parse(response.body)["errors"]
|
|
|
|
expect(message).to contain_exactly(I18n.t("upload.images.size_not_found"))
|
2015-12-21 10:08:14 -05:00
|
|
|
end
|
2013-04-02 19:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
describe '#show' do
|
2015-05-19 06:31:12 -04:00
|
|
|
let(:site) { "default" }
|
|
|
|
let(:sha) { Digest::SHA1.hexdigest("discourse") }
|
2018-06-03 23:13:52 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
def upload_file(file)
|
|
|
|
fake_logo = Rack::Test::UploadedFile.new(file_from_fixtures(file))
|
|
|
|
SiteSetting.authorized_extensions = "*"
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/uploads.json", params: {
|
|
|
|
file: fake_logo,
|
|
|
|
type: "composer",
|
|
|
|
}
|
|
|
|
url = JSON.parse(response.body)["url"]
|
|
|
|
upload = Upload.where(url: url).first
|
|
|
|
upload
|
|
|
|
end
|
2015-05-19 06:31:12 -04:00
|
|
|
|
2013-09-06 13:18:42 -04:00
|
|
|
it "returns 404 when using external storage" do
|
2018-06-03 23:13:52 -04:00
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_access_key_id = "fakeid7974664"
|
|
|
|
SiteSetting.s3_secret_access_key = "fakesecretid7974664"
|
2015-05-19 06:31:12 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
get "/uploads/#{site}/#{sha}.pdf"
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.response_code).to eq(404)
|
2013-09-06 13:18:42 -04:00
|
|
|
end
|
|
|
|
|
2016-12-19 13:39:04 -05:00
|
|
|
it "returns 404 when the upload doesn't exist" do
|
2018-06-03 23:13:52 -04:00
|
|
|
get "/uploads/#{site}/#{sha}.pdf"
|
|
|
|
expect(response.status).to eq(404)
|
2013-09-06 13:18:42 -04:00
|
|
|
end
|
|
|
|
|
2019-01-29 16:47:25 -05:00
|
|
|
it "returns 404 when the path is nil" do
|
|
|
|
upload = upload_file("logo.png")
|
|
|
|
upload.update_column(:url, "invalid-url")
|
|
|
|
|
|
|
|
get "/uploads/#{site}/#{upload.sha1}.#{upload.extension}"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2013-09-06 13:18:42 -04:00
|
|
|
it 'uses send_file' do
|
2018-06-03 23:13:52 -04:00
|
|
|
upload = upload_file("logo.png")
|
|
|
|
get "/uploads/#{site}/#{upload.sha1}.#{upload.extension}"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Disposition"]).to eq("attachment; filename=\"logo.png\"")
|
2013-09-06 13:18:42 -04:00
|
|
|
end
|
|
|
|
|
2018-08-19 22:41:46 -04:00
|
|
|
it "handles image without extension" do
|
2016-12-19 13:39:04 -05:00
|
|
|
SiteSetting.authorized_extensions = "*"
|
2018-06-03 23:13:52 -04:00
|
|
|
upload = upload_file("image_no_extension")
|
2016-12-19 13:39:04 -05:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
get "/uploads/#{site}/#{upload.sha1}.json"
|
|
|
|
expect(response.status).to eq(200)
|
2018-08-20 02:08:05 -04:00
|
|
|
expect(response.headers["Content-Disposition"]).to eq("attachment; filename=\"image_no_extension.png\"")
|
2018-08-19 22:41:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles file without extension" do
|
|
|
|
SiteSetting.authorized_extensions = "*"
|
|
|
|
upload = upload_file("not_an_image")
|
|
|
|
|
|
|
|
get "/uploads/#{site}/#{upload.sha1}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["Content-Disposition"]).to eq("attachment; filename=\"not_an_image\"")
|
2016-12-19 13:39:04 -05:00
|
|
|
end
|
|
|
|
|
2014-09-09 12:40:11 -04:00
|
|
|
context "prevent anons from downloading files" do
|
|
|
|
it "returns 404 when an anonymous user tries to download a file" do
|
2018-06-03 23:13:52 -04:00
|
|
|
upload = upload_file("logo.png")
|
|
|
|
delete "/session/#{user.username}.json" # upload a file, then sign out
|
2015-05-19 06:31:12 -04:00
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
SiteSetting.prevent_anons_from_downloading_files = true
|
|
|
|
get "/uploads/#{site}/#{upload.sha1}.#{upload.extension}"
|
|
|
|
expect(response.status).to eq(404)
|
2014-09-09 12:40:11 -04:00
|
|
|
end
|
|
|
|
end
|
2013-09-06 13:18:42 -04:00
|
|
|
end
|
|
|
|
|
2018-06-03 23:13:52 -04:00
|
|
|
describe '#lookup_urls' do
|
|
|
|
it 'can look up long urls' do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
|
|
|
|
post "/uploads/lookup-urls.json", params: { short_urls: [upload.short_url] }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
result = JSON.parse(response.body)
|
|
|
|
expect(result[0]["url"]).to eq(upload.url)
|
|
|
|
end
|
|
|
|
end
|
2019-02-20 21:13:37 -05:00
|
|
|
|
|
|
|
describe '#metadata' do
|
|
|
|
let(:upload) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
describe 'when url is missing' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
post "/uploads/lookup-metadata.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when not signed in' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
post "/uploads/lookup-metadata.json", params: { url: upload.url }
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when url is invalid' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
post "/uploads/lookup-metadata.json", params: { url: 'abc' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the right response" do
|
|
|
|
post "/uploads/lookup-metadata.json", params: { url: upload.url }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
result = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(result["original_filename"]).to eq(upload.original_filename)
|
|
|
|
expect(result["width"]).to eq(upload.width)
|
|
|
|
expect(result["height"]).to eq(upload.height)
|
|
|
|
expect(result["human_filesize"]).to eq(upload.human_filesize)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-02 19:17:17 -04:00
|
|
|
end
|