2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-06-22 23:35:06 -04:00
|
|
|
require_dependency 'site_setting_extension'
|
|
|
|
require_dependency 'site_settings/local_process_provider'
|
|
|
|
|
|
|
|
describe SiteSettingExtension do
|
2016-01-08 05:53:52 -05:00
|
|
|
|
2018-05-24 22:15:45 -04:00
|
|
|
# We disable message bus here to avoid a large amount
|
|
|
|
# of uneeded messaging, tests are careful to call refresh
|
|
|
|
# when they need to.
|
|
|
|
#
|
|
|
|
# DistributedCache used by locale handler can under certain
|
|
|
|
# cases take a tiny bit to stabalize.
|
|
|
|
#
|
|
|
|
# TODO: refactor SiteSettingExtension not to rely on statics in
|
|
|
|
# DefaultsProvider
|
|
|
|
#
|
|
|
|
before do
|
|
|
|
MessageBus.off
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
MessageBus.on
|
|
|
|
end
|
|
|
|
|
2016-01-08 05:53:52 -05:00
|
|
|
describe '#types' do
|
|
|
|
context "verify enum sequence" do
|
|
|
|
before do
|
|
|
|
@types = SiteSetting.types
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'string' should be at 1st position" do
|
|
|
|
expect(@types[:string]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'value_list' should be at 12th position" do
|
|
|
|
expect(@types[:value_list]).to eq(12)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-10 22:16:09 -05:00
|
|
|
|
2015-09-09 09:34:53 -04:00
|
|
|
let :provider_local do
|
2015-02-04 00:57:16 -05:00
|
|
|
SiteSettings::LocalProcessProvider.new
|
2014-03-28 01:36:17 -04:00
|
|
|
end
|
|
|
|
|
2015-09-09 09:34:53 -04:00
|
|
|
def new_settings(provider)
|
2018-05-24 22:15:45 -04:00
|
|
|
# we want to avoid leaking a big pile of MessageBus subscriptions here (1 per class)
|
|
|
|
# so we set listen_for_changes to false
|
2015-09-09 08:34:44 -04:00
|
|
|
Class.new do
|
2015-02-04 00:57:16 -05:00
|
|
|
extend SiteSettingExtension
|
2018-05-24 22:15:45 -04:00
|
|
|
self.listen_for_changes = false
|
2015-09-09 09:34:53 -04:00
|
|
|
self.provider = provider
|
2015-02-04 00:57:16 -05:00
|
|
|
end
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let :settings do
|
2015-09-09 09:34:53 -04:00
|
|
|
new_settings(provider_local)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
2014-03-28 01:36:17 -04:00
|
|
|
let :settings2 do
|
2015-09-09 09:34:53 -04:00
|
|
|
new_settings(provider_local)
|
2015-09-09 08:34:44 -04:00
|
|
|
end
|
|
|
|
|
2018-05-24 22:15:45 -04:00
|
|
|
it "Does not leak state cause changes are not linked" do
|
|
|
|
t1 = Thread.new do
|
|
|
|
5.times do
|
|
|
|
settings = new_settings(SiteSettings::LocalProcessProvider.new)
|
|
|
|
settings.setting(:title, 'test')
|
|
|
|
settings.title = 'title1'
|
|
|
|
expect(settings.title).to eq 'title1'
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
t2 = Thread.new do
|
|
|
|
5.times do
|
|
|
|
settings = new_settings(SiteSettings::LocalProcessProvider.new)
|
|
|
|
settings.setting(:title, 'test')
|
|
|
|
settings.title = 'title2'
|
|
|
|
expect(settings.title).to eq 'title2'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
t1.join
|
|
|
|
t2.join
|
|
|
|
end
|
|
|
|
|
2014-03-28 01:36:17 -04:00
|
|
|
describe "refresh!" do
|
|
|
|
|
|
|
|
it "will reset to default if provider vanishes" do
|
|
|
|
settings.setting(:hello, 1)
|
|
|
|
settings.hello = 100
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.hello).to eq(100)
|
2014-03-28 01:36:17 -04:00
|
|
|
|
|
|
|
settings.provider.clear
|
|
|
|
settings.refresh!
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.hello).to eq(1)
|
2014-03-28 01:36:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "will set to new value if provider changes" do
|
|
|
|
|
|
|
|
settings.setting(:hello, 1)
|
|
|
|
settings.hello = 100
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.hello).to eq(100)
|
2014-03-28 01:36:17 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
settings.provider.save(:hello, 99, SiteSetting.types[:integer])
|
2014-03-28 01:36:17 -04:00
|
|
|
settings.refresh!
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.hello).to eq(99)
|
2014-03-28 01:36:17 -04:00
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
it "publishes changes cross sites" do
|
2014-03-28 01:36:17 -04:00
|
|
|
settings.setting(:hello, 1)
|
|
|
|
settings2.setting(:hello, 1)
|
|
|
|
|
|
|
|
settings.hello = 100
|
|
|
|
|
|
|
|
settings2.refresh!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings2.hello).to eq(100)
|
2014-03-28 01:36:17 -04:00
|
|
|
|
|
|
|
settings.hello = 99
|
|
|
|
|
|
|
|
settings2.refresh!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings2.hello).to eq(99)
|
2014-03-28 01:36:17 -04:00
|
|
|
end
|
|
|
|
|
2018-07-24 05:24:34 -04:00
|
|
|
it "does not override types in the type supervisor" do
|
|
|
|
settings.setting(:foo, "bar")
|
|
|
|
settings.provider.save(:foo, "bar", SiteSetting.types[:enum])
|
|
|
|
settings.refresh!
|
|
|
|
expect(settings.foo).to eq("bar")
|
|
|
|
|
|
|
|
settings.foo = "baz"
|
|
|
|
expect(settings.foo).to eq("baz")
|
|
|
|
end
|
2014-03-28 01:36:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "multisite" do
|
|
|
|
it "has no db cross talk" do
|
|
|
|
settings.setting(:hello, 1)
|
|
|
|
settings.hello = 100
|
|
|
|
settings.provider.current_site = "boom"
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.hello).to eq(1)
|
2014-03-28 01:36:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-22 23:35:06 -04:00
|
|
|
describe "int setting" do
|
|
|
|
before do
|
|
|
|
settings.setting(:test_setting, 77)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have a key in all_settings" do
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(settings.all_settings.detect { |s| s[:setting] == :test_setting }).to be_present
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct desc" do
|
|
|
|
I18n.expects(:t).with("site_settings.test_setting").returns("test description")
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.description(:test_setting)).to eq("test description")
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct default" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_setting).to eq(77)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when overidden" do
|
|
|
|
after :each do
|
|
|
|
settings.remove_override!(:test_setting)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct override" do
|
|
|
|
settings.test_setting = 100
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_setting).to eq(100)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should coerce correct string to int" do
|
|
|
|
settings.test_setting = "101"
|
2015-04-25 11:33:26 -04:00
|
|
|
expect(settings.test_setting).to eq(101)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should coerce incorrect string to 0" do
|
|
|
|
settings.test_setting = "pie"
|
2015-04-25 11:33:26 -04:00
|
|
|
expect(settings.test_setting).to eq(0)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
2015-04-25 11:33:26 -04:00
|
|
|
it "should not set default when reset" do
|
2013-06-22 23:35:06 -04:00
|
|
|
settings.test_setting = 100
|
|
|
|
settings.setting(:test_setting, 77)
|
|
|
|
settings.refresh!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_setting).not_to eq(77)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
2014-01-27 13:05:35 -05:00
|
|
|
|
|
|
|
it "can be overridden with set" do
|
|
|
|
settings.set("test_setting", 12)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_setting).to eq(12)
|
2014-01-27 13:05:35 -05:00
|
|
|
end
|
2016-02-10 22:16:09 -05:00
|
|
|
|
|
|
|
it "should publish changes to clients" do
|
|
|
|
settings.setting("test_setting", 100)
|
2017-08-06 21:43:09 -04:00
|
|
|
settings.setting("test_setting", nil, client: true)
|
2016-02-10 22:16:09 -05:00
|
|
|
|
2017-10-01 23:34:57 -04:00
|
|
|
message = MessageBus.track_publish('/client_settings') do
|
2016-02-10 22:16:09 -05:00
|
|
|
settings.test_setting = 88
|
2017-10-01 23:34:57 -04:00
|
|
|
end.first
|
2016-02-10 22:16:09 -05:00
|
|
|
|
2017-10-01 23:34:57 -04:00
|
|
|
expect(message).to be_present
|
2016-02-10 22:16:09 -05:00
|
|
|
end
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "remove_override" do
|
2017-08-06 21:43:09 -04:00
|
|
|
before do
|
2017-08-06 21:31:50 -04:00
|
|
|
settings.setting(:test_override, "test")
|
2017-08-06 21:43:09 -04:00
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
it "correctly nukes overrides" do
|
2013-06-22 23:35:06 -04:00
|
|
|
settings.test_override = "bla"
|
|
|
|
settings.remove_override!(:test_override)
|
|
|
|
expect(settings.test_override).to eq("test")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "string setting" do
|
|
|
|
before do
|
|
|
|
settings.setting(:test_str, "str")
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct default" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_str).to eq("str")
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when overridden" do
|
|
|
|
after :each do
|
|
|
|
settings.remove_override!(:test_str)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should coerce int to string" do
|
|
|
|
settings.test_str = 100
|
2015-04-25 11:33:26 -04:00
|
|
|
expect(settings.test_str).to eq("100")
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
2014-01-27 13:05:35 -05:00
|
|
|
|
|
|
|
it "can be overridden with set" do
|
|
|
|
settings.set("test_str", "hi")
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_str).to eq("hi")
|
2014-01-27 13:05:35 -05:00
|
|
|
end
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-26 19:45:56 -05:00
|
|
|
describe "string setting with regex" do
|
|
|
|
it "Supports custom validation errors" do
|
|
|
|
settings.setting(:test_str, "bob", regex: "hi", regex_error: "oops")
|
|
|
|
settings.refresh!
|
|
|
|
|
|
|
|
begin
|
|
|
|
settings.test_str = "a"
|
|
|
|
rescue Discourse::InvalidParameters => e
|
|
|
|
message = e.message
|
|
|
|
end
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(message).to match(/oops/)
|
2015-02-26 19:45:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-22 23:35:06 -04:00
|
|
|
describe "bool setting" do
|
|
|
|
before do
|
|
|
|
settings.setting(:test_hello?, false)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct default" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_hello?).to eq(false)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when overridden" do
|
|
|
|
after do
|
|
|
|
settings.remove_override!(:test_hello?)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct override" do
|
|
|
|
settings.test_hello = true
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_hello?).to eq(true)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should coerce true strings to true" do
|
|
|
|
settings.test_hello = "true"
|
2015-04-25 11:33:26 -04:00
|
|
|
expect(settings.test_hello?).to be(true)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should coerce all other strings to false" do
|
|
|
|
settings.test_hello = "f"
|
2015-04-25 11:33:26 -04:00
|
|
|
expect(settings.test_hello?).to be(false)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
|
2015-04-25 11:33:26 -04:00
|
|
|
it "should not set default when reset" do
|
2013-06-22 23:35:06 -04:00
|
|
|
settings.test_hello = true
|
|
|
|
settings.setting(:test_hello?, false)
|
|
|
|
settings.refresh!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_hello?).not_to eq(false)
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
2014-01-27 13:05:35 -05:00
|
|
|
|
|
|
|
it "can be overridden with set" do
|
|
|
|
settings.set("test_hello", true)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_hello?).to eq(true)
|
2014-01-27 13:05:35 -05:00
|
|
|
end
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-26 02:39:09 -04:00
|
|
|
describe 'int enum' do
|
|
|
|
class TestIntEnumClass
|
|
|
|
def self.valid_value?(v)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def self.values
|
2017-07-27 21:20:09 -04:00
|
|
|
[1, 2, 3]
|
2015-08-26 02:39:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should coerce correctly' do
|
2015-09-09 09:34:53 -04:00
|
|
|
settings.setting(:test_int_enum, 1, enum: TestIntEnumClass)
|
|
|
|
settings.test_int_enum = "2"
|
|
|
|
settings.refresh!
|
2017-08-06 21:43:09 -04:00
|
|
|
expect(settings.defaults[:test_int_enum]).to eq(1)
|
2015-09-09 09:34:53 -04:00
|
|
|
expect(settings.test_int_enum).to eq(2)
|
2015-08-26 02:39:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-06-09 14:27:44 -04:00
|
|
|
describe 'enum setting' do
|
|
|
|
|
|
|
|
class TestEnumClass
|
|
|
|
def self.valid_value?(v)
|
2017-08-06 21:43:09 -04:00
|
|
|
self.values.include?(v)
|
2014-06-09 14:27:44 -04:00
|
|
|
end
|
|
|
|
def self.values
|
|
|
|
['en']
|
|
|
|
end
|
|
|
|
def self.translate_names?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let :test_enum_class do
|
|
|
|
TestEnumClass
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
settings.setting(:test_enum, 'en', enum: test_enum_class)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should have the correct default' do
|
|
|
|
expect(settings.test_enum).to eq('en')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not hose all_settings' do
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(settings.all_settings.detect { |s| s[:setting] == :test_enum }).to be_present
|
2014-06-09 14:27:44 -04:00
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
it 'should report error when being set other values' do
|
|
|
|
expect { settings.test_enum = 'not_in_enum' }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
2014-06-09 14:27:44 -04:00
|
|
|
context 'when overridden' do
|
2014-06-09 15:17:36 -04:00
|
|
|
after :each do
|
|
|
|
settings.remove_override!(:validated_setting)
|
|
|
|
end
|
|
|
|
|
2014-06-09 14:27:44 -04:00
|
|
|
it 'stores valid values' do
|
|
|
|
test_enum_class.expects(:valid_value?).with('fr').returns(true)
|
|
|
|
settings.test_enum = 'fr'
|
|
|
|
expect(settings.test_enum).to eq('fr')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects invalid values' do
|
|
|
|
test_enum_class.expects(:valid_value?).with('gg').returns(false)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect { settings.test_enum = 'gg' }.to raise_error(Discourse::InvalidParameters)
|
2014-06-09 14:27:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-13 14:02:47 -05:00
|
|
|
|
|
|
|
describe 'a setting with a category' do
|
|
|
|
before do
|
2017-07-27 21:20:09 -04:00
|
|
|
settings.setting(:test_setting, 88, category: :tests)
|
2013-11-13 14:02:47 -05:00
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the category in all_settings" do
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(settings.all_settings.find { |s| s[:setting] == :test_setting }[:category]).to eq(:tests)
|
2013-11-13 14:02:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when overidden" do
|
|
|
|
after :each do
|
|
|
|
settings.remove_override!(:test_setting)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the correct override" do
|
|
|
|
settings.test_setting = 101
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.test_setting).to eq(101)
|
2013-11-13 14:02:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should still have the correct category" do
|
|
|
|
settings.test_setting = 102
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(settings.all_settings.find { |s| s[:setting] == :test_setting }[:category]).to eq(:tests)
|
2013-11-13 14:02:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-09 15:17:36 -04:00
|
|
|
describe "setting with a validator" do
|
|
|
|
before do
|
2017-07-27 21:20:09 -04:00
|
|
|
settings.setting(:validated_setting, "info@example.com", type: 'email')
|
2014-06-09 15:17:36 -04:00
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
after :each do
|
|
|
|
settings.remove_override!(:validated_setting)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "stores valid values" do
|
2014-06-12 18:03:03 -04:00
|
|
|
EmailSettingValidator.any_instance.expects(:valid_value?).returns(true)
|
2014-06-09 15:17:36 -04:00
|
|
|
settings.validated_setting = 'success@example.com'
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.validated_setting).to eq('success@example.com')
|
2014-06-09 15:17:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "rejects invalid values" do
|
|
|
|
expect {
|
2014-06-12 18:03:03 -04:00
|
|
|
EmailSettingValidator.any_instance.expects(:valid_value?).returns(false)
|
2014-06-09 15:17:36 -04:00
|
|
|
settings.validated_setting = 'nope'
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.validated_setting).to eq("info@example.com")
|
2014-06-09 15:17:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows blank values" do
|
|
|
|
settings.validated_setting = ''
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.validated_setting).to eq('')
|
2014-06-09 15:17:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-27 13:05:35 -05:00
|
|
|
describe "set for an invalid setting name" do
|
|
|
|
it "raises an error" do
|
|
|
|
settings.setting(:test_setting, 77)
|
|
|
|
settings.refresh!
|
|
|
|
expect {
|
|
|
|
settings.set("provider", "haxxed")
|
2018-06-11 01:39:35 -04:00
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
2014-01-27 13:05:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
describe ".set_and_log" do
|
2018-05-14 13:05:55 -04:00
|
|
|
before do
|
2018-06-02 09:57:52 -04:00
|
|
|
settings.setting(:s3_secret_access_key, "old_secret_key", secret: true)
|
2018-05-14 13:05:55 -04:00
|
|
|
settings.setting(:title, "Discourse v1")
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
it "raises an error when set for an invalid setting name" do
|
|
|
|
expect {
|
|
|
|
settings.set_and_log("provider", "haxxed")
|
2018-06-11 01:39:35 -04:00
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
2018-05-14 13:05:55 -04:00
|
|
|
|
|
|
|
it "scrubs secret setting values from logs" do
|
|
|
|
settings.set_and_log("s3_secret_access_key", "new_secret_key")
|
|
|
|
expect(UserHistory.last.previous_value).to eq("[FILTERED]")
|
|
|
|
expect(UserHistory.last.new_value).to eq("[FILTERED]")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works" do
|
|
|
|
settings.set_and_log("title", "Discourse v2")
|
|
|
|
expect(settings.title).to eq("Discourse v2")
|
|
|
|
expect(UserHistory.last.previous_value).to eq("Discourse v1")
|
|
|
|
expect(UserHistory.last.new_value).to eq("Discourse v2")
|
|
|
|
end
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
2014-07-24 08:00:15 -04:00
|
|
|
describe "filter domain name" do
|
|
|
|
before do
|
|
|
|
settings.setting(:white_listed_spam_host_domains, "www.example.com")
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "filters domain" do
|
|
|
|
settings.set("white_listed_spam_host_domains", "http://www.discourse.org/")
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.white_listed_spam_host_domains).to eq("www.discourse.org")
|
2014-07-24 08:00:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns invalid domain as is, without throwing exception" do
|
|
|
|
settings.set("white_listed_spam_host_domains", "test!url")
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.white_listed_spam_host_domains).to eq("test!url")
|
2014-07-24 08:00:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-03 16:47:06 -05:00
|
|
|
describe "hidden" do
|
|
|
|
before do
|
|
|
|
settings.setting(:superman_identity, 'Clark Kent', hidden: true)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is in the `hidden_settings` collection" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.hidden_settings.include?(:superman_identity)).to eq(true)
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be retrieved" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.superman_identity).to eq("Clark Kent")
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is not present in all_settings by default" do
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(settings.all_settings.find { |s| s[:setting] == :superman_identity }).to be_blank
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is present in all_settings when we ask for hidden" do
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(settings.all_settings(true).find { |s| s[:setting] == :superman_identity }).to be_present
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "shadowed_by_global" do
|
2018-05-31 22:22:47 -04:00
|
|
|
|
|
|
|
context "default_locale" do
|
|
|
|
it "supports adding a default locale via a global" do
|
|
|
|
global_setting :default_locale, 'zh_CN'
|
|
|
|
settings.default_locale = 'en'
|
|
|
|
expect(settings.default_locale).to eq('zh_CN')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-03 16:47:06 -05:00
|
|
|
context "without global setting" do
|
|
|
|
before do
|
|
|
|
settings.setting(:trout_api_key, 'evil', shadowed_by_global: true)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not add the key to the shadowed_settings collection" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.shadowed_settings.include?(:trout_api_key)).to eq(false)
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can return the default value" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.trout_api_key).to eq('evil')
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can overwrite the default" do
|
|
|
|
settings.trout_api_key = 'tophat'
|
|
|
|
settings.refresh!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.trout_api_key).to eq('tophat')
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-09 19:54:40 -05:00
|
|
|
context "with blank global setting" do
|
|
|
|
before do
|
|
|
|
GlobalSetting.stubs(:nada).returns('')
|
|
|
|
settings.setting(:nada, 'nothing', shadowed_by_global: true)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return default cause nothing is set" do
|
|
|
|
expect(settings.nada).to eq('nothing')
|
|
|
|
end
|
2017-10-26 20:12:44 -04:00
|
|
|
|
2016-02-09 19:54:40 -05:00
|
|
|
end
|
|
|
|
|
2016-02-13 01:49:26 -05:00
|
|
|
context "with a false override" do
|
|
|
|
before do
|
|
|
|
GlobalSetting.stubs(:bool).returns(false)
|
|
|
|
settings.setting(:bool, true, shadowed_by_global: true)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return default cause nothing is set" do
|
|
|
|
expect(settings.bool).to eq(false)
|
|
|
|
end
|
2017-10-26 20:12:44 -04:00
|
|
|
|
|
|
|
it "should not trigger any message bus work if you try to set it" do
|
|
|
|
m = MessageBus.track_publish('/site_settings') do
|
|
|
|
settings.bool = true
|
|
|
|
expect(settings.bool).to eq(false)
|
|
|
|
end
|
|
|
|
expect(m.length).to eq(0)
|
|
|
|
end
|
2016-02-13 01:49:26 -05:00
|
|
|
end
|
|
|
|
|
2015-02-03 16:47:06 -05:00
|
|
|
context "with global setting" do
|
|
|
|
before do
|
|
|
|
GlobalSetting.stubs(:trout_api_key).returns('purringcat')
|
|
|
|
settings.setting(:trout_api_key, 'evil', shadowed_by_global: true)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the global setting instead of default" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.trout_api_key).to eq('purringcat')
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the global setting after a refresh" do
|
|
|
|
settings.refresh!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.trout_api_key).to eq('purringcat')
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
|
2016-08-11 04:04:45 -04:00
|
|
|
it "should add the key to the hidden_settings collection" do
|
|
|
|
expect(settings.hidden_settings.include?(:trout_api_key)).to eq(true)
|
|
|
|
|
|
|
|
['', nil].each_with_index do |setting, index|
|
|
|
|
GlobalSetting.stubs(:"trout_api_key_#{index}").returns(setting)
|
|
|
|
settings.setting(:"trout_api_key_#{index}", 'evil', shadowed_by_global: true)
|
|
|
|
settings.refresh!
|
|
|
|
expect(settings.hidden_settings.include?(:"trout_api_key_#{index}")).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-03 16:47:06 -05:00
|
|
|
it "should add the key to the shadowed_settings collection" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(settings.shadowed_settings.include?(:trout_api_key)).to eq(true)
|
2015-02-03 16:47:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-02 09:57:52 -04:00
|
|
|
describe "secret" do
|
|
|
|
before do
|
|
|
|
settings.setting(:superman_identity, 'Clark Kent', secret: true)
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is in the `secret_settings` collection" do
|
|
|
|
expect(settings.secret_settings.include?(:superman_identity)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be retrieved" do
|
|
|
|
expect(settings.superman_identity).to eq("Clark Kent")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is present in all_settings by default" do
|
|
|
|
secret_setting = settings.all_settings.find { |s| s[:setting] == :superman_identity }
|
|
|
|
expect(secret_setting).to be_present
|
|
|
|
expect(secret_setting[:secret]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
describe 'locale default overrides are respected' do
|
|
|
|
before do
|
|
|
|
settings.setting(:test_override, 'default', locale_default: { zh_CN: 'cn' })
|
|
|
|
settings.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
settings.remove_override!(:test_override)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures the default cache expired after overriding the default_locale' do
|
|
|
|
expect(settings.test_override).to eq('default')
|
|
|
|
settings.default_locale = 'zh_CN'
|
|
|
|
expect(settings.test_override).to eq('cn')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the saved setting even locale default exists' do
|
|
|
|
expect(settings.test_override).to eq('default')
|
|
|
|
settings.default_locale = 'zh_CN'
|
|
|
|
settings.test_override = 'saved'
|
|
|
|
expect(settings.test_override).to eq('saved')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.requires_refresh?' do
|
|
|
|
it 'always refresh default_locale always require refresh' do
|
|
|
|
expect(settings.requires_refresh?(:default_locale)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.default_locale' do
|
|
|
|
it 'is always loaded' do
|
|
|
|
expect(settings.default_locale).to eq 'en'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.default_locale=' do
|
|
|
|
it 'can be changed' do
|
|
|
|
settings.default_locale = 'zh_CN'
|
|
|
|
expect(settings.default_locale).to eq 'zh_CN'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refresh!' do
|
|
|
|
settings.expects(:refresh!)
|
|
|
|
settings.default_locale = 'zh_CN'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expires the cache' do
|
|
|
|
settings.default_locale = 'zh_CN'
|
|
|
|
expect(Rails.cache.exist?(SiteSettingExtension.client_settings_cache_key)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refreshes the client' do
|
|
|
|
Discourse.expects(:request_refresh!)
|
|
|
|
settings.default_locale = 'zh_CN'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-12 11:09:09 -04:00
|
|
|
describe "get_hostname" do
|
|
|
|
|
|
|
|
it "properly extracts the hostname" do
|
|
|
|
expect(settings.send(:get_hostname, "discourse.org")).to eq("discourse.org")
|
|
|
|
expect(settings.send(:get_hostname, " discourse.org ")).to eq("discourse.org")
|
|
|
|
expect(settings.send(:get_hostname, "@discourse.org")).to eq("discourse.org")
|
|
|
|
expect(settings.send(:get_hostname, "https://discourse.org")).to eq("discourse.org")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-22 23:35:06 -04:00
|
|
|
end
|