status_pin_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe StatusPin do
  4. describe 'Validations' do
  5. subject { Fabricate.build :status_pin }
  6. context 'with an account pinning statuses' do
  7. subject { Fabricate.build :status_pin, account: account }
  8. let(:account) { Fabricate(:account) }
  9. context 'with a self-owned status' do
  10. let(:status) { Fabricate(:status, account: account) }
  11. it { is_expected.to allow_value(status).for(:status) }
  12. end
  13. context 'with a status from someone else' do
  14. let(:status) { Fabricate(:status) }
  15. it { is_expected.to_not allow_value(status).for(:status).against(:base) }
  16. end
  17. context 'with a reblog status' do
  18. let(:status) { Fabricate(:status, reblog: Fabricate(:status, account: account)) }
  19. it { is_expected.to_not allow_value(status).for(:status).against(:base) }
  20. end
  21. context 'with a private status' do
  22. let(:status) { Fabricate(:status, account: account, visibility: :private) }
  23. it { is_expected.to allow_value(status).for(:status).against(:base) }
  24. end
  25. context 'with a direct status' do
  26. let(:status) { Fabricate(:status, account: account, visibility: :direct) }
  27. it { is_expected.to_not allow_value(status).for(:status).against(:base) }
  28. end
  29. end
  30. context 'with a validator pin limit' do
  31. before { stub_const('StatusPinValidator::PIN_LIMIT', 2) }
  32. context 'with a local account at the limit' do
  33. let(:account) { Fabricate :account }
  34. before { Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account) }
  35. it { is_expected.to_not allow_value(account).for(:account).against(:base).with_message(I18n.t('statuses.pin_errors.limit')) }
  36. end
  37. context 'with a remote account at the limit' do
  38. let(:account) { Fabricate :account, domain: 'remote.test' }
  39. before { Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account) }
  40. it { is_expected.to allow_value(account).for(:account) }
  41. end
  42. end
  43. end
  44. describe 'Callbacks' do
  45. describe 'Invalidating status via policy' do
  46. subject { Fabricate :status_pin, status: Fabricate(:status, account: account), account: account }
  47. context 'with a local account that owns the status and has a policy' do
  48. let(:account) { Fabricate :account, domain: nil }
  49. before do
  50. Fabricate :account_statuses_cleanup_policy, account: account
  51. account.statuses_cleanup_policy.record_last_inspected(subject.status.id + 1_024)
  52. end
  53. it 'calls the invalidator on destroy' do
  54. expect { subject.destroy }
  55. .to change(account.statuses_cleanup_policy, :last_inspected)
  56. end
  57. end
  58. context 'with a local account that owns the status and does not have a policy' do
  59. let(:account) { Fabricate :account, domain: nil }
  60. it 'does not call the invalidator on destroy' do
  61. expect { subject.destroy }
  62. .to_not change(account, :updated_at)
  63. end
  64. end
  65. context 'with a remote account' do
  66. let(:account) { Fabricate :account, domain: 'host.example' }
  67. it 'does not call the invalidator on destroy' do
  68. expect { subject.destroy }
  69. .to_not change(account, :updated_at)
  70. end
  71. end
  72. end
  73. end
  74. end