Browse Source

Case logs export (#288)

pull/291/head
Stéphane Meny 3 years ago committed by GitHub
parent
commit
55a29832a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Gemfile
  2. 2
      Gemfile.lock
  3. 75
      app/services/exports/case_log_export_service.rb
  4. 7
      lib/tasks/data_export.rake
  5. 2
      lib/tasks/data_import.rake
  6. 2
      spec/controllers/admin/case_logs_controller_spec.rb
  7. 2
      spec/controllers/admin/dashboard_controller_spec.rb
  8. 2
      spec/features/form/accessible_autocomplete_spec.rb
  9. 2
      spec/features/form/check_answers_page_spec.rb
  10. 2
      spec/features/form/conditional_questions_spec.rb
  11. 2
      spec/features/form/form_navigation_spec.rb
  12. 2
      spec/features/form/page_routing_spec.rb
  13. 2
      spec/features/form/progressive_total_field_spec.rb
  14. 2
      spec/features/form/saving_data_spec.rb
  15. 2
      spec/features/form/validations_spec.rb
  16. 1
      spec/features/user_spec.rb
  17. 321
      spec/fixtures/exports/case_logs.xml
  18. 5
      spec/helpers/form_page_error_helper_spec.rb
  19. 5
      spec/helpers/tasklist_helper_spec.rb
  20. 33
      spec/lib/tasks/data_export_spec.rb
  21. 2
      spec/lib/tasks/data_import_spec.rb
  22. 5
      spec/models/case_log_spec.rb
  23. 5
      spec/models/organisation_spec.rb
  24. 5
      spec/models/user_spec.rb
  25. 7
      spec/request_helper.rb
  26. 3
      spec/requests/case_logs_controller_spec.rb
  27. 5
      spec/requests/soft_validations_controller_spec.rb
  28. 42
      spec/services/exports/case_log_export_service_spec.rb
  29. 4
      spec/spec_helper.rb
  30. 5
      spec/views/case_log_index_view_spec.rb
  31. 2
      spec/views/form/page_view_spec.rb

1
Gemfile

@ -78,6 +78,7 @@ group :test do
gem "rspec-rails", require: false
gem "selenium-webdriver", require: false
gem "simplecov", require: false
gem "timecop", "~> 0.9.4"
gem "webmock", require: false
end

2
Gemfile.lock

@ -412,6 +412,7 @@ GEM
railties (>= 6.0.0)
strscan (3.0.1)
thor (1.2.1)
timecop (0.9.4)
timeout (0.2.0)
turbo-rails (1.0.1)
actionpack (>= 6.0.0)
@ -487,6 +488,7 @@ DEPENDENCIES
scss_lint-govuk
selenium-webdriver
simplecov
timecop (~> 0.9.4)
two_factor_authentication!
tzinfo-data
uk_postcode

75
app/services/exports/case_log_export_service.rb

@ -0,0 +1,75 @@
# Temporary instructions for reference
# (to be updated on feedback and deleted when implemented)
#
# create manifest file (one per run), have to be daily even with no data
# manifest => Manifest_2022_01_30_0001(i).csv
# folder_name / timestamp / full_path
#
# folder => core_year_month_f0001 (use day for now)
# file => dat_core_year_month_f0001_0001(i).xml (increment matches manifest for a given day)
#
# [Manifest generation]
# iterate manifests for the day and determine next increment
# read previous manifest if present (read timestamp / reuse folder name)
# otherwise determine next folder for the month
# hold writing manifest until we checked for data
#
# [Retrieve case logs]
# Find all case logs updates from last run of the day (midnight if none)
# Write manifest
# Determine next increment for the folder (inc = 1 if none)
# Export retrieved case logs into XML file
#
# [Zipping mechanism left for later]
module Exports
class CaseLogExportService
def initialize(storage_service, logger = Rails.logger)
@storage_service = storage_service
@logger = logger
end
def export_case_logs
case_logs = retrieve_case_logs
string_io = build_export_string_io(case_logs)
file_path = "#{get_folder_name}/#{get_file_name}.xml"
@storage_service.write_file(file_path, string_io)
end
private
def retrieve_case_logs
params = { from: Time.current.beginning_of_day, to: Time.current, status: CaseLog.statuses[:completed] }
CaseLog.where("updated_at >= :from and updated_at <= :to and status = :status", params)
end
def build_export_string_io(case_logs)
doc = Nokogiri::XML("<forms/>")
case_logs.each do |case_log|
form = doc.create_element("form")
doc.at("forms") << form
case_log.attributes.each do |key, value|
form << doc.create_element(key, value)
end
end
doc.write_xml_to(StringIO.new, encoding: "UTF-8")
end
def get_folder_name
"core_#{day_as_string}"
end
def get_file_name
"dat_core_#{day_as_string}_#{increment_as_string}"
end
def day_as_string
Time.current.strftime("%Y_%m_%d")
end
def increment_as_string(increment = 1)
sprintf("%04d", increment)
end
end
end

7
lib/tasks/data_export.rake

@ -0,0 +1,7 @@
namespace :core do
desc "Export data XMLs for import into Central Data System (CDS)"
task data_export: :environment do
storage_service = StorageService.new(PaasConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"])
Exports::CaseLogExportService.new(storage_service).export_case_logs
end
end

2
lib/tasks/data_import.rake

@ -1,5 +1,3 @@
require "nokogiri"
namespace :core do
desc "Import data XMLs from Softwire system"
task :data_import, %i[type path] => :environment do |_task, args|

2
spec/controllers/admin/case_logs_controller_spec.rb

@ -1,10 +1,8 @@
require "rails_helper"
require_relative "../../support/devise"
require_relative "../../request_helper"
describe Admin::CaseLogsController, type: :controller do
before do
RequestHelper.stub_http_requests
sign_in admin_user
end

2
spec/controllers/admin/dashboard_controller_spec.rb

@ -1,10 +1,8 @@
require "rails_helper"
require_relative "../../support/devise"
require_relative "../../request_helper"
describe Admin::DashboardController, type: :controller do
before do
RequestHelper.stub_http_requests
sign_in admin_user
end

2
spec/features/form/accessible_autocomplete_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Accessible Automcomplete" do
include Helpers
@ -15,7 +14,6 @@ RSpec.describe "Accessible Automcomplete" do
end
before do
RequestHelper.stub_http_requests
sign_in user
end

2
spec/features/form/check_answers_page_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Form Check Answers Page" do
include Helpers
@ -33,7 +32,6 @@ RSpec.describe "Form Check Answers Page" do
let(:id) { case_log.id }
before do
RequestHelper.stub_http_requests
sign_in user
end

2
spec/features/form/conditional_questions_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Form Conditional Questions" do
include Helpers
@ -16,7 +15,6 @@ RSpec.describe "Form Conditional Questions" do
let(:id) { case_log.id }
before do
RequestHelper.stub_http_requests
sign_in user
end

2
spec/features/form/form_navigation_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Form Navigation" do
include Helpers
@ -24,7 +23,6 @@ RSpec.describe "Form Navigation" do
end
before do
RequestHelper.stub_http_requests
sign_in user
end

2
spec/features/form/page_routing_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Form Page Routing" do
include Helpers
@ -17,7 +16,6 @@ RSpec.describe "Form Page Routing" do
let(:validator) { case_log._validators[nil].first }
before do
RequestHelper.stub_http_requests
allow(validator).to receive(:validate_pregnancy).and_return(true)
sign_in user
end

2
spec/features/form/progressive_total_field_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Accessible Automcomplete" do
include Helpers
@ -15,7 +14,6 @@ RSpec.describe "Accessible Automcomplete" do
end
before do
RequestHelper.stub_http_requests
sign_in user
end

2
spec/features/form/saving_data_spec.rb

@ -1,6 +1,5 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Form Saving Data" do
include Helpers
@ -32,7 +31,6 @@ RSpec.describe "Form Saving Data" do
end
before do
RequestHelper.stub_http_requests
sign_in user
end

2
spec/features/form/validations_spec.rb

@ -1,10 +1,8 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "validations" do
before do
RequestHelper.stub_http_requests
sign_in user
end

1
spec/features/user_spec.rb

@ -1,5 +1,4 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe "User Features" do
let!(:user) { FactoryBot.create(:user, last_sign_in_at: Time.zone.now) }

321
spec/fixtures/exports/case_logs.xml vendored

@ -0,0 +1,321 @@
<?xml version="1.0" encoding="UTF-8"?>
<forms>
<form>
<id>{id_1}</id>
<status>completed</status>
<created_at>2022-02-08 16:52:15 +0000</created_at>
<updated_at>2022-02-08 16:52:15 +0000</updated_at>
<tenant_code>BZ737</tenant_code>
<age1>35</age1>
<sex1>Female</sex1>
<ethnic>White: Irish</ethnic>
<national>Estonia</national>
<prevten>Private sector tenancy</prevten>
<ecstat1>Part-time - Less than 30 hours</ecstat1>
<hhmemb>2</hhmemb>
<relat2>Partner</relat2>
<age2>32</age2>
<sex2>Male</sex2>
<ecstat2>Not seeking work</ecstat2>
<relat3/>
<age3/>
<sex3/>
<ecstat3/>
<relat4/>
<age4/>
<sex4/>
<ecstat4/>
<relat5/>
<age5/>
<sex5/>
<ecstat5/>
<relat6/>
<age6/>
<sex6/>
<ecstat6/>
<relat7/>
<age7/>
<sex7/>
<ecstat7/>
<relat8/>
<age8/>
<sex8/>
<ecstat8/>
<homeless>Yes - other homelessness</homeless>
<underoccupation_benefitcap>No</underoccupation_benefitcap>
<leftreg>No - they left up to 5 years ago</leftreg>
<reservist>No</reservist>
<illness>Yes</illness>
<preg_occ>Yes</preg_occ>
<accessibility_requirements>No</accessibility_requirements>
<condition_effects>dummy</condition_effects>
<tenancy_code>BZ757</tenancy_code>
<startertenancy>No</startertenancy>
<tenancylength>5</tenancylength>
<tenancy>Secure (including flexible)</tenancy>
<landlord>This landlord</landlord>
<previous_postcode>SE2 6RT</previous_postcode>
<rsnvac>Tenant abandoned property</rsnvac>
<unittype_gn>House</unittype_gn>
<beds>3</beds>
<offered>2</offered>
<wchair>Yes</wchair>
<earnings>68</earnings>
<incfreq>Weekly</incfreq>
<benefits>Some</benefits>
<period>Every 2 weeks</period>
<layear>1 to 2 years</layear>
<lawaitlist>Less than 1 year</lawaitlist>
<property_postcode>NW1 5TY</property_postcode>
<reasonpref>Yes</reasonpref>
<reasonable_preference_reason>dummy</reasonable_preference_reason>
<cbl>Yes</cbl>
<chr>Yes</chr>
<cap>No</cap>
<other_reason_for_leaving_last_settled_home/>
<housingneeds_a>Yes</housingneeds_a>
<housingneeds_b>No</housingneeds_b>
<housingneeds_c>No</housingneeds_c>
<housingneeds_f>No</housingneeds_f>
<housingneeds_g>No</housingneeds_g>
<housingneeds_h>No</housingneeds_h>
<accessibility_requirements_prefer_not_to_say>No</accessibility_requirements_prefer_not_to_say>
<illness_type_1>No</illness_type_1>
<illness_type_2>Yes</illness_type_2>
<illness_type_3>No</illness_type_3>
<illness_type_4>No</illness_type_4>
<illness_type_8>No</illness_type_8>
<illness_type_5>No</illness_type_5>
<illness_type_6>No</illness_type_6>
<illness_type_7>No</illness_type_7>
<illness_type_9>No</illness_type_9>
<illness_type_10>No</illness_type_10>
<rp_homeless>Yes</rp_homeless>
<rp_insan_unsat>No</rp_insan_unsat>
<rp_medwel>No</rp_medwel>
<rp_hardship>No</rp_hardship>
<rp_dontknow>No</rp_dontknow>
<tenancyother/>
<override_net_income_validation/>
<property_owner_organisation>Test</property_owner_organisation>
<property_manager_organisation>Test</property_manager_organisation>
<sale_or_letting/>
<renewal>1</renewal>
<rent_type>1</rent_type>
<intermediate_rent_product_name>2</intermediate_rent_product_name>
<purchaser_code>798794</purchaser_code>
<reason>Permanently decanted from another property owned by this landlord</reason>
<propcode>123</propcode>
<majorrepairs>Yes</majorrepairs>
<la>Barnet</la>
<prevloc>Ashford</prevloc>
<hb>Housing benefit</hb>
<hbrentshortfall>Yes</hbrentshortfall>
<postcode>NW1</postcode>
<postcod2>5TY</postcod2>
<ppostc1>SE2</ppostc1>
<ppostc2>6RT</ppostc2>
<property_relet>No</property_relet>
<mrcdate>2022-02-08 16:52:15 +0000</mrcdate>
<mrcday>8</mrcday>
<mrcmonth>2</mrcmonth>
<mrcyear>2022</mrcyear>
<other_hhmemb>1</other_hhmemb>
<incref>No</incref>
<sale_completion_date/>
<startdate>2022-02-08 16:52:15 +0000</startdate>
<armedforces>A current or former regular in the UK Armed Forces (excluding National Service)</armedforces>
<first_time_property_let_as_social_housing/>
<unitletas>Affordable rent basis</unitletas>
<builtype>Purpose built</builtype>
<property_void_date>2019-11-03 00:00:00 +0000</property_void_date>
<owning_organisation_id>{owning_org_id_1}</owning_organisation_id>
<managing_organisation_id>{managing_org_id_1}</managing_organisation_id>
<renttype/>
<needstype>General needs</needstype>
<lettype>Affordable Rent General needs LA</lettype>
<postcode_known>Yes</postcode_known>
<la_known>Yes</la_known>
<is_la_inferred>false</is_la_inferred>
<day>8</day>
<month>2</month>
<year>2022</year>
<totchild>0</totchild>
<totelder>0</totelder>
<totadult>2</totadult>
<net_income_known>Yes</net_income_known>
<has_benefits>Yes</has_benefits>
<nocharge>No</nocharge>
<is_carehome>No</is_carehome>
<letting_in_sheltered_accomodation>No</letting_in_sheltered_accomodation>
<household_charge>Yes</household_charge>
<referral/>
<brent>200.0</brent>
<scharge>50.0</scharge>
<pscharge>40.0</pscharge>
<supcharg>35.0</supcharg>
<tcharge>325.0</tcharge>
<tshortfall>12.0</tshortfall>
<chcharge>7.0</chcharge>
<declaration>Yes</declaration>
</form>
<form>
<id>{id_2}</id>
<status>completed</status>
<created_at>2022-02-08 16:52:15 +0000</created_at>
<updated_at>2022-02-08 16:52:15 +0000</updated_at>
<tenant_code>BZ737</tenant_code>
<age1>35</age1>
<sex1>Female</sex1>
<ethnic>White: Irish</ethnic>
<national>Estonia</national>
<prevten>Private sector tenancy</prevten>
<ecstat1>Part-time - Less than 30 hours</ecstat1>
<hhmemb>2</hhmemb>
<relat2>Partner</relat2>
<age2>32</age2>
<sex2>Male</sex2>
<ecstat2>Not seeking work</ecstat2>
<relat3/>
<age3/>
<sex3/>
<ecstat3/>
<relat4/>
<age4/>
<sex4/>
<ecstat4/>
<relat5/>
<age5/>
<sex5/>
<ecstat5/>
<relat6/>
<age6/>
<sex6/>
<ecstat6/>
<relat7/>
<age7/>
<sex7/>
<ecstat7/>
<relat8/>
<age8/>
<sex8/>
<ecstat8/>
<homeless>Yes - other homelessness</homeless>
<underoccupation_benefitcap>No</underoccupation_benefitcap>
<leftreg>No - they left up to 5 years ago</leftreg>
<reservist>No</reservist>
<illness>Yes</illness>
<preg_occ>Yes</preg_occ>
<accessibility_requirements>No</accessibility_requirements>
<condition_effects>dummy</condition_effects>
<tenancy_code>BZ757</tenancy_code>
<startertenancy>No</startertenancy>
<tenancylength>5</tenancylength>
<tenancy>Secure (including flexible)</tenancy>
<landlord>This landlord</landlord>
<previous_postcode>SE2 6RT</previous_postcode>
<rsnvac>Tenant abandoned property</rsnvac>
<unittype_gn>House</unittype_gn>
<beds>3</beds>
<offered>2</offered>
<wchair>Yes</wchair>
<earnings>68</earnings>
<incfreq>Weekly</incfreq>
<benefits>Some</benefits>
<period>Every 2 weeks</period>
<layear>1 to 2 years</layear>
<lawaitlist>Less than 1 year</lawaitlist>
<property_postcode>NW1 5TY</property_postcode>
<reasonpref>Yes</reasonpref>
<reasonable_preference_reason>dummy</reasonable_preference_reason>
<cbl>Yes</cbl>
<chr>Yes</chr>
<cap>No</cap>
<other_reason_for_leaving_last_settled_home/>
<housingneeds_a>Yes</housingneeds_a>
<housingneeds_b>No</housingneeds_b>
<housingneeds_c>No</housingneeds_c>
<housingneeds_f>No</housingneeds_f>
<housingneeds_g>No</housingneeds_g>
<housingneeds_h>No</housingneeds_h>
<accessibility_requirements_prefer_not_to_say>No</accessibility_requirements_prefer_not_to_say>
<illness_type_1>No</illness_type_1>
<illness_type_2>Yes</illness_type_2>
<illness_type_3>No</illness_type_3>
<illness_type_4>No</illness_type_4>
<illness_type_8>No</illness_type_8>
<illness_type_5>No</illness_type_5>
<illness_type_6>No</illness_type_6>
<illness_type_7>No</illness_type_7>
<illness_type_9>No</illness_type_9>
<illness_type_10>No</illness_type_10>
<rp_homeless>Yes</rp_homeless>
<rp_insan_unsat>No</rp_insan_unsat>
<rp_medwel>No</rp_medwel>
<rp_hardship>No</rp_hardship>
<rp_dontknow>No</rp_dontknow>
<tenancyother/>
<override_net_income_validation/>
<property_owner_organisation>Test</property_owner_organisation>
<property_manager_organisation>Test</property_manager_organisation>
<sale_or_letting/>
<renewal>1</renewal>
<rent_type>1</rent_type>
<intermediate_rent_product_name>2</intermediate_rent_product_name>
<purchaser_code>798794</purchaser_code>
<reason>Permanently decanted from another property owned by this landlord</reason>
<propcode>123</propcode>
<majorrepairs>Yes</majorrepairs>
<la>Barnet</la>
<prevloc>Ashford</prevloc>
<hb>Housing benefit</hb>
<hbrentshortfall>Yes</hbrentshortfall>
<postcode>NW1</postcode>
<postcod2>5TY</postcod2>
<ppostc1>SE2</ppostc1>
<ppostc2>6RT</ppostc2>
<property_relet>No</property_relet>
<mrcdate>2022-02-08 16:52:15 +0000</mrcdate>
<mrcday>8</mrcday>
<mrcmonth>2</mrcmonth>
<mrcyear>2022</mrcyear>
<other_hhmemb>1</other_hhmemb>
<incref>No</incref>
<sale_completion_date/>
<startdate>2022-02-08 16:52:15 +0000</startdate>
<armedforces>A current or former regular in the UK Armed Forces (excluding National Service)</armedforces>
<first_time_property_let_as_social_housing/>
<unitletas>Affordable rent basis</unitletas>
<builtype>Purpose built</builtype>
<property_void_date>2019-11-03 00:00:00 +0000</property_void_date>
<owning_organisation_id>{owning_org_id_2}</owning_organisation_id>
<managing_organisation_id>{managing_org_id_2}</managing_organisation_id>
<renttype/>
<needstype>General needs</needstype>
<lettype>Affordable Rent General needs LA</lettype>
<postcode_known>Yes</postcode_known>
<la_known>Yes</la_known>
<is_la_inferred>false</is_la_inferred>
<day>8</day>
<month>2</month>
<year>2022</year>
<totchild>0</totchild>
<totelder>0</totelder>
<totadult>2</totadult>
<net_income_known>Yes</net_income_known>
<has_benefits>Yes</has_benefits>
<nocharge>No</nocharge>
<is_carehome>No</is_carehome>
<letting_in_sheltered_accomodation>No</letting_in_sheltered_accomodation>
<household_charge>Yes</household_charge>
<referral/>
<brent>200.0</brent>
<scharge>50.0</scharge>
<pscharge>40.0</pscharge>
<supcharg>35.0</supcharg>
<tcharge>325.0</tcharge>
<tshortfall>12.0</tshortfall>
<chcharge>7.0</chcharge>
<declaration>Yes</declaration>
</form>
</forms>

5
spec/helpers/form_page_error_helper_spec.rb

@ -1,11 +1,6 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe FormPageErrorHelper do
before do
RequestHelper.stub_http_requests
end
describe "#remove_other_page_errors" do
context "when non base other questions are removed" do
let!(:case_log) { FactoryBot.create(:case_log, :in_progress) }

5
spec/helpers/tasklist_helper_spec.rb

@ -1,11 +1,6 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe TasklistHelper do
before do
RequestHelper.stub_http_requests
end
let(:empty_case_log) { FactoryBot.create(:case_log) }
let(:case_log) { FactoryBot.create(:case_log, :in_progress) }

33
spec/lib/tasks/data_export_spec.rb

@ -0,0 +1,33 @@
require "rails_helper"
require "rake"
describe "rake core:data_export", type: task do
subject(:task) { Rake::Task["core:data_export"] }
let(:paas_instance) { "paas_export_instance" }
let(:storage_service) { instance_double(StorageService) }
let(:paas_config_service) { instance_double(PaasConfigurationService) }
let(:export_service) { instance_double(Exports::CaseLogExportService) }
before do
Rake.application.rake_require("tasks/data_export")
Rake::Task.define_task(:environment)
task.reenable
allow(StorageService).to receive(:new).and_return(storage_service)
allow(PaasConfigurationService).to receive(:new).and_return(paas_config_service)
allow(Exports::CaseLogExportService).to receive(:new).and_return(export_service)
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("EXPORT_PAAS_INSTANCE").and_return(paas_instance)
end
context "when exporting case logs" do
it "starts the export process" do
expect(StorageService).to receive(:new).with(paas_config_service, paas_instance)
expect(Exports::CaseLogExportService).to receive(:new).with(storage_service)
expect(export_service).to receive(:export_case_logs)
task.invoke
end
end
end

2
spec/lib/tasks/data_import_spec.rb

@ -5,7 +5,7 @@ describe "rake core:data_import", type: :task do
subject(:task) { Rake::Task["core:data_import"] }
let(:fixture_path) { "spec/fixtures/softwire_imports/organisations" }
let(:instance_name) { "my_instance" }
let(:instance_name) { "paas_import_instance" }
let(:organisation_type) { "organisation" }
let(:storage_service) { instance_double(StorageService) }

5
spec/models/case_log_spec.rb

@ -1,14 +1,9 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe CaseLog do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:managing_organisation) { owning_organisation }
before do
RequestHelper.stub_http_requests
end
describe "#form" do
let(:case_log) { FactoryBot.build(:case_log) }
let(:case_log_2) { FactoryBot.build(:case_log, startdate: Time.zone.local(2022, 1, 1)) }

5
spec/models/organisation_spec.rb

@ -1,11 +1,6 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe Organisation, type: :model do
before do
RequestHelper.stub_http_requests
end
describe "#new" do
let(:user) { FactoryBot.create(:user) }
let(:organisation) { user.organisation }

5
spec/models/user_spec.rb

@ -1,11 +1,6 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe User, type: :model do
before do
RequestHelper.stub_http_requests
end
describe "#new" do
let(:user) { FactoryBot.create(:user) }
let(:other_organisation) { FactoryBot.create(:organisation) }

7
spec/request_helper.rb

@ -1,12 +1,15 @@
require "webmock/rspec"
WebMock.disable_net_connect!(allow_localhost: true)
module RequestHelper
def self.stub_http_requests
WebMock.disable_net_connect!(allow_localhost: true)
WebMock.stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {})
WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/)
.to_return(status: 200, body: "", headers: {})
end
def self.real_http_requests
WebMock.allow_net_connect!
end
end

