min_length_spec.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/password_policy/error_examples'
  4. RSpec.describe PasswordPolicy::MinLength do
  5. it_behaves_like 'declaring an error'
  6. describe '.applicable?' do
  7. it "returns true when Setting 'password_min_size' is zero" do
  8. Setting.set('password_min_size', 0)
  9. expect(described_class).to be_applicable
  10. end
  11. it "returns true when Setting 'password_min_size' is 10" do
  12. Setting.set('password_min_size', 10)
  13. expect(described_class).to be_applicable
  14. end
  15. end
  16. describe '#valid?' do
  17. it "valid when password is longer than Setting 'password_min_size'" do
  18. Setting.set('password_min_size', 2)
  19. instance = described_class.new('good')
  20. expect(instance).to be_valid
  21. end
  22. it "not valid when password is shorter than Setting 'password_min_size'" do
  23. Setting.set('password_min_size', 2)
  24. instance = described_class.new('g')
  25. expect(instance).not_to be_valid
  26. end
  27. it "valid when password is exactly Setting 'password_min_size'" do
  28. Setting.set('password_min_size', 4)
  29. instance = described_class.new('good')
  30. expect(instance).to be_valid
  31. end
  32. it "valid when Setting 'password_min_size' is zero" do
  33. Setting.set('password_min_size', 0)
  34. instance = described_class.new('good')
  35. expect(instance).to be_valid
  36. end
  37. end
  38. describe 'error' do
  39. it "includes value of Setting 'password_min_size'" do
  40. Setting.set('password_min_size', 123)
  41. instance = described_class.new('')
  42. expect(instance.error.last).to be(123)
  43. end
  44. end
  45. end