2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-19 19:30:31 +08:00
|
|
|
require 'rails_helper'
|
2018-06-05 15:29:17 +08:00
|
|
|
require_dependency 'validators/upload_validator'
|
2018-04-19 19:30:31 +08:00
|
|
|
|
|
|
|
describe Validators::UploadValidator do
|
|
|
|
subject(:validator) { described_class.new }
|
|
|
|
|
|
|
|
describe 'validate' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-04-19 19:30:31 +08: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-20 03:09:12 +08:00
|
|
|
expect(created_upload.errors.full_messages.first).to include(I18n.t("activerecord.errors.messages.blank"))
|
2018-04-19 19:30:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows 'gz' as extension when uploading export file" do
|
|
|
|
SiteSetting.authorized_extensions = ""
|
|
|
|
|
2018-07-31 13:45:31 +08:00
|
|
|
expect(UploadCreator.new(csv_file, "#{filename}.gz", for_export: true).create_for(user.id)).to be_valid
|
2018-04-19 19:30:31 +08:00
|
|
|
end
|
|
|
|
|
2018-07-31 13:45:31 +08:00
|
|
|
it "allows uses max_export_file_size_kb when uploading export file" do
|
|
|
|
SiteSetting.max_attachment_size_kb = "0"
|
|
|
|
SiteSetting.authorized_extensions = "gz"
|
|
|
|
|
|
|
|
expect(UploadCreator.new(csv_file, "#{filename}.gz", for_export: true).create_for(user.id)).to be_valid
|
|
|
|
end
|
2018-11-12 14:11:32 +08: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 15:03:02 +08:00
|
|
|
|
|
|
|
describe 'upload for site settings' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:admin) }
|
2018-11-14 15:03:02 +08: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-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-11-14 15:03:02 +08:00
|
|
|
|
|
|
|
it 'should not allow the upload' do
|
|
|
|
expect(subject.validate(upload)).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-19 19:30:31 +08:00
|
|
|
end
|
|
|
|
end
|