follow_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Follow do
  4. let(:alice) { Fabricate(:account, username: 'alice') }
  5. let(:bob) { Fabricate(:account, username: 'bob') }
  6. describe 'validations' do
  7. subject { described_class.new(account: alice, target_account: bob, rate_limit: true) }
  8. it 'is invalid without an account' do
  9. follow = Fabricate.build(:follow, account: nil)
  10. follow.valid?
  11. expect(follow).to model_have_error_on_field(:account)
  12. end
  13. it 'is invalid without a target_account' do
  14. follow = Fabricate.build(:follow, target_account: nil)
  15. follow.valid?
  16. expect(follow).to model_have_error_on_field(:target_account)
  17. end
  18. it 'is invalid if account already follows too many people' do
  19. alice.update(following_count: FollowLimitValidator::LIMIT)
  20. expect(subject).to_not be_valid
  21. expect(subject).to model_have_error_on_field(:base)
  22. end
  23. it 'is valid if account is only on the brink of following too many people' do
  24. alice.update(following_count: FollowLimitValidator::LIMIT - 1)
  25. expect(subject).to be_valid
  26. expect(subject).to_not model_have_error_on_field(:base)
  27. end
  28. end
  29. describe 'recent' do
  30. it 'sorts so that more recent follows comes earlier' do
  31. follow0 = described_class.create!(account: alice, target_account: bob)
  32. follow1 = described_class.create!(account: bob, target_account: alice)
  33. a = described_class.recent.to_a
  34. expect(a.size).to eq 2
  35. expect(a[0]).to eq follow1
  36. expect(a[1]).to eq follow0
  37. end
  38. end
  39. describe 'revoke_request!' do
  40. let(:follow) { Fabricate(:follow, account: account, target_account: target_account) }
  41. let(:account) { Fabricate(:account) }
  42. let(:target_account) { Fabricate(:account) }
  43. it 'revokes the follow relation' do
  44. follow.revoke_request!
  45. expect(account.following?(target_account)).to be false
  46. end
  47. it 'creates a follow request' do
  48. follow.revoke_request!
  49. expect(account.requested?(target_account)).to be true
  50. end
  51. end
  52. end