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

Ensure durations are properly validated

Aaron Contreras47755d0066fb
diff --git a/app/contracts/settings/working_days_and_hours_params_contract.rb b/app/contracts/settings/working_days_and_hours_params_contract.rb
index 4a347f4c988..c6dbd9864c4 100644
--- a/app/contracts/settings/working_days_and_hours_params_contract.rb
+++ b/app/contracts/settings/working_days_and_hours_params_contract.rb
@@ -35,6 +35,7 @@ module Settings
validate :days_per_week_are_present
validate :days_per_month_are_present
validate :durations_are_positive_values
+ validate :durations_are_within_bounds
validate :days_per_week_and_days_per_month_are_consistent
validate :unique_job
@@ -73,6 +74,12 @@ module Settings
end
end
+ def durations_are_within_bounds
+ errors.add :base, :hours_per_day_is_out_of_bounds if hours_per_day.to_i > 24
+ errors.add :base, :days_per_week_is_out_of_bounds if days_per_week.to_i > 7
+ errors.add :base, :days_per_month_is_out_of_bounds if days_per_month.to_i > 31
+ end
+
def any_duration_is_negative_or_zero?
!hours_per_day.to_i.positive? ||
!days_per_week.to_i.positive? ||
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 67418d0c2b7..d165c770e56 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1035,6 +1035,9 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
days_per_week_are_missing: "The number of days per week must be defined."
days_per_month_are_missing: "The number of days per month must be defined."
durations_are_not_positive_numbers: "The durations must be positive numbers."
+ hours_per_day_is_out_of_bounds: "Hours per day can't be more than 24"
+ days_per_week_is_out_of_bounds: "Days per week can't be more than 7"
+ days_per_month_is_out_of_bounds: "Days per month can't be more than 31"
days_per_week_and_days_per_month_are_inconsistent: "The number of days per week and the number of days per month must be consistent."
time_entry:
attributes:
diff --git a/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb b/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb
index e2ed5cdbf7a..37fe7cdd54e 100644
--- a/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb
+++ b/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb
@@ -92,9 +92,29 @@ RSpec.describe Settings::WorkingDaysAndHoursParamsContract do
end
end
+ describe "Text durations" do
+ let(:params) { { working_days: [1], hours_per_day: "blah", days_per_week: "5", days_per_month: "20" } }
+
+ include_examples "contract is invalid", base: :durations_are_not_positive_numbers
+ end
+
describe "Negative durations" do
let(:params) { { working_days: [1], hours_per_day: -2, days_per_week: -5, days_per_month: -20 } }
include_examples "contract is invalid", base: :durations_are_not_positive_numbers
end
+
+ describe "Out-of-bounds durations" do
+ context "when hours_per_day is greater than 24" do
+ let(:params) { { working_days: [1], hours_per_day: 25, days_per_week: 5, days_per_month: 20 } }
+
+ include_examples "contract is invalid", base: :hours_per_day_is_out_of_bounds
+ end
+
+ context "when days_per_week is greater than 7 and days_per_month is greater than 31" do
+ let(:params) { { working_days: [1], hours_per_day: 8, days_per_week: 8, days_per_month: 32 } }
+
+ include_examples "contract is invalid", base: %i[days_per_week_is_out_of_bounds days_per_month_is_out_of_bounds]
+ end
+ end
end