Browse Source
* CLDC-3244: Replace postcodes_io gem to improve error handling * Fix status code in default postcodes api mocking * Fix more mocking * Remove blank line * Handle non-Excon errors, in case of unexpected api behaviour * Remove outdated test * Remove another outdated testpull/2640/head v0.4.73
Rachael Booth
4 months ago
committed by
GitHub
8 changed files with 91 additions and 45 deletions
@ -1,9 +1,80 @@ |
|||||||
require "rails_helper" |
require "rails_helper" |
||||||
|
|
||||||
describe PostcodeService do |
describe PostcodeService do |
||||||
|
let(:service) { described_class.new } |
||||||
|
|
||||||
|
describe "clean" do |
||||||
let(:postcode) { "s r81LS\u00A0" } |
let(:postcode) { "s r81LS\u00A0" } |
||||||
|
|
||||||
it "returns clean postcode" do |
it "returns clean postcode" do |
||||||
expect(described_class.clean(postcode)).to eq "SR81LS" |
expect(described_class.clean(postcode)).to eq "SR81LS" |
||||||
end |
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 |
end |
||||||
|
Loading…
Reference in new issue