PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/validates_email_format_of/shoulda_macros/validates_email_format_of.rb

https://github.com/ebwolf/openstreetmap-website
Ruby | 97 lines | 87 code | 9 blank | 1 comment | 0 complexity | 93c894c759b33f6935886f47d5870f7d MD5 | raw file
  1. module ValidatesEmailFormatOf
  2. module Shoulda
  3. def should_validate_email_format_of(field)
  4. metaclass = (class << self; self; end)
  5. metaclass.send(:define_method,:should_allow_values) do |klass,*values|
  6. should_allow_values_for(field, *values)
  7. end
  8. metaclass.send(:define_method,:should_not_allow_values) do |klass, *values|
  9. should_not_allow_values_for(field, values, :message => /valid e-mail/)
  10. end
  11. should_validate_email_format_of_klass(model_class, field)
  12. end
  13. def should_validate_email_format_of_klass(klass, field)
  14. context 'Typical valid email' do
  15. should_allow_values(klass,
  16. 'valid@example.com',
  17. 'Valid@test.example.com',
  18. 'valid+valid123@test.example.com',
  19. 'valid_valid123@test.example.com',
  20. 'valid-valid+123@test.example.co.uk',
  21. 'valid-valid+1.23@test.example.com.au',
  22. 'valid@example.co.uk',
  23. 'v@example.com',
  24. 'valid@example.ca',
  25. 'valid_@example.com',
  26. 'valid123.456@example.org',
  27. 'valid123.456@example.travel',
  28. 'valid123.456@example.museum',
  29. 'valid@example.mobi',
  30. 'valid@example.info',
  31. 'valid-@example.com')
  32. end
  33. context 'valid email from RFC 3696, page 6' do
  34. should_allow_values(klass,
  35. 'customer/department=shipping@example.com',
  36. '$A12345@example.com',
  37. '!def!xyz%abc@example.com',
  38. '_somename@example.com')
  39. end
  40. context 'valid email with apostrophe' do
  41. should_allow_values(klass, "test'test@example.com")
  42. end
  43. context 'valid email from http://www.rfc-editor.org/errata_search.php?rfc=3696' do
  44. should_allow_values(klass,
  45. '"Abc\@def"@example.com',
  46. '"Fred\ Bloggs"@example.com',
  47. '"Joe.\\Blow"@example.com')
  48. end
  49. context 'Typical invalid email' do
  50. should_not_allow_values(klass,
  51. 'invalid@example-com',
  52. 'invalid@example.com.',
  53. 'invalid@example.com_',
  54. 'invalid@example.com-',
  55. 'invalid-example.com',
  56. 'invalid@example.b#r.com',
  57. 'invalid@example.c',
  58. 'invali d@example.com',
  59. 'invalidexample.com',
  60. 'invalid@example.')
  61. end
  62. context 'invalid email with period starting local part' do
  63. should_not_allow_values(klass,'.invalid@example.com')
  64. end
  65. context 'invalid email with period ending local part' do
  66. should_not_allow_values(klass, 'invalid.@example.com')
  67. end
  68. context 'invalid email with consecutive periods' do
  69. should_not_allow_values(klass, 'invali..d@example.com')
  70. end
  71. # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
  72. context 'invalid email from http://tools.ietf.org/html/rfc3696, page 5' do
  73. should_not_allow_values(klass,
  74. 'Fred\ Bloggs_@example.com',
  75. 'Abc\@def+@example.com',
  76. 'Joe.\\Blow@example.com')
  77. end
  78. context 'invalid email exceeding length limits' do
  79. should_not_allow_values(klass,
  80. "#{'a' * 65}@example.com",
  81. "test@#{'a'*252}.com")
  82. end
  83. end
  84. end
  85. end
  86. Test::Unit::TestCase.extend(ValidatesEmailFormatOf::Shoulda)