2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
describe UploadValidator do
|
2018-04-19 07:30:31 -04:00
|
|
|
subject(:validator) { described_class.new }
|
|
|
|
|
|
|
|
describe 'validate' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-04-19 07:30:31 -04:00
|
|
|
let(:filename) { "discourse.csv" }
|
|
|
|
let(:csv_file) { file_from_fixtures(filename, "csv") }
|
|
|
|
|
|
|
|
it "should create an invalid upload when the filename is blank" do
|
|
|
|
SiteSetting.authorized_extensions = "*"
|
|
|
|
created_upload = UploadCreator.new(csv_file, nil).create_for(user.id)
|
|
|
|
validator.validate(created_upload)
|
|
|
|
expect(created_upload).to_not be_valid
|
2018-04-19 15:09:12 -04:00
|
|
|
expect(created_upload.errors.full_messages.first).to include(I18n.t("activerecord.errors.messages.blank"))
|
2018-04-19 07:30:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows 'gz' as extension when uploading export file" do
|
|
|
|
SiteSetting.authorized_extensions = ""
|
|
|
|
|
2019-07-18 08:34:48 -04:00
|
|
|
expect(UploadCreator.new(csv_file, "#{filename}.zip", for_export: true).create_for(user.id)).to be_valid
|
2018-04-19 07:30:31 -04:00
|
|
|
end
|
|
|
|
|
2018-07-31 01:45:31 -04:00
|
|
|
it "allows uses max_export_file_size_kb when uploading export file" do
|
|
|
|
SiteSetting.max_attachment_size_kb = "0"
|
2019-07-18 08:34:48 -04:00
|
|
|
SiteSetting.authorized_extensions = "zip"
|
2018-07-31 01:45:31 -04:00
|
|
|
|
2019-07-18 08:34:48 -04:00
|
|
|
expect(UploadCreator.new(csv_file, "#{filename}.zip", for_export: true).create_for(user.id)).to be_valid
|
2018-07-31 01:45:31 -04:00
|
|
|
end
|
2018-11-12 01:11:32 -05:00
|
|
|
|
FEATURE: Humanize file size error messages (#14398)
The file size error messages for max_image_size_kb and
max_attachment_size_kb are shown to the user in the KB
format, regardless of how large the limit is. Since we
are going to support uploading much larger files soon,
this KB-based limit soon becomes unfriendly to the end
user.
For example, if the max attachment size is set to 512000
KB, this is what the user sees:
> Sorry, the file you are trying to upload is too big (maximum
size is 512000KB)
This makes the user do math. In almost all file explorers that
a regular user would be familiar width, the file size is shown
in a format based on the maximum increment (e.g. KB, MB, GB).
This commit changes the behaviour to output a humanized file size
instead of the raw KB. For the above example, it would now say:
> Sorry, the file you are trying to upload is too big (maximum
size is 512 MB)
This humanization also handles decimals, e.g. 1536KB = 1.5 MB
2021-09-21 17:59:45 -04:00
|
|
|
describe "size validation" do
|
|
|
|
it "does not allow images that are too large" do
|
|
|
|
SiteSetting.max_image_size_kb = 1536
|
|
|
|
upload = Fabricate.build(:upload,
|
|
|
|
user: Fabricate(:admin),
|
|
|
|
original_filename: "test.png",
|
|
|
|
filesize: 2097152
|
|
|
|
)
|
|
|
|
subject.validate(upload)
|
|
|
|
expect(upload.errors.full_messages.first).to eq(
|
|
|
|
"Filesize #{I18n.t("upload.images.too_large_humanized", max_size: "1.5 MB")}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 01:11:32 -05:00
|
|
|
describe 'when allow_staff_to_upload_any_file_in_pm is true' do
|
|
|
|
it 'should allow uploads for pm' do
|
|
|
|
upload = Fabricate.build(:upload,
|
|
|
|
user: Fabricate(:admin),
|
|
|
|
original_filename: 'test.ico',
|
|
|
|
for_private_message: true
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(subject.validate(upload)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for a normal user' do
|
|
|
|
it 'should not allow uploads for pm' do
|
|
|
|
upload = Fabricate.build(:upload,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
original_filename: 'test.ico',
|
|
|
|
for_private_message: true
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(subject.validate(upload)).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-14 02:03:02 -05:00
|
|
|
|
|
|
|
describe 'upload for site settings' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:admin) }
|
2018-11-14 02:03:02 -05:00
|
|
|
|
|
|
|
let(:upload) do
|
|
|
|
Fabricate.build(:upload,
|
|
|
|
user: user,
|
|
|
|
original_filename: 'test.ico',
|
|
|
|
for_site_setting: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.authorized_extensions = 'png'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for admin user' do
|
|
|
|
it 'should allow the upload' do
|
|
|
|
expect(subject.validate(upload)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when filename is invalid' do
|
|
|
|
it 'should not allow the upload' do
|
|
|
|
upload.original_filename = 'test.txt'
|
|
|
|
expect(subject.validate(upload)).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for normal user' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-11-14 02:03:02 -05:00
|
|
|
|
|
|
|
it 'should not allow the upload' do
|
|
|
|
expect(subject.validate(upload)).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-19 07:30:31 -04:00
|
|
|
end
|
|
|
|
end
|