Browse Source

CLDC-1428 Fix date validation when creating a log via the api

The created_at field is populated when a log is first inserted into the database, but the validations are run prior to database insertion for a log being created by the API. Therefore if created_at isn't populated we use the current date and time.
Also added some time travel to the api test suite because it's creating 2021 logs after the cutoff date.
pull/853/head
David May-Miller 3 years ago
parent
commit
f640716d16
  1. 4
      app/models/validations/date_validations.rb
  2. 3
      spec/requests/lettings_logs_controller_spec.rb

4
app/models/validations/date_validations.rb

@ -31,7 +31,9 @@ module Validations::DateValidations
def validate_startdate(record)
return unless record.startdate && date_valid?("startdate", record)
if record.created_at > first_collection_end_date && record.startdate < second_collection_start_date
created_at = record.created_at || Time.zone.now
if created_at > first_collection_end_date && record.startdate < second_collection_start_date
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end

3
spec/requests/lettings_logs_controller_spec.rb

@ -1,6 +1,8 @@
require "rails_helper"
RSpec.describe LettingsLogsController, type: :request do
include ActiveSupport::Testing::TimeHelpers
let(:user) { FactoryBot.create(:user) }
let(:owning_organisation) { user.organisation }
let(:managing_organisation) { owning_organisation }
@ -49,6 +51,7 @@ RSpec.describe LettingsLogsController, type: :request do
end
before do
travel_to Time.utc(2022, 2, 8, 16, 52, 15)
post "/logs", headers:, params: params.to_json
end

Loading…
Cancel
Save