Use validation matchers for UniqueUsernameValidator spec (#37909)
This commit is contained in:
parent
40f92f3af8
commit
157583659a
@ -3,72 +3,63 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe UniqueUsernameValidator do
|
RSpec.describe UniqueUsernameValidator do
|
||||||
describe '#validate' do
|
subject { Fabricate.build :account, username: 'abcdef', domain: }
|
||||||
context 'when local account' do
|
|
||||||
it 'does not add errors if username is nil' do
|
|
||||||
account = instance_double(Account, username: nil, domain: nil, persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
|
||||||
expect(account.errors).to_not have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not add errors when existing one is subject itself' do
|
context 'when local account' do
|
||||||
account = Fabricate(:account, username: 'abcdef')
|
let(:domain) { nil }
|
||||||
expect(account).to be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'adds an error when the username is already used with ignoring cases' do
|
context 'when record is persisted and checking own name' do
|
||||||
Fabricate(:account, username: 'ABCdef')
|
before { subject.save }
|
||||||
account = instance_double(Account, username: 'abcDEF', domain: nil, persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
|
||||||
expect(account.errors).to have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not add errors when same username remote account exists' do
|
it { is_expected.to allow_value(subject.username).for(:username) }
|
||||||
Fabricate(:account, username: 'abcdef', domain: 'example.com')
|
end
|
||||||
account = instance_double(Account, username: 'abcdef', domain: nil, persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
context 'when username case insensitive in use already' do
|
||||||
expect(account.errors).to_not have_received(:add)
|
before { Fabricate :account, username: 'ABCdef' }
|
||||||
end
|
|
||||||
|
it { is_expected.to_not allow_value('abcDEF').for(:username).with_message(:taken) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when username on remote account is in use' do
|
||||||
|
before { Fabricate :account, username: 'ABCdef', domain: 'host.example' }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value('abcDEF').for(:username) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when remote account' do
|
context 'when remote account' do
|
||||||
it 'does not add errors if username is nil' do
|
let(:domain) { 'host.example' }
|
||||||
account = instance_double(Account, username: nil, domain: 'example.com', persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
context 'when record is persisted and checking own name' do
|
||||||
expect(account.errors).to_not have_received(:add)
|
before { subject.save }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value('abcdef').for(:username) }
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not add errors when existing one is subject itself' do
|
context 'when username case insensitive in use already' do
|
||||||
account = Fabricate(:account, username: 'abcdef', domain: 'example.com')
|
before { Fabricate :account, username: 'ABCdef', domain: 'host.example' }
|
||||||
expect(account).to be_valid
|
|
||||||
|
it { is_expected.to_not allow_value('abcDEF').for(:username) }
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'adds an error when the username is already used with ignoring cases' do
|
context 'when domain case insensitive in use already' do
|
||||||
Fabricate(:account, username: 'ABCdef', domain: 'example.com')
|
before { Fabricate :account, username: 'ABCdef', domain: 'HOST.EXAMPLE' }
|
||||||
account = instance_double(Account, username: 'abcDEF', domain: 'example.com', persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
it { is_expected.to_not allow_value('abcDEF').for(:username) }
|
||||||
expect(account.errors).to have_received(:add)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'adds an error when the domain is already used with ignoring cases' do
|
context 'when same username on other domain is in use already' do
|
||||||
Fabricate(:account, username: 'ABCdef', domain: 'example.com')
|
before { Fabricate :account, username: 'abcdef', domain: 'other.example' }
|
||||||
account = instance_double(Account, username: 'ABCdef', domain: 'EXAMPLE.COM', persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
|
||||||
expect(account.errors).to have_received(:add)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not add errors when account with the same username and another domain exists' do
|
it { is_expected.to allow_value('abcdef').for(:username) }
|
||||||
Fabricate(:account, username: 'abcdef', domain: 'example.com')
|
|
||||||
account = instance_double(Account, username: 'abcdef', domain: 'example2.com', persisted?: false, errors: activemodel_errors)
|
|
||||||
subject.validate(account)
|
|
||||||
expect(account.errors).to_not have_received(:add)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
context 'when account has blank username' do
|
||||||
|
subject { described_class.new.validate(account) }
|
||||||
|
|
||||||
def activemodel_errors
|
let(:account) { Fabricate.build :account, username: nil }
|
||||||
instance_double(ActiveModel::Errors, add: nil)
|
|
||||||
|
it { is_expected.to be_nil }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user