Browse Source

CLDC-3365 Validate BU file size (#2725)

* Validate BU file size

* Rebase update

* Update error messages
pull/2711/head^2
kosiakkatrina 2 months ago committed by GitHub
parent
commit
8585940769
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      app/models/forms/bulk_upload_lettings/upload_your_file.rb
  2. 11
      app/models/forms/bulk_upload_sales/upload_your_file.rb
  3. 2
      config/locales/en.yml
  4. 19
      spec/features/bulk_upload_lettings_logs_spec.rb
  5. 20
      spec/features/bulk_upload_sales_logs_spec.rb

11
app/models/forms/bulk_upload_lettings/upload_your_file.rb

@ -15,6 +15,7 @@ module Forms
validates :file, presence: true validates :file, presence: true
validate :validate_file_is_csv validate :validate_file_is_csv
validate :validate_file_size
def view_path def view_path
"bulk_upload_lettings_logs/forms/upload_your_file" "bulk_upload_lettings_logs/forms/upload_your_file"
@ -73,6 +74,16 @@ module Forms
errors.add(:file, :not_csv) errors.add(:file, :not_csv)
end end
end end
MAX_FILE_SIZE = 10.megabytes
def validate_file_size
return unless file
if file.size > MAX_FILE_SIZE
errors.add(:file, :file_too_large)
end
end
end end
end end
end end

11
app/models/forms/bulk_upload_sales/upload_your_file.rb

@ -14,6 +14,7 @@ module Forms
validates :file, presence: true validates :file, presence: true
validate :validate_file_is_csv validate :validate_file_is_csv
validate :validate_file_size
def view_path def view_path
"bulk_upload_sales_logs/forms/upload_your_file" "bulk_upload_sales_logs/forms/upload_your_file"
@ -67,6 +68,16 @@ module Forms
errors.add(:file, :not_csv) errors.add(:file, :not_csv)
end end
end end
MAX_FILE_SIZE = 10.megabytes
def validate_file_size
return unless file
if file.size > MAX_FILE_SIZE
errors.add(:file, :file_too_large)
end
end
end end
end end
end end

2
config/locales/en.yml

@ -90,11 +90,13 @@ en:
file: file:
blank: "Select which file to upload." blank: "Select which file to upload."
not_csv: "Your file must be in CSV format." not_csv: "Your file must be in CSV format."
file_too_large: "File must be 10MB or less. Check your file and delete data that does not need to be uploaded."
forms/bulk_upload_sales/upload_your_file: forms/bulk_upload_sales/upload_your_file:
attributes: attributes:
file: file:
blank: "Select which file to upload." blank: "Select which file to upload."
not_csv: "Your file must be in CSV format." not_csv: "Your file must be in CSV format."
file_too_large: "File must be 10MB or less. Check your file and delete data that does not need to be uploaded."
forms/bulk_upload_lettings/needstype: forms/bulk_upload_lettings/needstype:
attributes: attributes:
needstype: needstype:

19
spec/features/bulk_upload_lettings_logs_spec.rb

@ -74,6 +74,25 @@ RSpec.describe "Bulk upload lettings log" do
expect(page).to have_content("Upload lettings logs in bulk") expect(page).to have_content("Upload lettings logs in bulk")
end end
it "shows file to large error" do
stub_const("Forms::BulkUploadLettings::UploadYourFile::MAX_FILE_SIZE", 1.bytes)
visit("/lettings-logs")
click_link("Upload lettings logs in bulk")
expect(page).to have_content("Which year")
click_button("Continue")
choose(current_formatted_year)
click_button("Continue")
click_button("Continue")
allow_any_instance_of(Forms::BulkUploadLettings::UploadYourFile).to receive(:`).and_return("text/csv")
attach_file "file", file_fixture("2023_24_lettings_bulk_upload.xlsx")
click_button("Upload")
expect(page).to have_content("File must be 10MB or less. Check your file and delete data that does not need to be uploaded.")
end
end end
# rubocop:enable RSpec/AnyInstance # rubocop:enable RSpec/AnyInstance

20
spec/features/bulk_upload_sales_logs_spec.rb

@ -71,6 +71,26 @@ RSpec.describe "Bulk upload sales log" do
expect(page).to have_content("Upload sales logs in bulk") expect(page).to have_content("Upload sales logs in bulk")
end end
it "shows file to large error" do
stub_const("Forms::BulkUploadSales::UploadYourFile::MAX_FILE_SIZE", 1.bytes)
visit("/sales-logs")
click_link("Upload sales logs in bulk")
expect(page).to have_content("Which year")
click_button("Continue")
click_button("Continue")
choose("2023 to 2024")
click_button("Continue")
click_button("Continue")
allow_any_instance_of(Forms::BulkUploadSales::UploadYourFile).to receive(:`).and_return("text/csv")
attach_file "file", file_fixture("2023_24_lettings_bulk_upload.xlsx")
click_button("Upload")
expect(page).to have_content("File must be 10MB or less. Check your file and delete data that does not need to be uploaded.")
end
end end
# rubocop:enable RSpec/AnyInstance # rubocop:enable RSpec/AnyInstance

Loading…
Cancel
Save