diff --git a/.env.example b/.env.example index d30382d9e..6d4c5c053 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,5 @@ DB_USERNAME=postgres-user DB_PASSWORD=postgres-password CORE_EMAIL_USERNAME=email@example.com CORE_EMAIL_PASSWORD=password123 +OS_PLACES_API_KEY=xx + diff --git a/Gemfile b/Gemfile index deaf820b7..c707c3ef5 100644 --- a/Gemfile +++ b/Gemfile @@ -66,6 +66,7 @@ group :test do gem "factory_bot_rails" gem "selenium-webdriver", require: false gem "simplecov", require: false + gem "webmock" %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib| gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false end diff --git a/Gemfile.lock b/Gemfile.lock index d2f2bd632..6ef45d384 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -158,6 +158,8 @@ GEM childprocess (4.1.0) coderay (1.1.3) concurrent-ruby (1.1.9) + crack (0.4.5) + rexml crass (1.0.6) deep_merge (1.2.1) diff-lcs (1.4.4) @@ -192,6 +194,7 @@ GEM has_scope (0.8.0) actionpack (>= 5.2) activesupport (>= 5.2) + hashdiff (1.0.1) hotwire-rails (0.1.3) rails (>= 6.0.0) stimulus-rails @@ -389,6 +392,10 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) + webmock (3.14.0) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) webpacker (5.4.3) activesupport (>= 5.2) rack-proxy (>= 0.6.1) @@ -445,6 +452,7 @@ DEPENDENCIES uk_postcode view_component web-console (>= 4.1.0) + webmock webpacker (~> 5.0) RUBY VERSION diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 7ea9af741..6c5cbe68a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,3 +1,7 @@ +require "uri" +require "net/http" +require "json" + class CaseLogValidator < ActiveModel::Validator # Validations methods need to be called 'validate_' to run on model save # or form page submission @@ -193,6 +197,14 @@ private self.hhmemb = other_hhmemb + 1 if other_hhmemb.present? self.renttype = RENT_TYPE_MAPPING[rent_type] self.lettype = "#{renttype} #{needstype} #{owning_organisation['Org type']}" if renttype.present? && needstype.present? && owning_organisation["Org type"].present? + self.la = get_la(property_postcode) if property_postcode.present? + end + + def get_la(postcode) + uri = URI("https://api.os.uk/search/places/v1/postcode?key=#{ENV['OS_PLACES_API_KEY']}&postcode=#{postcode}&dataset=LPI") + res = Net::HTTP.get_response(uri) + response_body = JSON.parse(res.body) + response_body["results"][0]["LPI"]["ADMINISTRATIVE_AREA"].downcase.capitalize if res.is_a?(Net::HTTPSuccess) && (response_body["header"]["totalresults"]).to_i.positive? end def all_fields_completed? diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 8086aa3e3..f64024d1c 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1,8 +1,12 @@ require "rails_helper" +require_relative "../request_helper" RSpec.describe Form, type: :model do let(:owning_organisation) { FactoryBot.create(:organisation) } let(:managing_organisation) { owning_organisation } + before do + RequestHelper.stub_http_requests + end describe "#new" do it "validates age is a number" do @@ -1016,5 +1020,28 @@ RSpec.describe Form, type: :model do expect(record_from_db["month"]).to eq(10) expect(record_from_db["year"]).to eq(2021) end + + context "addresses" do + before do + stub_request(:get, /api.os.uk/) + .to_return(status: 200, body: "{\"header\": {\"totalresults\": \"1\"}, \"results\": [{\"LPI\": {\"ADMINISTRATIVE_AREA\": \"MANCHESTER\"}}]}", headers: {}) + end + + let!(:address_case_log) do + CaseLog.create({ + managing_organisation: organisation, + owning_organisation: organisation, + property_postcode: "M1 1AE", + }) + end + + it "correctly infers postcode" do + address_case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select la from case_logs where id=#{address_case_log.id}").to_a[0] + expect(address_case_log.la).to eq("Manchester") + expect(record_from_db["la"]).to eq("E08000003") + end + end end end diff --git a/spec/request_helper.rb b/spec/request_helper.rb new file mode 100644 index 000000000..2ab6169bf --- /dev/null +++ b/spec/request_helper.rb @@ -0,0 +1,9 @@ +require "webmock/rspec" + +module RequestHelper + def self.stub_http_requests + WebMock.disable_net_connect!(allow_localhost: true) + WebMock.stub_request(:get, /api.os.uk/) + .to_return(status: 200, body: "{\"header\": {\"totalresults\": \"0\"}}", headers: {}) + end +end