From f4c749e13bfebfe251edc7fb767d7467d4f4394a Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 7 Nov 2023 12:58:23 -0500 Subject: [PATCH 1/2] Remove explicit type setting for validator specs --- spec/validators/blacklisted_email_validator_spec.rb | 2 +- spec/validators/disallowed_hashtags_validator_spec.rb | 2 +- spec/validators/follow_limit_validator_spec.rb | 2 +- spec/validators/poll_validator_spec.rb | 2 +- spec/validators/status_pin_validator_spec.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/validators/blacklisted_email_validator_spec.rb b/spec/validators/blacklisted_email_validator_spec.rb index bfe2a11a999bd5..6292f0737ed757 100644 --- a/spec/validators/blacklisted_email_validator_spec.rb +++ b/spec/validators/blacklisted_email_validator_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe BlacklistedEmailValidator, type: :validator do +RSpec.describe BlacklistedEmailValidator do describe '#validate' do subject { described_class.new.validate(user); errors } diff --git a/spec/validators/disallowed_hashtags_validator_spec.rb b/spec/validators/disallowed_hashtags_validator_spec.rb index 7144d289188385..570ddb31c20d30 100644 --- a/spec/validators/disallowed_hashtags_validator_spec.rb +++ b/spec/validators/disallowed_hashtags_validator_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe DisallowedHashtagsValidator, type: :validator do +RSpec.describe DisallowedHashtagsValidator do let(:disallowed_tags) { [] } describe '#validate' do diff --git a/spec/validators/follow_limit_validator_spec.rb b/spec/validators/follow_limit_validator_spec.rb index 86b6511d6554f5..d116b35e0138ab 100644 --- a/spec/validators/follow_limit_validator_spec.rb +++ b/spec/validators/follow_limit_validator_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe FollowLimitValidator, type: :validator do +RSpec.describe FollowLimitValidator do describe '#validate' do before do allow_any_instance_of(described_class).to receive(:limit_reached?).with(account) do diff --git a/spec/validators/poll_validator_spec.rb b/spec/validators/poll_validator_spec.rb index 95feb043dbb9c6..f2a2534898d106 100644 --- a/spec/validators/poll_validator_spec.rb +++ b/spec/validators/poll_validator_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe PollValidator, type: :validator do +RSpec.describe PollValidator do describe '#validate' do before do validator.validate(poll) diff --git a/spec/validators/status_pin_validator_spec.rb b/spec/validators/status_pin_validator_spec.rb index e8f8a45434896a..d5109f990fb243 100644 --- a/spec/validators/status_pin_validator_spec.rb +++ b/spec/validators/status_pin_validator_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe StatusPinValidator, type: :validator do +RSpec.describe StatusPinValidator do describe '#validate' do before do subject.validate(pin) From 17b83595298cfc002ce6d5a37c561756fc97fa18 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 7 Nov 2023 14:18:19 -0500 Subject: [PATCH 2/2] Dont stub SUT in follow validator specs --- .../validators/follow_limit_validator_spec.rb | 80 +++++++++++++------ 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/spec/validators/follow_limit_validator_spec.rb b/spec/validators/follow_limit_validator_spec.rb index d116b35e0138ab..51b0683d2708f7 100644 --- a/spec/validators/follow_limit_validator_spec.rb +++ b/spec/validators/follow_limit_validator_spec.rb @@ -4,46 +4,74 @@ RSpec.describe FollowLimitValidator do describe '#validate' do - before do - allow_any_instance_of(described_class).to receive(:limit_reached?).with(account) do - limit_reached - end + context 'with a nil account' do + it 'does not add validation errors to base' do + follow = Fabricate.build(:follow, account: nil) + + follow.valid? - described_class.new.validate(follow) + expect(follow.errors[:base]).to be_empty + end end - let(:follow) { instance_double(Follow, account: account, errors: errors) } - let(:errors) { instance_double(ActiveModel::Errors, add: nil) } - let(:account) { instance_double(Account, nil?: _nil, local?: local, following_count: 0, followers_count: 0) } - let(:_nil) { true } - let(:local) { false } + context 'with a non-local account' do + it 'does not add validation errors to base' do + follow = Fabricate.build(:follow, account: Account.new(domain: 'host.example')) - context 'with follow.account.nil? || !follow.account.local?' do - let(:_nil) { true } + follow.valid? - it 'not calls errors.add' do - expect(errors).to_not have_received(:add).with(:base, any_args) + expect(follow.errors[:base]).to be_empty end end - context 'with !(follow.account.nil? || !follow.account.local?)' do - let(:_nil) { false } - let(:local) { true } + context 'with a local account' do + let(:account) { Account.new } + + context 'when the followers count is under the limit' do + before do + allow(account).to receive(:following_count).and_return(described_class::LIMIT - 100) + end + + it 'does not add validation errors to base' do + follow = Fabricate.build(:follow, account: account) - context 'when limit_reached?' do - let(:limit_reached) { true } + follow.valid? - it 'calls errors.add' do - expect(errors).to have_received(:add) - .with(:base, I18n.t('users.follow_limit_reached', limit: FollowLimitValidator::LIMIT)) + expect(follow.errors[:base]).to be_empty end end - context 'with !limit_reached?' do - let(:limit_reached) { false } + context 'when the following count is over the limit' do + before do + allow(account).to receive(:following_count).and_return(described_class::LIMIT + 100) + end + + context 'when the followers count is low' do + before do + allow(account).to receive(:followers_count).and_return(10) + end + + it 'adds validation errors to base' do + follow = Fabricate.build(:follow, account: account) + + follow.valid? + + expect(follow.errors[:base]).to include(I18n.t('users.follow_limit_reached', limit: FollowLimitValidator::LIMIT)) + end + end + + context 'when the followers count is high' do + before do + allow(account).to receive(:followers_count).and_return(100_000) + end + + it 'does not add validation errors to base' do + follow = Fabricate.build(:follow, account: account) + + follow.valid? - it 'not calls errors.add' do - expect(errors).to_not have_received(:add).with(:base, any_args) + expect(follow.errors[:base]).to be_empty + end end end end