diff --git a/lib/active_storage_validations/matchers/content_type_validator_matcher.rb b/lib/active_storage_validations/matchers/content_type_validator_matcher.rb index e6cad510..54c32ae0 100644 --- a/lib/active_storage_validations/matchers/content_type_validator_matcher.rb +++ b/lib/active_storage_validations/matchers/content_type_validator_matcher.rb @@ -19,13 +19,11 @@ def description def allowing(*types) @allowed_types = types.flatten - either_allowing_or_rejecting self end def rejecting(*types) @rejected_types = types.flatten - either_allowing_or_rejecting self end @@ -52,12 +50,6 @@ def failure_message protected - def either_allowing_or_rejecting - if @allowed_types && @rejected_types - raise ArgumentError, "You must specify either allowing or rejecting" - end - end - def responds_to_methods @subject.respond_to?(@attribute_name) && @subject.public_send(@attribute_name).respond_to?(:attach) && diff --git a/test/matchers/content_type_validator_matcher_test.rb b/test/matchers/content_type_validator_matcher_test.rb index 2f24746c..f6392b4d 100644 --- a/test/matchers/content_type_validator_matcher_test.rb +++ b/test/matchers/content_type_validator_matcher_test.rb @@ -4,16 +4,20 @@ require 'active_storage_validations/matchers' class ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher::Test < ActiveSupport::TestCase - test 'positive and negative' do + test 'positive match on both allowing and rejecting' do matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar) matcher.allowing('image/png') - assert_raise(ArgumentError) { matcher.rejecting('image/jpg') } + matcher.rejecting('image/jpg') + + assert matcher.matches?(User) end - test 'negative and positive' do + test 'negative match on both allowing and rejecting' do matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar) matcher.rejecting('image/png') - assert_raise(ArgumentError) { matcher.allowing('image/jpg') } + matcher.allowing('image/jpg') + + refute matcher.matches?(User) end test 'positive match when providing class' do @@ -51,4 +55,22 @@ class ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher::Test < Ac matcher.allowing('image/png') refute matcher.matches?(User.new) end + + test 'positive match for rejecting' do + matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar) + matcher.rejecting('image/jpeg') + assert matcher.matches?(User) + end + + test 'negative match for rejecting' do + matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar) + matcher.rejecting('image/png') + refute matcher.matches?(User) + end + + test 'positive match on subset of accepted content types' do + matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:photos) + matcher.allowing('image/png') + assert matcher.matches?(User) + end end