Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.3 KiB

require "rails_helper"
describe EmailCsvJob do
include Helpers
test_url = :test_url
let(:job) { described_class.new }
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
FactoryBot.create(
:lettings_log,
created_by: user,
ecstat1: 1,
)
end
let(:storage_service) { instance_double(Storage::S3Service) }
let(:mailer) { instance_double(CsvDownloadMailer) }
CLDC-1633 build feature csv download of sales logs (#1568) * create a method on the FormHandler that returns the sales form questions for all years in the order that they appear in the form * update csv email job to accomodate sales log export as well as lettings add to tests to reflec the changes made * write tests to cover the desired functionality of the SalesLogCsvService * create the SalesLogCsvService create a necessary method on the log to enable submission method to be included on the csv derive values for the two halves of previous postcode for export * add relevant links in the UI and pipe everything together in controllers amend organisations controller to have flexibility to download logs of either type add necessary methods to sales log controller, raising shared method to logs controller update routing for amendments and additions extract helper method to build urls for downloading logs within an organisation * correct various linter complaints and tech review suggestions * minor amendment to add old_id and reorder early columns * undo my 'clever' refactor that broke things * refactoring of csv service after some tech review and some UI testing in review app * update tests to include a test of a full export and all values in teh csv * correct minor routing error to ensure correct url is shown and tab selected after requesting csv email * update organisations controller requests spec file to cover new functionality and make a minor amendment to authentication scope in the controller after error found in testing * write request tests for the new functionality in the sales log controller, define authorisation in the controller * minor correction after rubocop's kind suggestion' * various corrections from first pass at PO, tech review, linter, etc * refactor :ordered_sales_questions_for_all_years * first pass at implementing flexible code-based form fixtures for testing * second pass * refactor all tests of :ordered_sales_questions_for_all_years to use new factories * some refactoring in the testing of the csv service * use that fact that params is always available in controllers and don't pass it around, inline some methods calls * correct minor bug to ensure that "Return to logs" link returns to the correct index page * remove reminder comments * write further tests on the manipulation of questions into the csv headers, update factories of form constituents to allow the creation of forms with richer questions * fix linter complaints * minor alterations after rebase to account for changes made on other branches * refactor after code review * tweak fixtures after rebase containing alterations to the factory defaults
2 years ago
let(:sales_log_csv_service) { instance_double(Csv::SalesLogCsvService) }
before do
FactoryBot.create(:lettings_log,
:completed,
created_by: user,
CLDC-1917 Add Startdate Validation (#1378) * feat: add validation with feature flag, typo fix and update tests * feat: flip feature toggle * feat: update feature toggle name * feat: fix form handler inequality * refactor: linting * refactor: use between in form handler * feat: remove feature toggle * feat: add dynamic date to lettings log factory * feat: fix log_summary_component_spec.rb tests * feat: update lettings_log.rb and start fixing lettings_log_spec.rb * feat: fix more tests * feat: fix more tests * feat: fix lettings log import service * refactor: linting * feat: fix checkboxes_spec.rb * feat: fix interruption_screen_helper_spec.rb * feat: fix check_answers_helper_spec.rb * feat: fix page_routing_spec.rb * feat: fix lettings_logs_field_import_service_spec.rb * feat: fix lettings_log_spec.rb * feat: fix question_spec.rb * feat: fix lettings_logs_controller_spec.rb * feat: fix check_answers_page_lettings_logs_spec.rb * feat: fix tenancy_validations_spec.rb * feat: fix validations_spec.rb * feat: fix accessible_autocomplete_spec.rb * feat: fix form_navigation_spec.rb * feat: fix soft_validations_spec.rb * feat: fix lettings_log_export_service_spec.rb * feat: fix saving_data_spec.rb * feat: fix page_spec.rb * feat: fix form_controller_spec.rb * refactor: linting * feat: fix subsection_spec.rb * feat: fix lettings_log_spec.rb * feat: fix financial_validations_spec.rb * feat: fix tasklist_page_spec.rb * feat: fix conditional_questions_spec.rb * feat: fix form_page_error_helper_spec.rb and log_summary_component_spec.rb * feat: fix lettings_log_csv_service_spec.rb * feat: fix tasklist_helper_spec.rb * refactor: linting * refactor: linting * feat: fix lettings_log_spec.rb * refactor: linting * refactor: replace financial year with collection yaer * feat: respond to PR comments pt. 1 * feat: respond to PR comments pt. 2
2 years ago
startdate: Time.zone.local(2022, 5, 1),
voiddate: Time.zone.local(2022, 5, 1),
mrcdate: Time.zone.local(2022, 5, 1))
allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(storage_service).to receive(:write_file)
allow(storage_service).to receive(:get_presigned_url).and_return(test_url)
CLDC-1633 build feature csv download of sales logs (#1568) * create a method on the FormHandler that returns the sales form questions for all years in the order that they appear in the form * update csv email job to accomodate sales log export as well as lettings add to tests to reflec the changes made * write tests to cover the desired functionality of the SalesLogCsvService * create the SalesLogCsvService create a necessary method on the log to enable submission method to be included on the csv derive values for the two halves of previous postcode for export * add relevant links in the UI and pipe everything together in controllers amend organisations controller to have flexibility to download logs of either type add necessary methods to sales log controller, raising shared method to logs controller update routing for amendments and additions extract helper method to build urls for downloading logs within an organisation * correct various linter complaints and tech review suggestions * minor amendment to add old_id and reorder early columns * undo my 'clever' refactor that broke things * refactoring of csv service after some tech review and some UI testing in review app * update tests to include a test of a full export and all values in teh csv * correct minor routing error to ensure correct url is shown and tab selected after requesting csv email * update organisations controller requests spec file to cover new functionality and make a minor amendment to authentication scope in the controller after error found in testing * write request tests for the new functionality in the sales log controller, define authorisation in the controller * minor correction after rubocop's kind suggestion' * various corrections from first pass at PO, tech review, linter, etc * refactor :ordered_sales_questions_for_all_years * first pass at implementing flexible code-based form fixtures for testing * second pass * refactor all tests of :ordered_sales_questions_for_all_years to use new factories * some refactoring in the testing of the csv service * use that fact that params is always available in controllers and don't pass it around, inline some methods calls * correct minor bug to ensure that "Return to logs" link returns to the correct index page * remove reminder comments * write further tests on the manipulation of questions into the csv headers, update factories of form constituents to allow the creation of forms with richer questions * fix linter complaints * minor alterations after rebase to account for changes made on other branches * refactor after code review * tweak fixtures after rebase containing alterations to the factory defaults
2 years ago
allow(Csv::SalesLogCsvService).to receive(:new).and_return(sales_log_csv_service)
allow(sales_log_csv_service).to receive(:prepare_csv).and_return("")
allow(CsvDownloadMailer).to receive(:new).and_return(mailer)
allow(mailer).to receive(:send_csv_download_mail)
end
CLDC-1633 build feature csv download of sales logs (#1568) * create a method on the FormHandler that returns the sales form questions for all years in the order that they appear in the form * update csv email job to accomodate sales log export as well as lettings add to tests to reflec the changes made * write tests to cover the desired functionality of the SalesLogCsvService * create the SalesLogCsvService create a necessary method on the log to enable submission method to be included on the csv derive values for the two halves of previous postcode for export * add relevant links in the UI and pipe everything together in controllers amend organisations controller to have flexibility to download logs of either type add necessary methods to sales log controller, raising shared method to logs controller update routing for amendments and additions extract helper method to build urls for downloading logs within an organisation * correct various linter complaints and tech review suggestions * minor amendment to add old_id and reorder early columns * undo my 'clever' refactor that broke things * refactoring of csv service after some tech review and some UI testing in review app * update tests to include a test of a full export and all values in teh csv * correct minor routing error to ensure correct url is shown and tab selected after requesting csv email * update organisations controller requests spec file to cover new functionality and make a minor amendment to authentication scope in the controller after error found in testing * write request tests for the new functionality in the sales log controller, define authorisation in the controller * minor correction after rubocop's kind suggestion' * various corrections from first pass at PO, tech review, linter, etc * refactor :ordered_sales_questions_for_all_years * first pass at implementing flexible code-based form fixtures for testing * second pass * refactor all tests of :ordered_sales_questions_for_all_years to use new factories * some refactoring in the testing of the csv service * use that fact that params is always available in controllers and don't pass it around, inline some methods calls * correct minor bug to ensure that "Return to logs" link returns to the correct index page * remove reminder comments * write further tests on the manipulation of questions into the csv headers, update factories of form constituents to allow the creation of forms with richer questions * fix linter complaints * minor alterations after rebase to account for changes made on other branches * refactor after code review * tweak fixtures after rebase containing alterations to the factory defaults
2 years ago
context "when exporting lettings logs" do
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/lettings-logs-.*\.csv/, anything)
job.perform(user)
end
it "includes the organisation name in the filename when one is provided" do
expect(storage_service).to receive(:write_file).with(/lettings-logs-#{organisation.name}-.*\.csv/, anything)
job.perform(user, nil, {}, nil, organisation)
end
end
CLDC-1633 build feature csv download of sales logs (#1568) * create a method on the FormHandler that returns the sales form questions for all years in the order that they appear in the form * update csv email job to accomodate sales log export as well as lettings add to tests to reflec the changes made * write tests to cover the desired functionality of the SalesLogCsvService * create the SalesLogCsvService create a necessary method on the log to enable submission method to be included on the csv derive values for the two halves of previous postcode for export * add relevant links in the UI and pipe everything together in controllers amend organisations controller to have flexibility to download logs of either type add necessary methods to sales log controller, raising shared method to logs controller update routing for amendments and additions extract helper method to build urls for downloading logs within an organisation * correct various linter complaints and tech review suggestions * minor amendment to add old_id and reorder early columns * undo my 'clever' refactor that broke things * refactoring of csv service after some tech review and some UI testing in review app * update tests to include a test of a full export and all values in teh csv * correct minor routing error to ensure correct url is shown and tab selected after requesting csv email * update organisations controller requests spec file to cover new functionality and make a minor amendment to authentication scope in the controller after error found in testing * write request tests for the new functionality in the sales log controller, define authorisation in the controller * minor correction after rubocop's kind suggestion' * various corrections from first pass at PO, tech review, linter, etc * refactor :ordered_sales_questions_for_all_years * first pass at implementing flexible code-based form fixtures for testing * second pass * refactor all tests of :ordered_sales_questions_for_all_years to use new factories * some refactoring in the testing of the csv service * use that fact that params is always available in controllers and don't pass it around, inline some methods calls * correct minor bug to ensure that "Return to logs" link returns to the correct index page * remove reminder comments * write further tests on the manipulation of questions into the csv headers, update factories of form constituents to allow the creation of forms with richer questions * fix linter complaints * minor alterations after rebase to account for changes made on other branches * refactor after code review * tweak fixtures after rebase containing alterations to the factory defaults
2 years ago
context "when exporting sales logs" do
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/sales-logs-.*\.csv/, anything)
job.perform(user, nil, {}, nil, nil, nil, "sales")
end
it "includes the organisation name in the filename when one is provided" do
expect(storage_service).to receive(:write_file).with(/sales-logs-#{organisation.name}-.*\.csv/, anything)
job.perform(user, nil, {}, nil, organisation, nil, "sales")
end
it "creates a SalesLogCsvService with the correct export type" do
expect(Csv::SalesLogCsvService).to receive(:new).with(export_type: "labels")
codes_only = false
job.perform(user, nil, {}, nil, nil, codes_only, "sales")
expect(Csv::SalesLogCsvService).to receive(:new).with(export_type: "codes")
codes_only = true
job.perform(user, nil, {}, nil, nil, codes_only, "sales")
end
end
it "sends an E-mail with the presigned URL and duration" do
expect(mailer).to receive(:send_csv_download_mail).with(user, test_url, instance_of(Integer))
job.perform(user)
end
context "when writing to S3" do
before do
FactoryBot.create_list(:lettings_log, 4, owning_organisation: other_organisation)
FactoryBot.create(:lettings_log, owning_organisation: other_organisation, status: "pending", skip_update_status: true)
end
def expect_csv
expect(storage_service).to receive(:write_file) do |_filename, data|
# Ignore byte order marker
csv = CSV.parse(data[1..])
yield(csv)
end
end
it "writes CSV data with headers" do
expect_csv do |csv|
expect(csv.first.first).to eq("id")
expect(csv.second.first).to eq(lettings_log.id.to_s)
end
job.perform(user)
end
context "when there is no organisation provided" do
it "only writes logs from the user's organisation" do
expect_csv do |csv|
# Headings + 2 rows
expect(csv.count).to eq(3)
end
job.perform(user)
end
end
context "when the user is support and an organisation is provided" do
let(:user) { FactoryBot.create(:user, :support) }
it "only writes logs from that organisation" do
expect_csv do |csv|
# other organisation => Headings + 4 rows
expect(csv.count).to eq(5)
end
job.perform(user, nil, {}, nil, other_organisation)
end
end
it "writes answer labels rather than values" do
expect_csv do |csv|
CLDC-1917 Add Startdate Validation (#1378) * feat: add validation with feature flag, typo fix and update tests * feat: flip feature toggle * feat: update feature toggle name * feat: fix form handler inequality * refactor: linting * refactor: use between in form handler * feat: remove feature toggle * feat: add dynamic date to lettings log factory * feat: fix log_summary_component_spec.rb tests * feat: update lettings_log.rb and start fixing lettings_log_spec.rb * feat: fix more tests * feat: fix more tests * feat: fix lettings log import service * refactor: linting * feat: fix checkboxes_spec.rb * feat: fix interruption_screen_helper_spec.rb * feat: fix check_answers_helper_spec.rb * feat: fix page_routing_spec.rb * feat: fix lettings_logs_field_import_service_spec.rb * feat: fix lettings_log_spec.rb * feat: fix question_spec.rb * feat: fix lettings_logs_controller_spec.rb * feat: fix check_answers_page_lettings_logs_spec.rb * feat: fix tenancy_validations_spec.rb * feat: fix validations_spec.rb * feat: fix accessible_autocomplete_spec.rb * feat: fix form_navigation_spec.rb * feat: fix soft_validations_spec.rb * feat: fix lettings_log_export_service_spec.rb * feat: fix saving_data_spec.rb * feat: fix page_spec.rb * feat: fix form_controller_spec.rb * refactor: linting * feat: fix subsection_spec.rb * feat: fix lettings_log_spec.rb * feat: fix financial_validations_spec.rb * feat: fix tasklist_page_spec.rb * feat: fix conditional_questions_spec.rb * feat: fix form_page_error_helper_spec.rb and log_summary_component_spec.rb * feat: fix lettings_log_csv_service_spec.rb * feat: fix tasklist_helper_spec.rb * refactor: linting * refactor: linting * feat: fix lettings_log_spec.rb * refactor: linting * refactor: replace financial year with collection yaer * feat: respond to PR comments pt. 1 * feat: respond to PR comments pt. 2
2 years ago
expect(csv.second[19]).to eq("Full-time – 30 hours or more")
end
job.perform(user)
end
it "writes filtered logs" do
expect_csv do |csv|
expect(csv.count).to eq(2)
end
job.perform(user, nil, { status: "completed" })
end
it "writes searched logs" do
expect_csv do |csv|
expect(csv.count).to eq(LettingsLog.search_by(lettings_log.id.to_s).count + 1)
end
job.perform(user, lettings_log.id.to_s)
end
context "when both filter and search applied" do
let(:postcode) { "XX1 1TG" }
before do
FactoryBot.create(:lettings_log, :in_progress, postcode_full: postcode, owning_organisation: organisation, created_by: user)
FactoryBot.create(:lettings_log, :completed, postcode_full: postcode, owning_organisation: organisation, created_by: user)
end
it "downloads logs matching both csv and filter logs" do
expect_csv do |csv|
expect(csv.count).to eq(2)
end
job.perform(user, postcode, { status: "completed" })
end
end
context "when there are more than 20 logs" do
before do
FactoryBot.create_list(:lettings_log, 26, owning_organisation: organisation)
end
it "does not paginate, it downloads all the user's logs" do
expect_csv do |csv|
# Heading + 2 + 26
expect(csv.count).to eq(29)
end
job.perform(user)
end
end
end
end
end