diff --git a/app/models/forms/bulk_upload_lettings/upload_your_file.rb b/app/models/forms/bulk_upload_lettings/upload_your_file.rb index 82b5c75d5..552710885 100644 --- a/app/models/forms/bulk_upload_lettings/upload_your_file.rb +++ b/app/models/forms/bulk_upload_lettings/upload_your_file.rb @@ -15,6 +15,7 @@ module Forms validates :file, presence: true validate :validate_file_is_csv + validate :validate_file_size def view_path "bulk_upload_lettings_logs/forms/upload_your_file" @@ -73,6 +74,16 @@ module Forms errors.add(:file, :not_csv) 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 diff --git a/app/models/forms/bulk_upload_sales/upload_your_file.rb b/app/models/forms/bulk_upload_sales/upload_your_file.rb index 83ce04e10..447574d1a 100644 --- a/app/models/forms/bulk_upload_sales/upload_your_file.rb +++ b/app/models/forms/bulk_upload_sales/upload_your_file.rb @@ -14,6 +14,7 @@ module Forms validates :file, presence: true validate :validate_file_is_csv + validate :validate_file_size def view_path "bulk_upload_sales_logs/forms/upload_your_file" @@ -67,6 +68,16 @@ module Forms errors.add(:file, :not_csv) 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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 106ccb9fa..4ef35d19e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -90,11 +90,13 @@ en: file: blank: "Select which file to upload." 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: attributes: file: blank: "Select which file to upload." 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: attributes: needstype: diff --git a/spec/features/bulk_upload_lettings_logs_spec.rb b/spec/features/bulk_upload_lettings_logs_spec.rb index 315e1461e..b3e403cbf 100644 --- a/spec/features/bulk_upload_lettings_logs_spec.rb +++ b/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") 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 # rubocop:enable RSpec/AnyInstance diff --git a/spec/features/bulk_upload_sales_logs_spec.rb b/spec/features/bulk_upload_sales_logs_spec.rb index b7098999d..b32df43e1 100644 --- a/spec/features/bulk_upload_sales_logs_spec.rb +++ b/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") 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 # rubocop:enable RSpec/AnyInstance