Skip to content

Commit

Permalink
Merge pull request #2334 from DFE-Digital/zero-byte-file-validation
Browse files Browse the repository at this point in the history
Add validation on minimum file size
  • Loading branch information
thomasleese authored Aug 14, 2024
2 parents 443d93c + 39137f0 commit 804b75e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/validators/file_upload_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def validate_file_size(record, attribute, uploaded_file)
if uploaded_file.size >= MAX_FILE_SIZE
record.errors.add attribute, :file_size_too_big
end

record.errors.add attribute, :file_size_too_small if uploaded_file.size <= 1
end

def validate_content_type(record, attribute, uploaded_file)
Expand Down
1 change: 0 additions & 1 deletion config/locales/assessor_interface.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ en:
original_attachment:
blank: Upload the required consent document
encrypted_pdf: The selected file is security protected
file_size_too_big: Files must be smaller than 50MB
invalid_content_type: Files must be in PDF, JPG, PNG, DOCX or DOC format
mismatch_content_type: Files must have matching file type and file name. For example, if the upload is called example.doc, it must be a DOC file.
assessor_interface/verify_professional_standing_form:
Expand Down
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ en:
messages:
comparison: can't be before start date
encrypted_pdf: file is encrypted
file_size_too_big: file size is too large
file_size_too_big: Files must be smaller than 50 MB
file_size_too_small: Files must be bigger than 0 MB
future: can't be in the future
invalid_content_type: is not in the correct format
mismatch_content_type: file format doesn't match extension
2 changes: 0 additions & 2 deletions config/locales/teacher_interface.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,11 @@ en:
original_attachment:
blank: Select a file to upload
encrypted_pdf: The selected file is security protected
file_size_too_big: Files must be smaller than 50MB
invalid_content_type: Files must be in PDF, JPG, PNG, DOCX or DOC format
mismatch_content_type: Files must have matching file type and file name. For example, if the upload is called example.doc, it must be a DOC file.
translated_attachment:
blank: Select a translation to upload
encrypted_pdf: The selected file is security protected
file_size_too_big: Files must be smaller than 50MB
invalid_content_type: Files must be in PDF, JPG, PNG, DOCX or DOC format
mismatch_content_type: Files must have matching file type and file name. For example, if the upload is called example.doc, it must be a DOC file.
teacher_interface/work_history_form:
Expand Down
8 changes: 8 additions & 0 deletions spec/validators/file_upload_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@

it { is_expected.not_to be_valid }
end

context "with a small file" do
let(:file) { fixture_file_upload("upload.pdf", "application/pdf") }

before { allow(file).to receive(:size).and_return(1) }

it { is_expected.not_to be_valid }
end
end

0 comments on commit 804b75e

Please sign in to comment.