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

Validation of SVG content type not working since update active_storage_validations to 1.3.1 #280

Closed
mantenie opened this issue Nov 11, 2024 · 3 comments · Fixed by #284
Closed
Assignees
Labels
bug Something isn't working

Comments

@mantenie
Copy link

Hello everyone,

I installed the latest version (1.3.1) of active_storage_validations to my Rails 7.0 project today. Since then I get the following errors in the specs:

Failure/Error:
     it { is_expected.to validate_content_type_of(:logo).allowing(
       "image/png", "image/gif", "image/jpeg", "image/svg+xml") }
   
     is expected to validate the content types of :logo
       the following content types should be allowed: :image/png, :image/gif, :image/jpeg, :image/svg+xml
       but :image/svg+xml was rejected

In my model I configured it like:

validates :logo, content_type: ["image/png", "image/jpeg", "image/gif", "image/svg+xml"]

Do any of you have a quick guess as to why this might be? I have already tried to change the content type to :svg, both in the specs and in the model. But apparently it has a problem with SVGs.

@mvz
Copy link
Contributor

mvz commented Nov 11, 2024

I'm having a similar problem, but with the docx content type. Here is a stand-alone test case that demonstrates the problem:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails", "~> 7.0.0"
  gem "sqlite3", "~> 1.4"
  gem "rspec-rails", "~> 7.1"
  gem "marcel", "~> 1.0.4"
  gem "active_storage_validations", "1.3.1"
end

require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"
require "rails/all"
require "rspec/rails"
require "active_storage_validations/matchers"
require "rspec/autorun"

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f

  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  config.active_storage.service = :local
  config.active_storage.service_configurations = {
    local: {
      root: Dir.tmpdir,
      service: "Disk"
    }
  }
end

ENV["DATABASE_URL"] = "sqlite3::memory:"

Rails.application.initialize!

require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s

ActiveRecord::Schema.define do
  CreateActiveStorageTables.new.change

  create_table :documents, force: true
end

class Document < ActiveRecord::Base
  has_one_attached :file
  validates :file, attached: true, content_type: [:docx, :pdf]
end

RSpec.configure do |config|
  config.include ActiveStorageValidations::Matchers, type: :model
end

RSpec.describe Document, type: :model do
  it "validates the correct mime types" do
    document = Document.new
    expect(document).to validate_content_type_of(:file)
      .allowing("application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "application/pdf")
  end
end

Output:

Failures:

  1) Document validates the correct mime types
     Failure/Error:
       expect(document).to validate_content_type_of(:file)
         .allowing("application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                   "application/pdf")
     
       is expected to validate the content types of :file
         the following content types should be allowed: :application/vnd.openxmlformats-officedocument.wordprocessingml.document, :application/pdf
         but :application/vnd.openxmlformats-officedocument.wordprocessingml.document was rejected

@Mth0158 Mth0158 added the bug Something isn't working label Nov 11, 2024
@Mth0158 Mth0158 self-assigned this Nov 11, 2024
@Mth0158 Mth0158 linked a pull request Nov 11, 2024 that will close this issue
Mth0158 added a commit that referenced this issue Nov 11, 2024
…tent-type-not-working-since-update-active_storage_validations-to-131

[Matcher] Fix content_type extension generation issue (#280)
@Mth0158
Copy link
Collaborator

Mth0158 commented Nov 11, 2024

Hi everyone,
It should be released in 1.3.2 in a few hours/days. Check for the release :)

@Mth0158 Mth0158 reopened this Nov 11, 2024
@Mth0158
Copy link
Collaborator

Mth0158 commented Nov 12, 2024

Released with 1.3.2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants