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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
move user preference validations to contract
ulfertscb989527fe82
diff --git a/app/contracts/user_preferences/base_contract.rb b/app/contracts/user_preferences/base_contract.rbindex 600f9ad1567..441f3a44a34 100644--- a/app/contracts/user_preferences/base_contract.rb+++ b/app/contracts/user_preferences/base_contract.rb@@ -30,10 +30,27 @@module UserPreferencesclass BaseContract < ::BaseContract+ property :settings+validate :user_allowed_to_access+ validates :settings,+ not_nil: true,+ json: {+ schema: ->(*) {+ UserPreferences::Schema.schema+ },+ if: -> { model.settings.present? }+ }++ validate :time_zone_correctness,+ if: -> { model.time_zone.present? }protected+ def time_zone_correctness+ errors.add(:time_zone, :inclusion) if model.time_zone.present? && model.canonical_time_zone.nil?+ end+### User preferences can only be accessed with the manage_user permission# or if an active, logged user is editing their own prefsdiff --git a/app/contracts/user_preferences/update_contract.rb b/app/contracts/user_preferences/update_contract.rbindex f796f472b36..2b6e0ce9947 100644--- a/app/contracts/user_preferences/update_contract.rb+++ b/app/contracts/user_preferences/update_contract.rb@@ -30,15 +30,5 @@module UserPreferencesclass UpdateContract < BaseContract- property :settings-- validates :settings,- exclusion: { in: [{}] },- json: {- schema: ->(*) {- UserPreferences::Schema.schema- },- if: -> { model.settings.present? }- }endenddiff --git a/app/models/user_preference.rb b/app/models/user_preference.rbindex 2a5fe7117ac..8873cce0e07 100644--- a/app/models/user_preference.rb+++ b/app/models/user_preference.rb@@ -35,9 +35,6 @@ class UserPreference < ApplicationRecordvalidates :user,presence: true- validate :time_zone_correctness,- if: -> { time_zone.present? }-### Retrieve keys from settings, and allow accessing# as boolean with ? suffix@@ -138,10 +135,6 @@ class UserPreference < ApplicationRecord%i[user user_id].include?(name.to_sym)end- def time_zone_correctness- errors.add(:time_zone, :inclusion) if time_zone.present? && canonical_time_zone.nil?- end-def supported_settings_method?(method_name)UserPreferences::Schema.properties.include?(method_name.to_s.gsub(/\?|=\z/, ''))enddiff --git a/app/models/user_preferences/schema.rb b/app/models/user_preferences/schema.rbindex 3786703023c..7a07011b1d0 100644--- a/app/models/user_preferences/schema.rb+++ b/app/models/user_preferences/schema.rb@@ -32,8 +32,6 @@ class UserPreferences::Schemaclass << selfPATH = Rails.root.join('config/schemas/user_preferences.schema.json')- # TODO: check if Time zones can be verified within the schema-class_attribute :extensions,default: {}diff --git a/app/validators/not_nil_validator.rb b/app/validators/not_nil_validator.rbnew file mode 100644index 00000000000..89cce6f18f9--- /dev/null+++ b/app/validators/not_nil_validator.rb@@ -0,0 +1,35 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class NotNilValidator < ActiveModel::EachValidator+ def validate_each(record, attribute, value)+ record.errors.add(attribute, :blank) if value.nil?+ end+enddiff --git a/spec/contracts/user_preferences/update_contract_spec.rb b/spec/contracts/user_preferences/update_contract_spec.rbindex 1cacc9aff69..17c013160a7 100644--- a/spec/contracts/user_preferences/update_contract_spec.rb+++ b/spec/contracts/user_preferences/update_contract_spec.rb@@ -34,10 +34,10 @@ describe UserPreferences::UpdateContract doinclude_context 'ModelContract shared context'let(:current_user) { FactoryBot.build_stubbed(:user) }- let(:user) { FactoryBot.build_stubbed :user }+ let(:preference_user) { current_user }let(:user_preference) doFactoryBot.build_stubbed(:user_preference,- user: user,+ user: preference_user,settings: settings&.with_indifferent_access)endlet(:settings) do@@ -57,11 +57,14 @@ describe UserPreferences::UpdateContract docontext 'when current_user is admin' dolet(:current_user) { FactoryBot.build_stubbed(:admin) }+ let(:preference_user) { FactoryBot.build_stubbed(:user) }it_behaves_like 'contract is valid'endcontext 'when current_user has manage_user permission' do+ let(:preference_user) { FactoryBot.build_stubbed(:user) }+before doallow(current_user).to receive(:allowed_to_globally?).with(:manage_user).and_return trueend@@ -70,14 +73,10 @@ describe UserPreferences::UpdateContract doendcontext 'when current_user is the own user' do- let(:current_user) { user }-it_behaves_like 'contract is valid'endcontext 'when current_user is the own user but not active' do- let(:current_user) { user }-before doallow(current_user).to receive(:active?).and_return falseend@@ -87,32 +86,27 @@ describe UserPreferences::UpdateContract docontext 'when current_user is anonymous' dolet(:current_user) { User.anonymous }- let(:user) { current_user }it_behaves_like 'contract user is unauthorized'endcontext 'when current_user is a regular user' do+ let(:preference_user) { FactoryBot.build_stubbed(:user) }+it_behaves_like 'contract user is unauthorized'end- context 'when the settings are nil' do- let(:settings) { nil }+ context 'with empty settings' do+ let(:settings) do+ {}+ end- it_behaves_like 'contract is invalid', settings: :exclusion+ it_behaves_like 'contract is valid'endcontext 'with a string for hide_mail' dolet(:settings) do{- auto_hide_popups: true,- comments_sorting: 'desc',- daily_reminders: {- enabled: true,- times: %w[08:00:00+00:00 12:00:00+00:00]- },- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,hide_mail: 'yes please'}end@@ -123,15 +117,10 @@ describe UserPreferences::UpdateContract docontext 'with a field within the daily_reminders having the wrong type' dolet(:settings) do{- auto_hide_popups: true,- comments_sorting: 'desc',daily_reminders: {enabled: 'sure',times: %w[08:00:00+00:00 12:00:00+00:00]- },- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,- hide_mail: true+ }}end@@ -141,14 +130,9 @@ describe UserPreferences::UpdateContract docontext 'with a field within the daily_reminders missing' dolet(:settings) do{- auto_hide_popups: true,- comments_sorting: 'desc',daily_reminders: {times: %w[08:00:00+00:00 12:00:00+00:00]- },- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,- hide_mail: true+ }}end@@ -158,15 +142,6 @@ describe UserPreferences::UpdateContract docontext 'with an extra property' dolet(:settings) do{- auto_hide_popups: true,- comments_sorting: 'desc',- daily_reminders: {- enabled: true,- times: %w[08:00:00+00:00 12:00:00+00:00]- },- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,- hide_mail: true,foo: true}end@@ -177,16 +152,11 @@ describe UserPreferences::UpdateContract docontext 'with an extra property within the daily_reminders' dolet(:settings) do{- auto_hide_popups: true,- comments_sorting: 'desc',daily_reminders: {enabled: true,times: %w[08:00:00+00:00 12:00:00+00:00],foo: true- },- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,- hide_mail: true+ }}end@@ -196,15 +166,10 @@ describe UserPreferences::UpdateContract docontext 'with an invalid time for the daily_reminders' dolet(:settings) do{- auto_hide_popups: true,- comments_sorting: 'desc',daily_reminders: {enabled: true,times: %w[abc 12:00:00+00:00]- },- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,- hide_mail: true+ }}end@@ -214,18 +179,47 @@ describe UserPreferences::UpdateContract docontext 'with an invalid order for comments_sorting' dolet(:settings) do{+ comments_sorting: 'up'+ }+ end++ it_behaves_like 'contract is invalid', comments_sorting: :inclusion+ end++ context 'without a time_zone' do+ let(:settings) do+ {+ hide_mail: true,auto_hide_popups: true,- comments_sorting: 'up',+ comments_sorting: 'desc',daily_reminders: {enabled: true,times: %w[08:00:00+00:00 12:00:00+00:00]},- time_zone: 'Brasilia',- warn_on_leaving_unsaved: true,- hide_mail: true+ warn_on_leaving_unsaved: true}end- it_behaves_like 'contract is invalid', comments_sorting: :inclusion+ it_behaves_like 'contract is valid'+ end++ context 'with a full time_zone' do+ let(:settings) do+ {+ time_zone: 'Europe/Paris'+ }+ end++ it_behaves_like 'contract is valid'+ end++ context 'with a malformed time_zone' do+ let(:settings) do+ {+ time_zone: '123Brasilia'+ }+ end++ it_behaves_like 'contract is invalid', time_zone: :inclusionendenddiff --git a/spec/models/user_preference_spec.rb b/spec/models/user_preference_spec.rbindex 8cf0f4a358f..9383751ddd2 100644--- a/spec/models/user_preference_spec.rb+++ b/spec/models/user_preference_spec.rb@@ -141,30 +141,15 @@ describe UserPreference dodescribe 'time_zone' doit 'allows to save short time zones' dosubject.time_zone = 'Berlin'- expect(subject).to be_validexpect(subject.time_zone).to eq('Berlin')expect(subject.canonical_time_zone).to eq('Europe/Berlin')endit 'allows to set full time zones' dosubject.time_zone = 'Europe/Paris'- expect(subject).to be_validexpect(subject.time_zone).to eq('Europe/Paris')expect(subject.canonical_time_zone).to eq('Europe/Paris')end-- it 'disallows invalid time zones' do- subject.time_zone = 'Berlin123'- expect(subject).not_to be_valid- end-- it 'allows empty values' do- subject.time_zone = nil- expect(subject).to be_valid-- subject.time_zone = ''- expect(subject).to be_valid- endenddescribe '[]=' do