length_spec.rb 1.5 KB

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