Use validation matchers for StatusLengthValidator spec (#37905)
This commit is contained in:
parent
e8ecf1719c
commit
66052e3ddd
@ -3,123 +3,88 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe StatusLengthValidator do
|
RSpec.describe StatusLengthValidator do
|
||||||
describe '#validate' do
|
subject { Fabricate.build :status }
|
||||||
before { stub_const("#{described_class}::MAX_CHARS", 500) } # Example values below are relative to this baseline
|
|
||||||
|
|
||||||
it 'does not add errors onto remote statuses' do
|
before { stub_const 'StatusLengthValidator::MAX_CHARS', 100 }
|
||||||
status = instance_double(Status, local?: false)
|
|
||||||
allow(status).to receive(:errors)
|
|
||||||
|
|
||||||
subject.validate(status)
|
let(:over_limit_text) { 'a' * described_class::MAX_CHARS * 2 }
|
||||||
|
|
||||||
expect(status).to_not have_received(:errors)
|
context 'when status is remote' do
|
||||||
end
|
before { subject.update! account: Fabricate(:account, domain: 'host.example') }
|
||||||
|
|
||||||
it 'does not add errors onto local reblogs' do
|
it { is_expected.to allow_value(over_limit_text).for(:text) }
|
||||||
status = instance_double(Status, local?: false, reblog?: true)
|
it { is_expected.to allow_value(over_limit_text).for(:spoiler_text).against(:text) }
|
||||||
allow(status).to receive(:errors)
|
end
|
||||||
|
|
||||||
subject.validate(status)
|
context 'when status is a local reblog' do
|
||||||
|
before { subject.update! reblog: Fabricate(:status) }
|
||||||
|
|
||||||
expect(status).to_not have_received(:errors)
|
it { is_expected.to allow_value(over_limit_text).for(:text) }
|
||||||
end
|
it { is_expected.to allow_value(over_limit_text).for(:spoiler_text).against(:text) }
|
||||||
|
end
|
||||||
|
|
||||||
it 'adds an error when content warning is over character limit' do
|
context 'when text is over character limit' do
|
||||||
status = status_double(spoiler_text: 'a' * 520)
|
it { is_expected.to_not allow_value(over_limit_text).for(:text).with_message(too_long_message) }
|
||||||
subject.validate(status)
|
end
|
||||||
expect(status.errors).to have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'adds an error when text is over character limit' do
|
context 'when content warning text is over character limit' do
|
||||||
status = status_double(text: 'a' * 520)
|
it { is_expected.to_not allow_value(over_limit_text).for(:spoiler_text).against(:text).with_message(too_long_message) }
|
||||||
subject.validate(status)
|
end
|
||||||
expect(status.errors).to have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'adds an error when text and content warning are over character limit total' do
|
context 'when text and content warning combine to exceed limit' do
|
||||||
status = status_double(spoiler_text: 'a' * 250, text: 'b' * 251)
|
before { subject.text = 'a' * 50 }
|
||||||
subject.validate(status)
|
|
||||||
expect(status.errors).to have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'reduces calculated length of auto-linkable space-separated URLs' do
|
it { is_expected.to_not allow_value('a' * 55).for(:spoiler_text).against(:text).with_message(too_long_message) }
|
||||||
text = [starting_string, example_link].join(' ')
|
end
|
||||||
status = status_double(text: text)
|
|
||||||
|
|
||||||
subject.validate(status)
|
context 'when text has space separated linkable URLs' do
|
||||||
expect(status.errors).to_not have_received(:add)
|
let(:text) { [starting_string, example_link].join(' ') }
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not reduce calculated length of non-autolinkable URLs' do
|
it { is_expected.to allow_value(text).for(:text) }
|
||||||
text = [starting_string, example_link].join
|
end
|
||||||
status = status_double(text: text)
|
|
||||||
|
|
||||||
subject.validate(status)
|
context 'when text has non-separated URLs' do
|
||||||
expect(status.errors).to have_received(:add)
|
let(:text) { [starting_string, example_link].join }
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not reduce calculated length of count overly long URLs' do
|
it { is_expected.to_not allow_value(text).for(:text).with_message(too_long_message) }
|
||||||
text = "http://example.com/valid?#{'#foo?' * 1000}"
|
end
|
||||||
status = status_double(text: text)
|
|
||||||
subject.validate(status)
|
|
||||||
expect(status.errors).to have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'counts only the front part of remote usernames' do
|
context 'with excessively long URLs' do
|
||||||
text = ('a' * 475) + " @alice@#{'b' * 30}.com"
|
let(:text) { "http://example.com/valid?#{'#foo?' * 1000}" }
|
||||||
status = status_double(text: text)
|
|
||||||
|
|
||||||
subject.validate(status)
|
it { is_expected.to_not allow_value(text).for(:text).with_message(too_long_message) }
|
||||||
expect(status.errors).to_not have_received(:add)
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it 'does count both parts of remote usernames for overly long domains' do
|
context 'when remote account usernames cause limit excess' do
|
||||||
text = "@alice@#{'b' * 500}.com"
|
let(:text) { ('a' * 75) + " @alice@#{'b' * 30}.com" }
|
||||||
status = status_double(text: text)
|
|
||||||
|
|
||||||
subject.validate(status)
|
it { is_expected.to allow_value(text).for(:text) }
|
||||||
expect(status.errors).to have_received(:add)
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it 'counts multi byte emoji as single character' do
|
context 'when remote usernames are attached to long domains' do
|
||||||
text = '✨' * 500
|
let(:text) { "@alice@#{'b' * Extractor::MAX_DOMAIN_LENGTH * 2}.com" }
|
||||||
status = status_double(text: text)
|
|
||||||
|
|
||||||
subject.validate(status)
|
it { is_expected.to_not allow_value(text).for(:text).with_message(too_long_message) }
|
||||||
expect(status.errors).to_not have_received(:add)
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it 'counts ZWJ sequence emoji as single character' do
|
context 'with special character strings' do
|
||||||
text = '🏳️⚧️' * 500
|
let(:multibyte_emoji) { '✨' * described_class::MAX_CHARS }
|
||||||
status = status_double(text: text)
|
let(:zwj_sequence) { '🏳️⚧️' * described_class::MAX_CHARS }
|
||||||
|
|
||||||
subject.validate(status)
|
it { is_expected.to allow_values(multibyte_emoji, zwj_sequence).for(:text) }
|
||||||
expect(status.errors).to_not have_received(:add)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def too_long_message
|
||||||
|
I18n.t('statuses.over_character_limit', max: described_class::MAX_CHARS)
|
||||||
|
end
|
||||||
|
|
||||||
def starting_string
|
def starting_string
|
||||||
'a' * 476
|
'a' * 76
|
||||||
end
|
end
|
||||||
|
|
||||||
def example_link
|
def example_link
|
||||||
"http://#{'b' * 30}.com/example"
|
"http://#{'b' * 30}.com/example"
|
||||||
end
|
end
|
||||||
|
|
||||||
def status_double(spoiler_text: '', text: '')
|
|
||||||
instance_double(
|
|
||||||
Status,
|
|
||||||
spoiler_text: spoiler_text,
|
|
||||||
text: text,
|
|
||||||
errors: activemodel_errors,
|
|
||||||
local?: true,
|
|
||||||
reblog?: false
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def activemodel_errors
|
|
||||||
instance_double(ActiveModel::Errors, add: nil)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user