Skip to content

Commit

Permalink
Merge pull request #202 from Mth0158/201-existing-specs-started-failing
Browse files Browse the repository at this point in the history
[Matcher] Fix misbehaviour on validate_content_type_of matcher (#201)
  • Loading branch information
igorkasyanchuk authored Oct 26, 2023
2 parents 6fb77ad + 0dc78cf commit 074f08c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def with_message(message)

def matches?(subject)
@subject = subject.is_a?(Class) ? subject.new : subject
responds_to_methods && allowed_types_allowed? && rejected_types_rejected? && validate_error_message?
responds_to_methods && allowed_types_allowed? && rejected_types_rejected? && validate_custom_message?
end

def failure_message
Expand Down Expand Up @@ -88,10 +88,12 @@ def type_allowed?(type)
end
end

def validate_error_message?
def validate_custom_message?
return true unless @custom_message

@subject.public_send(@attribute_name).attach(attachment_for('fake/fake'))
@subject.validate
@subject.errors.details[@attribute_name].all? do |error|
@subject.errors.details[@attribute_name].select{|error| error[:content_type]}.all? do |error|
error[:error].to_s.include?(error_message)
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User < ApplicationRecord
has_one_attached :image_regex
has_one_attached :conditional_image
has_one_attached :conditional_image_2
has_one_attached :moon_picture
has_one_attached :proc_avatar
has_many_attached :proc_photos
has_many_attached :proc_photo_with_messages
Expand All @@ -29,6 +30,8 @@ class User < ApplicationRecord
validates :conditional_image, attached: true, if: -> { name == 'Foo' }
validates :conditional_image_2, attached: true, content_type: -> (record) {[/\Aimage\/.*\z/]}, size: { less_than: 10.megabytes }, if: -> { name == 'Peter Griffin' }

validates :moon_picture, content_type: ['image/png'], size: { greater_than: 0 }

validates :proc_avatar, attached: { message: "must not be blank" }, content_type: -> (record) {:png}
validates :proc_photos, attached: true, content_type: -> (record) {['image/png', 'image/jpg', /\A.*\/pdf\z/]}
validates :proc_photo_with_messages, content_type: { in: -> (record) {['image/png', 'image/jpg', /\A.*\/pdf\z/]}, message: "must be an authorized type" }
Expand Down
6 changes: 6 additions & 0 deletions test/matchers/content_type_validator_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher::Test < Ac
assert matcher.matches?(User)
end

test 'matches when combined with a another validator which has errors (file size = 0)' do
matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:moon_picture)
matcher.allowing('image/png')
assert matcher.matches?(User)
end

class WithMessageMatcher < ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher::Test
test 'matches when provided with the model validation message' do
matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:photo_with_messages)
Expand Down

0 comments on commit 074f08c

Please sign in to comment.