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
def validate_rent_period(record)
if record.managing_organisation.present? && record.managing_organisation.rent_periods.present? &&
record.period && !record.managing_organisation.rent_periods.include?(record.period)
return unless record.managing_organisation && record.period
unless record.managing_organisation.rent_periods.include? record.period
record.errors.add :period, :wrong_rent_period, message: I18n.t(
"validations.financial.rent_period.invalid_for_org",
org_name: record.managing_organisation.name,

28
spec/models/validations/financial_validations_spec.rb

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

Loading…
Cancel
Save