0
0

Contribute more coverage for Account model (#32474)

This commit is contained in:
Matt Jankowski 2024-10-24 08:47:06 -04:00 committed by GitHub
parent b16435e79f
commit dea6c454fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -752,26 +752,42 @@ RSpec.describe Account do
end end
end end
describe '#prepare_contents' do describe 'Callbacks' do
subject { Fabricate.build :account, domain: domain, note: ' padded note ', display_name: ' padded name ' } describe 'Stripping content when required' do
context 'with a remote account' do
subject { Fabricate.build :account, domain: 'host.example', note: ' note ', display_name: ' display name ' }
context 'with local account' do it 'preserves content' do
let(:domain) { nil } expect { subject.valid? }
.to not_change(subject, :note)
it 'strips values' do .and not_change(subject, :display_name)
expect { subject.valid? } end
.to change(subject, :note).to('padded note')
.and(change(subject, :display_name).to('padded name'))
end end
end
context 'with remote account' do context 'with a local account' do
let(:domain) { 'host.example' } subject { Fabricate.build :account, domain: nil, note:, display_name: }
it 'preserves values' do context 'with populated fields' do
expect { subject.valid? } let(:note) { ' note ' }
.to not_change(subject, :note) let(:display_name) { ' display name ' }
.and(not_change(subject, :display_name))
it 'strips content' do
expect { subject.valid? }
.to change(subject, :note).to('note')
.and change(subject, :display_name).to('display name')
end
end
context 'with empty fields' do
let(:note) { nil }
let(:display_name) { nil }
it 'preserves content' do
expect { subject.valid? }
.to not_change(subject, :note)
.and not_change(subject, :display_name)
end
end
end end
end end
end end
@ -826,22 +842,19 @@ RSpec.describe Account do
end end
end end
describe 'validations' do describe 'Validations' do
it { is_expected.to validate_presence_of(:username) } it { is_expected.to validate_presence_of(:username) }
context 'when is local' do context 'when account is local' do
it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do subject { Fabricate.build :account, domain: nil }
_account = Fabricate(:account, username: 'the_doctor')
non_unique_account = Fabricate.build(:account, username: 'the_Doctor') context 'with an existing differently-cased username account' do
non_unique_account.valid? before { Fabricate :account, username: 'the_doctor' }
expect(non_unique_account).to model_have_error_on_field(:username)
it { is_expected.to_not allow_value('the_Doctor').for(:username) }
end end
it 'is invalid if the username is reserved' do it { is_expected.to_not allow_value('support').for(:username) }
account = Fabricate.build(:account, username: 'support')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid when username is reserved but record has already been created' do it 'is valid when username is reserved but record has already been created' do
account = Fabricate.build(:account, username: 'support') account = Fabricate.build(:account, username: 'support')
@ -849,9 +862,10 @@ RSpec.describe Account do
expect(account.valid?).to be true expect(account.valid?).to be true
end end
it 'is valid if we are creating an instance actor account with a period' do context 'with the instance actor' do
account = Fabricate.build(:account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'example.com') subject { Fabricate.build :account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true }
expect(account.valid?).to be true
it { is_expected.to allow_value('example.com').for(:username) }
end end
it 'is valid if we are creating a possibly-conflicting instance actor account' do it 'is valid if we are creating a possibly-conflicting instance actor account' do
@ -860,81 +874,31 @@ RSpec.describe Account do
expect(instance_account.valid?).to be true expect(instance_account.valid?).to be true
end end
it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do it { is_expected.to_not allow_values('the-doctor', 'the.doctor').for(:username) }
account = Fabricate.build(:account, username: 'the-doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is invalid if the username contains a period' do it { is_expected.to validate_length_of(:username).is_at_most(described_class::USERNAME_LENGTH_LIMIT) }
account = Fabricate.build(:account, username: 'the.doctor') it { is_expected.to validate_length_of(:display_name).is_at_most(described_class::DISPLAY_NAME_LENGTH_LIMIT) }
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is invalid if the username is longer than the character limit' do it { is_expected.to_not allow_values(account_note_over_limit).for(:note) }
account = Fabricate.build(:account, username: username_over_limit)
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is invalid if the display name is longer than the character limit' do
account = Fabricate.build(:account, display_name: display_name_over_limit)
account.valid?
expect(account).to model_have_error_on_field(:display_name)
end
it 'is invalid if the note is longer than the character limit' do
account = Fabricate.build(:account, note: account_note_over_limit)
account.valid?
expect(account).to model_have_error_on_field(:note)
end
end end
context 'when is remote' do context 'when account is remote' do
it 'is invalid if the username is same among accounts in the same normalized domain' do subject { Fabricate.build :account, domain: 'host.example' }
Fabricate(:account, domain: 'にゃん', username: 'username')
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username') context 'when a normalized domain account exists' do
account.valid? subject { Fabricate.build :account, domain: 'xn--r9j5b5b' }
expect(account).to model_have_error_on_field(:username)
before { Fabricate(:account, domain: 'にゃん', username: 'username') }
it { is_expected.to_not allow_values('username', 'Username').for(:username) }
end end
it 'is invalid if the username is not unique in case-insensitive comparison among accounts in the same normalized domain' do it { is_expected.to allow_values('the-doctor', username_over_limit).for(:username) }
Fabricate(:account, domain: 'にゃん', username: 'username') it { is_expected.to_not allow_values('the doctor').for(:username) }
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid even if the username contains hyphens' do it { is_expected.to allow_values(display_name_over_limit).for(:display_name) }
account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor')
account.valid?
expect(account).to_not model_have_error_on_field(:username)
end
it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do it { is_expected.to allow_values(account_note_over_limit).for(:note) }
account = Fabricate.build(:account, domain: 'domain', username: 'the doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid even if the username is longer than the character limit' do
account = Fabricate.build(:account, domain: 'domain', username: username_over_limit)
account.valid?
expect(account).to_not model_have_error_on_field(:username)
end
it 'is valid even if the display name is longer than the character limit' do
account = Fabricate.build(:account, domain: 'domain', display_name: display_name_over_limit)
account.valid?
expect(account).to_not model_have_error_on_field(:display_name)
end
it 'is valid even if the note is longer than the character limit' do
account = Fabricate.build(:account, domain: 'domain', note: account_note_over_limit)
account.valid?
expect(account).to_not model_have_error_on_field(:note)
end
end end
def username_over_limit def username_over_limit