Fortify coverage for `Follow` model (#32472)
This commit is contained in:
		
							parent
							
								
									1f3722904f
								
							
						
					
					
						commit
						c0f46e9031
					
				| 
						 | 
					@ -3,27 +3,26 @@
 | 
				
			||||||
require 'rails_helper'
 | 
					require 'rails_helper'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
RSpec.describe Follow do
 | 
					RSpec.describe Follow do
 | 
				
			||||||
  let(:alice) { Fabricate(:account, username: 'alice') }
 | 
					  describe 'Associations' do
 | 
				
			||||||
  let(:bob)   { Fabricate(:account, username: 'bob') }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  describe 'validations' do
 | 
					 | 
				
			||||||
    subject { described_class.new(account: alice, target_account: bob, rate_limit: true) }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    it { is_expected.to belong_to(:account).required }
 | 
					    it { is_expected.to belong_to(:account).required }
 | 
				
			||||||
    it { is_expected.to belong_to(:target_account).required }
 | 
					    it { is_expected.to belong_to(:target_account).required }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    it 'is invalid if account already follows too many people' do
 | 
					 | 
				
			||||||
      alice.update(following_count: FollowLimitValidator::LIMIT)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      expect(subject).to_not be_valid
 | 
					 | 
				
			||||||
      expect(subject).to model_have_error_on_field(:base)
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it 'is valid if account is only on the brink of following too many people' do
 | 
					  describe 'Validations' do
 | 
				
			||||||
      alice.update(following_count: FollowLimitValidator::LIMIT - 1)
 | 
					    subject { Fabricate.build :follow, rate_limit: true }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      expect(subject).to be_valid
 | 
					    let(:account) { Fabricate(:account) }
 | 
				
			||||||
      expect(subject).to_not model_have_error_on_field(:base)
 | 
					
 | 
				
			||||||
 | 
					    context 'when account follows too many people' do
 | 
				
			||||||
 | 
					      before { account.update(following_count: FollowLimitValidator::LIMIT) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it { is_expected.to_not allow_value(account).for(:account).against(:base) }
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context 'when account is on brink of following too many people' do
 | 
				
			||||||
 | 
					      before { account.update(following_count: FollowLimitValidator::LIMIT - 1) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it { is_expected.to allow_value(account).for(:account).against(:base) }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -54,4 +53,58 @@ RSpec.describe Follow do
 | 
				
			||||||
      expect(account.requested?(target_account)).to be true
 | 
					      expect(account.requested?(target_account)).to be true
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  describe '#local?' do
 | 
				
			||||||
 | 
					    it { is_expected.to_not be_local }
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  describe 'Callbacks' do
 | 
				
			||||||
 | 
					    describe 'Setting a URI' do
 | 
				
			||||||
 | 
					      context 'when URI exists' do
 | 
				
			||||||
 | 
					        subject { Fabricate.build :follow, uri: 'https://uri/value' }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it 'does not change' do
 | 
				
			||||||
 | 
					          expect { subject.save }
 | 
				
			||||||
 | 
					            .to not_change(subject, :uri)
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      context 'when URI is blank' do
 | 
				
			||||||
 | 
					        subject { Fabricate.build :follow, uri: nil }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it 'populates the value' do
 | 
				
			||||||
 | 
					          expect { subject.save }
 | 
				
			||||||
 | 
					            .to change(subject, :uri).to(be_present)
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    describe 'Maintaining counters' do
 | 
				
			||||||
 | 
					      subject { Fabricate.build :follow, account:, target_account: }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      let(:account) { Fabricate :account }
 | 
				
			||||||
 | 
					      let(:target_account) { Fabricate :account }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      before do
 | 
				
			||||||
 | 
					        account.account_stat.update following_count: 123
 | 
				
			||||||
 | 
					        target_account.account_stat.update followers_count: 123
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      describe 'saving the follow' do
 | 
				
			||||||
 | 
					        it 'increments counters' do
 | 
				
			||||||
 | 
					          expect { subject.save }
 | 
				
			||||||
 | 
					            .to change(account, :following_count).by(1)
 | 
				
			||||||
 | 
					            .and(change(target_account, :followers_count).by(1))
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      describe 'destroying the follow' do
 | 
				
			||||||
 | 
					        it 'decrements counters' do
 | 
				
			||||||
 | 
					          expect { subject.destroy }
 | 
				
			||||||
 | 
					            .to change(account, :following_count).by(-1)
 | 
				
			||||||
 | 
					            .and(change(target_account, :followers_count).by(-1))
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue