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 using allowing and rejecting together on content type matcher #172

Merged
merged 3 commits into from
Oct 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) &&
Expand Down
30 changes: 26 additions & 4 deletions test/matchers/content_type_validator_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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