sanitize_email 1.0.11 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ Version 1.1.0 - DEC.30.2013
2
+ * Add documentation for non-Rails setup by Peter Boling
3
+ * Add documentation for using sanitize_email's bundled Rspec Matchers by Peter Boling
4
+ * Add documentation for using sanitize_email's bundled Test Helpers by Peter Boling
5
+ * Stopped using method_missing internally for config access by Peter Boling
6
+ * Improved ease of setup with mail gem outside rails by auto-configuring the interceptor (default inactive) by Peter Boling
7
+
1
8
  Version 1.0.11 - DEC.30.2013
2
9
  * Fix travis build by Peter Boling
3
10
  * Fix test suite to run on Ruby 1.8.7 again, add back to Travis by Peter Boling
data/README.md CHANGED
@@ -41,24 +41,65 @@ It's particularly helpful when you want to prevent the delivery of email (e.g. i
41
41
 
42
42
  ## Re-routing Email on a Staging or QA Server
43
43
 
44
- Another very important use case for me is to transparently re-route email generated from a staging or QA server to an appropriate person. For example, it's common for us to set up a staging server for a client to use to view our progress and test out new features. It's important for any email that is generated from our web application be delivered to the client's inbox so that they can review the content and ensure that it's acceptable. Similarly, we set up QA instances for our own QA team and we use {rails-caddy}[http://github.com/jtrupiano/rails-caddy] to allow each QA person to configure it specifically for them.
44
+ Another very important use case for me is to transparently re-route email generated from a staging or QA server to an appropriate person. For example, it's common for us to set up a staging server for a client to use to view our progress and test out new features. It's important for any email that is generated from our web application be delivered to the client's inbox so that they can review the content and ensure that it's acceptable. Similarly, we set up QA instances for our own QA team and we use [rails-caddy](http://github.com/jtrupiano/rails-caddy) to allow each QA person to configure it specifically for them.
45
45
 
46
46
  ## Testing Email from a Hot Production Server
47
47
 
48
48
  If you install this gem on a production server (which I don't always do), you can load up script/console and override the to/cc/bcc on all emails for the duration of your console session. This allows you to poke and prod a live production instance, and route all email to your own inbox for inspection. The best part is that this can all be accomplished without changing a single line of your application code.
49
49
 
50
+ ## Using with a test suite as an alternative to the heavy email_spec
51
+
52
+ [email_spec](https://github.com/bmabey/email-spec) is a great gem, with awesome rspec matchers and helpers, but it has an undeclared dependency on ActionMailer. Sad face.
53
+
54
+ SanitizeEmail comes with some lightweight RspecMatchers covering most of what email_spec can do. It will help you test email functionality. It is useful when you are creating a gem to handle email features, or are writing a simple Ruby script, and don't want to pull in le Rails. SanitizeEmail has no dependencies. Your Mail system just needs to conform to the `register_interceptor` API.
55
+
50
56
  ## Install Like a Boss
51
57
 
52
58
  In Gemfile:
53
59
 
54
- gem 'flag_shih_tzu'
60
+ gem 'flag_shih_tzu'
55
61
 
56
62
  Then:
57
63
 
58
- $ bundle install
64
+ $ bundle install
65
+
66
+ ## Setup with Ruby
67
+
68
+ *keep scrolling for Rails, but read this for a better understanding of Magic*
69
+
70
+ There are four ways SanitizeEmail can be turned on; in order of precedence they are:
71
+
72
+ 1. SanitizeEmail.force_sanitize = true # by default it is nil
73
+ Only useful for local context. Inside a method where you will be sending an email,
74
+ set SanitizeEmail.force_sanitize = true just prior to delivering it. Also useful in the console.
75
+ 2. Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true)) # by default it is nil
76
+ If SanitizeEmail seems to not be sanitizing you have probably not registered the interceptor. SanitizeEmail tries to do this for you.
77
+ Note: If you are working in an environment that has a Mail or Mailer class that uses the register_interceptor API, the interceptor will already have been registered by SanitizeEmail (however, note lack of :engage => true):
78
+
79
+ Mail.register_interceptor(SanitizeEmail::Bleach.new
80
+ Without :engage => true the interceptor is inactive, and will require engaging via one of the other methods.
81
+ As an example you could do the following to engage SanitizeEmail:
82
+
83
+ SanitizeEmail::Config.configure {|config| config[:engage] = true }
84
+ 3. SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } # by default it is false
85
+ If you don't need to compute anything, then don't use the Proc, go with the next option.
86
+ 4. SanitizeEmail::Config.configure {|config| config[:engage] = true } # by default it is nil
87
+
88
+ ### Notes
89
+
90
+ Number 1, above, is the method used by the SanitizeEmail.sanitary block.
91
+ If installed but not configured, sanitize_email DOES NOTHING. Until configured the defaults leave it turned off.
59
92
 
93
+ ### Troubleshooting
60
94
 
61
- ## Setup With An Axe
95
+ IMPORTANT: You may need to setup your own register_interceptor. If sanitize_email doesn't seem to be working for you find your Mailer/Mail class and try this:
96
+
97
+ Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true))
98
+
99
+ If that causes an error you will know why sanitize_email doesn't work.
100
+ Otherwise it will start working according to the rest of the configuration.
101
+
102
+ ## Setup With Rails
62
103
 
