diff --git a/Gemfile b/Gemfile index 4dc93aaf7..a5c7dd01f 100644 --- a/Gemfile +++ b/Gemfile @@ -39,7 +39,6 @@ gem "devise_two_factor_authentication" # UK postcode parsing and validation gem "uk_postcode" # Get rich data from postcode lookups. Wraps postcodes.io -gem "postcodes_io" # Use Ruby objects to build reusable markup. A React inspired evolution of the presenter pattern gem "view_component", "~> 3.9" # Use the AWS S3 SDK as storage mechanism @@ -112,3 +111,5 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "cssbundling-rails" gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby] + +gem "excon", "~> 0.111.0" diff --git a/Gemfile.lock b/Gemfile.lock index 7cc2addda..cdf923ae0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,7 +183,7 @@ GEM et-orbi (1.2.11) tzinfo event_stream_parser (1.0.0) - excon (0.109.0) + excon (0.111.0) factory_bot (6.4.6) activesupport (>= 5.0.0) factory_bot_rails (6.4.3) @@ -302,8 +302,6 @@ GEM racc pg (1.5.5) possessive (1.0.1) - postcodes_io (0.4.0) - excon (~> 0.39) propshaft (0.8.0) actionpack (>= 7.0.0) activesupport (>= 7.0.0) @@ -533,6 +531,7 @@ DEPENDENCIES devise_two_factor_authentication dotenv-rails erb_lint + excon (~> 0.111.0) factory_bot_rails faker govuk-components (~> 5.1) @@ -549,7 +548,6 @@ DEPENDENCIES parallel_tests pg (~> 1.1) possessive - postcodes_io propshaft pry-byebug puma (~> 5.6) diff --git a/app/models/form/lettings/pages/no_females_pregnant_household_lead_age_value_check.rb b/app/models/form/lettings/pages/no_females_pregnant_household_lead_age_value_check.rb index 99d7e6bfa..68b1c7f09 100644 --- a/app/models/form/lettings/pages/no_females_pregnant_household_lead_age_value_check.rb +++ b/app/models/form/lettings/pages/no_females_pregnant_household_lead_age_value_check.rb @@ -2,13 +2,13 @@ class Form::Lettings::Pages::NoFemalesPregnantHouseholdLeadAgeValueCheck < ::For def initialize(id, hsh, subsection) super(id, hsh, subsection) @id = "no_females_pregnant_household_lead_age_value_check" - @depends_on = [{ "no_females_in_a_pregnant_household?" => true }] + @depends_on = [{ "all_male_tenants_in_a_pregnant_household?" => true }] @title_text = { "translation" => "soft_validations.pregnancy.title", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @informative_text = { - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } end diff --git a/app/models/form/lettings/pages/no_females_pregnant_household_lead_hhmemb_value_check.rb b/app/models/form/lettings/pages/no_females_pregnant_household_lead_hhmemb_value_check.rb index c39fecbc3..e1d1235ef 100644 --- a/app/models/form/lettings/pages/no_females_pregnant_household_lead_hhmemb_value_check.rb +++ b/app/models/form/lettings/pages/no_females_pregnant_household_lead_hhmemb_value_check.rb @@ -2,13 +2,13 @@ class Form::Lettings::Pages::NoFemalesPregnantHouseholdLeadHhmembValueCheck < :: def initialize(id, hsh, subsection) super @id = "no_females_pregnant_household_lead_hhmemb_value_check" - @depends_on = [{ "no_females_in_a_pregnant_household?" => true }] + @depends_on = [{ "all_male_tenants_in_a_pregnant_household?" => true }] @title_text = { "translation" => "soft_validations.pregnancy.title", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @informative_text = { - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } end diff --git a/app/models/form/lettings/pages/no_females_pregnant_household_lead_value_check.rb b/app/models/form/lettings/pages/no_females_pregnant_household_lead_value_check.rb index b90831b4b..416845735 100644 --- a/app/models/form/lettings/pages/no_females_pregnant_household_lead_value_check.rb +++ b/app/models/form/lettings/pages/no_females_pregnant_household_lead_value_check.rb @@ -2,13 +2,13 @@ class Form::Lettings::Pages::NoFemalesPregnantHouseholdLeadValueCheck < ::Form:: def initialize(id, hsh, subsection) super @id = "no_females_pregnant_household_lead_value_check" - @depends_on = [{ "no_females_in_a_pregnant_household?" => true }] + @depends_on = [{ "all_male_tenants_in_a_pregnant_household?" => true }] @title_text = { "translation" => "soft_validations.pregnancy.title", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @informative_text = { - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } end diff --git a/app/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check.rb b/app/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check.rb index 13c39ab11..12db3a08e 100644 --- a/app/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check.rb +++ b/app/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check.rb @@ -2,13 +2,13 @@ class Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonAgeValueCheck < ::F def initialize(id, hsh, subsection, person_index:) super(id, hsh, subsection) @id = "no_females_pregnant_household_person_#{person_index}_age_value_check" - @depends_on = [{ "no_females_in_a_pregnant_household?" => true, "age#{person_index}_known" => 0 }] + @depends_on = [{ "all_male_tenants_in_a_pregnant_household?" => true, "age#{person_index}_known" => 0 }] @title_text = { "translation" => "soft_validations.pregnancy.title", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @informative_text = { - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @person_index = person_index diff --git a/app/models/form/lettings/pages/no_females_pregnant_household_person_value_check.rb b/app/models/form/lettings/pages/no_females_pregnant_household_person_value_check.rb index 0f5e93021..f04333ec0 100644 --- a/app/models/form/lettings/pages/no_females_pregnant_household_person_value_check.rb +++ b/app/models/form/lettings/pages/no_females_pregnant_household_person_value_check.rb @@ -2,13 +2,13 @@ class Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonValueCheck < ::Form def initialize(id, hsh, subsection, person_index:) super(id, hsh, subsection) @id = "no_females_pregnant_household_person_#{person_index}_value_check" - @depends_on = [{ "no_females_in_a_pregnant_household?" => true, "details_known_#{person_index}" => 0 }] + @depends_on = [{ "all_male_tenants_in_a_pregnant_household?" => true, "details_known_#{person_index}" => 0 }] @title_text = { "translation" => "soft_validations.pregnancy.title", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @informative_text = { - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @person_index = person_index diff --git a/app/models/form/lettings/pages/no_females_pregnant_household_value_check.rb b/app/models/form/lettings/pages/no_females_pregnant_household_value_check.rb index bf4494135..9c84a953f 100644 --- a/app/models/form/lettings/pages/no_females_pregnant_household_value_check.rb +++ b/app/models/form/lettings/pages/no_females_pregnant_household_value_check.rb @@ -2,13 +2,13 @@ class Form::Lettings::Pages::NoFemalesPregnantHouseholdValueCheck < ::Form::Page def initialize(id, hsh, subsection) super @id = "no_females_pregnant_household_value_check" - @depends_on = [{ "no_females_in_a_pregnant_household?" => true }] + @depends_on = [{ "all_male_tenants_in_a_pregnant_household?" => true }] @title_text = { "translation" => "soft_validations.pregnancy.title", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } @informative_text = { - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [{ "key" => "sex1", "label" => true, "i18n_template" => "sex1" }], } end diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index 5f1ac9c7a..41309cef0 100644 --- a/app/models/validations/soft_validations.rb +++ b/app/models/validations/soft_validations.rb @@ -67,8 +67,8 @@ module Validations::SoftValidations end end - def no_females_in_a_pregnant_household? - !females_in_the_household? && all_tenants_gender_information_completed? && preg_occ == 1 + def all_male_tenants_in_a_pregnant_household? + all_male_tenants_in_the_household? && all_tenants_gender_information_completed? && preg_occ == 1 end def female_in_pregnant_household_in_soft_validation_range? @@ -226,6 +226,14 @@ private end end + def all_male_tenants_in_the_household? + person_count = hhmemb || 8 + + (1..person_count).all? do |n| + public_send("sex#{n}") == "M" + end + end + def tenant_is_retired?(economic_status) economic_status == 5 end diff --git a/app/services/postcode_service.rb b/app/services/postcode_service.rb index c3c6dcfdd..8cfe9c43c 100644 --- a/app/services/postcode_service.rb +++ b/app/services/postcode_service.rb @@ -1,26 +1,24 @@ class PostcodeService - def initialize - @pio = Postcodes::IO.new - end - def lookup(postcode) # Avoid network calls when postcode is invalid return unless postcode.match(POSTCODE_REGEXP) - postcode_lookup = nil + result = nil begin # URI encoding only supports ASCII characters ascii_postcode = self.class.clean(postcode) - Timeout.timeout(30) { postcode_lookup = @pio.lookup(ascii_postcode) } - rescue Timeout::Error - Rails.logger.warn("Postcodes.io lookup timed out") - end - if postcode_lookup && postcode_lookup.info.present? - { - location_code: postcode_lookup.codes["admin_district"], - location_admin_district: postcode_lookup&.admin_district, + response = Excon.get("https://api.postcodes.io/postcodes/#{ascii_postcode}", idempotent: true, timeout: 30, expects: [200]) + parsed_response = JSON.parse(response.body) + result = { + location_code: parsed_response["result"]["codes"]["admin_district"], + location_admin_district: parsed_response["result"]["admin_district"], } + rescue Excon::Error => e + Rails.logger.warn("Postcode lookup request was not successful: #{e} #{e.response.body}") + rescue StandardError => e + Rails.logger.error("Unexpected error with postcode lookup request: #{e}") end + result end def self.clean(postcode) diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 4033c2534..6ebc090b6 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1170,7 +1170,7 @@ "no_females_pregnant_household_lead_hhmemb_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { @@ -1312,7 +1312,7 @@ "no_females_pregnant_household_lead_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { @@ -1433,7 +1433,7 @@ "no_females_pregnant_household_lead_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { @@ -2091,7 +2091,7 @@ "no_females_pregnant_household_person_2_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age2_known": 0 } ], @@ -2214,7 +2214,7 @@ "no_females_pregnant_household_person_2_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_2": 0 } ], @@ -2626,7 +2626,7 @@ "no_females_pregnant_household_person_3_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age3_known": 0 } ], @@ -2749,7 +2749,7 @@ "no_females_pregnant_household_person_3_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_3": 0 } ], @@ -3158,7 +3158,7 @@ "no_females_pregnant_household_person_4_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age4_known": 0 } ], @@ -3281,7 +3281,7 @@ "no_females_pregnant_household_person_4_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_4": 0 } ], @@ -3687,7 +3687,7 @@ "no_females_pregnant_household_person_5_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age5_known": 0 } ], @@ -3810,7 +3810,7 @@ "no_females_pregnant_household_person_5_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_5": 0 } ], @@ -4213,7 +4213,7 @@ "no_females_pregnant_household_person_6_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age6_known": 0 } ], @@ -4336,7 +4336,7 @@ "no_females_pregnant_household_person_6_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_6": 0 } ], @@ -4736,7 +4736,7 @@ "no_females_pregnant_household_person_7_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age7_known": 0 } ], @@ -4859,7 +4859,7 @@ "no_females_pregnant_household_person_7_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_7": 0 } ], @@ -5256,7 +5256,7 @@ "no_females_pregnant_household_person_8_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age8_known": 0 } ], @@ -5379,7 +5379,7 @@ "no_females_pregnant_household_person_8_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_8": 0 } ], @@ -5794,7 +5794,7 @@ "no_females_pregnant_household_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { diff --git a/config/forms/2022_2023.json b/config/forms/2022_2023.json index 6065653fd..54cd67fd4 100644 --- a/config/forms/2022_2023.json +++ b/config/forms/2022_2023.json @@ -1200,7 +1200,7 @@ "no_females_pregnant_household_lead_hhmemb_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { @@ -1372,7 +1372,7 @@ "no_females_pregnant_household_lead_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { @@ -1523,7 +1523,7 @@ "no_females_pregnant_household_lead_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { @@ -2163,7 +2163,7 @@ "no_females_pregnant_household_person_2_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age2_known": 0 } ], @@ -2316,7 +2316,7 @@ "no_females_pregnant_household_person_2_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_2": 0 } ], @@ -2746,7 +2746,7 @@ "no_females_pregnant_household_person_3_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age3_known": 0 } ], @@ -2899,7 +2899,7 @@ "no_females_pregnant_household_person_3_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_3": 0 } ], @@ -3326,7 +3326,7 @@ "no_females_pregnant_household_person_4_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age4_known": 0 } ], @@ -3479,7 +3479,7 @@ "no_females_pregnant_household_person_4_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_4": 0 } ], @@ -3903,7 +3903,7 @@ "no_females_pregnant_household_person_5_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age5_known": 0 } ], @@ -4056,7 +4056,7 @@ "no_females_pregnant_household_person_5_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_5": 0 } ], @@ -4477,7 +4477,7 @@ "no_females_pregnant_household_person_6_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age6_known": 0 } ], @@ -4630,7 +4630,7 @@ "no_females_pregnant_household_person_6_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_6": 0 } ], @@ -5048,7 +5048,7 @@ "no_females_pregnant_household_person_7_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age7_known": 0 } ], @@ -5201,7 +5201,7 @@ "no_females_pregnant_household_person_7_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_7": 0 } ], @@ -5616,7 +5616,7 @@ "no_females_pregnant_household_person_8_age_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "age8_known": 0 } ], @@ -5769,7 +5769,7 @@ "no_females_pregnant_household_person_8_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true, + "all_male_tenants_in_a_pregnant_household?": true, "details_known_8": 0 } ], @@ -6205,7 +6205,7 @@ "no_females_pregnant_household_value_check": { "depends_on": [ { - "no_females_in_a_pregnant_household?": true + "all_male_tenants_in_a_pregnant_household?": true } ], "title_text": { diff --git a/config/locales/en.yml b/config/locales/en.yml index c8c5cffc4..c666746da 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -723,7 +723,7 @@ Make sure these answers are correct." hint_text: "This is higher than the purchase price minus the discount." pregnancy: title: "You told us somebody in the household is pregnant" - no_females: "You also told us there are no female tenants living at the property." + all_male_tenants: "You also told us that all the tenants living at the property are male." females_not_in_soft_age_range: "You also told us that any female tenants living at the property are in the following age ranges:" major_repairs_date: title_text: "You told us the property has been vacant for 2 years." diff --git a/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb index 0d61b08f1..818a2b589 100644 --- a/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb +++ b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb @@ -25,7 +25,7 @@ RSpec.describe "bulk_update" do allow(ENV).to receive(:[]).with("BULK_UPLOAD_BUCKET").and_return(instance_name) WebMock.stub_request(:get, /api\.postcodes\.io/) - .to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) + .to_return(status: 404, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/B11BB/) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E09000033"}}}', headers: {}) end diff --git a/spec/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check_spec.rb b/spec/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check_spec.rb index 06f17b190..d62fccc13 100644 --- a/spec/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check_spec.rb +++ b/spec/models/form/lettings/pages/no_females_pregnant_household_person_age_value_check_spec.rb @@ -33,7 +33,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonAgeValueCh [ { "age2_known" => 0, - "no_females_in_a_pregnant_household?" => true, + "all_male_tenants_in_a_pregnant_household?" => true, }, ], ) @@ -54,7 +54,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonAgeValueCh it "has the correct informative_text" do expect(page.informative_text).to eq({ - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [ { "key" => "sex1", @@ -78,7 +78,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonAgeValueCh [ { "age3_known" => 0, - "no_females_in_a_pregnant_household?" => true, + "all_male_tenants_in_a_pregnant_household?" => true, }, ], ) @@ -99,7 +99,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonAgeValueCh it "has the correct informative_text" do expect(page.informative_text).to eq({ - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [ { "key" => "sex1", diff --git a/spec/models/form/lettings/pages/no_females_pregnant_household_person_value_check_spec.rb b/spec/models/form/lettings/pages/no_females_pregnant_household_person_value_check_spec.rb index 4be9a5ef2..dca600e0f 100644 --- a/spec/models/form/lettings/pages/no_females_pregnant_household_person_value_check_spec.rb +++ b/spec/models/form/lettings/pages/no_females_pregnant_household_person_value_check_spec.rb @@ -33,7 +33,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonValueCheck [ { "details_known_2" => 0, - "no_females_in_a_pregnant_household?" => true, + "all_male_tenants_in_a_pregnant_household?" => true, }, ], ) @@ -54,7 +54,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonValueCheck it "has the correct informative_text" do expect(page.informative_text).to eq({ - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [ { "key" => "sex1", @@ -78,7 +78,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonValueCheck [ { "details_known_3" => 0, - "no_females_in_a_pregnant_household?" => true, + "all_male_tenants_in_a_pregnant_household?" => true, }, ], ) @@ -99,7 +99,7 @@ RSpec.describe Form::Lettings::Pages::NoFemalesPregnantHouseholdPersonValueCheck it "has the correct informative_text" do expect(page.informative_text).to eq({ - "translation" => "soft_validations.pregnancy.no_females", + "translation" => "soft_validations.pregnancy.all_male_tenants", "arguments" => [ { "key" => "sex1", diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 2001c0bfa..92452f197 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -329,17 +329,6 @@ RSpec.describe LettingsLog do .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) end - context "when the local authority lookup times out" do - before do - allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) - end - - it "logs a warning" do - expect(Rails.logger).to receive(:warn).with("Postcodes.io lookup timed out") - address_lettings_log.update!({ postcode_known: 1, postcode_full: "M1 1AD" }) - end - end - it "correctly resets all fields if property postcode not known" do address_lettings_log.update!({ postcode_known: 0 }) diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index ddc696be4..a568ca330 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -628,17 +628,6 @@ RSpec.describe SalesLog, type: :model do .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) end - context "when the local authority lookup times out" do - before do - allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) - end - - it "logs a warning" do - expect(Rails.logger).to receive(:warn).with("Postcodes.io lookup timed out") - address_sales_log.update!({ pcodenk: 1, postcode_full: "M1 1AD" }) - end - end - it "correctly resets all fields if property postcode not known" do address_sales_log.update!({ pcodenk: 1 }) diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 6a3d9e051..8c7380fde 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -153,24 +153,24 @@ RSpec.describe Validations::SoftValidations do end describe "pregnancy soft validations" do - context "when there are no female tenants" do + context "when all tenants are male" do it "shows the interruption screen" do record.age1 = 43 record.sex1 = "M" record.preg_occ = 1 record.hhmemb = 1 record.age1_known = 0 - expect(record.no_females_in_a_pregnant_household?).to be true + expect(record.all_male_tenants_in_a_pregnant_household?).to be true end end - context "when there are no female tenants and age of other tenants is unknown" do + context "when there all tenants are male and age of tenants is unknown" do it "shows the interruption screen" do record.sex1 = "M" record.preg_occ = 1 record.hhmemb = 1 record.age1_known = 1 - expect(record.no_females_in_a_pregnant_household?).to be true + expect(record.all_male_tenants_in_a_pregnant_household?).to be true end end @@ -206,7 +206,7 @@ RSpec.describe Validations::SoftValidations do record.sex1 = "F" record.preg_occ = 1 record.hhmemb = 1 - expect(record.no_females_in_a_pregnant_household?).to be false + expect(record.all_male_tenants_in_a_pregnant_household?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false end end @@ -215,7 +215,7 @@ RSpec.describe Validations::SoftValidations do it "does not show the interruption screen" do record.preg_occ = 1 record.hhmemb = 2 - expect(record.no_females_in_a_pregnant_household?).to be false + expect(record.all_male_tenants_in_a_pregnant_household?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false end end diff --git a/spec/request_helper.rb b/spec/request_helper.rb index 15d178218..f1f208ec6 100644 --- a/spec/request_helper.rb +++ b/spec/request_helper.rb @@ -4,7 +4,7 @@ module RequestHelper def self.stub_http_requests WebMock.disable_net_connect!(allow_localhost: true) WebMock.stub_request(:get, /api\.postcodes\.io/) - .to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) + .to_return(status: 404, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA11AA") .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) diff --git a/spec/services/postcode_service_spec.rb b/spec/services/postcode_service_spec.rb index ecf20fdac..abc61129f 100644 --- a/spec/services/postcode_service_spec.rb +++ b/spec/services/postcode_service_spec.rb @@ -1,9 +1,80 @@ require "rails_helper" describe PostcodeService do - let(:postcode) { "s r81LS\u00A0" } + let(:service) { described_class.new } - it "returns clean postcode" do - expect(described_class.clean(postcode)).to eq "SR81LS" + describe "clean" do + let(:postcode) { "s r81LS\u00A0" } + + it "returns clean postcode" do + expect(described_class.clean(postcode)).to eq "SR81LS" + end + end + + describe "lookup" do + before do + Excon.defaults[:mock] = true + Excon.defaults[:stubs] = :local + end + + context "when the request returns a success response" do + before do + Excon.stub({}, { body: '{"result": { "admin_district": "District", "codes": { "admin_district": "123" } } }', status: 200 }) + end + + it "returns the admin district and admin district code" do + result = service.lookup("A00 0AA") + expect(result[:location_code]).to eq("123") + expect(result[:location_admin_district]).to eq("District") + end + end + + context "when the request returns a not found response" do + before do + Excon.stub({}, { status: 404 }) + end + + it "returns nil" do + result = service.lookup("A00 0AA") + expect(result).to be_nil + end + + it "logs the error at warning level" do + expect(Rails.logger).to receive(:warn).with(match "404 Not Found") + service.lookup("A00 0AA") + end + end + + context "when the request returns an error response" do + before do + Excon.stub({}, { body: "This is an error message that is not valid json", status: 500 }) + end + + it "returns nil" do + result = service.lookup("A00 0AA") + expect(result).to be_nil + end + + it "logs the error at warning level" do + expect(Rails.logger).to receive(:warn).with(match "This is an error message that is not valid json") + service.lookup("A00 0AA") + end + end + + context "when the request returns a success response that causes later errors" do + before do + Excon.stub({}, { body: '{"result": { "admin_district": "District" } }', status: 200 }) + end + + it "returns nil" do + result = service.lookup("A00 0AA") + expect(result).to be_nil + end + + it "logs the error at error level" do + expect(Rails.logger).to receive(:error).with(match "Unexpected error with postcode lookup request") + service.lookup("A00 0AA") + end + end end end