2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-12-20 13:17:21 +08:00
|
|
|
require 'tempfile'
|
|
|
|
|
2017-03-13 22:47:43 +08:00
|
|
|
class GlobalSetting
|
|
|
|
def self.reset_secret_key_base!
|
|
|
|
@safe_secret_key_base = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-02 13:47:57 +08:00
|
|
|
describe GlobalSetting do
|
2017-03-13 22:19:02 +08:00
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
describe '.use_s3_assets?' do
|
|
|
|
it 'returns false by default' do
|
|
|
|
expect(GlobalSetting.use_s3?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true once set' do
|
|
|
|
global_setting :s3_bucket, 'test_bucket'
|
|
|
|
global_setting :s3_region, 'ap-australia'
|
|
|
|
global_setting :s3_access_key_id, '123'
|
|
|
|
global_setting :s3_secret_access_key, '123'
|
|
|
|
|
|
|
|
expect(GlobalSetting.use_s3?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-13 22:19:02 +08:00
|
|
|
describe '.safe_secret_key_base' do
|
|
|
|
it 'sets redis token if it is somehow flushed after 30 seconds' do
|
2017-03-13 22:47:43 +08:00
|
|
|
|
|
|
|
# we have to reset so we reset all times and test runs consistently
|
|
|
|
GlobalSetting.reset_secret_key_base!
|
|
|
|
|
|
|
|
freeze_time Time.now
|
|
|
|
|
2017-03-13 22:19:02 +08:00
|
|
|
token = GlobalSetting.safe_secret_key_base
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.without_namespace.del(GlobalSetting::REDIS_SECRET_KEY)
|
2017-03-13 22:47:43 +08:00
|
|
|
freeze_time Time.now + 20
|
2017-03-13 22:19:02 +08:00
|
|
|
|
|
|
|
GlobalSetting.safe_secret_key_base
|
2019-12-03 17:05:53 +08:00
|
|
|
new_token = Discourse.redis.without_namespace.get(GlobalSetting::REDIS_SECRET_KEY)
|
2017-03-13 22:19:02 +08:00
|
|
|
expect(new_token).to eq(nil)
|
|
|
|
|
2017-03-13 22:47:43 +08:00
|
|
|
freeze_time Time.now + 11
|
2017-03-13 22:19:02 +08:00
|
|
|
|
|
|
|
GlobalSetting.safe_secret_key_base
|
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
new_token = Discourse.redis.without_namespace.get(GlobalSetting::REDIS_SECRET_KEY)
|
2017-03-13 22:19:02 +08:00
|
|
|
expect(new_token).to eq(token)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-16 05:38:24 +08:00
|
|
|
describe '.add_default' do
|
|
|
|
after do
|
|
|
|
class <<GlobalSetting; remove_method :foo_bar_foo; end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can correctly add defaults" do
|
|
|
|
GlobalSetting.add_default "foo_bar_foo", 1
|
|
|
|
expect(GlobalSetting.foo_bar_foo).to eq(1)
|
|
|
|
|
|
|
|
GlobalSetting.add_default "cdn_url", "a"
|
|
|
|
expect(GlobalSetting.foo_bar_foo).not_to eq("a")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-02 13:47:57 +08:00
|
|
|
describe '.redis_config' do
|
|
|
|
describe 'when slave config is not present' do
|
|
|
|
it "should not set any connector" do
|
|
|
|
expect(GlobalSetting.redis_config[:connector]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when slave config is present' do
|
2017-02-03 08:38:19 +08:00
|
|
|
before do
|
|
|
|
GlobalSetting.reset_redis_config!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-02-02 13:47:57 +08:00
|
|
|
GlobalSetting.reset_redis_config!
|
2017-02-03 08:38:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should set the right connector" do
|
2017-02-02 13:47:57 +08:00
|
|
|
GlobalSetting.expects(:redis_slave_port).returns(6379).at_least_once
|
|
|
|
GlobalSetting.expects(:redis_slave_host).returns('0.0.0.0').at_least_once
|
|
|
|
|
|
|
|
expect(GlobalSetting.redis_config[:connector]).to eq(DiscourseRedis::Connector)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-02 07:46:09 +08:00
|
|
|
describe GlobalSetting::EnvProvider do
|
|
|
|
it "can detect keys from env" do
|
|
|
|
ENV['DISCOURSE_BLA'] = '1'
|
2015-08-14 15:01:06 +08:00
|
|
|
ENV['DISCOURSE_BLA_2'] = '2'
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(GlobalSetting::EnvProvider.new.keys).to include(:bla)
|
2015-08-14 15:01:06 +08:00
|
|
|
expect(GlobalSetting::EnvProvider.new.keys).to include(:bla_2)
|
2014-01-02 07:46:09 +08:00
|
|
|
end
|
|
|
|
end
|
2017-02-02 13:47:57 +08:00
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
describe GlobalSetting::FileProvider do
|
|
|
|
it "can parse a simple file" do
|
|
|
|
f = Tempfile.new('foo')
|
|
|
|
f.write(" # this is a comment\n")
|
|
|
|
f.write("\n")
|
2015-03-09 09:15:10 +08:00
|
|
|
f.write(" a = 1000 # this is a comment\n")
|
2013-12-20 13:17:21 +08:00
|
|
|
f.write("b = \"10 # = 00\" # this is a # comment\n")
|
|
|
|
f.write("c = \'10 # = 00\' # this is a # comment\n")
|
2013-12-30 05:42:55 +08:00
|
|
|
f.write("d =\n")
|
2015-03-09 09:15:10 +08:00
|
|
|
f.write("#f = 1\n")
|
2015-08-14 15:01:06 +08:00
|
|
|
f.write("a1 = 1\n")
|
2013-12-20 13:17:21 +08:00
|
|
|
f.close
|
|
|
|
|
|
|
|
provider = GlobalSetting::FileProvider.from(f.path)
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(provider.lookup(:a, "")).to eq 1000
|
|
|
|
expect(provider.lookup(:b, "")).to eq "10 # = 00"
|
|
|
|
expect(provider.lookup(:c, "")).to eq "10 # = 00"
|
|
|
|
expect(provider.lookup(:d, "bob")).to eq nil
|
|
|
|
expect(provider.lookup(:e, "bob")).to eq "bob"
|
|
|
|
expect(provider.lookup(:f, "bob")).to eq "bob"
|
|
|
|
expect(provider.lookup(:a1, "")).to eq 1
|
2013-12-20 13:17:21 +08:00
|
|
|
|
2015-08-14 15:01:06 +08:00
|
|
|
expect(provider.keys.sort).to eq [:a, :a1, :b, :c, :d]
|
2014-01-02 07:46:09 +08:00
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
f.unlink
|
|
|
|
end
|
2014-01-02 07:46:09 +08:00
|
|
|
|
2014-02-15 05:35:30 +08:00
|
|
|
it "uses ERB" do
|
|
|
|
f = Tempfile.new('foo')
|
|
|
|
f.write("a = <%= 500 %> # this is a comment\n")
|
|
|
|
f.close
|
|
|
|
|
|
|
|
provider = GlobalSetting::FileProvider.from(f.path)
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(provider.lookup(:a, "")).to eq 500
|
2014-02-15 05:35:30 +08:00
|
|
|
|
|
|
|
f.unlink
|
|
|
|
end
|
2013-12-20 13:17:21 +08:00
|
|
|
end
|