domain_validator_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe DomainValidator do
  4. let(:record) { record_class.new }
  5. context 'with no options' do
  6. let(:record_class) do
  7. Class.new do
  8. include ActiveModel::Validations
  9. attr_accessor :domain
  10. validates :domain, domain: true
  11. end
  12. end
  13. describe '#validate_each' do
  14. context 'with a nil value' do
  15. it 'does not add errors' do
  16. record.domain = nil
  17. expect(record).to be_valid
  18. expect(record.errors).to be_empty
  19. end
  20. end
  21. context 'with a valid domain' do
  22. it 'does not add errors' do
  23. record.domain = 'example.com'
  24. expect(record).to be_valid
  25. expect(record.errors).to be_empty
  26. end
  27. end
  28. context 'with a domain that is too long' do
  29. it 'adds an error' do
  30. record.domain = "#{'a' * 300}.com"
  31. expect(record).to_not be_valid
  32. expect(record.errors.where(:domain)).to_not be_empty
  33. end
  34. end
  35. context 'with a domain with an empty segment' do
  36. it 'adds an error' do
  37. record.domain = '.example.com'
  38. expect(record).to_not be_valid
  39. expect(record.errors.where(:domain)).to_not be_empty
  40. end
  41. end
  42. context 'with a domain with an invalid character' do
  43. it 'adds an error' do
  44. record.domain = '*.example.com'
  45. expect(record).to_not be_valid
  46. expect(record.errors.where(:domain)).to_not be_empty
  47. end
  48. end
  49. context 'with a domain that would fail parsing' do
  50. it 'adds an error' do
  51. record.domain = '/'
  52. expect(record).to_not be_valid
  53. expect(record.errors.where(:domain)).to_not be_empty
  54. end
  55. end
  56. end
  57. end
  58. context 'with acct option' do
  59. let(:record_class) do
  60. Class.new do
  61. include ActiveModel::Validations
  62. attr_accessor :acct
  63. validates :acct, domain: { acct: true }
  64. end
  65. end
  66. describe '#validate_each' do
  67. context 'with a nil value' do
  68. it 'does not add errors' do
  69. record.acct = nil
  70. expect(record).to be_valid
  71. expect(record.errors).to be_empty
  72. end
  73. end
  74. context 'with no domain' do
  75. it 'does not add errors' do
  76. record.acct = 'hoge_123'
  77. expect(record).to be_valid
  78. expect(record.errors).to be_empty
  79. end
  80. end
  81. context 'with a valid domain' do
  82. it 'does not add errors' do
  83. record.acct = 'hoge_123@example.com'
  84. expect(record).to be_valid
  85. expect(record.errors).to be_empty
  86. end
  87. end
  88. context 'with an invalid domain' do
  89. it 'adds an error' do
  90. record.acct = 'hoge_123@.example.com'
  91. expect(record).to_not be_valid
  92. expect(record.errors.where(:acct)).to_not be_empty
  93. end
  94. end
  95. end
  96. end
  97. end