From 9aceb9e01f8263c16c8f053f22240d784c669b49 Mon Sep 17 00:00:00 2001 From: Jack S Date: Fri, 21 Jul 2023 11:36:25 +0100 Subject: [PATCH] Bugfix - handle empty file --- .../bulk_upload/lettings/validator.rb | 2 +- config/locales/en.yml | 2 + spec/services/bulk_upload/processor_spec.rb | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 0759a5cd2..7362a4aec 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -145,7 +145,7 @@ private def validate_file_not_empty if File.size(path).zero? - errors.add(:file, :blank) + errors.add(:base, :blank_file) halt_validations! end diff --git a/config/locales/en.yml b/config/locales/en.yml index f4d968d44..9cea2445b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -55,12 +55,14 @@ en: bulk_upload/lettings/validator: attributes: base: + blank_file: Template is blank - The template must be filled in for us to create the logs and check if data is correct. wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" over_max_column_count: "Too many columns, please ensure you have used the correct template" wrong_template: "Incorrect start dates, please ensure you have used the correct template" bulk_upload/sales/validator: attributes: base: + blank_file: Template is blank - The template must be filled in for us to create the logs and check if data is correct. wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" over_max_column_count: "Too many columns, please ensure you have used the correct template" wrong_template: "Incorrect sale dates, please ensure you have used the correct template" diff --git a/spec/services/bulk_upload/processor_spec.rb b/spec/services/bulk_upload/processor_spec.rb index 733f8ed5e..f47157b95 100644 --- a/spec/services/bulk_upload/processor_spec.rb +++ b/spec/services/bulk_upload/processor_spec.rb @@ -207,6 +207,47 @@ RSpec.describe BulkUpload::Processor do end end + context "when processing an empty file" do + let(:mock_downloader) do + instance_double( + BulkUpload::Downloader, + call: nil, + path:, + delete_local_file!: nil, + ) + end + + let(:file) { Tempfile.new } + let(:path) { file.path } + + let(:log) do + build( + :lettings_log, + :completed, + renttype: 3, + age1: 20, + ) + end + + before do + allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) + end + + it "sends failure email" do + mail_double = instance_double("ActionMailer::MessageDelivery", deliver_later: nil) + + allow(BulkUploadMailer).to receive(:send_bulk_upload_failed_service_error_mail).and_return(mail_double) + + processor.call + + expect(BulkUploadMailer).to have_received(:send_bulk_upload_failed_service_error_mail).with( + bulk_upload:, + errors: ["Template is blank - The template must be filled in for us to create the logs and check if data is correct."], + ) + expect(mail_double).to have_received(:deliver_later) + end + end + context "when a bulk upload has an in progress log" do let(:mock_downloader) do instance_double(