2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-04-02 18:17:17 -05:00
|
|
|
|
|
|
|
describe UploadsController do
|
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
context '.create' do
|
2013-04-02 18:17:17 -05:00
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
it 'requires you to be logged in' do
|
2015-01-09 11:04:02 -06:00
|
|
|
expect { xhr :post, :create }.to raise_error(Discourse::NotLoggedIn)
|
2013-04-02 18:17:17 -05:00
|
|
|
end
|
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
context 'logged in' do
|
|
|
|
|
|
|
|
before { @user = log_in :user }
|
2013-04-02 18:17:17 -05:00
|
|
|
|
2013-06-15 02:54:49 -05:00
|
|
|
let(:logo) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({
|
|
|
|
filename: 'logo.png',
|
2014-07-14 10:34:23 -05:00
|
|
|
tempfile: file_from_fixtures("logo.png")
|
2013-06-15 02:54:49 -05:00
|
|
|
})
|
2013-04-02 18:17:17 -05:00
|
|
|
end
|
|
|
|
|
2015-12-21 09:08:14 -06:00
|
|
|
let(:fake_jpg) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({
|
|
|
|
filename: 'fake.jpg',
|
|
|
|
tempfile: file_from_fixtures("fake.jpg")
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2013-06-15 02:54:49 -05:00
|
|
|
let(:text_file) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({
|
2014-04-29 12:12:35 -05:00
|
|
|
filename: 'LICENSE.TXT',
|
2013-06-15 02:54:49 -05:00
|
|
|
tempfile: File.new("#{Rails.root}/LICENSE.txt")
|
|
|
|
})
|
|
|
|
end
|
2013-04-02 18:17:17 -05:00
|
|
|
|
2017-05-18 05:13:13 -05:00
|
|
|
it 'expects a type' do
|
|
|
|
expect { xhr :post, :create, file: logo }.to raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
2016-12-18 17:16:18 -06:00
|
|
|
|
2017-05-18 05:13:13 -05:00
|
|
|
it 'parameterize the type' do
|
2017-06-23 05:13:48 -05:00
|
|
|
subject.expects(:create_upload).with(logo, nil, "super_long_type_with_charssuper_long_type_with_char", false, false)
|
2017-05-18 05:13:13 -05:00
|
|
|
xhr :post, :create, file: logo, type: "super \# long \//\\ type with \\. $%^&*( chars" * 5
|
2016-12-18 17:16:18 -06:00
|
|
|
end
|
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
it 'is successful with an image' do
|
2017-05-10 17:16:57 -05:00
|
|
|
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything)
|
2015-05-25 10:59:00 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
message = MessageBus.track_publish do
|
2015-05-25 10:59:00 -05:00
|
|
|
xhr :post, :create, file: logo, type: "avatar"
|
2015-05-19 18:39:58 -05:00
|
|
|
end.first
|
2013-07-23 17:54:18 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
expect(response.status).to eq 200
|
2013-07-23 17:54:18 -05:00
|
|
|
|
2015-05-25 10:59:00 -05:00
|
|
|
expect(message.channel).to eq("/uploads/avatar")
|
2017-06-12 15:41:29 -05:00
|
|
|
expect(message.data["id"]).to be
|
2015-05-19 18:39:58 -05:00
|
|
|
end
|
2014-04-29 12:12:35 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
it 'is successful with an attachment' do
|
2017-06-12 15:41:29 -05:00
|
|
|
SiteSetting.authorized_extensions = "*"
|
2015-05-25 10:59:00 -05:00
|
|
|
|
|
|
|
Jobs.expects(:enqueue).never
|
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
message = MessageBus.track_publish do
|
2015-05-25 10:59:00 -05:00
|
|
|
xhr :post, :create, file: text_file, type: "composer"
|
2015-05-19 18:39:58 -05:00
|
|
|
end.first
|
2014-04-29 12:12:35 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
expect(response.status).to eq 200
|
2015-05-25 10:59:00 -05:00
|
|
|
expect(message.channel).to eq("/uploads/composer")
|
2017-06-12 15:41:29 -05:00
|
|
|
expect(message.data["id"]).to be
|
2015-06-21 06:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is successful with synchronous api' do
|
2017-04-14 23:11:02 -05:00
|
|
|
SiteSetting.authorized_extensions = "*"
|
2015-06-21 06:52:52 -05:00
|
|
|
controller.stubs(:is_api?).returns(true)
|
|
|
|
|
2017-05-10 17:16:57 -05:00
|
|
|
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything)
|
2017-05-26 02:19:09 -05:00
|
|
|
|
|
|
|
stub_request(:head, 'http://example.com/image.png')
|
2017-04-14 23:11:02 -05:00
|
|
|
stub_request(:get, "http://example.com/image.png").to_return(body: File.read('spec/fixtures/images/logo.png'))
|
2015-06-21 06:52:52 -05:00
|
|
|
|
|
|
|
xhr :post, :create, url: 'http://example.com/image.png', type: "avatar", synchronous: true
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(json["id"]).to be
|
2015-05-19 18:39:58 -05:00
|
|
|
end
|
2014-04-29 12:12:35 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
it 'correctly sets retain_hours for admins' do
|
|
|
|
log_in :admin
|
2017-05-10 17:16:57 -05:00
|
|
|
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never
|
2014-04-29 12:12:35 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
message = MessageBus.track_publish do
|
|
|
|
xhr :post, :create, file: logo, retain_hours: 100, type: "profile_background"
|
|
|
|
end.first
|
2014-04-29 12:12:35 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
id = message.data["id"]
|
|
|
|
expect(Upload.find(id).retain_hours).to eq(100)
|
2013-06-15 02:54:49 -05:00
|
|
|
end
|
2013-04-02 18:17:17 -05:00
|
|
|
|
2015-08-18 04:39:51 -05:00
|
|
|
it 'requires a file' do
|
|
|
|
Jobs.expects(:enqueue).never
|
|
|
|
|
|
|
|
message = MessageBus.track_publish do
|
|
|
|
xhr :post, :create, type: "composer"
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
2017-05-10 17:16:57 -05:00
|
|
|
expect(message.data["errors"]).to contain_exactly(I18n.t("upload.file_missing"))
|
2015-08-18 04:39:51 -05:00
|
|
|
end
|
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
it 'properly returns errors' do
|
2017-06-12 15:41:29 -05:00
|
|
|
SiteSetting.max_attachment_size_kb = 1
|
2013-04-02 18:17:17 -05:00
|
|
|
|
2015-05-25 10:59:00 -05:00
|
|
|
Jobs.expects(:enqueue).never
|
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
message = MessageBus.track_publish do
|
|
|
|
xhr :post, :create, file: text_file, type: "avatar"
|
|
|
|
end.first
|
2013-04-02 18:17:17 -05:00
|
|
|
|
2015-05-19 18:39:58 -05:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(message.data["errors"]).to be
|
2013-04-02 18:17:17 -05:00
|
|
|
end
|
|
|
|
|
2015-11-12 03:26:45 -06:00
|
|
|
it 'ensures allow_uploaded_avatars is enabled when uploading an avatar' do
|
2017-06-12 15:41:29 -05:00
|
|
|
SiteSetting.allow_uploaded_avatars = false
|
2015-11-12 03:26:45 -06:00
|
|
|
xhr :post, :create, file: logo, type: "avatar"
|
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures sso_overrides_avatar is not enabled when uploading an avatar' do
|
2017-06-12 15:41:29 -05:00
|
|
|
SiteSetting.sso_overrides_avatar = true
|
2015-11-12 03:26:45 -06:00
|
|
|
xhr :post, :create, file: logo, type: "avatar"
|
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
2017-06-12 15:41:29 -05: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
|
|
|
|
@user.update_columns(moderator: true)
|
|
|
|
|
|
|
|
message = MessageBus.track_publish do
|
2017-06-23 05:13:48 -05:00
|
|
|
xhr :post, :create, file: text_file, type: "composer", for_private_message: "true"
|
2017-06-12 15:41:29 -05:00
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(message.data["id"]).to be
|
|
|
|
end
|
|
|
|
|
2015-12-21 09:08:14 -06:00
|
|
|
it 'returns an error when it could not determine the dimensions of an image' do
|
2017-05-10 17:16:57 -05:00
|
|
|
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything).never
|
2015-12-21 09:08:14 -06:00
|
|
|
|
|
|
|
message = MessageBus.track_publish do
|
|
|
|
xhr :post, :create, file: fake_jpg, type: "composer"
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
|
|
|
|
expect(message.channel).to eq("/uploads/composer")
|
2017-05-10 17:16:57 -05:00
|
|
|
expect(message.data["errors"]).to contain_exactly(I18n.t("upload.images.size_not_found"))
|
2015-12-21 09:08:14 -06:00
|
|
|
end
|
|
|
|
|
2013-04-02 18:17:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
context '.show' do
|
|
|
|
|
2015-05-19 05:31:12 -05:00
|
|
|
let(:site) { "default" }
|
|
|
|
let(:sha) { Digest::SHA1.hexdigest("discourse") }
|
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
it "returns 404 when using external storage" do
|
|
|
|
store = stub(internal?: false)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
2014-05-06 08:41:59 -05:00
|
|
|
Upload.expects(:find_by).never
|
2015-05-19 05:31:12 -05:00
|
|
|
|
|
|
|
get :show, site: site, sha: sha, extension: "pdf"
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response.response_code).to eq(404)
|
2013-09-06 12:18:42 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 12:39:04 -06:00
|
|
|
it "returns 404 when the upload doesn't exist" do
|
2015-05-20 08:32:31 -05:00
|
|
|
Upload.stubs(:find_by).returns(nil)
|
2014-09-23 00:50:26 -05:00
|
|
|
|
2015-05-19 05:31:12 -05:00
|
|
|
get :show, site: site, sha: sha, extension: "pdf"
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response.response_code).to eq(404)
|
2013-09-06 12:18:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses send_file' do
|
2014-04-14 15:55:57 -05:00
|
|
|
upload = build(:upload)
|
2015-05-19 05:31:12 -05:00
|
|
|
Upload.expects(:find_by).with(sha1: sha).returns(upload)
|
2014-04-14 15:55:57 -05:00
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
controller.stubs(:render)
|
|
|
|
controller.expects(:send_file)
|
2014-04-14 15:55:57 -05:00
|
|
|
|
2015-05-19 05:31:12 -05:00
|
|
|
get :show, site: site, sha: sha, extension: "zip"
|
2013-09-06 12:18:42 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 12:39:04 -06:00
|
|
|
it "handles file without extension" do
|
|
|
|
SiteSetting.authorized_extensions = "*"
|
2017-05-23 12:31:20 -05:00
|
|
|
Fabricate(:upload, original_filename: "image_file", sha1: sha)
|
2016-12-19 12:39:04 -06:00
|
|
|
controller.stubs(:render)
|
|
|
|
controller.expects(:send_file)
|
|
|
|
|
|
|
|
get :show, site: site, sha: sha
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2014-09-09 11:40:11 -05:00
|
|
|
context "prevent anons from downloading files" do
|
|
|
|
|
2017-06-12 15:41:29 -05:00
|
|
|
before { SiteSetting.prevent_anons_from_downloading_files = true }
|
2014-09-09 11:40:11 -05:00
|
|
|
|
|
|
|
it "returns 404 when an anonymous user tries to download a file" do
|
|
|
|
Upload.expects(:find_by).never
|
2015-05-19 05:31:12 -05:00
|
|
|
|
|
|
|
get :show, site: site, sha: sha, extension: "pdf"
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response.response_code).to eq(404)
|
2014-09-09 11:40:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-09-06 12:18:42 -05:00
|
|
|
end
|
|
|
|
|
2013-04-02 18:17:17 -05:00
|
|
|
end
|