redis_mail 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rspec
19
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ ---
2
+ language: ruby
3
+ script: bundle exec rspec
4
+ rvm:
5
+ - 1.9.3
6
+ services:
7
+ - redis-server
8
+ notifications:
9
+ email: false
10
+ campfire:
11
+ rooms:
12
+ - secure: ! 'j8CJevgIDpE/cQwKm9z5uVY8nXmvQFOoQP/OIieEBMPd7IaSv6YVNMmjHju3
13
+
14
+ rKVL0471g0/+hfK1etfxeaygHjLVg1GmOnYH/CK97l3I3BnP5pEu7zz0wSs3
15
+
16
+ 5Legz1OnsK11U1FAjfSkv088V3/IgyTVegU/SaSiSi03uUsRSVs='
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_mail.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 PeopleAdmin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # RedisMail
2
+
3
+ A Redis-backed `delivery_method` for the Mail gem (and therefore, Rails'
4
+ ActionMailer).
5
+
6
+ If your tests run in the same process as the code you are testing, use the
7
+ :test delivery method (`Mail::TestMailer`) instead.
8
+
9
+ However, in acceptance test scenarios, your test code often runs in a separate
10
+ process from the code being tested. This makes the `Mail::TestMailer.deliveries`
11
+ in-memory array useless.
12
+
13
+ The :file delivery method (`Mail::FileDelivery`) is another option, however it
14
+ stores all messages to a recipient in a single file, with no easy way to parse
15
+ the messages individually.
16
+
17
+ RedisMail allows you to capture e-mails sent from one process (your application
18
+ under test) and make assertions in another (your test code). Each message
19
+ can be retrieived and parsed individually.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ gem 'redis_mail'
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install redis_mail
34
+
35
+ ## Configre your application
36
+
37
+ ### Rails 3.x +
38
+
39
+ Configure ActionMailer to use RedisMail. In *config/environments/test.rb*:
40
+
41
+ config.action_mailer.delivery_method = RedisMail::Deliverer
42
+
43
+ Set your connection to redis. In *config/initializers/redismail.rb*:
44
+
45
+ RedisMail.redis = Redis.new(:host => myredis.host.com)
46
+
47
+ ### Outside of Rails (using Mail gem)
48
+
49
+ Set the delivery method on the mail message:
50
+
51
+ message = Mail.new(:to => "someone@example.com", :subject => "Hello")
52
+ message.delivery_method(RedisMail::Deliverer, {})
53
+
54
+ ## Retrieving message information
55
+
56
+ ### RedisMail.clear_all
57
+
58
+ Clears all received messages for any recipient. Run this between each test!
59
+
60
+ ### RedisMail.clear_mailbox(recipient)
61
+
62
+ Clears all recieved messages for a specific recipient.
63
+
64
+ ### RedisMail.delivieries_count
65
+
66
+ Total number of messages delivered to any recipient.
67
+
68
+ Example usage:
69
+
70
+ RedisMail.clear_all
71
+ invoke_action_in_application_that_sends_two_emails
72
+ raise "Expected emails missing" unless RedisMail.deliveries_count == 2
73
+
74
+ ### RedisMail.delivieries_count_to(recipient)
75
+
76
+ Total number of messages delivered to a specific recipient.
77
+
78
+ ### RedisMail.delivieries
79
+
80
+ An array of all messages delivered to any receipient. Each message can be
81
+ parsed using classes provided by the `Mail` gem, for finer grained assertions.
82
+
83
+ Example usage:
84
+
85
+ RedisMail.clear_all
86
+ invoke_action_that_sends_welcome_email
87
+ sent_message = Mail.new(RedisMail.deliveries.first)
88
+ raise "Unexpected subject" unless sent_message.subject == "Welcome!"
89
+
90
+ ### RedisMail.delivieries_to
91
+
92
+ An array of messages delivered to a given recipient. Messages are ordered from
93
+ oldest to newest.
94
+
95
+ ### RedisMail.mailboxes
96
+
97
+ An array of recipient addresses that can be used in calls to `deliveries_to`,
98
+ `deliveries_count_to`, or `clear_mailbox`.
99
+
100
+ ## Contributing
101
+
102
+ 1. [Fork it](https://github.com/PeopleAdmin/redis_mail/fork_select)
103
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
104
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
105
+ 4. Push to the branch (`git push origin my-new-feature`)
106
+ 5. [Create new Pull Request](https://github.com/PeopleAdmin/redis_mail/pull/new/master)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
1
+ require 'redis-namespace'
2
+ module RedisMail
3
+ class Deliverer
4
+ attr :settings
5
+
6
+ def initialize(values)
7
+ @settings = values
8
+ end
9
+
10
+ def deliver!(mail)
11
+ mail.destinations.uniq.each do |to|
12
+ RedisMail.deliver to, mail.to_s
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module RedisMail
2
+ VERSION = "0.8.0"
3
+ end
data/lib/redis_mail.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "redis_mail/version"
2
+ require "redis_mail/deliverer"
3
+
4
+ module RedisMail
5
+ extend self
6
+
7
+ def redis=(server)
8
+ @redis = Redis::Namespace.new(:redis_mail, redis: server)
9
+ end
10
+
11
+ def redis
12
+ raise "RedisMail.redis must be set to a Redis connection" unless @redis
13
+ @redis
14
+ end
15
+
16
+ def clear_mailbox(recipient)
17
+ redis.del mailbox_key(recipient)
18
+ redis.srem :mailboxes, recipient
19
+ end
20
+
21
+ def clear_all
22
+ mailboxes.reduce(false) do |cleared,mailbox|
23
+ clear_mailbox(mailbox) || cleared
24
+ end
25
+ end
26
+
27
+ def mailboxes
28
+ redis.smembers :mailboxes
29
+ end
30
+
31
+ def deliveries_to(recipient)
32
+ redis.lrange mailbox_key(recipient), 0, -1
33
+ end
34
+
35
+ def deliveries
36
+ mailboxes.map{|recipient| deliveries_to(recipient)}.flatten
37
+ end
38
+
39
+ def deliveries_count
40
+ mailboxes.reduce(0){|sum, recipient|
41
+ sum + redis.llen(mailbox_key(recipient))
42
+ }
43
+ end
44
+
45
+ def deliveries_count_to(recipient)
46
+ redis.llen(mailbox_key(recipient))
47
+ end
48
+
49
+ def deliver(recipient, message)
50
+ redis.sadd :mailboxes, recipient
51
+ redis.rpush mailbox_key(recipient), message
52
+ end
53
+
54
+ private
55
+
56
+ def mailbox_key(recipient)
57
+ "mailbox:#{recipient}"
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_mail/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "redis_mail"
8
+ gem.version = RedisMail::VERSION
9
+ gem.authors = ["Joshua Flanagan"]
10
+ gem.email = ["jflanagan@peopleadmin.com"]
11
+ gem.description = %q{A Redis-backed delivery_method for Mail gem}
12
+ gem.summary = %q{Stores emails in Redis for easy retrieval in test scenarios.}
13
+ gem.homepage = "https://github.com/PeopleAdmin/redis_mail"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "redis", ">= 2.2.0"
20
+ gem.add_dependency "redis-namespace"
21
+ gem.add_development_dependency "mail"
22
+ gem.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,75 @@
1
+ require 'mail'
2
+ require 'redis'
3
+ require 'redis_mail'
4
+
5
+ describe RedisMail::Deliverer do
6
+ let(:redis) { RedisMail.redis = Redis.new.tap {|r| r.select 15} }
7
+
8
+ before { redis.flushdb }
9
+
10
+ context "delivering a message" do
11
+ let(:sent_message) {
12
+ Mail.new {
13
+ to "someone@example.com"
14
+ to << "another@example.com"
15
+ from "author@example.com"
16
+ subject "Hello world"
17
+ body "This is the test email"
18
+ cc "interested@example.com"
19
+ }
20
+ }
21
+
22
+ before do
23
+ sent_message.delivery_method(RedisMail::Deliverer, {})
24
+ sent_message.deliver!
25
+ end
26
+
27
+ it "creates a mailbox for tracking messages to each recipient" do
28
+ mailboxes = redis.smembers "redis_mail:mailboxes"
29
+ mailboxes.should have(3).items
30
+ mailboxes.should include "someone@example.com"
31
+ mailboxes.should include "another@example.com"
32
+ mailboxes.should include "interested@example.com"
33
+ end
34
+
35
+ it "stores the entire message for each recipient" do
36
+ sent = redis.lpop "redis_mail:mailbox:someone@example.com"
37
+ sent.should_not be_nil
38
+ sent.should == sent_message.to_s
39
+
40
+ sent = redis.lpop "redis_mail:mailbox:another@example.com"
41
+ sent.should_not be_nil
42
+ sent.should == sent_message.to_s
43
+
44
+ sent = redis.lpop "redis_mail:mailbox:interested@example.com"
45
+ sent.should_not be_nil
46
+ sent.should == sent_message.to_s
47
+ end
48
+
49
+ context "delivering multiple message to same recipient" do
50
+ let(:second_message) { Mail.new to: "someone@example.com", subject: "second" }
51
+ let(:third_message) { Mail.new to: "someone@example.com", subject: "third" }
52
+
53
+ before do
54
+ [second_message, third_message].each do |message|
55
+ message.delivery_method(RedisMail::Deliverer, {redis: redis})
56
+ message.deliver!
57
+ end
58
+ end
59
+
60
+ it "stores the messages in chronological order" do
61
+ sent = redis.lpop "redis_mail:mailbox:someone@example.com"
62
+ Mail.new(sent).should == sent_message
63
+
64
+ sent = redis.lpop "redis_mail:mailbox:someone@example.com"
65
+ Mail.new(sent).should == second_message
66
+
67
+ sent = redis.lpop "redis_mail:mailbox:someone@example.com"
68
+ Mail.new(sent).should == third_message
69
+
70
+ sent = redis.lpop "redis_mail:mailbox:someone@example.com"
71
+ sent.should be_nil
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,170 @@
1
+ require 'mail'
2
+ require 'redis'
3
+ require 'redis_mail'
4
+
5
+ describe RedisMail do
6
+ let(:redis) { RedisMail.redis = Redis.new.tap {|r| r.select 15} }
7
+ before { redis.flushdb }
8
+
9
+ describe ".clear_mailbox" do
10
+ before do
11
+ send "someone@example.com", "First"
12
+ send "someone@example.com", "Second"
13
+ send "someone@example.com", "Third"
14
+ end
15
+
16
+ it "removes all messages from the given mailbox" do
17
+ redis.llen("redis_mail:mailbox:someone@example.com").should == 3
18
+ RedisMail.clear_mailbox "someone@example.com"
19
+ redis.llen("redis_mail:mailbox:someone@example.com").should == 0
20
+ end
21
+
22
+ it "removes the mailbox" do
23
+ redis.smembers("redis_mail:mailboxes").should include("someone@example.com")
24
+ RedisMail.clear_mailbox "someone@example.com"
25
+ redis.smembers("redis_mail:mailboxes").should_not include("someone@example.com")
26
+ end
27
+
28
+ it "returns true when messages are cleared" do
29
+ RedisMail.clear_mailbox("someone@example.com").should be_true
30
+ end
31
+ end
32
+
33
+ describe ".clear_all" do
34
+ before do
35
+ send "someone@example.com", "First"
36
+ send "another@example.com", "Second"
37
+ send "different@example.com", "Third"
38
+ end
39
+
40
+ it "removes all messages from all given mailboxes" do
41
+ redis.llen("redis_mail:mailbox:someone@example.com").should == 1
42
+ redis.llen("redis_mail:mailbox:another@example.com").should == 1
43
+ redis.llen("redis_mail:mailbox:different@example.com").should == 1
44
+ RedisMail.clear_all
45
+ redis.llen("redis_mail:mailbox:someone@example.com").should == 0
46
+ redis.llen("redis_mail:mailbox:another@example.com").should == 0
47
+ redis.llen("redis_mail:mailbox:different@example.com").should == 0
48
+ end
49
+
50
+ it "removes the mailboxes" do
51
+ redis.scard("redis_mail:mailboxes").should == 3
52
+ RedisMail.clear_all
53
+ redis.scard("redis_mail:mailboxes").should == 0
54
+ end
55
+
56
+ it "returns true when messages are cleared" do
57
+ RedisMail.clear_all.should be_true
58
+ end
59
+ end
60
+
61
+ describe ".deliveries_to" do
62
+ before do
63
+ send "someone@example.com", "Beta"
64
+ send "another@example.com", "Gamma"
65
+ send "someone@example.com", "Alpha"
66
+ end
67
+
68
+ it "returns the messages for a given recipient, in chronological order" do
69
+ deliveries = RedisMail.deliveries_to "someone@example.com"
70
+ deliveries.should have(2).items
71
+ deliveries[0].should include("Beta")
72
+ deliveries[1].should include("Alpha")
73
+ end
74
+ end
75
+
76
+ describe ".deliveries" do
77
+ before do
78
+ send "someone@example.com", "Beta"
79
+ send "another@example.com", "Gamma"
80
+ send "someone@example.com", "Alpha"
81
+ end
82
+
83
+ it "returns all messages received" do
84
+ deliveries = RedisMail.deliveries
85
+ deliveries.should have(3).items
86
+ deliveries.any?{|d| d.include?("Alpha")}.should be_true
87
+ deliveries.any?{|d| d.include?("Beta")}.should be_true
88
+ deliveries.any?{|d| d.include?("Gamma")}.should be_true
89
+ end
90
+ end
91
+
92
+ describe ".deliveries_count" do
93
+ before do
94
+ send "someone@example.com", "Beta"
95
+ send "another@example.com", "Gamma"
96
+ send "someone@example.com", "Alpha"
97
+ end
98
+
99
+ it "returns the number of messages received" do
100
+ RedisMail.deliveries_count.should == 3
101
+ end
102
+ end
103
+
104
+ describe ".deliveries_count_to" do
105
+ before do
106
+ send "someone@example.com", "Beta"
107
+ send "another@example.com", "Gamma"
108
+ send "someone@example.com", "Alpha"
109
+ end
110
+
111
+ it "returns the number of messages sent to a given address" do
112
+ RedisMail.deliveries_count_to("someone@example.com").should == 2
113
+ end
114
+ end
115
+
116
+ describe ".mailboxes" do
117
+ before do
118
+ send "someone@example.com", "Beta"
119
+ send "another@example.com", "Gamma"
120
+ send "someone@example.com", "Alpha"
121
+ end
122
+
123
+ it "returns a list of email address which have received messages" do
124
+ mailboxes = RedisMail.mailboxes
125
+ mailboxes.should have(2).recipients
126
+ mailboxes.should include("someone@example.com")
127
+ mailboxes.should include("another@example.com")
128
+ end
129
+ end
130
+
131
+ context "when no messages have been sent" do
132
+ it "returns an empty array for .mailboxes" do
133
+ RedisMail.mailboxes.should == []
134
+ end
135
+
136
+ it "returns an empty array for .deliveries" do
137
+ RedisMail.deliveries.should == []
138
+ end
139
+
140
+ it "returns an empty array for .deliveries_to" do
141
+ RedisMail.deliveries_to("someone@example.com").should == []
142
+ end
143
+
144
+ it "returns 0 for .deliveries_count" do
145
+ RedisMail.deliveries_count.should == 0
146
+ end
147
+
148
+ it "returns 0 for .deliveries_count_to" do
149
+ RedisMail.deliveries_count_to("someone@example.com").should == 0
150
+ end
151
+
152
+ it "returns false for .clear_mailbox" do
153
+ RedisMail.clear_mailbox("someone@example.com").should be_false
154
+ end
155
+
156
+ it "returns false for .clear_all" do
157
+ RedisMail.clear_all.should be_false
158
+ end
159
+ end
160
+
161
+ def send(recipient, subject)
162
+ message = Mail.new {
163
+ to recipient
164
+ from "author@example.com"
165
+ subject subject
166
+ }
167
+ message.delivery_method(RedisMail::Deliverer, {redis: redis})
168
+ message.deliver!
169
+ end
170
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_mail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joshua Flanagan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ prerelease: false
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.2.0
22
+ none: false
23
+ type: :runtime
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: 2.2.0
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis-namespace
32
+ prerelease: false
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ type: :runtime
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: mail
48
+ prerelease: false
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ type: :development
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ none: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ prerelease: false
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ none: false
71
+ type: :development
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ description: A Redis-backed delivery_method for Mail gem
79
+ email:
80
+ - jflanagan@peopleadmin.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/redis_mail.rb
92
+ - lib/redis_mail/deliverer.rb
93
+ - lib/redis_mail/version.rb
94
+ - redis_mail.gemspec
95
+ - spec/deliverer_spec.rb
96
+ - spec/redis_mail_spec.rb
97
+ homepage: https://github.com/PeopleAdmin/redis_mail
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ none: false
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ none: false
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.24
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Stores emails in Redis for easy retrieval in test scenarios.
121
+ test_files:
122
+ - spec/deliverer_spec.rb
123
+ - spec/redis_mail_spec.rb