2019-01-03 05:10:02 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
describe UnreservedUsernameValidator do
|
|
|
|
let(:record_class) do
|
|
|
|
Class.new do
|
|
|
|
include ActiveModel::Validations
|
|
|
|
attr_accessor :username
|
2019-01-03 05:10:02 +01:00
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
validates_with UnreservedUsernameValidator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
let(:record) { record_class.new }
|
2019-01-03 05:10:02 +01:00
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
describe '#validate' do
|
|
|
|
context 'when username is nil' do
|
|
|
|
it 'does not add errors' do
|
|
|
|
record.username = nil
|
2019-01-03 05:10:02 +01:00
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
expect(record).to be_valid
|
|
|
|
expect(record.errors).to be_empty
|
2019-01-03 05:10:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
context 'when PAM is enabled' do
|
|
|
|
before do
|
|
|
|
allow(Devise).to receive(:pam_authentication).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a pam service available' do
|
|
|
|
let(:service) { double }
|
|
|
|
let(:pam_class) do
|
|
|
|
Class.new do
|
|
|
|
def self.account(service, username); end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const('Rpam2', pam_class)
|
|
|
|
allow(Devise).to receive(:pam_controlled_service).and_return(service)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account exists' do
|
|
|
|
before do
|
|
|
|
allow(Rpam2).to receive(:account).with(service, 'username').and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds errors to the record' do
|
|
|
|
record.username = 'username'
|
|
|
|
|
|
|
|
expect(record).to_not be_valid
|
|
|
|
expect(record.errors.first.attribute).to eq(:username)
|
|
|
|
expect(record.errors.first.type).to eq(:reserved)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account does not exist' do
|
|
|
|
before do
|
|
|
|
allow(Rpam2).to receive(:account).with(service, 'username').and_return(false)
|
|
|
|
end
|
2019-01-03 05:10:02 +01:00
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
it 'does not add errors to the record' do
|
|
|
|
record.username = 'username'
|
2019-01-03 05:10:02 +01:00
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
expect(record).to be_valid
|
|
|
|
expect(record.errors).to be_empty
|
|
|
|
end
|
2019-01-03 05:10:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
context 'without a pam service' do
|
|
|
|
before do
|
|
|
|
allow(Devise).to receive(:pam_controlled_service).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are not any reserved usernames' do
|
|
|
|
before do
|
|
|
|
stub_reserved_usernames(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add errors to the record' do
|
|
|
|
record.username = 'username'
|
|
|
|
|
|
|
|
expect(record).to be_valid
|
|
|
|
expect(record.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are reserved usernames' do
|
|
|
|
before do
|
|
|
|
stub_reserved_usernames(%w(alice bob))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the username is reserved' do
|
|
|
|
it 'adds errors to the record' do
|
|
|
|
record.username = 'alice'
|
|
|
|
|
|
|
|
expect(record).to_not be_valid
|
|
|
|
expect(record.errors.first.attribute).to eq(:username)
|
|
|
|
expect(record.errors.first.type).to eq(:reserved)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the username is not reserved' do
|
|
|
|
it 'does not add errors to the record' do
|
|
|
|
record.username = 'chris'
|
|
|
|
|
|
|
|
expect(record).to be_valid
|
|
|
|
expect(record.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 05:10:02 +01:00
|
|
|
|
2023-10-17 13:05:28 +02:00
|
|
|
def stub_reserved_usernames(value)
|
|
|
|
allow(Setting).to receive(:[]).with('reserved_usernames').and_return(value)
|
2019-01-03 05:10:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|