Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file extensions in uppercase #289

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_storage_validations/content_type_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def extension_matches_content_type?(record, attribute, attachable)

extension = @attachable_filename.split('.').last
possible_extensions = Marcel::TYPE_EXTS[@attachable_content_type]
return true if possible_extensions && extension.in?(possible_extensions)
return true if possible_extensions && extension.downcase.in?(possible_extensions)

errors_options = initialize_and_populate_error_options(options, attachable)
add_error(record, attribute, ERROR_TYPES.first, **errors_options)
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/content_type/validator/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def self.example_for(type, several: false)
validates :extension_two_extensions_docx, content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
has_one_attached :extension_two_extensions_pdf
validates :extension_two_extensions_pdf, content_type: 'application/pdf'
has_one_attached :extension_upcase_extension
validates :extension_upcase_extension, content_type: 'application/pdf'
has_one_attached :extension_missing_extension
validates :extension_missing_extension, content_type: 'application/pdf'

%w(symbol string regex).each do |type|
has_one_attached :"with_#{type}"
Expand Down
35 changes: 35 additions & 0 deletions test/validators/content_type_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,41 @@
it { is_expected_to_be_valid }
end
end

describe "when the extension is in uppercase" do
subject { model.public_send(attribute).attach(pdf_file_with_extension_in_uppercase) and model }

let(:attribute) { :extension_upcase_extension }
let(:pdf_file_with_extension_in_uppercase) do
pdf_file.tap do |file|
file[:filename][".pdf"] = ".PDF"
end
end

it { is_expected_to_be_valid }
end

describe "when the extension is missing" do
subject { model.public_send(attribute).attach(pdf_file_without_extension) and model }

let(:attribute) { :extension_missing_extension }
let(:pdf_file_without_extension) do
pdf_file.tap do |file|
file[:filename][".pdf"] = ""
end
end
let(:error_options) do
{
authorized_types: "PDF",
content_type: pdf_file_without_extension[:content_type],
filename: pdf_file_without_extension[:filename]
}
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end

describe ":with" do
Expand Down
Loading