3
spec/requests/case_logs_controller_spec.rb

@ -1,5 +1,4 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe CaseLogsController, type: :request do
let(:owning_organisation) { FactoryBot.create(:organisation) }
@ -20,7 +19,6 @@ RSpec.describe CaseLogsController, type: :request do
end
before do
RequestHelper.stub_http_requests
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("API_USER").and_return(api_username)
allow(ENV).to receive(:[]).with("API_KEY").and_return(api_password)
@ -159,7 +157,6 @@ RSpec.describe CaseLogsController, type: :request do
let(:headers) { { "Accept" => "text/html" } }
before do
RequestHelper.stub_http_requests
sign_in user
get "/logs", headers: headers, params: {}
end

5
spec/requests/soft_validations_controller_spec.rb

@ -1,15 +1,10 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe SoftValidationsController, type: :request do
let(:params) { { case_log_id: case_log.id } }
let(:url) { "/logs/#{case_log.id}/net-income/soft-validations" }
let(:user) { FactoryBot.create(:user) }
before do
RequestHelper.stub_http_requests
end
context "when a user is not signed in" do
let(:case_log) { FactoryBot.create(:case_log, :in_progress) }

42
spec/services/exports/case_log_export_service_spec.rb

@ -0,0 +1,42 @@
require "rails_helper"
RSpec.describe Exports::CaseLogExportService do
let(:storage_service) { instance_double(StorageService) }
let(:export_filepath) { "spec/fixtures/exports/case_logs.xml" }
let(:export_file) { File.open(export_filepath, "r:UTF-8") }
let(:expected_filename) { "core_2022_02_08/dat_core_2022_02_08_0001.xml" }
let(:case_logs) { FactoryBot.create_list(:case_log, 2, :completed) }
def replace_entity_ids(export_template)
export_template.sub!(/\{id_1\}/, case_logs[0]["id"].to_s)
export_template.sub!(/\{id_2\}/, case_logs[1]["id"].to_s)
export_template.sub!(/\{owning_org_id_1\}/, case_logs[0]["owning_organisation_id"].to_s)
export_template.sub!(/\{owning_org_id_2\}/, case_logs[1]["owning_organisation_id"].to_s)
export_template.sub!(/\{managing_org_id_1\}/, case_logs[0]["managing_organisation_id"].to_s)
export_template.sub!(/\{managing_org_id_2\}/, case_logs[1]["managing_organisation_id"].to_s)
end
context "when exporting case logs" do
subject(:export_service) { described_class.new(storage_service) }
before do
Timecop.freeze(Time.new(2022, 2, 8, 16, 52, 15, "+00:00"))
case_logs
end
it "generate an XML export file with the expected filename" do
actual_filename = nil
allow(storage_service).to receive(:write_file) { |filename, _| actual_filename = filename }
export_service.export_case_logs
expect(actual_filename).to eq(expected_filename)
end
it "generate an XML export file with the expected content" do
actual_stringio = nil
allow(storage_service).to receive(:write_file) { |_, stringio| actual_stringio = stringio }
actual_content = replace_entity_ids(export_file.read)
export_service.export_case_logs
expect(actual_stringio&.string).to eq(actual_content)
end
end
end

