sanitize_email 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,30 @@
1
1
  # Copyright (c) 2008-16 Peter H. Boling of RailsBling.com
2
2
  # Released under the MIT license
3
- require 'sanitize_email/test_helpers'
4
- require 'sanitize_email/mail_ext'
3
+ # Note: the RspecMatchers are composed matchers:
4
+ # See: http://www.relishapp.com/rspec/rspec-expectations/v/3-5/docs/composing-matchers
5
+
6
+ require "sanitize_email/mail_ext"
5
7
 
6
8
  module SanitizeEmail
7
- # Provides matchers that can be used in Rspec tests to assert the behavior of email
9
+ # Provides matchers that can be used in
10
+ # Rspec tests to assert the behavior of email
8
11
  module RspecMatchers
9
- include SanitizeEmail::TestHelpers
10
- [:from, :to, :cc, :bcc, :reply_to].each do |attribute|
12
+ [:from, :to, :cc, :bcc, :subject, :reply_to].each do |attribute|
11
13
  RSpec::Matchers.define "have_#{attribute}" do |matcher|
12
14
  match do |actual|
13
- email_matching(matcher, attribute, actual)
15
+ @actual = actual.send(attribute)
16
+ @actual = @actual.join(", ") if @actual.respond_to?(:join)
17
+ expect(@actual).to match(matcher)
14
18
  end
15
19
  end
16
20
  end
17
21
 
18
- [:subject].each do |attribute|
19
- RSpec::Matchers.define "have_#{attribute}" do |matcher|
22
+ [:from, :to, :cc, :bcc, :subject, :reply_to].each do |attribute|
23
+ RSpec::Matchers.define "match_#{attribute}" do |matcher|
20
24
  match do |actual|
21
- string_matching_attribute(matcher, attribute, actual)
25
+ @actual = actual.send(attribute)
26
+ @actual = @actual.join(", ") if @actual.respond_to?(:join)
27
+ expect(@actual).to match(matcher)
22
28
  end
23
29
  end
24
30
  end
@@ -26,46 +32,59 @@ module SanitizeEmail
26
32
  [:from, :to, :cc, :bcc, :subject, :reply_to].each do |attribute|
27
33
  RSpec::Matchers.define "be_#{attribute}" do |matcher|
28
34
  match do |actual|
29
- string_matching(matcher, attribute, actual)
35
+ @actual = actual.send(attribute)
36
+ @actual = @actual.join(", ") if @actual.respond_to?(:join)
37
+ expect(@actual).to be(matcher)
30
38
  end
31
39
  end
32
40
  end
33
41
 
34
42
  RSpec::Matchers.define "have_to_username" do |matcher|
35
- def get_username(email_message)
43
+ def get_to_username(email_message)
44
+ username_header = email_message.header["X-Sanitize-Email-To"]
45
+ return username_header unless username_header.is_a?(Mail::Field)
36
46
  email_message.header.fields[3].value
37
47
  end
38
48
  match do |actual|
39
- string_matching(matcher, :to_username, get_username(actual))
49
+ @actual = get_to_username(actual)
50
+ expect(@actual).to match(matcher)
51
+ end
52
+ end
53
+
54
+ RSpec::Matchers.define "have_cc_username" do |matcher|
55
+ def get_cc_username(email_message)
56
+ username_header = email_message.header["X-Sanitize-Email-Cc"]
57
+ return username_header unless username_header.is_a?(Mail::Field)
58
+ email_message.header.fields[3].value
59
+ end
60
+ match do |actual|
61
+ @actual = get_cc_username(actual)
62
+ expect(@actual).to match(matcher)
40
63
  end
41
64
  end
42
65
 
43
66
  # Cribbed from email_spec gem
44
67
  RSpec::Matchers.define "have_body_text" do |matcher|
68
+ def get_fuzzy_body(email_message)
69
+ email_message.default_part_body.to_s.gsub(/\s+/, " ")
70
+ end
71
+
72
+ def get_fuzzy_matcher(to_fuzz)
73
+ to_fuzz.gsub(/\s+/, " ")
74
+ end
45
75
  match do |actual|
