diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb index 93b5b3ea544f..962f12750a5f 100644 --- a/modules/costs/app/models/time_entry.rb +++ b/modules/costs/app/models/time_entry.rb @@ -36,6 +36,9 @@ class TimeEntry < ApplicationRecord belongs_to :rate, -> { where(type: %w[HourlyRate DefaultHourlyRate]) }, class_name: "Rate" belongs_to :logged_by, class_name: "User" + MIN_TIME = 0 # => 00:00 + MAX_TIME = (60 * 24) - 1 # => 23:59 + acts_as_customizable acts_as_journalized @@ -49,14 +52,14 @@ class TimeEntry < ApplicationRecord if: -> { TimeEntry.must_track_start_and_end_time? } validates :start_time, - numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 60 * 24 }, + numericality: { only_integer: true, greater_than_or_equal_to: MIN_TIME, less_than_or_equal_to: MAX_TIME }, allow_blank: true validates :end_time, numericality: { only_integer: true, greater_than: ->(te) { te.start_time.to_i }, - less_than_or_equal_to: 60 * 24 + less_than_or_equal_to: MAX_TIME # TODO: nice error message }, allow_blank: true diff --git a/modules/costs/spec/models/time_entry_spec.rb b/modules/costs/spec/models/time_entry_spec.rb index b3110c1291fa..521288515a69 100644 --- a/modules/costs/spec/models/time_entry_spec.rb +++ b/modules/costs/spec/models/time_entry_spec.rb @@ -441,11 +441,16 @@ def ensure_membership(project, user, permissions) expect(time_entry).to be_valid end - it "allows integer values between 0 and 1440" do + it "allows integer values between 0 and 1439" do time_entry.end_time = 1337 expect(time_entry).to be_valid end + it "does not allow values > 1439" do + time_entry.end_time = 1440 + expect(time_entry).not_to be_valid + end + it "does not allow non integer values" do time_entry.end_time = 1.5 expect(time_entry).not_to be_valid