Browse Source

bulk upload checks column count of CSV (#1423)

pull/1426/head
Phil Lee 2 years ago committed by GitHub
parent
commit
98cc162dda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/mailers/bulk_upload_mailer.rb
  2. 11
      app/services/bulk_upload/lettings/validator.rb
  3. 1
      app/services/bulk_upload/lettings/year2022/csv_parser.rb
  4. 1
      app/services/bulk_upload/lettings/year2023/csv_parser.rb
  5. 6
      app/services/bulk_upload/processor.rb
  6. 5
      config/locales/en.yml
  7. 3
      spec/mailers/bulk_upload_mailer_spec.rb
  8. 12
      spec/services/bulk_upload/lettings/validator_spec.rb
  9. 1
      spec/services/bulk_upload/processor_spec.rb

3
app/mailers/bulk_upload_mailer.rb

@ -100,7 +100,7 @@ class BulkUploadMailer < NotifyMailer
) )
end end
def send_bulk_upload_failed_service_error_mail(bulk_upload:) def send_bulk_upload_failed_service_error_mail(bulk_upload:, errors: [])
bulk_upload_link = if bulk_upload.lettings? bulk_upload_link = if bulk_upload.lettings?
start_bulk_upload_lettings_logs_url start_bulk_upload_lettings_logs_url
else else
@ -115,6 +115,7 @@ class BulkUploadMailer < NotifyMailer
upload_timestamp: bulk_upload.created_at, upload_timestamp: bulk_upload.created_at,
lettings_or_sales: bulk_upload.log_type, lettings_or_sales: bulk_upload.log_type,
year_combo: bulk_upload.year_combo, year_combo: bulk_upload.year_combo,
errors: errors.map { |e| "- #{e}" }.join("\n"),
bulk_upload_link:, bulk_upload_link:,
}, },
) )

11
app/services/bulk_upload/lettings/validator.rb

@ -9,6 +9,7 @@ class BulkUpload::Lettings::Validator
attr_reader :bulk_upload, :path attr_reader :bulk_upload, :path
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_min_columns
validate :validate_max_columns validate :validate_max_columns
def initialize(bulk_upload:, path:) def initialize(bulk_upload:, path:)
@ -127,12 +128,20 @@ private
end end
end end
def validate_min_columns
return if halt_validations?
column_count = rows.map(&:size).min
errors.add(:base, :under_min_column_count) if column_count < csv_parser.class::MIN_COLUMNS
end
def validate_max_columns def validate_max_columns
return if halt_validations? return if halt_validations?
column_count = rows.map(&:size).max column_count = rows.map(&:size).max
errors.add(:file, :column_count) if column_count > csv_parser.class::MAX_COLUMNS errors.add(:base, :over_max_column_count) if column_count > csv_parser.class::MAX_COLUMNS
end end
def halt_validations! def halt_validations!

1
app/services/bulk_upload/lettings/year2022/csv_parser.rb

@ -1,6 +1,7 @@
require "csv" require "csv"
class BulkUpload::Lettings::Year2022::CsvParser class BulkUpload::Lettings::Year2022::CsvParser
MIN_COLUMNS = 134
MAX_COLUMNS = 136 MAX_COLUMNS = 136
attr_reader :path attr_reader :path

1
app/services/bulk_upload/lettings/year2023/csv_parser.rb

@ -1,6 +1,7 @@
require "csv" require "csv"
class BulkUpload::Lettings::Year2023::CsvParser class BulkUpload::Lettings::Year2023::CsvParser
MIN_COLUMNS = 141
MAX_COLUMNS = 143 MAX_COLUMNS = 143
attr_reader :path attr_reader :path

6
app/services/bulk_upload/processor.rb

@ -8,7 +8,7 @@ class BulkUpload::Processor
def call def call
download download
return send_failure_mail if validator.invalid? return send_failure_mail(errors: validator.errors.full_messages) if validator.invalid?
validator.call validator.call
@ -62,9 +62,9 @@ private
validator.create_logs? && bulk_upload.logs.group(:status).count.keys == %w[completed] validator.create_logs? && bulk_upload.logs.group(:status).count.keys == %w[completed]
end end
def send_failure_mail def send_failure_mail(errors: [])
BulkUploadMailer BulkUploadMailer
.send_bulk_upload_failed_service_error_mail(bulk_upload:) .send_bulk_upload_failed_service_error_mail(bulk_upload:, errors:)
.deliver_later .deliver_later
end end

5
config/locales/en.yml

@ -41,6 +41,11 @@ en:
activemodel: activemodel:
errors: errors:
models: models:
bulk_upload/lettings/validator:
attributes:
base:
over_max_column_count: "too many columns, please ensure you have used the correct template"
under_min_column_count: "too few columns, please ensure you have used the correct template"
forms/bulk_upload_lettings/year: forms/bulk_upload_lettings/year:
attributes: attributes:
year: year:

3
spec/mailers/bulk_upload_mailer_spec.rb

@ -72,11 +72,12 @@ RSpec.describe BulkUploadMailer do
upload_timestamp: bulk_upload.created_at, upload_timestamp: bulk_upload.created_at,
lettings_or_sales: bulk_upload.log_type, lettings_or_sales: bulk_upload.log_type,
year_combo: bulk_upload.year_combo, year_combo: bulk_upload.year_combo,
errors: "- foo\n- bar",
bulk_upload_link: start_bulk_upload_lettings_logs_url, bulk_upload_link: start_bulk_upload_lettings_logs_url,
}, },
) )
mailer.send_bulk_upload_failed_service_error_mail(bulk_upload:) mailer.send_bulk_upload_failed_service_error_mail(bulk_upload:, errors: %w[foo bar])
end end
end end

12
spec/services/bulk_upload/lettings/validator_spec.rb

@ -28,6 +28,18 @@ RSpec.describe BulkUpload::Lettings::Validator do
end end
end end
context "when file has too few columns" do
before do
file.write("a," * 132)
file.write("\n")
file.rewind
end
it "is not valid" do
expect(validator).not_to be_valid
end
end
context "when incorrect headers" context "when incorrect headers"
end end

1
spec/services/bulk_upload/processor_spec.rb

@ -21,6 +21,7 @@ RSpec.describe BulkUpload::Processor do
BulkUpload::Lettings::Validator, BulkUpload::Lettings::Validator,
invalid?: true, invalid?: true,
call: nil, call: nil,
errors: [],
) )
end end

Loading…
Cancel
Save