46
- # Normalize all the whitespace, to improve match fuzz
47
- if matcher.is_a?(String)
48
- actual_text = actual.default_part_body.to_s.gsub(/\s+/, " ")
49
- matcher_text = matcher.gsub(/\s+/, " ")
50
- raise SanitizeEmail::TestHelpers::UnexpectedMailType, "Cannot find #{matcher} in body" unless actual_text.respond_to?(:include?)
51
- actual_text.include?(matcher_text)
52
- else
53
- actual_text = actual.default_part_body.to_s
54
- raise SanitizeEmail::TestHelpers::UnexpectedMailType, "Cannot find #{matcher} in body" unless actual_text.respond_to?(:=~)
55
- !!(actual_text =~ matcher)
56
- end
76
+ @actual = get_fuzzy_body(actual)
77
+ fuzzy_matcher = get_fuzzy_matcher(matcher)
78
+ expect(@actual).to match(fuzzy_matcher)
57
79
  end
58
80
  end
59
81
 
60
82
  # Cribbed from email_spec gem
61
83
  RSpec::Matchers.define "have_header" do |name, matcher|
62
84
  match do |actual|
63
- header = actual.header
64
- if matcher.is_a?(String)
65
- header[name].to_s == matcher
66
- else
67
- header[name].to_s =~ matcher
68
- end
85
+ @actual = actual.header[name]
86
+ @actual = @actual.value unless @actual.nil?
87
+ expect(@actual).to match(matcher)
69
88
  end
70
89
  end
71
90
 
@@ -1,5 +1,7 @@
1
1
  # Copyright (c) 2008-16 Peter H. Boling of RailsBling.com
2
2
  # Released under the MIT license
3
+ # Note: the RspecMatchers no longer use these methods. Instead they are composed matchers:
4
+ # See: http://www.relishapp.com/rspec/rspec-expectations/v/3-5/docs/composing-matchers
3
5
 
4
6
  module SanitizeEmail
5
7
  # Helpers for test-unit
@@ -8,16 +10,16 @@ module SanitizeEmail
8
10
  # Error raised when unable to match an expected part of email in order to fail the test
9
11
  class UnexpectedMailType < StandardError; end
10
12
 
11
- def string_matching_attribute(matcher, part, mail_or_part)
12
- string_matching(matcher, part, mail_or_part.send(part))
13
+ def string_matching_attribute(matcher, part, attribute)
14
+ string_matching(matcher, part, attribute)
13
15
  end
14
16
 
15
- def string_matching(matcher, part, mail_or_part)
16
- if mail_or_part.respond_to?(:=~) # Can we match a regex against it?
17
+ def string_matching(matcher, part, attribute)
18
+ if attribute.respond_to?(:=~) # Can we match a regex against it?
17
19
  if matcher.is_a?(Regexp)
18
- mail_or_part =~ matcher
20
+ attribute =~ matcher
19
21
  else
20
- mail_or_part =~ Regexp.new(Regexp.escape(matcher))
22
+ attribute =~ Regexp.new(Regexp.escape(matcher))
21
23
  end
22
24
  else
23
25
  raise UnexpectedMailType, "Cannot match #{matcher} for #{part}"
@@ -25,13 +27,17 @@ module SanitizeEmail
25
27
  end
26
28
 
27
29
  # Normalize arrays to strings
28
- def array_matching(matcher, part, mail_or_part)
29
- mail_or_part = mail_or_part.join(', ') if mail_or_part.respond_to?(:join)
30
- string_matching(matcher, part, mail_or_part)
30
+ def array_matching(matcher, part, attribute)
31
+ attribute = attribute.join(", ") if attribute.respond_to?(:join)
32
+ string_matching(matcher, part, attribute)
31
33
  end
32
34
 
33
35
  def email_matching(matcher, part, mail_or_part)
34
- array_matching(matcher, part, mail_or_part.send(part))
36
+ email_attribute_matching(matcher, part, mail_or_part.send(part))
37
+ end
38
+
39
+ def email_attribute_matching(matcher, part, attribute)
40
+ array_matching(matcher, part, attribute)
35
41
  end
36
42
 
37
43
  end
@@ -2,5 +2,5 @@
2
2
  # Released under the MIT license
3
3
 
