email_regex_test.rb 4.4 KB

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