unreserved_username_validator_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe UnreservedUsernameValidator do
  4. let(:record_class) do
  5. Class.new do
  6. include ActiveModel::Validations
  7. attr_accessor :username
  8. validates_with UnreservedUsernameValidator
  9. end
  10. end
  11. let(:record) { record_class.new }
  12. describe '#validate' do
  13. context 'when username is nil' do
  14. it 'does not add errors' do
  15. record.username = nil
  16. expect(record).to be_valid
  17. expect(record.errors).to be_empty
  18. end
  19. end
  20. context 'when PAM is enabled' do
  21. before do
  22. allow(Devise).to receive(:pam_authentication).and_return(true)
  23. end
  24. context 'with a pam service available' do
  25. let(:service) { double }
  26. let(:pam_class) do
  27. Class.new do
  28. def self.account(service, username); end
  29. end
  30. end
  31. before do
  32. stub_const('Rpam2', pam_class)
  33. allow(Devise).to receive(:pam_controlled_service).and_return(service)
  34. end
  35. context 'when the account exists' do
  36. before do
  37. allow(Rpam2).to receive(:account).with(service, 'username').and_return(true)
  38. end
  39. it 'adds errors to the record' do
  40. record.username = 'username'
  41. expect(record).to_not be_valid
  42. expect(record.errors.first.attribute).to eq(:username)
  43. expect(record.errors.first.type).to eq(:reserved)
  44. end
  45. end
  46. context 'when the account does not exist' do
  47. before do
  48. allow(Rpam2).to receive(:account).with(service, 'username').and_return(false)
  49. end
  50. it 'does not add errors to the record' do
  51. record.username = 'username'
  52. expect(record).to be_valid
  53. expect(record.errors).to be_empty
  54. end
  55. end
  56. end
  57. context 'without a pam service' do
  58. before do
  59. allow(Devise).to receive(:pam_controlled_service).and_return(false)
  60. end
  61. context 'when there are not any reserved usernames' do
  62. before do
  63. stub_reserved_usernames(nil)
  64. end
  65. it 'does not add errors to the record' do
  66. record.username = 'username'
  67. expect(record).to be_valid
  68. expect(record.errors).to be_empty
  69. end
  70. end
  71. context 'when there are reserved usernames' do
  72. before do
  73. stub_reserved_usernames(%w(alice bob))
  74. end
  75. context 'when the username is reserved' do
  76. it 'adds errors to the record' do
  77. record.username = 'alice'
  78. expect(record).to_not be_valid
  79. expect(record.errors.first.attribute).to eq(:username)
  80. expect(record.errors.first.type).to eq(:reserved)
  81. end
  82. end
  83. context 'when the username is not reserved' do
  84. it 'does not add errors to the record' do
  85. record.username = 'chris'
  86. expect(record).to be_valid
  87. expect(record.errors).to be_empty
  88. end
  89. end
  90. end
  91. def stub_reserved_usernames(value)
  92. allow(Setting).to receive(:[]).with('reserved_usernames').and_return(value)
  93. end
  94. end
  95. end
  96. end
  97. end