mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Upload Site Settings. (#6573)
This commit is contained in:
@@ -703,4 +703,39 @@ describe SiteSettingExtension do
|
||||
|
||||
end
|
||||
|
||||
describe '.client_settings_json_uncached' do
|
||||
it 'should return the right json value' do
|
||||
upload = Fabricate(:upload)
|
||||
settings.setting(:upload_type, upload.id.to_s, type: :upload, client: true)
|
||||
settings.setting(:string_type, 'haha', client: true)
|
||||
settings.refresh!
|
||||
|
||||
expect(settings.client_settings_json_uncached).to eq(
|
||||
%Q|{"default_locale":"en","upload_type":"#{upload.url}","string_type":"haha"}|
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.setup_methods' do
|
||||
describe 'for uploads site settings' do
|
||||
let(:upload) { Fabricate(:upload) }
|
||||
let(:upload2) { Fabricate(:upload) }
|
||||
|
||||
it 'should return the upload record' do
|
||||
settings.setting(:some_upload, upload.id.to_s, type: :upload)
|
||||
|
||||
expect(settings.some_upload).to eq(upload)
|
||||
|
||||
# Ensure that we cache the upload record
|
||||
expect(settings.some_upload.object_id).to eq(
|
||||
settings.some_upload.object_id
|
||||
)
|
||||
|
||||
settings.some_upload = upload2
|
||||
|
||||
expect(settings.some_upload).to eq(upload2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -79,6 +79,10 @@ describe SiteSettings::TypeSupervisor do
|
||||
it "'uploaded_image_list' should be at 17th position" do
|
||||
expect(SiteSettings::TypeSupervisor.types[:uploaded_image_list]).to eq(17)
|
||||
end
|
||||
|
||||
it "'upload' should be at the right position" do
|
||||
expect(SiteSettings::TypeSupervisor.types[:upload]).to eq(18)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -151,10 +155,11 @@ describe SiteSettings::TypeSupervisor do
|
||||
settings.setting(:type_validator, 5, validator: 'TestSmallThanTenValidator')
|
||||
settings.setting(:type_mock_validate_method, 'no_value')
|
||||
settings.setting(:type_custom, 'custom', type: 'list')
|
||||
settings.setting(:type_upload, '', type: 'upload')
|
||||
settings.refresh!
|
||||
end
|
||||
|
||||
describe '.to_db_value' do
|
||||
describe '#to_db_value' do
|
||||
let(:true_val) { 't' }
|
||||
let(:false_val) { 'f' }
|
||||
|
||||
@@ -187,6 +192,13 @@ describe SiteSettings::TypeSupervisor do
|
||||
expect(settings.type_supervisor.to_db_value(:type_string, 'a')).to eq ['a', SiteSetting.types[:string]]
|
||||
end
|
||||
|
||||
it 'returns the upload id' do
|
||||
upload = Fabricate(:upload)
|
||||
|
||||
expect(settings.type_supervisor.to_db_value(:type_upload, upload))
|
||||
.to eq([upload.id, SiteSetting.types[:upload]])
|
||||
end
|
||||
|
||||
it 'returns enum value with string default' do
|
||||
expect(settings.type_supervisor.to_db_value(:type_enum_default_string, 2)).to eq ['2', SiteSetting.types[:enum]]
|
||||
expect(settings.type_supervisor.to_db_value(:type_enum_default_string, '2')).to eq ['2', SiteSetting.types[:enum]]
|
||||
@@ -224,7 +236,7 @@ describe SiteSettings::TypeSupervisor do
|
||||
end
|
||||
end
|
||||
|
||||
describe '.to_rb_value' do
|
||||
describe '#to_rb_value' do
|
||||
let(:true_val) { 't' }
|
||||
let(:false_val) { 'f' }
|
||||
|
||||
@@ -266,6 +278,16 @@ describe SiteSettings::TypeSupervisor do
|
||||
expect(settings.type_supervisor.to_rb_value(:type_string, 2)).to eq '2'
|
||||
end
|
||||
|
||||
it 'returns the upload record' do
|
||||
upload = Fabricate(:upload)
|
||||
|
||||
expect(settings.type_supervisor.to_rb_value(:type_upload, ''))
|
||||
.to eq('')
|
||||
|
||||
expect(settings.type_supervisor.to_rb_value(:type_upload, upload.id))
|
||||
.to eq(upload.id)
|
||||
end
|
||||
|
||||
it 'returns value with string default' do
|
||||
expect(settings.type_supervisor.to_rb_value(:type_enum_default_string, 2)).to eq '2'
|
||||
expect(settings.type_supervisor.to_rb_value(:type_enum_default_string, '2')).to eq '2'
|
||||
@@ -284,6 +306,18 @@ describe SiteSettings::TypeSupervisor do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#get_type' do
|
||||
before do
|
||||
settings.setting(:type_null, nil)
|
||||
settings.setting(:type_upload, '', type: :upload)
|
||||
end
|
||||
|
||||
it 'should return the right type that has been registered' do
|
||||
expect(settings.type_supervisor.get_type(:type_null)).to eq(:null)
|
||||
expect(settings.type_supervisor.get_type(:type_upload)).to eq(:upload)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.type_hash' do
|
||||
class TestEnumClass2
|
||||
def self.valid_value?(v)
|
||||
|
||||
@@ -53,5 +53,42 @@ describe Validators::UploadValidator do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'upload for site settings' do
|
||||
let(:user) { Fabricate(:admin) }
|
||||
|
||||
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
|
||||
let(:user) { Fabricate(:user) }
|
||||
|
||||
it 'should not allow the upload' do
|
||||
expect(subject.validate(upload)).to eq(nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -214,38 +214,49 @@ describe Wizard::StepUpdater do
|
||||
|
||||
context "logos step" do
|
||||
it "updates the fields correctly" do
|
||||
updater = wizard.create_updater('logos',
|
||||
logo_url: '/uploads/logo.png',
|
||||
logo_small_url: '/uploads/logo-small.png')
|
||||
upload = Fabricate(:upload)
|
||||
upload2 = Fabricate(:upload)
|
||||
|
||||
updater = wizard.create_updater(
|
||||
'logos',
|
||||
logo: upload.url,
|
||||
logo_small: upload2.url
|
||||
)
|
||||
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(wizard.completed_steps?('logos')).to eq(true)
|
||||
expect(SiteSetting.logo_url).to eq('/uploads/logo.png')
|
||||
expect(SiteSetting.logo_small_url).to eq('/uploads/logo-small.png')
|
||||
expect(SiteSetting.logo).to eq(upload)
|
||||
expect(SiteSetting.logo_small).to eq(upload2)
|
||||
end
|
||||
end
|
||||
|
||||
context "icons step" do
|
||||
it "updates the fields correctly" do
|
||||
upload = Fabricate(:upload)
|
||||
upload2 = Fabricate(:upload)
|
||||
|
||||
updater = wizard.create_updater('icons',
|
||||
favicon_url: "/uploads/favicon.png",
|
||||
apple_touch_icon_url: "/uploads/apple.png")
|
||||
favicon: upload.url,
|
||||
apple_touch_icon: upload2.url
|
||||
)
|
||||
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(wizard.completed_steps?('icons')).to eq(true)
|
||||
expect(SiteSetting.favicon_url).to eq('/uploads/favicon.png')
|
||||
expect(SiteSetting.apple_touch_icon_url).to eq('/uploads/apple.png')
|
||||
expect(SiteSetting.favicon).to eq(upload)
|
||||
expect(SiteSetting.apple_touch_icon).to eq(upload2)
|
||||
end
|
||||
|
||||
it "updates large_icon_url if the uploaded icon size is greater than 180x180" do
|
||||
it "updates large_icon if the uploaded icon size is greater than 180x180" do
|
||||
upload = Fabricate(:upload, width: 512, height: 512)
|
||||
updater = wizard.create_updater('icons', apple_touch_icon_url: upload.url)
|
||||
updater = wizard.create_updater('icons', apple_touch_icon: upload.url)
|
||||
updater.update
|
||||
|
||||
expect(updater).to be_success
|
||||
expect(SiteSetting.large_icon_url).to eq(upload.url)
|
||||
expect(SiteSetting.large_icon).to eq(upload)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user