diff --git a/app/models/log.rb b/app/models/log.rb index 1012f60ee..17f372db0 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -82,7 +82,7 @@ class Log < ApplicationRecord service = AddressClient.new(address_string) service.call - return errors.add(:address_line1_input, :address_error, message: service.error) if service.error.present? + return errors.add(:address_selection, :address_error, message: service.error) if service.error.present? if address_selection.between?(0, 9) presenter = AddressDataPresenter.new(service.result[address_selection]) diff --git a/spec/shared/shared_log_examples.rb b/spec/shared/shared_log_examples.rb index 354a8b1ee..7202544ba 100644 --- a/spec/shared/shared_log_examples.rb +++ b/spec/shared/shared_log_examples.rb @@ -106,5 +106,67 @@ RSpec.shared_examples "shared log examples" do |log_type| end end end + + describe "#process_address_change!" do + context "when address_line1_input and postcode_full_input set to a value" do + let(:log) do + log = build( + log_type, + address_selection: 0, + address_line1: "Address line 1", + postcode_full: "AA1 1AA", + county: "county", + ) + log.save!(validate: false) + log + end + + it "updates log fields" do + log.address_line1_input = "New address line 1" + log.postcode_full_input = "BB2 2BB" + + allow_any_instance_of(AddressClient).to receive(:call) + allow_any_instance_of(AddressClient).to receive(:result).and_return([{ + "UPRN" => "UPRN", + "UDPRN" => "UDPRN", + "ADDRESS" => "full address", + "SUB_BUILDING_NAME" => "0", + "BUILDING_NAME" => "building name", + "THOROUGHFARE_NAME" => "thoroughfare", + "POST_TOWN" => "posttown", + "POSTCODE" => "postcode", + }]) + + expect { log.process_address_change! }.to change(log, :address_line1).from("Address line 1").to("0, Building Name, Thoroughfare") + .and change(log, :town_or_city).from(nil).to("Posttown") + .and change(log, :postcode_full).from("AA1 1AA").to("POSTCODE") + .and change(log, :uprn_confirmed).from(nil).to(1) + .and change(log, :uprn).from(nil).to("UPRN") + .and change(log, :uprn_known).from(nil).to(1) + .and change(log, :address_selection).from(0).to(nil) + .and change(log, :county).from("county").to(nil) + end + end + + context "when address inputs are nil" do + let(:log) { create(log_type, address_selection: nil, address_line1_input: nil, postcode_full_input: nil) } + + it "does not update log" do + expect { log.process_address_change! }.not_to change(log, :attributes) + end + end + + context "when service errors" do + let(:log) { build(log_type, :in_progress, address_selection: 0, address_line1_input: "123", postcode_full_input: "AA1 1AA") } + let(:error_message) { "error" } + + it "adds error to log" do + allow_any_instance_of(AddressClient).to receive(:call) + allow_any_instance_of(AddressClient).to receive(:error).and_return(error_message) + + expect { log.process_address_change! }.to change { log.errors[:address_selection] }.from([]).to([error_message]) + end + end + end end # rubocop:enable RSpec/AnyInstance