2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-12-20 00:17:21 -05:00
|
|
|
require 'tempfile'
|
|
|
|
|
2017-03-13 10:47:43 -04:00
|
|
|
class GlobalSetting
|
|
|
|
def self.reset_secret_key_base!
|
|
|
|
@safe_secret_key_base = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-02 00:47:57 -05:00
|
|
|
describe GlobalSetting do
|
2017-03-13 10:19:02 -04:00
|
|
|
|
2017-10-06 01:20:01 -04: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 10:19:02 -04:00
|
|
|
describe '.safe_secret_key_base' do
|
|
|
|
it 'sets redis token if it is somehow flushed after 30 seconds' do
|
2017-03-13 10:47:43 -04: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 10:19:02 -04:00
|
|
|
token = GlobalSetting.safe_secret_key_base
|
|
|
|
$redis.without_namespace.del(GlobalSetting::REDIS_SECRET_KEY)
|
2017-03-13 10:47:43 -04:00
|
|
|
freeze_time Time.now + 20
|
2017-03-13 10:19:02 -04:00
|
|
|
|
|
|
|
GlobalSetting.safe_secret_key_base
|
|
|
|
new_token = $redis.without_namespace.get(GlobalSetting::REDIS_SECRET_KEY)
|
|
|
|
expect(new_token).to eq(nil)
|
|
|
|
|
2017-03-13 10:47:43 -04:00
|
|
|
freeze_time Time.now + 11
|
2017-03-13 10:19:02 -04:00
|
|
|
|
|
|
|
GlobalSetting.safe_secret_key_base
|
|
|
|
|
|
|
|
new_token = $redis.without_namespace.get(GlobalSetting::REDIS_SECRET_KEY)
|
|
|
|
expect(new_token).to eq(token)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-15 16:38:24 -05: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 00:47:57 -05: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-02 19:38:19 -05:00
|
|
|
before do
|
|
|
|
GlobalSetting.reset_redis_config!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-02-02 00:47:57 -05:00
|
|
|
GlobalSetting.reset_redis_config!
|
2017-02-02 19:38:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should set the right connector" do
|
2017-02-02 00:47:57 -05: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-01 18:46:09 -05:00
|
|
|
describe GlobalSetting::EnvProvider do
|
|
|
|
it "can detect keys from env" do
|
|
|
|
ENV['DISCOURSE_BLA'] = '1'
|
2015-08-14 03:01:06 -04:00
|
|
|
ENV['DISCOURSE_BLA_2'] = '2'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(GlobalSetting::EnvProvider.new.keys).to include(:bla)
|
2015-08-14 03:01:06 -04:00
|
|
|
expect(GlobalSetting::EnvProvider.new.keys).to include(:bla_2)
|
2014-01-01 18:46:09 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-02 00:47:57 -05:00
|
|
|
|
2013-12-20 00:17:21 -05: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-08 21:15:10 -04:00
|
|
|
f.write(" a = 1000 # this is a comment\n")
|
2013-12-20 00:17:21 -05:00
|
|
|
f.write("b = \"10 # = 00\" # this is a # comment\n")
|
|
|
|
f.write("c = \'10 # = 00\' # this is a # comment\n")
|
2013-12-29 16:42:55 -05:00
|
|
|
f.write("d =\n")
|
2015-03-08 21:15:10 -04:00
|
|
|
f.write("#f = 1\n")
|
2015-08-14 03:01:06 -04:00
|
|
|
f.write("a1 = 1\n")
|
2013-12-20 00:17:21 -05:00
|
|
|
f.close
|
|
|
|
|
|
|
|
provider = GlobalSetting::FileProvider.from(f.path)
|
|
|
|
|
2017-07-27 21:20:09 -04: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 00:17:21 -05:00
|
|
|
|
2015-08-14 03:01:06 -04:00
|
|
|
expect(provider.keys.sort).to eq [:a, :a1, :b, :c, :d]
|
2014-01-01 18:46:09 -05:00
|
|
|
|
2013-12-20 00:17:21 -05:00
|
|
|
f.unlink
|
|
|
|
end
|
2014-01-01 18:46:09 -05:00
|
|
|
|
2014-02-14 16:35:30 -05: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-27 21:20:09 -04:00
|
|
|
expect(provider.lookup(:a, "")).to eq 500
|
2014-02-14 16:35:30 -05:00
|
|
|
|
|
|
|
f.unlink
|
|
|
|
end
|
2013-12-20 00:17:21 -05:00
|
|
|
end
|