Enhance coverage for `StatusPin` model (#32515)

pull/2892/head
Matt Jankowski 2024-10-28 03:34:58 -04:00 committed by GitHub
parent 9074c1fac9
commit 1edb527072
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 94 additions and 58 deletions

View File

@ -17,11 +17,17 @@ class StatusPin < ApplicationRecord
validates_with StatusPinValidator validates_with StatusPinValidator
after_destroy :invalidate_cleanup_info after_destroy :invalidate_cleanup_info, if: %i(account_matches_status_account? account_local?)
delegate :local?, to: :account, prefix: true
private
def invalidate_cleanup_info def invalidate_cleanup_info
return unless status&.account_id == account_id && account.local?
account.statuses_cleanup_policy&.invalidate_last_inspected(status, :unpin) account.statuses_cleanup_policy&.invalidate_last_inspected(status, :unpin)
end end
def account_matches_status_account?
status&.account_id == account_id
end
end end

View File

@ -3,70 +3,100 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe StatusPin do RSpec.describe StatusPin do
describe 'validations' do describe 'Validations' do
it 'allows pins of own statuses' do subject { Fabricate.build :status_pin }
account = Fabricate(:account)
status = Fabricate(:status, account: account)
expect(described_class.new(account: account, status: status).save).to be true context 'with an account pinning statuses' do
subject { Fabricate.build :status_pin, account: account }
let(:account) { Fabricate(:account) }
context 'with a self-owned status' do
let(:status) { Fabricate(:status, account: account) }
it { is_expected.to allow_value(status).for(:status) }
end end
it 'does not allow pins of statuses by someone else' do context 'with a status from someone else' do
account = Fabricate(:account) let(:status) { Fabricate(:status) }
status = Fabricate(:status)
expect(described_class.new(account: account, status: status).save).to be false it { is_expected.to_not allow_value(status).for(:status).against(:base) }
end end
it 'does not allow pins of reblogs' do context 'with a reblog status' do
account = Fabricate(:account) let(:status) { Fabricate(:status, reblog: Fabricate(:status, account: account)) }
status = Fabricate(:status, account: account)
reblog = Fabricate(:status, reblog: status)
expect(described_class.new(account: account, status: reblog).save).to be false it { is_expected.to_not allow_value(status).for(:status).against(:base) }
end end
it 'does allow pins of direct statuses' do context 'with a private status' do
account = Fabricate(:account) let(:status) { Fabricate(:status, account: account, visibility: :private) }
status = Fabricate(:status, account: account, visibility: :private)
expect(described_class.new(account: account, status: status).save).to be true it { is_expected.to allow_value(status).for(:status).against(:base) }
end end
it 'does not allow pins of direct statuses' do context 'with a direct status' do
account = Fabricate(:account) let(:status) { Fabricate(:status, account: account, visibility: :direct) }
status = Fabricate(:status, account: account, visibility: :direct)
expect(described_class.new(account: account, status: status).save).to be false it { is_expected.to_not allow_value(status).for(:status).against(:base) }
end
end end
context 'with a pin limit' do context 'with a validator pin limit' do
before { stub_const('StatusPinValidator::PIN_LIMIT', 2) } before { stub_const('StatusPinValidator::PIN_LIMIT', 2) }
it 'does not allow pins above the max' do context 'with a local account at the limit' do
account = Fabricate(:account) let(:account) { Fabricate :account }
Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account) before { Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account) }
pin = described_class.new(account: account, status: Fabricate(:status, account: account)) it { is_expected.to_not allow_value(account).for(:account).against(:base).with_message(I18n.t('statuses.pin_errors.limit')) }
expect(pin.save)
.to be(false)
expect(pin.errors[:base])
.to contain_exactly(I18n.t('statuses.pin_errors.limit'))
end end
it 'allows pins above the max for remote accounts' do context 'with a remote account at the limit' do
account = Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') let(:account) { Fabricate :account, domain: 'remote.test' }
Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account) before { Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account) }
pin = described_class.new(account: account, status: Fabricate(:status, account: account)) it { is_expected.to allow_value(account).for(:account) }
expect(pin.save) end
.to be(true) end
end
expect(pin.errors[:base]) describe 'Callbacks' do
.to be_empty describe 'Invalidating status via policy' do
subject { Fabricate :status_pin, status: Fabricate(:status, account: account), account: account }
context 'with a local account that owns the status and has a policy' do
let(:account) { Fabricate :account, domain: nil }
before do
Fabricate :account_statuses_cleanup_policy, account: account
account.statuses_cleanup_policy.record_last_inspected(subject.status.id + 1_024)
end
it 'calls the invalidator on destroy' do
expect { subject.destroy }
.to change(account.statuses_cleanup_policy, :last_inspected)
end
end
context 'with a local account that owns the status and does not have a policy' do
let(:account) { Fabricate :account, domain: nil }
it 'does not call the invalidator on destroy' do
expect { subject.destroy }
.to_not change(account, :updated_at)
end
end
context 'with a remote account' do
let(:account) { Fabricate :account, domain: 'host.example' }
it 'does not call the invalidator on destroy' do
expect { subject.destroy }
.to_not change(account, :updated_at)
end
end end
end end
end end