DEV: introduce new API to look up dynamic site setting

This removes all uses of both `send` and `public_send` from consumers of
SiteSetting and instead introduces a `get` helper for dynamic lookup

This leads to much cleaner and safer code long term as we are always explicit
to test that a site setting is really there before sending an arbitrary
string to the class

It also removes a couple of risky stubs from the auth provider test
This commit is contained in:
Sam Saffron
2019-05-07 11:00:09 +10:00
parent 47995d2d89
commit 9be70a22cd
33 changed files with 99 additions and 73 deletions

View File

@@ -130,10 +130,11 @@ describe Plugin::Instance do
end
it 'patches the enabled? function for auth_providers if not defined' do
SiteSetting.stubs(:ubuntu_login_enabled).returns(false)
plugin = Plugin::Instance.new
# lets piggy back on another boolean setting, so we don't dirty our SiteSetting object
SiteSetting.enable_badges = false
# No enabled_site_setting
authenticator = Auth::Authenticator.new
plugin.auth_provider(authenticator: authenticator)
@@ -143,26 +144,28 @@ describe Plugin::Instance do
# With enabled site setting
plugin = Plugin::Instance.new
authenticator = Auth::Authenticator.new
plugin.auth_provider(enabled_setting: 'ubuntu_login_enabled', authenticator: authenticator)
plugin.auth_provider(enabled_setting: 'enable_badges', authenticator: authenticator)
plugin.notify_before_auth
expect(authenticator.enabled?).to eq(false)
# Defines own method
plugin = Plugin::Instance.new
SiteSetting.stubs(:ubuntu_login_enabled).returns(true)
SiteSetting.enable_badges = true
authenticator = Class.new(Auth::Authenticator) do
def enabled?
false
end
end.new
plugin.auth_provider(enabled_setting: 'ubuntu_login_enabled', authenticator: authenticator)
plugin.auth_provider(enabled_setting: 'enable_badges', authenticator: authenticator)
plugin.notify_before_auth
expect(authenticator.enabled?).to eq(false)
end
context "activate!" do
before do
SiteSetting.stubs(:ubuntu_login_enabled).returns(false)
# lets piggy back on another boolean setting, so we don't dirty our SiteSetting object
SiteSetting.enable_badges = false
end
it "can activate plugins correctly" do

View File

@@ -532,6 +532,23 @@ describe SiteSettingExtension do
end
end
describe ".get" do
before do
settings.setting(:title, "Discourse v1")
settings.refresh!
end
it "works correctly" do
expect {
settings.get("frogs_in_africa")
}.to raise_error(Discourse::InvalidParameters)
expect(settings.get(:title)).to eq("Discourse v1")
expect(settings.get("title")).to eq("Discourse v1")
end
end
describe ".set_and_log" do
before do
settings.setting(:s3_secret_access_key, "old_secret_key", secret: true)