feat: add GlobalConfigService to support env vars migration (#3288)

This commit is contained in:
Vishnu Narayanan
2021-11-03 23:04:42 +05:30
committed by GitHub
parent 03b1a3d045
commit 97ee1bfa97
7 changed files with 104 additions and 39 deletions

View File

@@ -0,0 +1,44 @@
require 'rails_helper'
describe GlobalConfigService do
subject(:trigger) { described_class }
describe 'execute' do
context 'when called with default options' do
before do
# to clear redis cache
GlobalConfig.clear_cache
end
it 'set default value if not found on db nor env var' do
value = GlobalConfig.get('ENABLE_ACCOUNT_SIGNUP')
expect(value['ENABLE_ACCOUNT_SIGNUP']).to eq nil
described_class.load('ENABLE_ACCOUNT_SIGNUP', 'true')
value = GlobalConfig.get('ENABLE_ACCOUNT_SIGNUP')
expect(value['ENABLE_ACCOUNT_SIGNUP']).to eq 'true'
expect(InstallationConfig.find_by(name: 'ENABLE_ACCOUNT_SIGNUP')&.value).to eq 'true'
end
it 'get value from env variable if not found on DB' do
with_modified_env ENABLE_ACCOUNT_SIGNUP: 'false' do
expect(InstallationConfig.find_by(name: 'ENABLE_ACCOUNT_SIGNUP')&.value).to eq nil
described_class.load('ENABLE_ACCOUNT_SIGNUP', 'true')
value = GlobalConfig.get('ENABLE_ACCOUNT_SIGNUP')
expect(value['ENABLE_ACCOUNT_SIGNUP']).to eq 'false'
end
end
it 'get value from DB if found' do
# Set a value in db first and make sure this value
# is not respected even when load() method is called with
# another value.
InstallationConfig.where(name: 'ENABLE_ACCOUNT_SIGNUP').first_or_create(value: 'true')
described_class.load('ENABLE_ACCOUNT_SIGNUP', 'false')
value = GlobalConfig.get('ENABLE_ACCOUNT_SIGNUP')
expect(value['ENABLE_ACCOUNT_SIGNUP']).to eq 'true'
end
end
end
end