4
4
  module SanitizeEmail
5
- VERSION = '1.2.0'
5
+ VERSION = "1.2.1".freeze
6
6
  end
@@ -2,12 +2,12 @@
2
2
  # Released under the MIT license
3
3
 
4
4
  module SanitizeEmail
5
- require 'sanitize_email/version'
6
- require 'sanitize_email/deprecation'
7
- require 'sanitize_email/config'
8
- require 'sanitize_email/mail_header_tools'
9
- require 'sanitize_email/overridden_addresses'
10
- require 'sanitize_email/bleach'
5
+ require "sanitize_email/version"
6
+ require "sanitize_email/deprecation"
7
+ require "sanitize_email/config"
8
+ require "sanitize_email/mail_header_tools"
9
+ require "sanitize_email/overridden_addresses"
10
+ require "sanitize_email/bleach"
11
11
 
12
12
  # Error is raised when a block parameter is required and not provided to a method
13
13
  class MissingBlockParameter < StandardError; end
@@ -15,9 +15,9 @@ module SanitizeEmail
15
15
  # Allow non-rails implementations to use this gem
16
16
  if defined?(::Rails)
17
17
  if defined?(::Rails::Engine)
18
- require 'sanitize_email/engine'
18
+ require "sanitize_email/engine"
19
19
  elsif ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR == 0
20
- require 'sanitize_email/railtie'
20
+ require "sanitize_email/railtie"
21
21
  else
22
22
  raise "Please use the 0.X.X versions of sanitize_email for Rails 2.X and below."
23
23
  end
@@ -65,16 +65,18 @@ module SanitizeEmail
65
65
  end
66
66
  @force_sanitize = nil
67
67
 
68
- # Regardless of the Config settings of SanitizeEmail you can do a local override to send sanitary email in any environment.
69
- # You have access to all the same configuration options in the parameter hash as you can set in the actual
70
- # SanitizeEmail.configure block.
68
+ # Regardless of the Config settings of SanitizeEmail you can do a local
69
+ # override to send sanitary email in any environment.
70
+ # You have access to all the same configuration options in the parameter hash
71
+ # as you can set in the actual SanitizeEmail.configure block.
71
72
  #
72
- # SanitizeEmail.sanitary({:sanitized_to => 'boo@example.com'}) do # these config options are merged with the globals
73
+ # SanitizeEmail.sanitary({:sanitized_to => "boo@example.com"}) do
73
74
  # Mail.deliver do
74
- # from 'from@example.org'
75
- # to 'to@example.org' # Will actually be sent to the override addresses, in this case: boo@example.com
76
- # reply_to 'reply_to@example.org'
77
- # subject 'subject'
75
+ # from "from@example.org"
76
+ # # Will actually be sent to the override addresses, not this one:
77
+ # to "to@example.org"
78
+ # reply_to "reply_to@example.org"
79
+ # subject "subject"
78
80
  # end
79
81
  # end
80
82
  #
@@ -88,14 +90,15 @@ module SanitizeEmail
88
90
  end
89
91
  end
90
92
 
91
- # Regardless of the Config settings of SanitizeEmail you can do a local override to force unsanitary email in any environment.
93
+ # Regardless of the Config settings of SanitizeEmail you can do a local
94
+ # override to force unsanitary email in any environment.
92
95
  #
93
96
  # SanitizeEmail.unsanitary do
94
97
  # Mail.deliver do
95
- # from 'from@example.org'
96
- # to 'to@example.org'
97
- # reply_to 'reply_to@example.org'
98
- # subject 'subject'
98
+ # from "from@example.org"
99
+ # to "to@example.org"
100
+ # reply_to "reply_to@example.org"
101
+ # subject "subject"
99
102
  # end
100
103
  # end
101
104
  #
@@ -45,5 +45,7 @@ EOS
45
45
  s.add_development_dependency("roodi", [">= 2.1.0"])
46
46
  s.add_development_dependency("rake", [">= 0"])
47
47
  s.add_development_dependency("pry", [">= 0"])
48
+ s.add_development_dependency("appraisal")
49
+ s.add_development_dependency("wwtd")
48
50
  s.add_development_dependency("coveralls")
49
51
  end