63
104
  Create an initializer, if you are using rails, or otherwise configure:
64
105
 
@@ -117,6 +158,114 @@ SanitizeEmail.configure block.
117
158
  end
118
159
  ```
119
160
 
161
+ ## Use sanitize_email in your test suite!
162
+
163
+ ### rspec
164
+
165
+ In your `spec_helper.rb`:
166
+
167
+ require 'sanitize_email'
168
+ # rspec matchers are *not* loaded by default in sanitize_email, as it is not primarily a gem for test suites.
169
+ require 'sanitize_email/rspec_matchers'
170
+
171
+ SanitizeEmail::Config.configure do |config|
172
+ config[:sanitized_to] = 'sanitize_email@example.org'
173
+ config[:sanitized_cc] = 'sanitize_email@example.org'
174
+ config[:sanitized_bcc] = 'sanitize_email@example.org'
175
+ # run/call whatever logic should turn sanitize_email on and off in this Proc.
176
+ # config[:activation_proc] = Proc.new { true }
177
+ # Since this configuration is *inside* the spec_helper, it might be assumed that we always want to sanitize. If we don't want to it can be easily manipulated with SanitizeEmail.unsanitary and SanitizeEmail.sanitary block helpers.
178
+ # Thus instead of using the Proc (slower) we just engage it always:
179
+ config[:engage] = true
180
+ config[:use_actual_email_prepended_to_subject] = true # or false
181
+ config[:use_actual_environment_prepended_to_subject] = true # or false
182
+ config[:use_actual_email_as_sanitized_user_name] = true # or false
183
+ end
184
+
185
+ # If your mail system is not one that sanitize_email automatically configures an interceptor for (ActionMailer, Mail) then you will need to do the equivalent for whatever Mail system you are using:
186
+ # Mail.register_interceptor(SanitizeEmail::Bleach.new)
187
+
188
+ RSpec.configure do |config|
189
+ # ...
190
+ # From sanitize_email gem
191
+ config.include SanitizeEmail::RspecMatchers
192
+ end
193
+
194
+ context "an email test" do
195
+ subject { Mail.new(@message_hash) }
196
+ it { should have_to "sanitize_email@example.org" }
197
+ end
198
+
199
+ #### have_* matchers
200
+
201
+ These will look for an email address in any of the following
202
+
203
+ :from, :to, :cc, :bcc, :subject, :reply_to
204
+
205
+ Example:
206
+
207
+ context "the subject line must have the email address sanitize_email@example.org" do
208
+ subject { Mail.new(@message_hash) }
209
+ it { should have_subject "sanitize_email@example.org" }
210
+ end
211
+
212
+ #### be_* matchers
213
+
214
+ These will look for a matching string in any of the following
215
+
216
+ :from, :to, :cc, :bcc, :subject, :reply_to
217
+
218
+ Example:
219
+
220
+ context "the subject line must have the string 'foobarbaz'" do
221
+ subject { Mail.new(@message_hash) }
222
+ it { should be_subject "foobarbaz" }
223
+ end
224
+
225
+ #### have_to_username matcher
226
+
227
+ The `username` in the `:to` field is when the `:to` field is formatted like this:
228
+
229
+ Peter Boling <sanitize_email@example.org>
230
+
231
+ Example:
232
+
233
+ context "the to field must have the username 'Peter Boling'" do
234
+ subject { Mail.new(@message_hash) }
235
+ it { should have_to_username "Peter Boling" }
236
+ end
237
+
238
+ ### non-rspec (Test::Unit, mini-test, etc)
239
+
240
+ In your setup file:
241
+
242
+ require 'sanitize_email'
243
+ # test helpers are *not* loaded by default in sanitize_email, as it is not primarily a gem for test suites.
244
+ require 'sanitize_email/test_helpers'
245
+
246
+ SanitizeEmail::Config.configure do |config|
247
+ config[:sanitized_to] = 'sanitize_email@example.org'
248
+ config[:sanitized_cc] = 'sanitize_email@example.org'
249
+ config[:sanitized_bcc] = 'sanitize_email@example.org'
250
+ # run/call whatever logic should turn sanitize_email on and off in this Proc.
251
+ # config[:activation_proc] = Proc.new { true }
252
+ # Since this configuration is *inside* the spec_helper, it might be assumed that we always want to sanitize. If we don't want to it can be easily manipulated with SanitizeEmail.unsanitary and SanitizeEmail.sanitary block helpers.
253
+ # Thus instead of using the Proc (slower) we just engage it always:
254
+ config[:engage] = true
255
+ config[:use_actual_email_prepended_to_subject] = true # or false
256
+ config[:use_actual_environment_prepended_to_subject] = true # or false
257
+ config[:use_actual_email_as_sanitized_user_name] = true # or false
258
+ end
259
+
260
+ # If your mail system is not one that sanitize_email automatically configures an interceptor for (ActionMailer, Mail) then you will need to do the equivalent for whatever Mail system you are using:
261
+ # Mail.register_interceptor(SanitizeEmail::Bleach.new)
262
+
263
+ # You need to know what to do here... somehow get the methods into rhw scope of your tests.
264
+ # Something like this maybe?
265
+ include SanitizeEmail::TestHelpers
266
+ # Look here to see what it gives you:
267
+ # https://github.com/pboling/sanitize_email/blob/master/lib/sanitize_email/test_helpers.rb
268
+
120
269
  ## Deprecations
121
270
 
122
271
  Sometimes things get deprecated (meaning they still work, but are noisy about it). If this happens to you, and you like your head in the sand, call this number:
@@ -20,8 +20,17 @@ module SanitizeEmail
20
20
  else
21
21
  raise "Please use the 0.X.X versions of sanitize_email for Rails 2.X and below."
22
22
  end
23
- elsif defined?(Mailer) && Mailer.respond_to?(:register_interceptor)
24
- Mailer.register_interceptor(SanitizeEmail::Bleach.new)
23
+ else
24
+ if defined?(Mailer)
25
+ mailer = Mailer
26
+ elsif defined?(Mail)
27
+ mailer = Mail
28
+ end
29
+ if mailer.respond_to?(:register_interceptor)
30
+ mailer.register_interceptor(SanitizeEmail::Bleach.new)
31
+ else
32
+ warn "SanitizeEmail was unable to detect a compatible Mail class to register and interceptor on."
33
+ end
25
34
  end
26
35
 
27
36
  def self.[](key)
@@ -36,17 +45,17 @@ module SanitizeEmail
36
45
  # NOTE: Deprecated method
37
46
  # We have to actually define because we can't deprecate methods that are hooked up via method_missing
38
47
  def self.sanitized_recipients
39
- SanitizeEmail[:sanitized_recipients]
48
+ SanitizeEmail::Config.config[:sanitized_recipients]
40
49
  end
41
50
 
42
51
  # NOTE: Deprecated method
43
52
  # We have to actually define because we can't deprecate methods that are hooked up via method_missing
44
53
  def self.local_environments
45
- SanitizeEmail[:local_environments]
54
+ SanitizeEmail::Config.config[:local_environments]
46
55
  end
47
56
 
48
57
  def self.activate?(message)
49
- proc = SanitizeEmail[:activation_proc]
58
+ proc = SanitizeEmail::Config.config[:activation_proc]
50
59
  proc.call(message) if proc.respond_to?(:call)
51
60
  end
52
61
 
@@ -11,7 +11,7 @@ module SanitizeEmail
11
11
 
12
12
  def initialize(args = {})
13
13
  # Not using extract_options! because non-rails compatibility is a goal
14
- @engage = args[:engage] || SanitizeEmail[:engage]
14
+ @engage = args[:engage] || SanitizeEmail::Config.config[:engage]
15
15
  end
16
16
 
17
17
  # If all recipient addresses are white-listed the field is left alone.
@@ -29,15 +29,26 @@ module SanitizeEmail
29
29
  end
30
30
 
31
31
  # This method will be called by the Hook to determine if an override should occur
32
- # There are three ways SanitizeEmail can be turned on; in order of precedence they are:
32
+ # There are four ways SanitizeEmail can be turned on; in order of precedence they are:
33
33
  #
34
34
  # 1. SanitizeEmail.force_sanitize = true # by default it is nil
35
+ # Only useful for local context. Inside a method where you will be sending an email,
36
+ # set SanitizeEmail.force_sanitize = true just prior to delivering it. Also useful in the console.
35
37
  # 2. Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true)) # by default it is nil
36
- # 3. SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } be default it is false
38
+ # If SanitizeEmail seems to not be sanitizing you have probably not registered the interceptor. SanitizeEmail tries to do this for you.
39
+ # Note: If you are working in an environment that has a Mail or Mailer class that uses the register_interceptor API, the interceptor will already have been registered by SanitizeEmail (however, note lack of :engage => true):
40
+ # Mail.register_interceptor(SanitizeEmail::Bleach.new
41
+ # Without :engage => true the interceptor is inactive, and will require engaging via one of the other methods.
42
+ # As an example you could do the following to engage SanitizeEmail:
43
+ # SanitizeEmail::Config.configure {|config| config[:engage] = true }
44
+ # 3. SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } # by default it is false
45
+ # If you don't need to compute anything, then don't use the Proc, go with the next option.
46
+ # 4. SanitizeEmail::Config.configure {|config| config[:engage] = true } # by default it is nil
37
47
  #
38
48
  # Note: Number 1 is the method used by the SanitizeEmail.sanitary block
39
- # Note: Number 2 would not be used unless you setup your own register_interceptor)
40
- # If installed but not configured, sanitize email DOES NOTHING. Until configured the defaults leave it turned off.
49
+ # Note: Number 2 See note accompanying 2: you may need to setup your own register_interceptor
50
+ #
51
+ # If installed but not configured, sanitize_email DOES NOTHING. Until configured the defaults leave it turned off.
41
52
  def sanitize_engaged?(message)
42
53
 
43
54
  # Don't sanitize the message if it will not be delivered
@@ -15,11 +15,11 @@ module SanitizeEmail
15
15
 
16
16
  def initialize(message, args = {})
17
17
  # Not using extract_options! because non-rails compatibility is a goal
18
- @sanitized_to = args[:sanitized_to] || SanitizeEmail[:sanitized_to]
19
- @sanitized_cc = args[:sanitized_cc] || SanitizeEmail[:sanitized_cc]
20
- @sanitized_bcc = args[:sanitized_bcc] || SanitizeEmail[:sanitized_bcc]
21
- @good_list = args[:good_list] || SanitizeEmail[:good_list] || []
22
- @bad_list = args[:bad_list] || SanitizeEmail[:bad_list] || []
18
+ @sanitized_to = args[:sanitized_to] || SanitizeEmail::Config.config[:sanitized_to]
19
+ @sanitized_cc = args[:sanitized_cc] || SanitizeEmail::Config.config[:sanitized_cc]
20
+ @sanitized_bcc = args[:sanitized_bcc] || SanitizeEmail::Config.config[:sanitized_bcc]
21
+ @good_list = args[:good_list] || SanitizeEmail::Config.config[:good_list] || []
22
+ @bad_list = args[:bad_list] || SanitizeEmail::Config.config[:bad_list] || []
23
23
  @overridden_to = self.to_override(message.to)
24
24
  @overridden_cc = self.cc_override(message.cc)
25
25
  @overridden_bcc = self.bcc_override(message.bcc)
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2008-13 Peter H. Boling of RailsBling.com
2
2
  # Released under the MIT license
3
3
  module SanitizeEmail
4
- VERSION = '1.0.11'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanitize_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.11
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: