Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

allow have dates parsed from file and env

ulferts285650e6c35e
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 0090360ce34..7245109f253 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -94,6 +94,8 @@ module Settings
end
def valid?
+ # TODO: it would make sense to also check the type of the value (e.g. boolean).
+ # But as using e.g. 0 for a boolean is quite common, that would break.
!allowed ||
(format == :array && (value - allowed).empty?) ||
allowed.include?(value)
@@ -194,7 +196,7 @@ module Settings
file_config = {}
if File.file?(filename)
- file_config = YAML.load_file(filename)
+ file_config = load_yaml(ERB.new(File.read(filename)).result)
if file_config.is_a? Hash
file_config
@@ -294,7 +296,7 @@ module Settings
# To specify specific values, one can use !!str (-> '') or !!null (-> nil)
return original_value if original_value == ''
- parsed = YAML.safe_load(original_value)
+ parsed = load_yaml(original_value)
if parsed.is_a?(String)
original_value
@@ -305,6 +307,10 @@ module Settings
raise ArgumentError, "Configuration value for '#{key}' is invalid: #{e.message}"
end
+ def load_yaml(source)
+ YAML::safe_load(source, permitted_classes: [Symbol, Date])
+ end
+
attr_accessor :loaded
end
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 38f25c84a48..ee72b85e13f 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -87,6 +87,13 @@ describe Settings::Definition do
.to be false
end
+ it 'overriding date configuration from ENV will cast the value' do
+ stub_const('ENV', { 'OPENPROJECT_CONSENT__TIME' => '2222-01-01' })
+
+ expect(all.detect { |d| d.name == 'consent_time' }.value)
+ .to eql Date.parse('2222-01-01')
+ end
+
it 'overriding configuration from ENV will set it to non writable' do
stub_const('ENV', { 'OPENPROJECT_EDITION' => 'bim' })
@@ -142,29 +149,29 @@ describe Settings::Definition do
include_context 'with clean definitions'
let(:file_contents) do
- {
- 'default' => {
- 'edition' => 'bim',
- 'sendmail_location' => 'default location'
- },
- 'test' => {
- 'smtp_address' => 'test address',
- 'sendmail_location' => 'test location',
- 'bogus' => 'bogusvalue'
- }
- }
+ <<~YAML
+ ---
+ default:
+ edition: 'bim'
+ sendmail_location: 'default_location'
+ test:
+ smtp_address: 'test address'
+ sendmail_location: 'test location'
+ bogus: 'bogusvalue'
+ consent_time: 2222-01-01
+ YAML
end
before do
- allow(YAML)
- .to receive(:load_file)
- .with(Rails.root.join('config/configuration.yml'))
- .and_return(file_contents)
-
allow(File)
.to receive(:file?)
- .with(Rails.root.join('config/configuration.yml'))
- .and_return(true)
+ .with(Rails.root.join('config/configuration.yml'))
+ .and_return(true)
+
+ allow(File)
+ .to receive(:read)
+ .with(Rails.root.join('config/configuration.yml'))
+ .and_return(file_contents)
# Loading of the config file is disabled in test env normally.
allow(Rails.env)
@@ -207,13 +214,18 @@ describe Settings::Definition do
.to be_nil
end
+ it 'correctly parses date objects' do
+ expect(all.detect { |d| d.name == 'consent_time' }.value)
+ .to eql DateTime.parse("2222-01-01")
+ end
+
context 'when having invalid values in the file' do
let(:file_contents) do
- {
- 'default' => {
- 'smtp_openssl_verify_mode' => 'bogus'
- }
- }
+ <<~YAML
+ ---
+ default:
+ smtp_openssl_verify_mode: 'bogus'
+ YAML
end
it 'is invalid' do