From ac4ee2a1fbbad40be9bf1bf7c85e84bcf07b3640 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 15 Sep 2022 15:33:00 +0100 Subject: [PATCH] Cldc 1600 refactor form handler (#875) * Add previous, current and next forms to form handler * Add current, previous and next sales forms to form handler * Implement current_lettings_form, current_sales_form and store year and form type in form * refactor lettings_forms * Use current, previous and next forms in lettings log model * Use current, previous and next forms in sales log model * use current, previous and next forms in csv service * Remove "startyear_endyear" forms from form handler * Remove name from form initializer and add an optional start year * refactor get_form * update csv test, fix form initialize * rebase fix * Refactor form_name_from_start_year method out * remove unused variable * fix typo, add date tests * rebase, fix tests * add comment to before test block * Change the FormHandler back to only contain the form objects * extract name --- app/models/form.rb | 9 +- app/models/form_handler.rb | 30 +++- app/models/lettings_log.rb | 4 +- app/models/rent_period.rb | 2 +- app/models/sales_log.rb | 4 +- app/models/validations/date_validations.rb | 8 +- app/services/csv/lettings_log_csv_service.rb | 8 +- spec/features/form/check_answers_page_spec.rb | 2 + .../form/conditional_questions_spec.rb | 2 + spec/features/form/form_navigation_spec.rb | 2 + spec/features/form/validations_spec.rb | 13 +- .../conditional_questions_helper_spec.rb | 5 + .../helpers/question_attribute_helper_spec.rb | 5 + spec/helpers/tasklist_helper_spec.rb | 5 + spec/jobs/email_csv_job_spec.rb | 5 + spec/models/form/page_spec.rb | 5 + spec/models/form/question_spec.rb | 5 + spec/models/form/section_spec.rb | 5 + spec/models/form/subsection_spec.rb | 2 + spec/models/form_handler_spec.rb | 146 ++++++++++++++++-- spec/models/form_spec.rb | 4 +- spec/models/lettings_log_spec.rb | 11 +- spec/models/rent_period_spec.rb | 4 +- spec/models/sales_log_spec.rb | 2 +- .../validations/financial_validations_spec.rb | 5 + .../validations/household_validations_spec.rb | 5 + .../validations/shared_validations_spec.rb | 5 + spec/requests/form_controller_spec.rb | 28 +++- .../requests/lettings_logs_controller_spec.rb | 2 + .../csv/lettings_log_csv_service_spec.rb | 2 +- .../lettings_log_export_service_spec.rb | 8 +- ...lettings_logs_field_import_service_spec.rb | 4 +- .../lettings_logs_import_service_spec.rb | 8 +- 33 files changed, 291 insertions(+), 64 deletions(-) diff --git a/app/models/form.rb b/app/models/form.rb index 549415dd4..22321f431 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -3,9 +3,8 @@ class Form :start_date, :end_date, :type, :name, :setup_definition, :setup_sections, :form_sections - def initialize(form_path, name, sections_in_form = [], type = "lettings") + def initialize(form_path, start_year = "", sections_in_form = [], type = "lettings") if type == "sales" - @name = name @setup_sections = [Form::Sales::Sections::Setup.new(nil, nil, self)] @form_sections = sections_in_form.map { |sec| sec.new(nil, nil, self) } @type = "sales" @@ -13,8 +12,8 @@ class Form @subsections = sections.flat_map(&:subsections) @pages = subsections.flat_map(&:pages) @questions = pages.flat_map(&:questions) - @start_date = Time.zone.local(name[0..3], 4, 1) - @end_date = Time.zone.local(start_date.year + 1, 7, 1) + @start_date = Time.zone.local(start_year, 4, 1) + @end_date = Time.zone.local(start_year + 1, 7, 1) @form_definition = { "form_type" => type, "start_date" => start_date, @@ -24,7 +23,6 @@ class Form else raise "No form definition file exists for given year".freeze unless File.exist?(form_path) - @name = name @setup_sections = [Form::Lettings::Sections::Setup.new(nil, nil, self)] @form_definition = JSON.parse(File.open(form_path).read) @form_sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) } @@ -36,6 +34,7 @@ class Form @start_date = Time.iso8601(form_definition["start_date"]) @end_date = Time.iso8601(form_definition["end_date"]) end + @name = "#{start_date.year}_#{end_date.year}_#{type}" end def get_subsection(id) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index bfee3169f..40d993cd6 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -10,26 +10,46 @@ class FormHandler @forms[form] end - def current_form - forms[forms.keys.max_by(&:to_i)] + def current_lettings_form + forms["current_lettings"] + end + + def current_sales_form + forms["current_sales"] end def sales_forms sales_sections = [] # Add section classes here e.g. Form::Sales::Property::Sections::PropertyInformation - { "2022_2023_sales" => Form.new(nil, "2022_2023_sales", sales_sections, "sales") } + current_form = Form.new(nil, current_collection_start_year, sales_sections, "sales") + previous_form = Form.new(nil, current_collection_start_year - 1, sales_sections, "sales") + { "current_sales" => current_form, + "previous_sales" => previous_form } end def lettings_forms forms = {} directories.each do |directory| Dir.glob("#{directory}/*.json").each do |form_path| - form_name = File.basename(form_path, ".json") - forms[form_name] = Form.new(form_path, form_name) + form = Form.new(form_path) + + form_to_set = form_name_from_start_year(form.start_date.year, "lettings") + forms[form_to_set] = form if forms[form_to_set].blank? end end forms end + def current_collection_start_year + today = Time.zone.now + window_end_date = Time.zone.local(today.year, 4, 1) + today < window_end_date ? today.year - 1 : today.year + end + + def form_name_from_start_year(year, type) + form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}" } + form_mappings[current_collection_start_year - year] + end + private def get_all_forms diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 33d5974ce..c85f2d0c3 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -55,7 +55,7 @@ class LettingsLog < Log RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze def form - FormHandler.instance.get_form(form_name) || FormHandler.instance.forms.first.second + FormHandler.instance.get_form(form_name) || FormHandler.instance.current_lettings_form end def recalculate_start_year! @@ -66,7 +66,7 @@ class LettingsLog < Log def form_name return unless startdate - "#{collection_start_year}_#{collection_start_year + 1}" + FormHandler.instance.form_name_from_start_year(collection_start_year, "lettings") end def self.editable_fields diff --git a/app/models/rent_period.rb b/app/models/rent_period.rb index 77e154f4c..d48fe20ea 100644 --- a/app/models/rent_period.rb +++ b/app/models/rent_period.rb @@ -1,5 +1,5 @@ class RentPeriod def self.rent_period_mappings - FormHandler.instance.current_form.get_question("period", nil).answer_options + FormHandler.instance.current_lettings_form.get_question("period", nil).answer_options end end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index f73b518e5..6c4ae2ec8 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -23,11 +23,11 @@ class SalesLog < Log def form_name return unless saledate - "#{collection_start_year}_#{collection_start_year + 1}_sales" + FormHandler.instance.form_name_from_start_year(collection_start_year, "sales") end def form - FormHandler.instance.get_form(form_name) || FormHandler.instance.get_form("2022_2023_sales") + FormHandler.instance.get_form(form_name) || FormHandler.instance.current_sales_form end def optional_fields diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 8f8d87220..f5361e5d7 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -60,19 +60,19 @@ module Validations::DateValidations private def first_collection_start_date - @first_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.min + @first_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.min end def first_collection_end_date - @first_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.min + @first_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.min end def second_collection_start_date - @second_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.max + @second_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.max end def second_collection_end_date - @second_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.max + @second_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.max end def date_valid?(question, record) diff --git a/app/services/csv/lettings_log_csv_service.rb b/app/services/csv/lettings_log_csv_service.rb index 6070e91c2..101cd7e6c 100644 --- a/app/services/csv/lettings_log_csv_service.rb +++ b/app/services/csv/lettings_log_csv_service.rb @@ -51,7 +51,13 @@ module Csv def ordered_form_questions downloaded_form_years = LettingsLog.all.map(&:collection_start_year).uniq.compact - downloaded_form_fields = downloaded_form_years.count == 1 && downloaded_form_years[0].present? ? FormHandler.instance.get_form("#{downloaded_form_years[0]}_#{downloaded_form_years[0] + 1}").questions : FormHandler.instance.forms.first.second.questions + + if downloaded_form_years.count == 1 && downloaded_form_years[0].present? + form_name = FormHandler.instance.form_name_from_start_year(downloaded_form_years[0], "lettings") + downloaded_form_fields = FormHandler.instance.get_form(form_name).questions + else + downloaded_form_fields = FormHandler.instance.current_lettings_form.questions + end move_checkbox_answer_options(downloaded_form_fields) end diff --git a/spec/features/form/check_answers_page_spec.rb b/spec/features/form/check_answers_page_spec.rb index be5a8d46a..c5080b4bc 100644 --- a/spec/features/form/check_answers_page_spec.rb +++ b/spec/features/form/check_answers_page_spec.rb @@ -39,9 +39,11 @@ RSpec.describe "Form Check Answers Page" do ) end let(:id) { lettings_log.id } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do sign_in user + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end context "when the user needs to check their answers for a subsection" do diff --git a/spec/features/form/conditional_questions_spec.rb b/spec/features/form/conditional_questions_spec.rb index 051027bae..4f3f44b5a 100644 --- a/spec/features/form/conditional_questions_spec.rb +++ b/spec/features/form/conditional_questions_spec.rb @@ -13,9 +13,11 @@ RSpec.describe "Form Conditional Questions" do ) end let(:id) { lettings_log.id } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do sign_in user + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end context "with a page where some questions are only conditionally shown, depending on how you answer the first question" do diff --git a/spec/features/form/form_navigation_spec.rb b/spec/features/form/form_navigation_spec.rb index 207ae19a3..ffa9bdd88 100644 --- a/spec/features/form/form_navigation_spec.rb +++ b/spec/features/form/form_navigation_spec.rb @@ -32,9 +32,11 @@ RSpec.describe "Form Navigation" do hhmemb: { type: "numeric", answer: 1, path: "household-number-of-members" }, } end + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do sign_in user + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end describe "Create a new lettings log" do diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 6675150e0..8defb15f6 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -2,11 +2,7 @@ require "rails_helper" require_relative "helpers" RSpec.describe "validations" do - before do - sign_in user - end - - include Helpers + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:user) { FactoryBot.create(:user) } let(:lettings_log) do FactoryBot.create( @@ -35,6 +31,13 @@ RSpec.describe "validations" do end let(:id) { lettings_log.id } + before do + sign_in user + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end + + include Helpers + describe "Question validation" do context "when the tenant age is invalid" do it "shows validation for under 0" do diff --git a/spec/helpers/conditional_questions_helper_spec.rb b/spec/helpers/conditional_questions_helper_spec.rb index 6a83b1d0a..c1b8f3a7f 100644 --- a/spec/helpers/conditional_questions_helper_spec.rb +++ b/spec/helpers/conditional_questions_helper_spec.rb @@ -3,6 +3,11 @@ require "rails_helper" RSpec.describe ConditionalQuestionsHelper do let(:lettings_log) { FactoryBot.build(:lettings_log) } let(:page) { lettings_log.form.get_page("armed_forces") } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end describe "conditional questions for page" do let(:conditional_pages) { %w[leftreg] } diff --git a/spec/helpers/question_attribute_helper_spec.rb b/spec/helpers/question_attribute_helper_spec.rb index 393db953e..96f2f7ef6 100644 --- a/spec/helpers/question_attribute_helper_spec.rb +++ b/spec/helpers/question_attribute_helper_spec.rb @@ -4,6 +4,11 @@ RSpec.describe QuestionAttributeHelper do let(:lettings_log) { FactoryBot.build(:lettings_log) } let(:form) { lettings_log.form } let(:questions) { form.get_page("rent").questions } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end describe "html attributes" do it "returns empty hash if fields-to-add or result-field are empty " do diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index 9e770e1ff..ff4902a55 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -3,6 +3,11 @@ require "rails_helper" RSpec.describe TasklistHelper do let(:empty_lettings_log) { FactoryBot.create(:lettings_log) } let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end describe "get next incomplete section" do it "returns the first subsection name if it is not completed" do diff --git a/spec/jobs/email_csv_job_spec.rb b/spec/jobs/email_csv_job_spec.rb index b5661baf0..1e3860af9 100644 --- a/spec/jobs/email_csv_job_spec.rb +++ b/spec/jobs/email_csv_job_spec.rb @@ -9,6 +9,11 @@ describe EmailCsvJob do let(:user) { FactoryBot.create(:user) } let(:organisation) { user.organisation } let(:other_organisation) { FactoryBot.create(:organisation) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end context "when a log exists" do let!(:lettings_log) do diff --git a/spec/models/form/page_spec.rb b/spec/models/form/page_spec.rb index e4a780beb..bb448aca6 100644 --- a/spec/models/form/page_spec.rb +++ b/spec/models/form/page_spec.rb @@ -14,6 +14,11 @@ RSpec.describe Form::Page, type: :model do let(:subsection) { Form::Subsection.new(subsection_id, subsection_definition, section) } let(:page_id) { "net_income" } let(:page_definition) { subsection_definition["pages"][page_id] } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end it "has an id" do expect(page.id).to eq(page_id) diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index 0c0b97a64..da59ad354 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -16,6 +16,11 @@ RSpec.describe Form::Question, type: :model do let(:page) { Form::Page.new(page_id, page_definition, subsection) } let(:question_id) { "earnings" } let(:question_definition) { page_definition["questions"][question_id] } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end it "has an id" do expect(question.id).to eq(question_id) diff --git a/spec/models/form/section_spec.rb b/spec/models/form/section_spec.rb index 52f6effc8..2585b67a8 100644 --- a/spec/models/form/section_spec.rb +++ b/spec/models/form/section_spec.rb @@ -7,6 +7,11 @@ RSpec.describe Form::Section, type: :model do let(:form) { lettings_log.form } let(:section_id) { "household" } let(:section_definition) { form.form_definition["sections"][section_id] } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end it "has an id" do expect(section.id).to eq(section_id) diff --git a/spec/models/form/subsection_spec.rb b/spec/models/form/subsection_spec.rb index a119a212e..46880a33a 100644 --- a/spec/models/form/subsection_spec.rb +++ b/spec/models/form/subsection_spec.rb @@ -11,9 +11,11 @@ RSpec.describe Form::Subsection, type: :model do let(:section) { Form::Section.new(section_id, section_definition, form) } let(:subsection_id) { "household_characteristics" } let(:subsection_definition) { section_definition["subsections"][subsection_id] } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do RequestHelper.stub_http_requests + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end it "has an id" do diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 4e6a61913..94e92e01a 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -1,43 +1,163 @@ require "rails_helper" RSpec.describe FormHandler do - let(:test_form_name) { "2021_2022" } + let(:form_handler) { described_class.instance } + + before { Singleton.__init__(described_class) } # reload FormHandler Instance to update form definitions between runs + + context "when accessing a form in a different year" do + before do + Timecop.freeze(Time.utc(2021, 8, 3)) + end + + after do + Timecop.unfreeze + end + + it "is able to load a current lettings form" do + form = form_handler.get_form("current_lettings") + expect(form).to be_a(Form) + expect(form.pages.count).to eq(45) + end + + it "is able to load a next lettings form" do + form = form_handler.get_form("next_lettings") + expect(form).to be_a(Form) + expect(form.pages.count).to eq(12) + end + end describe "Get all forms" do it "is able to load all the forms" do - form_handler = described_class.instance all_forms = form_handler.forms expect(all_forms.count).to be >= 1 - expect(all_forms[test_form_name]).to be_a(Form) + expect(all_forms["current_sales"]).to be_a(Form) end end describe "Get specific form" do - it "is able to load a specific form" do - form_handler = described_class.instance - form = form_handler.get_form(test_form_name) + it "is able to load a current lettings form" do + form = form_handler.get_form("current_lettings") + expect(form).to be_a(Form) + expect(form.pages.count).to eq(12) + end + + it "is able to load a previous lettings form" do + form = form_handler.get_form("previous_lettings") expect(form).to be_a(Form) expect(form.pages.count).to eq(45) end + + it "is able to load a current sales form" do + form = form_handler.get_form("current_sales") + expect(form).to be_a(Form) + expect(form.pages.count).to eq(4) + expect(form.name).to eq("2022_2023_sales") + end + + it "is able to load a previous sales form" do + form = form_handler.get_form("previous_sales") + expect(form).to be_a(Form) + expect(form.pages.count).to eq(4) + expect(form.name).to eq("2021_2022_sales") + end end describe "Current form" do it "returns the latest form by date" do - form_handler = described_class.instance - form = form_handler.current_form + form = form_handler.current_lettings_form expect(form).to be_a(Form) expect(form.start_date.year).to eq(2022) end end + describe "Current collection start year" do + context "when the date is after 1st of April" do + before do + Timecop.freeze(Time.utc(2022, 8, 3)) + end + + after do + Timecop.unfreeze + end + + it "returns the same year as the the current start year" do + expect(form_handler.current_collection_start_year).to eq(2022) + end + + it "returns the correct current lettings form name" do + expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("current_lettings") + end + + it "returns the correct previous lettings form name" do + expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("previous_lettings") + end + + it "returns the correct next lettings form name" do + expect(form_handler.form_name_from_start_year(2023, "lettings")).to eq("next_lettings") + end + + it "returns the correct current sales form name" do + expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("current_sales") + end + + it "returns the correct previous sales form name" do + expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("previous_sales") + end + + it "returns the correct next sales form name" do + expect(form_handler.form_name_from_start_year(2023, "sales")).to eq("next_sales") + end + end + + context "with the date before 1st of April" do + before do + Timecop.freeze(Time.utc(2022, 2, 3)) + end + + after do + Timecop.unfreeze + end + + it "returns the previous year as the current start year" do + expect(form_handler.current_collection_start_year).to eq(2021) + end + + it "returns the correct current lettings form name" do + expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("current_lettings") + end + + it "returns the correct previous lettings form name" do + expect(form_handler.form_name_from_start_year(2020, "lettings")).to eq("previous_lettings") + end + + it "returns the correct next lettings form name" do + expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("next_lettings") + end + + it "returns the correct current sales form name" do + expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("current_sales") + end + + it "returns the correct previous sales form name" do + expect(form_handler.form_name_from_start_year(2020, "sales")).to eq("previous_sales") + end + + it "returns the correct next sales form name" do + expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("next_sales") + end + end + end + it "loads the form once at boot time" do form_handler = described_class.instance - expect(Form).not_to receive(:new).with(:any, test_form_name) - expect(form_handler.get_form(test_form_name)).to be_a(Form) + expect(Form).not_to receive(:new).with(:any, "current_sales") + expect(form_handler.get_form("current_sales")).to be_a(Form) end - it "can get a saleslog form" do - form_handler = described_class.instance - expect(form_handler.get_form("2022_2023_sales")).to be_a(Form) + it "correctly sets form type and start year" do + form = form_handler.forms["current_lettings"] + expect(form.type).to eq("lettings") + expect(form.start_date.year).to eq(2022) end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 0d0ba0fef..9305b61b9 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -209,9 +209,9 @@ RSpec.describe Form, type: :model do describe "when creating a sales log" do it "creates a valid sales form" do sections = [] - form = described_class.new(nil, "2022_23_sales", sections, "sales") + form = described_class.new(nil, 2022, sections, "sales") expect(form.type).to eq("sales") - expect(form.name).to eq("2022_23_sales") + expect(form.name).to eq("2022_2023_sales") expect(form.setup_sections.count).to eq(1) expect(form.setup_sections[0].class).to eq(Form::Sales::Sections::Setup) expect(form.sections.count).to eq(1) diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index d53207226..2dbf53595 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -4,6 +4,11 @@ RSpec.describe LettingsLog do let(:owning_organisation) { FactoryBot.create(:organisation) } let(:different_managing_organisation) { FactoryBot.create(:organisation) } let(:created_by_user) { FactoryBot.create(:user) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end it "inherits from log" do expect(described_class).to be < Log @@ -23,9 +28,9 @@ RSpec.describe LettingsLog do it "has returns the correct form based on the start date" do expect(lettings_log.form_name).to be_nil expect(lettings_log.form).to be_a(Form) - expect(lettings_log_2.form_name).to eq("2021_2022") + expect(lettings_log_2.form_name).to eq("previous_lettings") expect(lettings_log_2.form).to be_a(Form) - expect(lettings_log_year_2.form_name).to eq("2023_2024") + expect(lettings_log_year_2.form_name).to eq("next_lettings") expect(lettings_log_year_2.form).to be_a(Form) end @@ -1647,7 +1652,7 @@ RSpec.describe LettingsLog do end context "when a lettings log is a supported housing log" do - let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") } before do lettings_log.needstype = 2 diff --git a/spec/models/rent_period_spec.rb b/spec/models/rent_period_spec.rb index 5749d7660..266c839ff 100644 --- a/spec/models/rent_period_spec.rb +++ b/spec/models/rent_period_spec.rb @@ -2,10 +2,10 @@ require "rails_helper" RSpec.describe RentPeriod, type: :model do describe "rent period mapping" do - let(:form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") } + let(:form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do - allow(FormHandler.instance).to receive(:current_form).and_return(form) + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(form) end it "maps rent period id to display names" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 8ccbf082a..7ab423266 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -34,7 +34,7 @@ RSpec.describe SalesLog, type: :model do it "has returns the correct form based on the start date" do expect(sales_log.form_name).to be_nil expect(sales_log.form).to be_a(Form) - expect(sales_log_2.form_name).to eq("2022_2023_sales") + expect(sales_log_2.form_name).to eq("current_sales") expect(sales_log_2.form).to be_a(Form) end end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 3a97afe94..895840cdb 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -5,6 +5,11 @@ RSpec.describe Validations::FinancialValidations do let(:validator_class) { Class.new { include Validations::FinancialValidations } } let(:record) { FactoryBot.create(:lettings_log) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end describe "earnings and income frequency" do it "when earnings are provided it validates that income frequency must be provided" do diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index c5e523c2b..7b467d384 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -5,6 +5,11 @@ RSpec.describe Validations::HouseholdValidations do let(:validator_class) { Class.new { include Validations::HouseholdValidations } } let(:record) { FactoryBot.create(:lettings_log) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end describe "reasonable preference validations" do context "when reasonable preference is homeless" do diff --git a/spec/models/validations/shared_validations_spec.rb b/spec/models/validations/shared_validations_spec.rb index 0a6ab16c6..2be3f52f3 100644 --- a/spec/models/validations/shared_validations_spec.rb +++ b/spec/models/validations/shared_validations_spec.rb @@ -5,8 +5,13 @@ RSpec.describe Validations::SharedValidations do let(:validator_class) { Class.new { include Validations::SharedValidations } } let(:record) { FactoryBot.create(:lettings_log) } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } describe "numeric min max validations" do + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end + context "when validating age" do it "validates that person 1's age is a number" do record.age1 = "random" diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 4b13b8895..f879985b8 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -5,13 +5,6 @@ RSpec.describe FormController, type: :request do let(:user) { FactoryBot.create(:user) } let(:organisation) { user.organisation } let(:other_organisation) { FactoryBot.create(:organisation) } - let!(:lettings_log) do - FactoryBot.create( - :lettings_log, - owning_organisation: organisation, - managing_organisation: organisation, - ) - end let!(:unauthorized_lettings_log) do FactoryBot.create( :lettings_log, @@ -38,8 +31,21 @@ RSpec.describe FormController, type: :request do ) end let(:headers) { { "Accept" => "text/html" } } + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + + before do + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + end context "when a user is not signed in" do + let!(:lettings_log) do + FactoryBot.create( + :lettings_log, + owning_organisation: organisation, + managing_organisation: organisation, + ) + end + describe "GET" do it "does not let you get lettings logs pages you don't have access to" do get "/lettings-logs/#{lettings_log.id}/person-1-age", headers: headers, params: {} @@ -61,6 +67,14 @@ RSpec.describe FormController, type: :request do end context "when a user is signed in" do + let!(:lettings_log) do + FactoryBot.create( + :lettings_log, + owning_organisation: organisation, + managing_organisation: organisation, + ) + end + before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) sign_in user diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 8aafd30dc..651655139 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -18,11 +18,13 @@ RSpec.describe LettingsLogsController, type: :request do "Authorization" => basic_credentials, } end + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do 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) + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end describe "POST #create" do diff --git a/spec/services/csv/lettings_log_csv_service_spec.rb b/spec/services/csv/lettings_log_csv_service_spec.rb index 5b8350a4c..36b2f857a 100644 --- a/spec/services/csv/lettings_log_csv_service_spec.rb +++ b/spec/services/csv/lettings_log_csv_service_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe Csv::LettingsLogCsvService do context "when the user is support" do let(:user) { FactoryBot.create(:user, :support) } - let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") } before do LettingsLog.create!(startdate: "2021-10-10", created_at: Time.utc(2022, 2, 8, 16, 52, 15)) diff --git a/spec/services/exports/lettings_log_export_service_spec.rb b/spec/services/exports/lettings_log_export_service_spec.rb index f419ba30c..ee17edf0e 100644 --- a/spec/services/exports/lettings_log_export_service_spec.rb +++ b/spec/services/exports/lettings_log_export_service_spec.rb @@ -8,8 +8,8 @@ RSpec.describe Exports::LettingsLogExportService do let(:xml_export_file) { File.open("spec/fixtures/exports/general_needs_log.xml", "r:UTF-8") } let(:local_manifest_file) { File.open("spec/fixtures/exports/manifest.xml", "r:UTF-8") } - let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } - let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") } + let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json") } let(:expected_master_manifest_filename) { "Manifest_2022_05_01_0001.csv" } let(:expected_master_manifest_rerun) { "Manifest_2022_05_01_0002.csv" } @@ -33,8 +33,8 @@ RSpec.describe Exports::LettingsLogExportService do allow(storage_service).to receive(:write_file) # Stub the form handler to use the real form - allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) - allow(FormHandler.instance).to receive(:get_form).with("2022_2023").and_return(real_2022_2023_form) + allow(FormHandler.instance).to receive(:get_form).with("previous_lettings").and_return(real_2021_2022_form) + allow(FormHandler.instance).to receive(:get_form).with("current_lettings").and_return(real_2022_2023_form) end context "when exporting daily lettings logs in XML" do diff --git a/spec/services/imports/lettings_logs_field_import_service_spec.rb b/spec/services/imports/lettings_logs_field_import_service_spec.rb index 386cde725..685617041 100644 --- a/spec/services/imports/lettings_logs_field_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_field_import_service_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Imports::LettingsLogsFieldImportService do let(:storage_service) { instance_double(Storage::S3Service) } let(:logger) { instance_double(ActiveSupport::Logger) } - let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") } let(:fixture_directory) { "spec/fixtures/imports/logs" } let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } @@ -27,7 +27,7 @@ RSpec.describe Imports::LettingsLogsFieldImportService do FactoryBot.create(:user, old_user_id:, organisation:) # Stub the form handler to use the real form - allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) + allow(FormHandler.instance).to receive(:get_form).with("previous_lettings").and_return(real_2021_2022_form) WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/) .to_return(status: 200, body: '{"status":200,"result":{"codes":{"admin_district":"E08000035"}}}', headers: {}) diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 3af3601a2..c74726216 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -6,8 +6,8 @@ RSpec.describe Imports::LettingsLogsImportService do let(:storage_service) { instance_double(Storage::S3Service) } let(:logger) { instance_double(ActiveSupport::Logger) } - let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } - let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") } + let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json") } let(:fixture_directory) { "spec/fixtures/imports/logs" } let(:organisation) { FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") } @@ -35,8 +35,8 @@ RSpec.describe Imports::LettingsLogsImportService do FactoryBot.create(:location, old_visible_id: 10, postcode: "LS166FT", scheme_id: scheme2.id, mobility_type: "W") # Stub the form handler to use the real form - allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) - allow(FormHandler.instance).to receive(:get_form).with("2022_2023").and_return(real_2022_2023_form) + allow(FormHandler.instance).to receive(:get_form).with("previous_lettings").and_return(real_2021_2022_form) + allow(FormHandler.instance).to receive(:get_form).with("current_lettings").and_return(real_2022_2023_form) end context "when importing lettings logs" do