|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe Katalyst::GoogleApis::Geocoding::ReverseService do |
| 6 | + subject(:action) { described_class.call(latlng:, credentials:) } |
| 7 | + |
| 8 | + let(:latlng) { "-34.9172454,138.62046709999998" } |
| 9 | + let(:credentials) { instance_double(Katalyst::GoogleApis::Credentials) } |
| 10 | + let(:response) do |
| 11 | + { results: [ |
| 12 | + { place: "//places.googleapis.com/places/ChIJVVVxcUjJsGoRJZhZ-6cVzgg", |
| 13 | + placeId: "ChIJVVVxcUjJsGoRJZhZ-6cVzgg", |
| 14 | + location: { latitude: -34.9172326, longitude: 138.6204668 }, |
| 15 | + granularity: "ROOFTOP", |
| 16 | + viewport: |
| 17 | + { low: { latitude: -34.9185815802915, longitude: 138.6191178197085 }, |
| 18 | + high: { latitude: -34.9158836197085, longitude: 138.62181578029148 } }, |
| 19 | + formattedAddress: "64 North Terrace, Kent Town SA 5067, Australia", |
| 20 | + postalAddress: |
| 21 | + { regionCode: "AU", |
| 22 | + languageCode: "en", |
| 23 | + postalCode: "5067", |
| 24 | + administrativeArea: "SA", |
| 25 | + locality: "Kent Town", |
| 26 | + addressLines: ["64 North Terrace"] }, |
| 27 | + addressComponents: |
| 28 | + [{ longText: "64", shortText: "64", types: ["street_number"] }, |
| 29 | + { longText: "North Terrace", |
| 30 | + shortText: "North Terrace", |
| 31 | + types: ["route"], |
| 32 | + languageCode: "en" }, |
| 33 | + { longText: "Kent Town", |
| 34 | + shortText: "Kent Town", |
| 35 | + types: ["locality", "political"], |
| 36 | + languageCode: "en" }, |
| 37 | + { longText: "The City of Norwood Payneham and St Peters", |
| 38 | + shortText: "Norwood Payneham and St Peters", |
| 39 | + types: ["administrative_area_level_2", "political"], |
| 40 | + languageCode: "en" }, |
| 41 | + { longText: "South Australia", |
| 42 | + shortText: "SA", |
| 43 | + types: ["administrative_area_level_1", "political"], |
| 44 | + languageCode: "en" }, |
| 45 | + { longText: "Australia", |
| 46 | + shortText: "AU", |
| 47 | + types: ["country", "political"], |
| 48 | + languageCode: "en" }, |
| 49 | + { longText: "5067", shortText: "5067", types: ["postal_code"] }], |
| 50 | + types: ["establishment", "point_of_interest"], |
| 51 | + plusCode: |
| 52 | + { globalCode: "4QQW3JMC+45", |
| 53 | + compoundCode: "3JMC+45 Kent Town SA, Australia" } }, |
| 54 | + ] } |
| 55 | + end |
| 56 | + |
| 57 | + before do |
| 58 | + allow(credentials).to receive(:apply!) |
| 59 | + end |
| 60 | + |
| 61 | + def stub_api_request(status: 200, content_type: "application/json", response: self.response) |
| 62 | + stub_request(:get, /geocode.googleapis.com/).to_return( |
| 63 | + status:, |
| 64 | + headers: { "Content-Type" => content_type }, |
| 65 | + body: response.is_a?(String) ? response : response.to_json, |
| 66 | + ) |
| 67 | + end |
| 68 | + |
| 69 | + it "sends request to reverse geocoding with latlng" do |
| 70 | + stub_api_request |
| 71 | + |
| 72 | + action |
| 73 | + |
| 74 | + expect(a_request(:get, "https://geocode.googleapis.com/v4beta/geocode/location/-34.9172454,138.62046709999998")) |
| 75 | + .to have_been_made.once |
| 76 | + end |
| 77 | + |
| 78 | + it "extracts and exposes location details", :aggregate_failures do |
| 79 | + stub_api_request |
| 80 | + |
| 81 | + expect(action).to have_attributes( |
| 82 | + formatted_address: "64 North Terrace, Kent Town SA 5067, Australia", |
| 83 | + latlng: "-34.9172326,138.6204668", |
| 84 | + ) |
| 85 | + expect(action.locations).to be_an(Array) |
| 86 | + expect(action.first_location).to include(formattedAddress: "64 North Terrace, Kent Town SA 5067, Australia") |
| 87 | + end |
| 88 | + |
| 89 | + it "raises service errors returned by the API" do |
| 90 | + stub_api_request(status: 400, response: { |
| 91 | + error: { |
| 92 | + code: 400, |
| 93 | + message: "Invalid latlng format", |
| 94 | + status: "INVALID_ARGUMENT", |
| 95 | + }, |
| 96 | + }) |
| 97 | + |
| 98 | + expect do |
| 99 | + action |
| 100 | + end.to raise_error(having_attributes(code: 400, status: "INVALID_ARGUMENT", message: /Invalid latlng/)) |
| 101 | + end |
| 102 | + |
| 103 | + it "returns nil location details when no geocoding results are returned" do |
| 104 | + stub_api_request(response: { results: [] }) |
| 105 | + |
| 106 | + expect(action).to have_attributes(locations: [], first_location: nil, formatted_address: nil, latlng: nil) |
| 107 | + end |
| 108 | + |
| 109 | + it "raises on non-json responses" do |
| 110 | + stub_api_request(status: 500, content_type: "text/plain", response: "") |
| 111 | + |
| 112 | + expect { action }.to raise_error(having_attributes(code: 500, message: /Unexpected HTTP response/)) |
| 113 | + end |
| 114 | + |
| 115 | + it "raises on invalid JSON response bodies" do |
| 116 | + stub_api_request(response: "{ invalid") |
| 117 | + |
| 118 | + expect { action }.to raise_error(JSON::ParserError) |
| 119 | + end |
| 120 | + |
| 121 | + it "raises network errors" do |
| 122 | + stub_request(:get, /geocode.googleapis.com/).to_raise(Curl::Err::TimeoutError.new) |
| 123 | + |
| 124 | + expect { action }.to raise_error(Curl::Err::TimeoutError) |
| 125 | + end |
| 126 | +end |
0 commit comments