email_regex_test.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class EmailRegexTest < ActiveSupport::TestCase
  4. test 'should be able to detect valid/invalid the regex filter' do
  5. # check with exact email, check_mode = true
  6. sender = 'foobar@foo.bar'
  7. from = 'foobar@foo.bar'
  8. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  9. assert_equal(true, regex)
  10. # check with exact email
  11. sender = 'foobar@foo.bar'
  12. from = 'foobar@foo.bar'
  13. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  14. assert_equal(true, regex)
  15. # check with regex: filter check_mode = true
  16. sender = 'regex:foobar@.*'
  17. from = 'foobar@foo.bar'
  18. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  19. assert_equal(true, regex)
  20. # check with regex: filter
  21. sender = 'regex:foobar@.*'
  22. from = 'foobar@foo.bar'
  23. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  24. assert_equal(true, regex)
  25. # check regex with regex: filter
  26. sender = 'regex:??'
  27. from = 'foobar@foo.bar'
  28. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  29. assert_equal(false, regex)
  30. # check regex with raise error (check_mode = true)
  31. assert_raises("Can't use regex '??' on 'foobar@foo.bar'") do
  32. sender = 'regex:??'
  33. from = 'foobar@foo.bar'
  34. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  35. end
  36. sender = 'regex:[]'
  37. from = 'foobar@foo.bar'
  38. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  39. assert_equal(false, regex)
  40. # check regex with raise error, (check_mode = true)
  41. assert_raises("Can't use regex '[]' on 'foobar@foo.bar'") do
  42. sender = 'regex:[]'
  43. from = 'foobar@foo.bar'
  44. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  45. end
  46. # check regex with empty field
  47. sender = '{}'
  48. from = 'foobar@foo.bar'
  49. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  50. assert_equal(false, regex)
  51. # check regex with empty field and raise error (check_mode = true)
  52. sender = '{}'
  53. from = 'foobar@foo.bar'
  54. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  55. assert_equal(false, regex)
  56. # check regex with empty field
  57. sender = ''
  58. from = 'foobar@foo.bar'
  59. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  60. assert_equal(true, regex)
  61. # check regex with empty field
  62. sender = ''
  63. from = 'foobar@foo.bar'
  64. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  65. assert_equal(true, regex)
  66. # check regex with regex: wildcard
  67. sender = 'regex:*'
  68. from = 'foobar@foo.bar'
  69. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  70. assert_equal(false, regex)
  71. # check regex with regex: wildcard and raise error (check_mode = true)
  72. assert_raises("Can't use regex '*' on 'foobar@foo.bar'") do
  73. sender = 'regex:*'
  74. from = 'foobar@foo.bar'
  75. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  76. end
  77. # check email with wildcard
  78. sender = '*'
  79. from = 'foobar@foo.bar'
  80. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  81. assert_equal(true, regex)
  82. # check email with wildcard (check_mode = true)
  83. sender = '*'
  84. from = 'foobar@foo.bar'
  85. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  86. assert_equal(true, regex)
  87. # check email with a different sender
  88. sender = 'regex:nagios@.*'
  89. from = 'foobar@foo.bar'
  90. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: false)
  91. assert_equal(false, regex)
  92. # check email with a different sender with checkmode = true
  93. sender = 'regex:nagios@.*'
  94. from = 'foobar@foo.bar'
  95. regex = Channel::Filter::Match::EmailRegex.match(value: from, match_rule: sender, check_mode: true)
  96. assert_equal(false, regex)
  97. end
  98. end