Browse Source

update validation to reflect the fact that an org having no associated rent periods no longer means they accept all rent periods

update tests adding both cases and removing unnecessary additional db additions
pull/2389/head
Arthur Campbell 2 years ago
parent
commit
6d9a6020f4
  1. 5
      app/models/validations/financial_validations.rb
  2. 28
      spec/models/validations/financial_validations_spec.rb

5
app/models/validations/financial_validations.rb

@ -145,8 +145,9 @@ module Validations::FinancialValidations
end end
def validate_rent_period(record) def validate_rent_period(record)
if record.managing_organisation.present? && record.managing_organisation.rent_periods.present? && return unless record.managing_organisation && record.period
record.period && !record.managing_organisation.rent_periods.include?(record.period)
unless record.managing_organisation.rent_periods.include? record.period
record.errors.add :period, :wrong_rent_period, message: I18n.t( record.errors.add :period, :wrong_rent_period, message: I18n.t(
"validations.financial.rent_period.invalid_for_org", "validations.financial.rent_period.invalid_for_org",
org_name: record.managing_organisation.name, org_name: record.managing_organisation.name,

28
spec/models/validations/financial_validations_spec.rb

@ -139,23 +139,33 @@ RSpec.describe Validations::FinancialValidations do
end end
describe "rent period validations" do describe "rent period validations" do
let(:organisation) { FactoryBot.create(:organisation) } let(:user) { create(:user) }
let(:user) { FactoryBot.create(:user) } let(:record) { create(:lettings_log, owning_organisation: user.organisation, managing_organisation: user.organisation, assigned_to: user) }
let(:record) { FactoryBot.create(:lettings_log, owning_organisation: user.organisation, managing_organisation: organisation, assigned_to: user) } let(:used_period) { 2 }
before do before do
FactoryBot.create(:organisation_relationship, parent_organisation: user.organisation, child_organisation: organisation) create(:organisation_rent_period, organisation: user.organisation, rent_period: used_period)
FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 2) record.period = period
end end
context "when the organisation only uses specific rent periods" do context "when the log uses a period that the org allows" do
it "validates that the selected rent period is used by the managing organisation" do let(:period) { used_period }
record.period = 3
it "does not apply a validation" do
financial_validator.validate_rent_period(record)
expect(record.errors["period"]).to be_empty
end
end
context "when the log uses a period that the org does not allow" do
let(:period) { used_period + 1 }
it "does apply a validation" do
financial_validator.validate_rent_period(record) financial_validator.validate_rent_period(record)
expect(record.errors["period"]) expect(record.errors["period"])
.to include(match I18n.t( .to include(match I18n.t(
"validations.financial.rent_period.invalid_for_org", "validations.financial.rent_period.invalid_for_org",
org_name: organisation.name, org_name: user.organisation.name,
rent_period: "every 4 weeks", rent_period: "every 4 weeks",
)) ))
end end

Loading…
Cancel
Save