4
spec/spec_helper.rb

@ -16,6 +16,7 @@
require "factory_bot"
require "simplecov"
require "request_helper"
SimpleCov.start "rails" do
add_filter "/bin/"
@ -108,4 +109,7 @@ RSpec.configure do |config|
# Kernel.srand config.seed
config.include FactoryBot::Syntax::Methods
config.before { RequestHelper.stub_http_requests }
config.after { RequestHelper.real_http_requests }
end

5
spec/views/case_log_index_view_spec.rb

@ -1,11 +1,6 @@
require "rails_helper"
require_relative "../request_helper"
RSpec.describe "case_logs/index" do
before do
RequestHelper.stub_http_requests
end
let(:in_progress_log) { FactoryBot.create(:case_log, :in_progress) }
let(:completed_log) { FactoryBot.create(:case_log, :completed) }

2
spec/views/form/page_view_spec.rb

@ -1,5 +1,4 @@
require "rails_helper"
require_relative "../../request_helper"
RSpec.describe "form/page" do
let(:case_log) { FactoryBot.create(:case_log, :in_progress) }
@ -19,7 +18,6 @@ RSpec.describe "form/page" do
end
before do
RequestHelper.stub_http_requests
assign(:case_log, case_log)
assign(:page, page)
assign(:subsection, subsection)

Loading…
Cancel
Save