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

make nested hash env names work with env_alias for Setting

Christophe Bliard73a8421c42e2
diff --git a/app/helpers/user_consent_helper.rb b/app/helpers/user_consent_helper.rb
index f529eebc882..dd9ec1ef4e4 100644
--- a/app/helpers/user_consent_helper.rb
+++ b/app/helpers/user_consent_helper.rb
@@ -47,7 +47,7 @@ module ::UserConsentHelper
def user_consent_instructions(_user, locale: I18n.locale)
all = Setting.consent_info
- all.fetch(locale) { all.values.first }
+ all.fetch(locale.to_s) { all.values.first }
end
def consent_checkbox_label(locale: I18n.locale)
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 505350dd25f..b21da1d496f 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -317,7 +317,12 @@ module Settings
end
def each_env_var_hash_override(definition)
- hash_override_matcher = /^(?:#{env_name(definition)}|#{env_name_legacy(definition)})_(.+)/i
+ hash_override_matcher =
+ if definition.env_alias
+ /^(?:#{env_name(definition)}|#{env_name_legacy(definition)}|#{env_name_alias(definition)})_(.+)/i
+ else
+ /^(?:#{env_name(definition)}|#{env_name_legacy(definition)})_(.+)/i
+ end
ENV.each do |env_var_name, env_var_value|
env_var_name.match(hash_override_matcher) do |m|
yield env_var_name, env_var_value, m[1]
diff --git a/modules/recaptcha/app/controllers/recaptcha/request_controller.rb b/modules/recaptcha/app/controllers/recaptcha/request_controller.rb
index db5628a7fe2..4cc878e7a79 100644
--- a/modules/recaptcha/app/controllers/recaptcha/request_controller.rb
+++ b/modules/recaptcha/app/controllers/recaptcha/request_controller.rb
@@ -28,7 +28,7 @@ module ::Recaptcha
def verify
if valid_recaptcha?
- save_recpatcha_verification_success!
+ save_recaptcha_verification_success!
complete_stage_redirect
else
fail_recaptcha I18n.t('recaptcha.error_captcha')
@@ -39,7 +39,7 @@ module ::Recaptcha
##
# Insert that the account was verified
- def save_recpatcha_verification_success!
+ def save_recaptcha_verification_success!
# Remove all previous
::Recaptcha::Entry.where(user_id: @authenticated_user.id).delete_all
::Recaptcha::Entry.create!(user_id: @authenticated_user.id, version: recaptcha_version)
diff --git a/modules/recaptcha/spec/controllers/request_controller_spec.rb b/modules/recaptcha/spec/controllers/request_controller_spec.rb
index c6de6e3ff3a..bb403328418 100644
--- a/modules/recaptcha/spec/controllers/request_controller_spec.rb
+++ b/modules/recaptcha/spec/controllers/request_controller_spec.rb
@@ -2,11 +2,21 @@ require 'spec_helper'
describe ::Recaptcha::RequestController, type: :controller do
let(:user) { create :user }
+
+ include_context 'with settings' do
+ let(:settings) do
+ {
+ plugin_openproject_recaptcha: {
+ 'recaptcha_type' => 'v2',
+ 'website_key' => 'A',
+ 'secret_key' => 'B'
+ }
+ }
+ end
+ end
+
before do
login_as user
- allow(Setting)
- .to receive(:plugin_openproject_recaptcha)
- .and_return(recaptcha_type: 'v2', website_key: 'A', secret_key: 'B')
session[:authenticated_user_id] = user.id
session[:stage_secrets] = { recaptcha: 'asdf' }
@@ -21,7 +31,7 @@ describe ::Recaptcha::RequestController, type: :controller do
it 'skips if user is verified' do
allow(::Recaptcha::Entry)
- .to receive_message_chain(:where, :exists?)
+ .to receive(:exists?).with(user_id: user.id)
.and_return true
get :perform
@@ -32,25 +42,27 @@ describe ::Recaptcha::RequestController, type: :controller do
let(:user) { create :admin }
it 'skips the verification' do
- expect(controller).not_to receive(:perform)
+ allow(controller).to receive(:perform)
get :perform
expect(response).to redirect_to stage_success_path(stage: :recaptcha, secret: 'asdf')
+ expect(controller).not_to have_received(:perform)
end
end
end
describe 'verify' do
it 'succeeds assuming verification works' do
- allow(@controller).to receive(:valid_recaptcha?).and_return true
- expect(@controller).to receive(:save_recpatcha_verification_success!)
+ allow(controller).to receive(:valid_recaptcha?).and_return true
+ allow(controller).to receive(:save_recaptcha_verification_success!)
post :verify
expect(flash[:error]).to be_nil
expect(response).to redirect_to stage_success_path(stage: :recaptcha, secret: 'asdf')
+ expect(controller).to have_received(:save_recaptcha_verification_success!)
end
it 'fails assuming verification fails' do
- allow(@controller).to receive(:valid_recaptcha?).and_return false
+ allow(controller).to receive(:valid_recaptcha?).and_return false
post :verify
expect(flash[:error]).to be_present
expect(response).to redirect_to stage_failure_path(stage: :recaptcha)
diff --git a/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy_manager.rb b/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy_manager.rb
index 461406cf6d5..e910bd9067b 100644
--- a/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy_manager.rb
+++ b/modules/two_factor_authentication/lib/open_project/two_factor_authentication/token_strategy_manager.rb
@@ -93,7 +93,7 @@ module OpenProject::TwoFactorAuthentication
types << s.device_type
end
- classes = types.to_h { |type| [type, ::TwoFactorAuthentication::Device.const_get(type.to_s.camelize)] }
+ types.index_with { |type| ::TwoFactorAuthentication::Device.const_get(type.to_s.camelize) }
end
##
diff --git a/modules/two_factor_authentication/spec/models/devices/totp_spec.rb b/modules/two_factor_authentication/spec/models/devices/totp_spec.rb
index 89edc5f2605..7caa0e9e44d 100644
--- a/modules/two_factor_authentication/spec/models/devices/totp_spec.rb
+++ b/modules/two_factor_authentication/spec/models/devices/totp_spec.rb
@@ -83,7 +83,7 @@ describe ::TwoFactorAuthentication::Device::Totp, with_2fa_ee: true, type: :mode
expect(subject.verify_token(valid)).to be false
- future = Time.current + 1.minute
+ future = 1.minute.from_now
Timecop.freeze(future) do
valid = totp.now
expect(subject.verify_token(valid)).to be true
diff --git a/modules/two_factor_authentication/spec/services/token_delivery/totp_spec.rb b/modules/two_factor_authentication/spec/services/token_delivery/totp_spec.rb
index 3763db73db2..196af3ab178 100644
--- a/modules/two_factor_authentication/spec/services/token_delivery/totp_spec.rb
+++ b/modules/two_factor_authentication/spec/services/token_delivery/totp_spec.rb
@@ -5,17 +5,12 @@ describe ::OpenProject::TwoFactorAuthentication::TokenStrategy::Totp, with_2fa_e
let!(:user) { create :user }
let!(:device) { create :two_factor_authentication_device_totp, user: user, default: true }
- before do
- allow(OpenProject::Configuration)
- .to receive(:[]).with('2fa')
- .and_return(active_strategies: [:totp])
- end
-
describe '#verify' do
subject { ::TwoFactorAuthentication::TokenService.new user: user }
+
let(:result) { subject.verify token }
- context '#valid current token' do
+ context 'with valid current token' do
let(:token) { device.totp.now }
it 'is validated' do
@@ -30,7 +25,7 @@ describe ::OpenProject::TwoFactorAuthentication::TokenStrategy::Totp, with_2fa_e
end
end
- context 'invalid token' do
+ context 'with invalid token' do
let(:token) { 'definitely invalid' }
it 'is not validated' do
@@ -39,7 +34,7 @@ describe ::OpenProject::TwoFactorAuthentication::TokenStrategy::Totp, with_2fa_e
end
end
- context 'assuming internal error' do
+ context 'with internal error' do
let(:token) { 1234 }
before do
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 638a3419f95..a1251e044f0 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -322,29 +322,30 @@ describe Settings::Definition do
described_class.all << definition_2fa
end
- # it 'allows overriding settings from ENV with aliased env name' do
- # require Rails.root.join('modules/two_factor_authentication/lib/open_project/two_factor_authentication/engine')
- # stub_const(
- # 'ENV',
- # {
- # 'OPENPROJECT_2FA' => '{"enforced": true, "allow_remember_for_days": 15}'
- # }
- # )
-
- # expect(value_for('plugin_openproject_two_factor_authentication'))
- # .to eq(active_strategies: [], enforced: true, allow_remember_for_days: 15)
- # end
-
it 'allows overriding settings hash partially from ENV with aliased env name' do
+ stub_const(
+ 'ENV',
+ {
+ 'OPENPROJECT_2FA_ENFORCED' => 'true',
+ 'OPENPROJECT_2FA_ALLOW__REMEMBER__FOR__DAYS' => '15'
+ }
+ )
+
+ described_class.send(:override_value, definition_2fa) # override from env manually after changing ENV
+ expect(value_for('plugin_openproject_two_factor_authentication'))
+ .to eq('active_strategies' => [:totp], 'enforced' => true, 'allow_remember_for_days' => 15)
+ end
+
+ it 'allows overriding settings hash from ENV with aliased env name' do
stub_const(
'ENV',
{
'OPENPROJECT_2FA' => '{"enforced": true, "allow_remember_for_days": 15}'
}
)
- Settings::Definition.send(:override_value, definition_2fa) # override from env manually after changing ENV
+ described_class.send(:override_value, definition_2fa) # override from env manually after changing ENV
expect(value_for('plugin_openproject_two_factor_authentication'))
- .to eq({ 'active_strategies' => [], 'enforced' => true, 'allow_remember_for_days' => 15 })
+ .to eq({ 'active_strategies' => [:totp], 'enforced' => true, 'allow_remember_for_days' => 15 })
end
end
diff --git a/spec/support/shared/with_settings.rb b/spec/support/shared/with_settings.rb
index 9b131391d82..6206cd03bb3 100644
--- a/spec/support/shared/with_settings.rb
+++ b/spec/support/shared/with_settings.rb
@@ -67,7 +67,7 @@ module WithSettingsMixin
raise "#{k} is not a valid setting" unless Setting.respond_to?(name)
expect(name).not_to start_with("localized_"), -> do
- base = name[10..-1]
+ base = name[10..]
"Don't use `#{name}` in `with_settings`. Do this: `with_settings: { #{base}: { \"en\" => \"#{v}\" } }`"
end
diff --git a/spec/views/account/register.html.erb_spec.rb b/spec/views/account/register.html.erb_spec.rb
index 7469e03e530..dbe3f6fbb8e 100644
--- a/spec/views/account/register.html.erb_spec.rb
+++ b/spec/views/account/register.html.erb_spec.rb
@@ -43,13 +43,13 @@ describe 'account/register', type: :view do
let(:auth_source) { create :auth_source }
let(:user) { build :user, auth_source: auth_source }
- it 'should not show a login field' do
+ it 'does not show a login field' do
expect(rendered).not_to include('user[login]')
end
end
context 'without auth source' do
- it 'should show a login field' do
+ it 'shows a login field' do
expect(rendered).to include('user[login]')
end
end
@@ -67,21 +67,21 @@ describe 'account/register', type: :view do
let(:auth_source) { create :auth_source }
let(:user) { build :user, auth_source: auth_source }
- it 'should not show a login field' do
+ it 'does not show a login field' do
expect(rendered).not_to include('user[login]')
end
- it 'should show an email field' do
+ it 'shows an email field' do
expect(rendered).to include('user[mail]')
end
end
context 'without auth source' do
- it 'should not show a login field' do
+ it 'does not show a login field' do
expect(rendered).not_to include('user[login]')
end
- it 'should show an email field' do
+ it 'shows an email field' do
expect(rendered).to include('user[mail]')
end
end
@@ -96,7 +96,7 @@ describe 'account/register', type: :view do
assign(:user, user)
end
- it 'should render the registration footer from the settings' do
+ it 'renders the registration footer from the settings' do
render
expect(rendered).to include(footer)
@@ -106,8 +106,8 @@ describe 'account/register', type: :view do
context "with consent required", with_settings: {
consent_required: true,
consent_info: {
- en: "You must consent!",
- de: "Du musst zustimmen!"
+ 'en' => "You must consent!",
+ 'de' => "Du musst zustimmen!"
}
} do
let(:locale) { raise "you have to define the locale" }
diff --git a/spec_legacy/support/legacy_assertions.rb b/spec_legacy/support/legacy_assertions.rb
index 312b702a038..311afe5a340 100644
--- a/spec_legacy/support/legacy_assertions.rb
+++ b/spec_legacy/support/legacy_assertions.rb
@@ -82,7 +82,7 @@ module LegacyAssertionsAndHelpers
file
end
- def with_settings(options, &_block)
+ def with_legacy_settings(options, &_block)
saved_settings = options.keys.inject({}) { |h, k| h[k] = Setting[k].dup; h }
options.each { |k, v| Setting[k] = v }
yield
diff --git a/spec_legacy/unit/mail_handler_spec.rb b/spec_legacy/unit/mail_handler_spec.rb
index 980d2bfca31..a445b5cff38 100644
--- a/spec_legacy/unit/mail_handler_spec.rb
+++ b/spec_legacy/unit/mail_handler_spec.rb
@@ -175,7 +175,7 @@ describe MailHandler, type: :model do
assert_equal false, submit_email('ticket_without_from_header.eml')
end
- context 'without default start_date', with_settings: { work_package_startdate_is_adddate: false } do
+ context 'without default start_date', with_legacy_settings: { work_package_startdate_is_adddate: false } do
it 'should add work package with invalid attributes' do
issue = submit_email('ticket_with_invalid_attributes.eml', allow_override: 'type,category,priority')
assert issue.is_a?(WorkPackage)
@@ -309,7 +309,7 @@ describe MailHandler, type: :model do
end
context 'with min password length',
- with_settings: { password_min_length: 15 } do
+ with_legacy_settings: { password_min_length: 15 } do
it 'should new user from attributes should respect minimum password length' do
user = MailHandler.new_user_from_attributes('[email protected]')
assert user.valid?
diff --git a/spec_legacy/unit/project_spec.rb b/spec_legacy/unit/project_spec.rb
index e42b0a3e5cf..73d090770a8 100644
--- a/spec_legacy/unit/project_spec.rb
+++ b/spec_legacy/unit/project_spec.rb
@@ -140,7 +140,7 @@ describe Project, type: :model do
end
context 'with modules',
- with_settings: { default_projects_modules: ['work_package_tracking', 'repository'] } do
+ with_legacy_settings: { default_projects_modules: ['work_package_tracking', 'repository'] } do
it 'should enabled module names' do
project = Project.new