Skip to content

Commit

Permalink
Reduce .times usage in StatusPin and add PIN_LIMIT constant in …
Browse files Browse the repository at this point in the history
…validator (mastodon#27945)
  • Loading branch information
mjankowski authored and vmstan committed Dec 14, 2023
1 parent 7ef7445 commit 4b66378
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
4 changes: 3 additions & 1 deletion app/validators/status_pin_validator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

class StatusPinValidator < ActiveModel::Validator
PIN_LIMIT = 5

def validate(pin)
pin.errors.add(:base, I18n.t('statuses.pin_errors.reblog')) if pin.status.reblog?
pin.errors.add(:base, I18n.t('statuses.pin_errors.ownership')) if pin.account_id != pin.status.account_id
pin.errors.add(:base, I18n.t('statuses.pin_errors.direct')) if pin.status.direct_visibility?
pin.errors.add(:base, I18n.t('statuses.pin_errors.limit')) if pin.account.status_pins.count > 4 && pin.account.local?
pin.errors.add(:base, I18n.t('statuses.pin_errors.limit')) if pin.account.status_pins.count >= PIN_LIMIT && pin.account.local?
end
end
41 changes: 20 additions & 21 deletions spec/models/status_pin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,34 @@
expect(described_class.new(account: account, status: status).save).to be false
end

max_pins = 5
it 'does not allow pins above the max' do
account = Fabricate(:account)
status = []
context 'with a pin limit' do
before { stub_const('StatusPinValidator::PIN_LIMIT', 2) }

(max_pins + 1).times do |i|
status[i] = Fabricate(:status, account: account)
end
it 'does not allow pins above the max' do
account = Fabricate(:account)

Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account)

pin = described_class.new(account: account, status: Fabricate(:status, account: account))
expect(pin.save)
.to be(false)

max_pins.times do |i|
expect(described_class.new(account: account, status: status[i]).save).to be true
expect(pin.errors[:base])
.to contain_exactly(I18n.t('statuses.pin_errors.limit'))
end

expect(described_class.new(account: account, status: status[max_pins]).save).to be false
end
it 'allows pins above the max for remote accounts' do
account = Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/')

it 'allows pins above the max for remote accounts' do
account = Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/')
status = []
Fabricate.times(StatusPinValidator::PIN_LIMIT, :status_pin, account: account)

(max_pins + 1).times do |i|
status[i] = Fabricate(:status, account: account)
end
pin = described_class.new(account: account, status: Fabricate(:status, account: account))
expect(pin.save)
.to be(true)

max_pins.times do |i|
expect(described_class.new(account: account, status: status[i]).save).to be true
expect(pin.errors[:base])
.to be_empty
end

expect(described_class.new(account: account, status: status[max_pins]).save).to be true
end
end
end

0 comments on commit 4b66378

Please sign in to comment.