diff --git a/app/services/postcode_service.rb b/app/services/postcode_service.rb index a77887b89..db2132e01 100644 --- a/app/services/postcode_service.rb +++ b/app/services/postcode_service.rb @@ -22,9 +22,9 @@ class PostcodeService OpenStruct.new({ location_code: postcode_lookup.codes["admin_district"], location_admin_district: postcode_lookup&.admin_district, - incode: postcode_lookup.incode, - outcode: postcode_lookup.outcode, - result?: postcode_lookup.outcode.present?, + incode: postcode_lookup&.incode, + outcode: postcode_lookup&.outcode, + result?: postcode_lookup&.outcode&.present?, }) end end diff --git a/spec/request_helper.rb b/spec/request_helper.rb index 694a97e22..6efff2422 100644 --- a/spec/request_helper.rb +++ b/spec/request_helper.rb @@ -7,13 +7,13 @@ module RequestHelper .to_return(status: 200, 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: {}) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\", \"outcode\": \"AA1\", \"incode\": \"1AA\", \"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA12AA") - .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 2AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 2AA\", \"outcode\": \"AA1\", \"incode\": \"2AA\", \"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/NW1L5DP") - .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"NW1L 5DP\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"NW1L 5DP\", \"outcode\": \"NW1L\", \"incode\": \"5DP\", \"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/ZZ11ZZ") - .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"ZZ1 1ZZ\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"ZZ1 1ZZ\", \"outcode\": \"ZZ1\", \"incode\": \"1ZZ\", \"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/) .to_return(status: 200, body: "", headers: {}) diff --git a/spec/services/postcode_service_spec.rb b/spec/services/postcode_service_spec.rb index ecf20fdac..18d717314 100644 --- a/spec/services/postcode_service_spec.rb +++ b/spec/services/postcode_service_spec.rb @@ -6,4 +6,26 @@ describe PostcodeService do it "returns clean postcode" do expect(described_class.clean(postcode)).to eq "SR81LS" end + + describe "#lookup" do + context "with valid postcode" do + subject(:result) { described_class.new.lookup("AA11AA") } + + it "returns hash" do + expect(result[:location_code]).to eq("E09000033") + expect(result[:location_admin_district]).to eq("Westminster") + expect(result[:outcode]).to eq("AA1") + expect(result[:incode]).to eq("1AA") + expect(result[:result?]).to be true + end + + it "returns object" do + expect(result.location_code).to eq("E09000033") + expect(result.location_admin_district).to eq("Westminster") + expect(result.outcode).to eq("AA1") + expect(result.incode).to eq("1AA") + expect(result.result?).to be true + end + end + end end