Use validation matchers for ExistingUsernameValidator spec (#37749)
This commit is contained in:
parent
e17bbed88f
commit
0b8ce7200a
@ -3,82 +3,44 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe ExistingUsernameValidator do
|
RSpec.describe ExistingUsernameValidator do
|
||||||
|
subject { record_class.new }
|
||||||
|
|
||||||
let(:record_class) do
|
let(:record_class) do
|
||||||
Class.new do
|
Class.new do
|
||||||
include ActiveModel::Validations
|
include ActiveModel::Validations
|
||||||
|
|
||||||
attr_accessor :contact, :friends
|
attr_accessor :contact, :friends
|
||||||
|
|
||||||
def self.name
|
def self.name = 'Record'
|
||||||
'Record'
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :contact, existing_username: true
|
validates :contact, existing_username: true
|
||||||
validates :friends, existing_username: { multiple: true }
|
validates :friends, existing_username: { multiple: true }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
let(:record) { record_class.new }
|
|
||||||
|
|
||||||
describe '#validate_each' do
|
context 'with a nil value' do
|
||||||
context 'with a nil value' do
|
it { is_expected.to allow_value(nil).for(:contact) }
|
||||||
it 'does not add errors' do
|
end
|
||||||
record.contact = nil
|
|
||||||
|
|
||||||
expect(record).to be_valid
|
context 'when there are no accounts' do
|
||||||
expect(record.errors).to be_empty
|
it { is_expected.to_not allow_value('user@example.com').for(:contact).with_message(I18n.t('existing_username_validator.not_found')) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are accounts' do
|
||||||
|
before { Fabricate(:account, domain: 'example.com', username: 'user') }
|
||||||
|
|
||||||
|
context 'when the value does not match' do
|
||||||
|
it { is_expected.to_not allow_value('friend@other.host').for(:contact).with_message(I18n.t('existing_username_validator.not_found')) }
|
||||||
|
|
||||||
|
context 'when multiple is true' do
|
||||||
|
it { is_expected.to_not allow_value('friend@other.host').for(:friends).with_message(I18n.t('existing_username_validator.not_found_multiple', usernames: 'friend@other.host')) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when there are no accounts' do
|
context 'when the value does match' do
|
||||||
it 'adds errors to the record' do
|
it { is_expected.to allow_value('user@example.com').for(:contact) }
|
||||||
record.contact = 'user@example.com'
|
|
||||||
|
|
||||||
expect(record).to_not be_valid
|
it { is_expected.to allow_value('user@example.com').for(:friends) }
|
||||||
expect(record.errors.first.attribute).to eq(:contact)
|
|
||||||
expect(record.errors.first.type).to eq I18n.t('existing_username_validator.not_found')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when there are accounts' do
|
|
||||||
before { Fabricate(:account, domain: 'example.com', username: 'user') }
|
|
||||||
|
|
||||||
context 'when the value does not match' do
|
|
||||||
it 'adds errors to the record' do
|
|
||||||
record.contact = 'friend@other.host'
|
|
||||||
|
|
||||||
expect(record).to_not be_valid
|
|
||||||
expect(record.errors.first.attribute).to eq(:contact)
|
|
||||||
expect(record.errors.first.type).to eq I18n.t('existing_username_validator.not_found')
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when multiple is true' do
|
|
||||||
it 'adds errors to the record' do
|
|
||||||
record.friends = 'friend@other.host'
|
|
||||||
|
|
||||||
expect(record).to_not be_valid
|
|
||||||
expect(record.errors.first.attribute).to eq(:friends)
|
|
||||||
expect(record.errors.first.type).to eq I18n.t('existing_username_validator.not_found_multiple', usernames: 'friend@other.host')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when the value does match' do
|
|
||||||
it 'does not add errors to the record' do
|
|
||||||
record.contact = 'user@example.com'
|
|
||||||
|
|
||||||
expect(record).to be_valid
|
|
||||||
expect(record.errors).to be_empty
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when multiple is true' do
|
|
||||||
it 'does not add errors to the record' do
|
|
||||||
record.friends = 'user@example.com'
|
|
||||||
|
|
||||||
expect(record).to be_valid
|
|
||||||
expect(record.errors).to be_empty
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user