mail-store-agent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.mkd ADDED
@@ -0,0 +1,51 @@
1
+ # MailStoreAgent
2
+
3
+ ## Background
4
+
5
+ Most websites that authenticate users require some form of user account self-management.
6
+ Users need to register with e-mail and password, verify their e-mail address,
7
+ and to occasionally reset their e-mail and password.
8
+
9
+ I like to delegate this to other services with [OpenID](http://en.wikipedia.org/wiki/OpenID)
10
+ and [OAuth](http://en.wikipedia.org/wiki/Oauth),
11
+ but not all clients agree to delegate ownership of their customers' authentication records.
12
+
13
+ So I'm often stuck with implementing these boilerplace features yet again,
14
+ each of the above use-cases require sending out e-mails to confirm or facilitate.
15
+ Testing this can be a pain-in-the-butt.
16
+
17
+ Fortunately, [Mikel Lindsaar](https://github.com/mikel)'s
18
+ [Mail](https://github.com/mikel/mail) gem
19
+ has a simple and effective way to store and not send e-mails in order to facilitate test scripts.
20
+
21
+ Mail::TestMailer
22
+
23
+ Mail.defaults do
24
+ delivery_method :test
25
+ end
26
+
27
+ # send a few mails ...
28
+
29
+ Mail::TestMailer.deliveries.is_a? Array # ==> true
30
+ Mail::TestMailer.deliveries.first.is_a? Mail::Message # ==> true
31
+
32
+ I want to take this a step further so that I can verify that mail got sent to the right people in the right order:
33
+
34
+ ## SYNOPSIS
35
+
36
+ require 'mail'
37
+ require 'mail-store-agent'
38
+
39
+ Mail.defaults do
40
+ delivery_method :test
41
+ end
42
+
43
+ Mail::TestMailer.deliveries = MailStoreAgent.new
44
+
45
+ # send some mail to someone@someplace.com ...
46
+
47
+ Mail::TestMailer.deliveries.get('someone@someplace.com').is_a? Mail::Message # or nil
48
+
49
+ ## LICENSE
50
+ * <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">mail-store-agent.gem</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://westside-consulting.com/" property="cc:attributionName" rel="cc:attributionURL">Lawrence Siden</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="https://github.com/lsiden" rel="dct:source">github.com</a>.
51
+ * [Ruby License](http://www.ruby-lang.org/en/LICENSE.txt)
@@ -0,0 +1,35 @@
1
+
2
+ class MailStoreAgent < Array
3
+
4
+ def initialize
5
+ @queues = {}
6
+ @next_unsorted = 0 # index of next unsorted e-mail
7
+ end
8
+
9
+ def accounts
10
+ self.sort_mail!
11
+ return @queues.keys
12
+ end
13
+
14
+ def get(address)
15
+ self.sort_mail!
16
+ return @queues[address] ? @queues[address].shift : nil
17
+ end
18
+
19
+ protected
20
+ def sort_mail!
21
+ self[@next_unsorted..self.length].each do |email|
22
+ #raise "MailStoreAgent is intended to be used as value for Mail::TestMailer.deliveries=(). See README" \
23
+ # unless email.respond_to?(:destinations) && email.destinations.respond_to?(:each)
24
+
25
+ if email.respond_to?(:destinations) && email.destinations.respond_to?(:each) then
26
+ email.destinations.each do |dest|
27
+ @queues[dest] = [] if @queues[dest].nil?
28
+ @queues[dest].push email
29
+ end
30
+ end
31
+ end
32
+ @next_unsorted = self.length
33
+ end
34
+
35
+ end
@@ -0,0 +1,98 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'mail'
4
+
5
+ require File.join(File.dirname(__FILE__), %w[ .. lib mail-store-agent ])
6
+
7
+ describe MailStoreAgent do
8
+ before(:all) do
9
+ Mail.defaults do
10
+ delivery_method :test # don't use '='!
11
+ Mail::TestMailer.deliveries = MailStoreAgent.new
12
+ end
13
+ end
14
+
15
+ it 'is an instance of Array' do
16
+ MailStoreAgent.is_a? Array
17
+ end
18
+
19
+ it 'can be given as argument to Mail::TestMailer#deliveries=()' do
20
+ Mail::TestMailer.deliveries.is_a? MailStoreAgent
21
+ Mail::TestMailer.deliveries.should have(0).messages
22
+
23
+ end
24
+
25
+ it 'can store mail' do
26
+ mail = Mail.deliver do
27
+ to 'tarzan@jungle.com'
28
+ from 'tarzan@jungle.com'
29
+ subject 'just testing'
30
+ body 'get this?'
31
+ end
32
+ mail.should be_instance_of Mail::Message
33
+ mail.perform_deliveries.should == true
34
+ mail.errors.each {|e| puts e}
35
+ Mail::TestMailer.deliveries.should have(1).message
36
+
37
+ Mail.deliver do
38
+ to 'tarzan@jungle.com'
39
+ from 'tarzan@jungle.com'
40
+ subject 'still testing'
41
+ body 'get this, too?'
42
+ end
43
+
44
+ Mail.deliver do
45
+ to 'jane@jungle.com'
46
+ from 'tarzan@jungle.com'
47
+ subject 'testing now'
48
+ body 'get this?'
49
+ end
50
+
51
+ mail = Mail.deliver do
52
+ to 'tarzan@jungle.com'
53
+ from 'tarzan@jungle.com'
54
+ subject 'keep on testing'
55
+ body 'what about this?'
56
+ end
57
+ Mail::TestMailer.deliveries.should have(4).messages
58
+ end
59
+
60
+ it 'knows what accounts have been sent mail' do
61
+ Mail::TestMailer.deliveries.accounts == ['tarzan@jungle.com', 'jane@jungle.com']
62
+ end
63
+
64
+ it 'can return next e-mail for any given address' do
65
+ mail = Mail::TestMailer.deliveries.get('jane@jungle.com')
66
+ mail.should be_kind_of Mail::Message
67
+ mail.to[0].should == 'jane@jungle.com'
68
+ mail.subject.should == 'testing now'
69
+ end
70
+
71
+ it 'returns nil if there is no more mail for address' do
72
+ Mail::TestMailer.deliveries.get('jane@jungle.com').should be_nil
73
+ end
74
+
75
+ it 'returns nil if given a non-existent address' do
76
+ Mail::TestMailer.deliveries.get('blah@foo.bar').should be_nil
77
+ end
78
+
79
+ it 'returns emails in order' do
80
+ Mail::TestMailer.deliveries.get('tarzan@jungle.com').subject.should == 'just testing'
81
+ Mail::TestMailer.deliveries.get('tarzan@jungle.com').subject.should == 'still testing'
82
+ Mail::TestMailer.deliveries.get('tarzan@jungle.com').subject.should == 'keep on testing'
83
+ Mail::TestMailer.deliveries.get('tarzan@jungle.com').should be_nil
84
+ end
85
+
86
+ it 'does not delete the mail' do
87
+ Mail::TestMailer.deliveries.should have(4).entries
88
+ end
89
+
90
+ it 'acts like Array and ignores contents that are not instances of Mail::Message' do
91
+ Mail::TestMailer.deliveries.push :foo
92
+ Mail::TestMailer.deliveries.push 'baz'
93
+ Mail::TestMailer.deliveries.push Object.new
94
+ Mail::TestMailer.deliveries.accounts == ['tarzan@jungle.com', 'jane@jungle.com']
95
+ Mail::TestMailer.deliveries.get('tarzan@jungle.com').should be_nil
96
+ Mail::TestMailer.deliveries.should have(7).items
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail-store-agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Larry Siden, Westside Consulting LLC
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mail
16
+ requirement: &79348610 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *79348610
25
+ description: Provides per-email-account sequential access to Mail::TestMailer.deliveries
26
+ email: lsiden@westside-consulting.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - README.mkd
32
+ - lib/mail-store-agent.rb
33
+ - spec/mail_store_agent_spec.rb
34
+ homepage: http://github.com/lsiden/mail-store-agent/
35
+ licenses:
36
+ - Creative Commons Share-Alike Unported
37
+ - Ruby License
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.10
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Tiny mail agent for extracting delivered mail messages from Mail::TestMailer
60
+ test_files: []