From f4de4e35a304294487f5fdc54cae5de81908e560 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 22 Apr 2022 15:28:37 +0100 Subject: [PATCH 1/4] Add error to postcode for LA validation --- .../local_authority_validations.rb | 2 ++ config/locales/en.yml | 1 + .../local_authority_validations_spec.rb | 27 ++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index 0409572b9..b5f001e64 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -18,7 +18,9 @@ module Validations::LocalAuthorityValidations record.la && !record.owning_organisation.local_authorities.include?(record.la) la_name = record.form.get_question("la", record).label_from_value(record.la) org_name = record.owning_organisation.name + postcode = record.postcode_full record.errors.add :la, I18n.t("validations.property.la.la_invalid_for_org", org_name:, la_name:) + record.errors.add :postcode_known, I18n.t("validations.property.la.postcode_invalid_for_org", org_name:, postcode:) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5a2b36387..0dc8dec7e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -69,6 +69,7 @@ en: la: la_known: "Enter name of local authority" la_invalid_for_org: "%{org_name} does not operate in %{la_name}" + postcode_invalid_for_org: "%{org_name} does not operate in %{postcode}" rsnvac: first_let_not_social: "Reason for vacancy cannot be first let if unit has been previously let as social housing" first_let_social: "Reason for vacancy must be first let if unit has been previously let as social housing" diff --git a/spec/models/validations/local_authority_validations_spec.rb b/spec/models/validations/local_authority_validations_spec.rb index 1ad97a72f..4d9a93dfc 100644 --- a/spec/models/validations/local_authority_validations_spec.rb +++ b/spec/models/validations/local_authority_validations_spec.rb @@ -37,15 +37,6 @@ RSpec.describe Validations::LocalAuthorityValidations do end describe "#validate_la" do - context "when the rent type is London affordable" do - it "expects that the local authority is in London" do - record.la = "E09000033" - record.rent_type = 2 - local_auth_validator.validate_la(record) - expect(record.errors["la"]).to be_empty - end - end - context "when previous la is known" do it "la has to be provided" do record.la_known = 1 @@ -80,6 +71,24 @@ RSpec.describe Validations::LocalAuthorityValidations do local_auth_validator.validate_la(record) expect(record.errors["la"]).to be_empty end + + context "when a postcode is given" do + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"York\",\"codes\":{\"admin_district\": \"E06000014\"}}}", headers: {}) + end + + it "validates that it matches a local authority the organisation operates in" do + record.postcode_full = "YO10 3RR" + record.save + expect(record.errors["postcode_known"]) + .to include(match I18n.t( + "validations.property.la.postcode_invalid_for_org", + org_name: organisation.name, + postcode: "YO103RR", + )) + end + end end context "when the organisation has not listed specific local authorities it operates in" do From 13cbb13d8c430a8d2989ae771bd3e5ab7ddccb4e Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 22 Apr 2022 15:51:34 +0100 Subject: [PATCH 2/4] Postcode format --- app/models/validations/local_authority_validations.rb | 2 +- .../validations/local_authority_validations_spec.rb | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index b5f001e64..ae38d5b25 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -18,7 +18,7 @@ module Validations::LocalAuthorityValidations record.la && !record.owning_organisation.local_authorities.include?(record.la) la_name = record.form.get_question("la", record).label_from_value(record.la) org_name = record.owning_organisation.name - postcode = record.postcode_full + postcode = UKPostcode.parse(record.postcode_full) record.errors.add :la, I18n.t("validations.property.la.la_invalid_for_org", org_name:, la_name:) record.errors.add :postcode_known, I18n.t("validations.property.la.postcode_invalid_for_org", org_name:, postcode:) end diff --git a/spec/models/validations/local_authority_validations_spec.rb b/spec/models/validations/local_authority_validations_spec.rb index 4d9a93dfc..42c816c06 100644 --- a/spec/models/validations/local_authority_validations_spec.rb +++ b/spec/models/validations/local_authority_validations_spec.rb @@ -73,19 +73,15 @@ RSpec.describe Validations::LocalAuthorityValidations do end context "when a postcode is given" do - before do - stub_request(:get, /api.postcodes.io/) - .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"York\",\"codes\":{\"admin_district\": \"E06000014\"}}}", headers: {}) - end - it "validates that it matches a local authority the organisation operates in" do record.postcode_full = "YO10 3RR" - record.save + record.la = "E06000014" + local_auth_validator.validate_la(record) expect(record.errors["postcode_known"]) .to include(match I18n.t( "validations.property.la.postcode_invalid_for_org", org_name: organisation.name, - postcode: "YO103RR", + postcode: "YO10 3RR", )) end end From 19d7e7f984d933f7edaa2857fc2cdeebdaf958d5 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 22 Apr 2022 15:59:28 +0100 Subject: [PATCH 3/4] Postcode may not be set --- app/models/validations/local_authority_validations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index ae38d5b25..e02394fe9 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -18,7 +18,7 @@ module Validations::LocalAuthorityValidations record.la && !record.owning_organisation.local_authorities.include?(record.la) la_name = record.form.get_question("la", record).label_from_value(record.la) org_name = record.owning_organisation.name - postcode = UKPostcode.parse(record.postcode_full) + postcode = UKPostcode.parse(record.postcode_full) if record.postcode_full record.errors.add :la, I18n.t("validations.property.la.la_invalid_for_org", org_name:, la_name:) record.errors.add :postcode_known, I18n.t("validations.property.la.postcode_invalid_for_org", org_name:, postcode:) end From ee946111e4ae81e8a37e15106c5e82e6bf22e303 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 22 Apr 2022 18:24:03 +0100 Subject: [PATCH 4/4] Update error string --- config/locales/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 0dc8dec7e..456ce9805 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -69,7 +69,7 @@ en: la: la_known: "Enter name of local authority" la_invalid_for_org: "%{org_name} does not operate in %{la_name}" - postcode_invalid_for_org: "%{org_name} does not operate in %{postcode}" + postcode_invalid_for_org: "Postcode must be in an area covered by %{org_name}" rsnvac: first_let_not_social: "Reason for vacancy cannot be first let if unit has been previously let as social housing" first_let_social: "Reason for vacancy must be first let if unit has been previously let as social housing"