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.

129 lines
5.6 KiB

require "rails_helper"
describe EmailCsvJob do
include Helpers
let(:job) { described_class.new }
let(:user) { FactoryBot.create(:user) }
let(:storage_service) { instance_double(Storage::S3Service) }
let(:mailer) { instance_double(CsvDownloadMailer) }
let(:sales_log_csv_service) { instance_double(Csv::SalesLogCsvService) }
let(:lettings_log_csv_service) { instance_double(Csv::LettingsLogCsvService) }
let(:search_term) { "meaning" }
let(:filters) { { "user" => "you", "status" => %w[in_progress] } }
let(:all_orgs) { false }
let(:organisation) { build(:organisation) }
let(:codes_only_export) { true }
let(:lettings_logs) { build_list(:lettings_log, 5) }
let(:sales_logs) { build_list(:sales_log, 5) }
before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(storage_service).to receive(:write_file)
allow(Csv::SalesLogCsvService).to receive(:new).and_return(sales_log_csv_service)
allow(sales_log_csv_service).to receive(:prepare_csv).and_return("")
allow(Csv::LettingsLogCsvService).to receive(:new).and_return(lettings_log_csv_service)
allow(lettings_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
context "when exporting lettings logs" do
before do
allow(FilterManager).to receive(:filter_logs).and_return(lettings_logs)
end
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
it "calls the filter manager with the arguments provided" do
expect(FilterManager).to receive(:filter_logs).with(a_kind_of(ActiveRecord::Relation), search_term, filters, all_orgs, user)
job.perform(user, search_term, filters, all_orgs, organisation, codes_only_export)
end
CLDC-3438 Update log csv download flow (#2403) * Filter out empty options from selected filters * Redirect to year filter if it is not selected * Add remaining filter questions * Display lettings filters CYA * Add cancel, conditionally display send email * Refactor * Add removed tests * Update method name * Add sales logs filters * Update tests * Set label size on filter questions * Refactor * Deduplicate filter views * Rename methods * Authenticate pages * Update filtering per organisation * Update routing from CYA * lint * Refactor check_your_answers_filters_list * Redirect if year is not given for sales csv * Update formatted organisations methods * Update missing and wrong copy * Update cancel and back buttons * CLDC-3442 Download logs csv per year (#2404) * Specify year in lettings csv download service * Specify year in sales csv download service * Define non_question_fields per year * Tidy up missing fields and tests * Fix request tests * Refactor * Add missing param * CLDC-3348 Update sales CSV for support (#2407) * Update sales csv for support * CLDC-3352 Update sales CSV address order (#2410) * Update sales support csv address ordering * Refactor * CLDC-3367 Rename sales support CSV fields (#2413) * Rename sales support CSV fields * Update sales CSV user fields order (#2416) * CLDC-2586 Rename scheme sensitive (#2405) * Rename scheme sensitive in lettings csv * Rename scheme sensitive in schemes csv * CLDC-3347 Show soft validations in support CSVs only (#2406) * Show soft validations in support csvs only * CLDC-3352 Update lettings CSV address order (#2409) * Update lettings support csv address ordering * Refactor * CLDC-3415/3421/3422 Add lettings CSV updates (#2415) * Add first_time_property_let_as_social_housing to coordinators csv * Update lettings CSV user fields order * Remove new_old from lettings CSV * Remove duplicate rent value check from CSV (#2423) * Refactor CSV service (#2425) * Extract and move lettings methods * Extract and more sales methods
7 months ago
it "creates a LettingsLogCsvService with the correct export type and year" do
expect(Csv::LettingsLogCsvService).to receive(:new).with(user:, export_type: "labels", year: 2023)
codes_only = false
CLDC-3438 Update log csv download flow (#2403) * Filter out empty options from selected filters * Redirect to year filter if it is not selected * Add remaining filter questions * Display lettings filters CYA * Add cancel, conditionally display send email * Refactor * Add removed tests * Update method name * Add sales logs filters * Update tests * Set label size on filter questions * Refactor * Deduplicate filter views * Rename methods * Authenticate pages * Update filtering per organisation * Update routing from CYA * lint * Refactor check_your_answers_filters_list * Redirect if year is not given for sales csv * Update formatted organisations methods * Update missing and wrong copy * Update cancel and back buttons * CLDC-3442 Download logs csv per year (#2404) * Specify year in lettings csv download service * Specify year in sales csv download service * Define non_question_fields per year * Tidy up missing fields and tests * Fix request tests * Refactor * Add missing param * CLDC-3348 Update sales CSV for support (#2407) * Update sales csv for support * CLDC-3352 Update sales CSV address order (#2410) * Update sales support csv address ordering * Refactor * CLDC-3367 Rename sales support CSV fields (#2413) * Rename sales support CSV fields * Update sales CSV user fields order (#2416) * CLDC-2586 Rename scheme sensitive (#2405) * Rename scheme sensitive in lettings csv * Rename scheme sensitive in schemes csv * CLDC-3347 Show soft validations in support CSVs only (#2406) * Show soft validations in support csvs only * CLDC-3352 Update lettings CSV address order (#2409) * Update lettings support csv address ordering * Refactor * CLDC-3415/3421/3422 Add lettings CSV updates (#2415) * Add first_time_property_let_as_social_housing to coordinators csv * Update lettings CSV user fields order * Remove new_old from lettings CSV * Remove duplicate rent value check from CSV (#2423) * Refactor CSV service (#2425) * Extract and move lettings methods * Extract and more sales methods
7 months ago
job.perform(user, nil, {}, nil, nil, codes_only, "lettings", 2023)
expect(Csv::LettingsLogCsvService).to receive(:new).with(user:, export_type: "codes", year: 2024)
codes_only = true
CLDC-3438 Update log csv download flow (#2403) * Filter out empty options from selected filters * Redirect to year filter if it is not selected * Add remaining filter questions * Display lettings filters CYA * Add cancel, conditionally display send email * Refactor * Add removed tests * Update method name * Add sales logs filters * Update tests * Set label size on filter questions * Refactor * Deduplicate filter views * Rename methods * Authenticate pages * Update filtering per organisation * Update routing from CYA * lint * Refactor check_your_answers_filters_list * Redirect if year is not given for sales csv * Update formatted organisations methods * Update missing and wrong copy * Update cancel and back buttons * CLDC-3442 Download logs csv per year (#2404) * Specify year in lettings csv download service * Specify year in sales csv download service * Define non_question_fields per year * Tidy up missing fields and tests * Fix request tests * Refactor * Add missing param * CLDC-3348 Update sales CSV for support (#2407) * Update sales csv for support * CLDC-3352 Update sales CSV address order (#2410) * Update sales support csv address ordering * Refactor * CLDC-3367 Rename sales support CSV fields (#2413) * Rename sales support CSV fields * Update sales CSV user fields order (#2416) * CLDC-2586 Rename scheme sensitive (#2405) * Rename scheme sensitive in lettings csv * Rename scheme sensitive in schemes csv * CLDC-3347 Show soft validations in support CSVs only (#2406) * Show soft validations in support csvs only * CLDC-3352 Update lettings CSV address order (#2409) * Update lettings support csv address ordering * Refactor * CLDC-3415/3421/3422 Add lettings CSV updates (#2415) * Add first_time_property_let_as_social_housing to coordinators csv * Update lettings CSV user fields order * Remove new_old from lettings CSV * Remove duplicate rent value check from CSV (#2423) * Refactor CSV service (#2425) * Extract and move lettings methods * Extract and more sales methods
7 months ago
job.perform(user, nil, {}, nil, nil, codes_only, "lettings", 2024)
end
it "passes the logs returned by the filter manager to the csv service" do
expect(lettings_log_csv_service).to receive(:prepare_csv).with(lettings_logs)
job.perform(user, nil, {}, nil, nil, codes_only_export)
end
it "creates a CsvDownload record" do
job.perform(user, nil, {}, nil, nil, codes_only_export, "lettings")
expect(CsvDownload.count).to eq(1)
expect(CsvDownload.first.user).to eq(user)
expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/lettings-logs-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("lettings")
expect(CsvDownload.first.expiration_time).to eq(172_800)
end
end
context "when exporting sales logs" do
before do
allow(FilterManager).to receive(:filter_logs).and_return(sales_logs)
end
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
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
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 "calls the filter manager with the arguments provided" do
expect(FilterManager).to receive(:filter_logs).with(a_kind_of(ActiveRecord::Relation), search_term, filters, all_orgs, user)
job.perform(user, search_term, filters, all_orgs, organisation, codes_only_export, "sales")
end
CLDC-3438 Update log csv download flow (#2403) * Filter out empty options from selected filters * Redirect to year filter if it is not selected * Add remaining filter questions * Display lettings filters CYA * Add cancel, conditionally display send email * Refactor * Add removed tests * Update method name * Add sales logs filters * Update tests * Set label size on filter questions * Refactor * Deduplicate filter views * Rename methods * Authenticate pages * Update filtering per organisation * Update routing from CYA * lint * Refactor check_your_answers_filters_list * Redirect if year is not given for sales csv * Update formatted organisations methods * Update missing and wrong copy * Update cancel and back buttons * CLDC-3442 Download logs csv per year (#2404) * Specify year in lettings csv download service * Specify year in sales csv download service * Define non_question_fields per year * Tidy up missing fields and tests * Fix request tests * Refactor * Add missing param * CLDC-3348 Update sales CSV for support (#2407) * Update sales csv for support * CLDC-3352 Update sales CSV address order (#2410) * Update sales support csv address ordering * Refactor * CLDC-3367 Rename sales support CSV fields (#2413) * Rename sales support CSV fields * Update sales CSV user fields order (#2416) * CLDC-2586 Rename scheme sensitive (#2405) * Rename scheme sensitive in lettings csv * Rename scheme sensitive in schemes csv * CLDC-3347 Show soft validations in support CSVs only (#2406) * Show soft validations in support csvs only * CLDC-3352 Update lettings CSV address order (#2409) * Update lettings support csv address ordering * Refactor * CLDC-3415/3421/3422 Add lettings CSV updates (#2415) * Add first_time_property_let_as_social_housing to coordinators csv * Update lettings CSV user fields order * Remove new_old from lettings CSV * Remove duplicate rent value check from CSV (#2423) * Refactor CSV service (#2425) * Extract and move lettings methods * Extract and more sales methods
7 months ago
it "creates a SalesLogCsvService with the correct export type and year" do
expect(Csv::SalesLogCsvService).to receive(:new).with(user:, export_type: "labels", year: 2022)
codes_only = false
CLDC-3438 Update log csv download flow (#2403) * Filter out empty options from selected filters * Redirect to year filter if it is not selected * Add remaining filter questions * Display lettings filters CYA * Add cancel, conditionally display send email * Refactor * Add removed tests * Update method name * Add sales logs filters * Update tests * Set label size on filter questions * Refactor * Deduplicate filter views * Rename methods * Authenticate pages * Update filtering per organisation * Update routing from CYA * lint * Refactor check_your_answers_filters_list * Redirect if year is not given for sales csv * Update formatted organisations methods * Update missing and wrong copy * Update cancel and back buttons * CLDC-3442 Download logs csv per year (#2404) * Specify year in lettings csv download service * Specify year in sales csv download service * Define non_question_fields per year * Tidy up missing fields and tests * Fix request tests * Refactor * Add missing param * CLDC-3348 Update sales CSV for support (#2407) * Update sales csv for support * CLDC-3352 Update sales CSV address order (#2410) * Update sales support csv address ordering * Refactor * CLDC-3367 Rename sales support CSV fields (#2413) * Rename sales support CSV fields * Update sales CSV user fields order (#2416) * CLDC-2586 Rename scheme sensitive (#2405) * Rename scheme sensitive in lettings csv * Rename scheme sensitive in schemes csv * CLDC-3347 Show soft validations in support CSVs only (#2406) * Show soft validations in support csvs only * CLDC-3352 Update lettings CSV address order (#2409) * Update lettings support csv address ordering * Refactor * CLDC-3415/3421/3422 Add lettings CSV updates (#2415) * Add first_time_property_let_as_social_housing to coordinators csv * Update lettings CSV user fields order * Remove new_old from lettings CSV * Remove duplicate rent value check from CSV (#2423) * Refactor CSV service (#2425) * Extract and move lettings methods * Extract and more sales methods
7 months ago
job.perform(user, nil, {}, nil, nil, codes_only, "sales", 2022)
expect(Csv::SalesLogCsvService).to receive(:new).with(user:, export_type: "codes", year: 2023)
codes_only = true
CLDC-3438 Update log csv download flow (#2403) * Filter out empty options from selected filters * Redirect to year filter if it is not selected * Add remaining filter questions * Display lettings filters CYA * Add cancel, conditionally display send email * Refactor * Add removed tests * Update method name * Add sales logs filters * Update tests * Set label size on filter questions * Refactor * Deduplicate filter views * Rename methods * Authenticate pages * Update filtering per organisation * Update routing from CYA * lint * Refactor check_your_answers_filters_list * Redirect if year is not given for sales csv * Update formatted organisations methods * Update missing and wrong copy * Update cancel and back buttons * CLDC-3442 Download logs csv per year (#2404) * Specify year in lettings csv download service * Specify year in sales csv download service * Define non_question_fields per year * Tidy up missing fields and tests * Fix request tests * Refactor * Add missing param * CLDC-3348 Update sales CSV for support (#2407) * Update sales csv for support * CLDC-3352 Update sales CSV address order (#2410) * Update sales support csv address ordering * Refactor * CLDC-3367 Rename sales support CSV fields (#2413) * Rename sales support CSV fields * Update sales CSV user fields order (#2416) * CLDC-2586 Rename scheme sensitive (#2405) * Rename scheme sensitive in lettings csv * Rename scheme sensitive in schemes csv * CLDC-3347 Show soft validations in support CSVs only (#2406) * Show soft validations in support csvs only * CLDC-3352 Update lettings CSV address order (#2409) * Update lettings support csv address ordering * Refactor * CLDC-3415/3421/3422 Add lettings CSV updates (#2415) * Add first_time_property_let_as_social_housing to coordinators csv * Update lettings CSV user fields order * Remove new_old from lettings CSV * Remove duplicate rent value check from CSV (#2423) * Refactor CSV service (#2425) * Extract and move lettings methods * Extract and more sales methods
7 months ago
job.perform(user, nil, {}, nil, nil, codes_only, "sales", 2023)
end
it "passes the logs returned by the filter manager to the csv service" do
expect(sales_log_csv_service).to receive(:prepare_csv).with(sales_logs)
job.perform(user, nil, {}, nil, nil, codes_only_export, "sales")
end
it "creates a CsvDownload record" do
job.perform(user, nil, {}, nil, nil, codes_only_export, "sales")
expect(CsvDownload.count).to eq(1)
expect(CsvDownload.first.user).to eq(user)
expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/sales-logs-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("sales")
expect(CsvDownload.first.expiration_time).to eq(172_800)
end
end
it "sends an E-mail with the presigned URL and duration" do
expect(mailer).to receive(:send_csv_download_mail).with(user, /csv-downloads/, instance_of(Integer))
job.perform(user)
end
end