hectoregm-email_spec 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +65 -0
- data/MIT-LICENSE.txt +19 -0
- data/README.rdoc +126 -0
- data/Rakefile +51 -0
- data/examples/rails_root/app/controllers/application.rb +15 -0
- data/examples/rails_root/app/controllers/welcome_controller.rb +9 -0
- data/examples/rails_root/app/helpers/application_helper.rb +3 -0
- data/examples/rails_root/app/helpers/welcome_helper.rb +2 -0
- data/examples/rails_root/app/models/user.rb +2 -0
- data/examples/rails_root/app/models/user_mailer.rb +12 -0
- data/examples/rails_root/app/views/user_mailer/signup.erb +3 -0
- data/examples/rails_root/app/views/welcome/confirm.html.erb +1 -0
- data/examples/rails_root/app/views/welcome/index.html.erb +8 -0
- data/examples/rails_root/app/views/welcome/signup.html.erb +1 -0
- data/examples/rails_root/config/boot.rb +109 -0
- data/examples/rails_root/config/database.yml +22 -0
- data/examples/rails_root/config/environment.rb +22 -0
- data/examples/rails_root/config/environments/development.rb +17 -0
- data/examples/rails_root/config/environments/production.rb +24 -0
- data/examples/rails_root/config/environments/test.rb +33 -0
- data/examples/rails_root/config/initializers/inflections.rb +10 -0
- data/examples/rails_root/config/initializers/mime_types.rb +5 -0
- data/examples/rails_root/config/initializers/new_rails_defaults.rb +17 -0
- data/examples/rails_root/config/routes.rb +43 -0
- data/examples/rails_root/cucumber.yml +1 -0
- data/examples/rails_root/db/migrate/20090125013728_create_users.rb +11 -0
- data/examples/rails_root/db/schema.rb +19 -0
- data/examples/rails_root/doc/README_FOR_APP +5 -0
- data/examples/rails_root/features/errors.feature +42 -0
- data/examples/rails_root/features/example.feature +59 -0
- data/examples/rails_root/features/step_definitions/email_steps.rb +74 -0
- data/examples/rails_root/features/step_definitions/user_steps.rb +23 -0
- data/examples/rails_root/features/step_definitions/webrat_steps.rb +104 -0
- data/examples/rails_root/features/support/env.rb +21 -0
- data/examples/rails_root/public/404.html +30 -0
- data/examples/rails_root/public/422.html +30 -0
- data/examples/rails_root/public/500.html +33 -0
- data/examples/rails_root/public/dispatch.rb +10 -0
- data/examples/rails_root/public/favicon.ico +0 -0
- data/examples/rails_root/public/images/rails.png +0 -0
- data/examples/rails_root/public/javascripts/application.js +2 -0
- data/examples/rails_root/public/javascripts/controls.js +963 -0
- data/examples/rails_root/public/javascripts/dragdrop.js +973 -0
- data/examples/rails_root/public/javascripts/effects.js +1128 -0
- data/examples/rails_root/public/javascripts/prototype.js +4320 -0
- data/examples/rails_root/public/robots.txt +5 -0
- data/examples/rails_root/script/about +4 -0
- data/examples/rails_root/script/autospec +5 -0
- data/examples/rails_root/script/console +3 -0
- data/examples/rails_root/script/cucumber +7 -0
- data/examples/rails_root/script/dbconsole +3 -0
- data/examples/rails_root/script/destroy +3 -0
- data/examples/rails_root/script/generate +3 -0
- data/examples/rails_root/script/performance/benchmarker +3 -0
- data/examples/rails_root/script/performance/profiler +3 -0
- data/examples/rails_root/script/performance/request +3 -0
- data/examples/rails_root/script/plugin +3 -0
- data/examples/rails_root/script/process/inspector +3 -0
- data/examples/rails_root/script/process/reaper +3 -0
- data/examples/rails_root/script/process/spawner +3 -0
- data/examples/rails_root/script/runner +3 -0
- data/examples/rails_root/script/server +3 -0
- data/examples/rails_root/script/spec +5 -0
- data/examples/rails_root/script/spec_server +125 -0
- data/examples/rails_root/spec/controllers/welcome_controller_spec.rb +15 -0
- data/examples/rails_root/spec/model_factory.rb +6 -0
- data/examples/rails_root/spec/models/user_mailer_spec.rb +58 -0
- data/examples/rails_root/spec/models/user_spec.rb +5 -0
- data/examples/rails_root/spec/rcov.opts +2 -0
- data/examples/rails_root/spec/spec.opts +4 -0
- data/examples/rails_root/spec/spec_helper.rb +51 -0
- data/examples/rails_root/stories/all.rb +4 -0
- data/examples/rails_root/stories/helper.rb +3 -0
- data/examples/rails_root/vendor/plugins/email_spec/rails_generators/email_spec/email_spec_generator.rb +17 -0
- data/examples/rails_root/vendor/plugins/email_spec/rails_generators/email_spec/templates/email_steps.rb +74 -0
- data/install.rb +0 -0
- data/lib/email_spec/address_converter.rb +27 -0
- data/lib/email_spec/cucumber.rb +19 -0
- data/lib/email_spec/deliveries.rb +55 -0
- data/lib/email_spec/email_viewer.rb +70 -0
- data/lib/email_spec/helpers.rb +128 -0
- data/lib/email_spec/matchers.rb +122 -0
- data/lib/email_spec.rb +14 -0
- data/rails_generators/email_spec/email_spec_generator.rb +17 -0
- data/rails_generators/email_spec/templates/email_steps.rb +74 -0
- data/spec/email_spec/matchers_spec.rb +159 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +4 -0
- metadata +187 -0
@@ -0,0 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
ENV['RSPEC'] = 'true' # allows autotest to discover rspec
|
3
|
+
ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux
|
4
|
+
system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) ||
|
5
|
+
$stderr.puts("Unable to find autotest. Please install ZenTest or fix your PATH")
|
@@ -0,0 +1,125 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin
|
3
|
+
require 'rubygems'
|
4
|
+
require 'drb/drb'
|
5
|
+
require 'rbconfig'
|
6
|
+
require 'spec'
|
7
|
+
require 'optparse'
|
8
|
+
|
9
|
+
# This is based on Florian Weber's TDDMate
|
10
|
+
module Spec
|
11
|
+
module Runner
|
12
|
+
class RailsSpecServer
|
13
|
+
def run(argv, stderr, stdout)
|
14
|
+
$stdout = stdout
|
15
|
+
$stderr = stderr
|
16
|
+
|
17
|
+
unless ActiveRecord::Base.respond_to?(:clear_reloadable_connections!)
|
18
|
+
base = ActiveRecord::Base
|
19
|
+
def base.clear_reloadable_connections!
|
20
|
+
active_connections.each do |name, conn|
|
21
|
+
if conn.requires_reloading?
|
22
|
+
conn.disconnect!
|
23
|
+
active_connections.delete(name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ActiveRecord::Base.clear_reloadable_connections!
|
30
|
+
|
31
|
+
if ActionController.const_defined?(:Dispatcher)
|
32
|
+
dispatcher = ::ActionController::Dispatcher.new($stdout)
|
33
|
+
dispatcher.cleanup_application
|
34
|
+
elsif ::Dispatcher.respond_to?(:reset_application!)
|
35
|
+
::Dispatcher.reset_application!
|
36
|
+
else
|
37
|
+
raise "Application reloading failed"
|
38
|
+
end
|
39
|
+
if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
|
40
|
+
Fixtures.reset_cache
|
41
|
+
end
|
42
|
+
|
43
|
+
if ::ActiveSupport.const_defined?(:Dependencies)
|
44
|
+
::ActiveSupport::Dependencies.mechanism = :load
|
45
|
+
else
|
46
|
+
::Dependencies.mechanism = :load
|
47
|
+
end
|
48
|
+
|
49
|
+
require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
|
50
|
+
load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
|
51
|
+
|
52
|
+
if in_memory_database?
|
53
|
+
load "#{RAILS_ROOT}/db/schema.rb" # use db agnostic schema by default
|
54
|
+
ActiveRecord::Migrator.up('db/migrate') # use migrations
|
55
|
+
end
|
56
|
+
|
57
|
+
::Spec::Runner::CommandLine.run(
|
58
|
+
::Spec::Runner::OptionParser.parse(
|
59
|
+
argv,
|
60
|
+
$stderr,
|
61
|
+
$stdout
|
62
|
+
)
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def in_memory_database?
|
67
|
+
ENV["RAILS_ENV"] == "test" and
|
68
|
+
::ActiveRecord::Base.connection.class.to_s == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" and
|
69
|
+
::Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
puts "Loading Rails environment"
|
75
|
+
|
76
|
+
ENV["RAILS_ENV"] = "test"
|
77
|
+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
78
|
+
require 'dispatcher'
|
79
|
+
|
80
|
+
def restart_test_server
|
81
|
+
puts "restarting"
|
82
|
+
config = ::Config::CONFIG
|
83
|
+
ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
|
84
|
+
command_line = [ruby, $0, ARGV].flatten.join(' ')
|
85
|
+
exec(command_line)
|
86
|
+
end
|
87
|
+
|
88
|
+
def daemonize(pid_file = nil)
|
89
|
+
return yield if $DEBUG
|
90
|
+
pid = Process.fork{
|
91
|
+
Process.setsid
|
92
|
+
Dir.chdir(RAILS_ROOT)
|
93
|
+
trap("SIGINT"){ exit! 0 }
|
94
|
+
trap("SIGTERM"){ exit! 0 }
|
95
|
+
trap("SIGHUP"){ restart_test_server }
|
96
|
+
File.open("/dev/null"){|f|
|
97
|
+
STDERR.reopen f
|
98
|
+
STDIN.reopen f
|
99
|
+
STDOUT.reopen f
|
100
|
+
}
|
101
|
+
yield
|
102
|
+
}
|
103
|
+
puts "spec_server launched. (PID: %d)" % pid
|
104
|
+
File.open(pid_file,"w"){|f| f.puts pid } if pid_file
|
105
|
+
exit! 0
|
106
|
+
end
|
107
|
+
|
108
|
+
options = Hash.new
|
109
|
+
opts = OptionParser.new
|
110
|
+
opts.on("-d", "--daemon"){|v| options[:daemon] = true }
|
111
|
+
opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
|
112
|
+
opts.parse!(ARGV)
|
113
|
+
|
114
|
+
puts "Ready"
|
115
|
+
exec_server = lambda {
|
116
|
+
trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
|
117
|
+
DRb.start_service("druby://127.0.0.1:8989", Spec::Runner::RailsSpecServer.new)
|
118
|
+
DRb.thread.join
|
119
|
+
}
|
120
|
+
|
121
|
+
if options[:daemon]
|
122
|
+
daemonize(options[:pid], &exec_server)
|
123
|
+
else
|
124
|
+
exec_server.call
|
125
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe WelcomeController do
|
4
|
+
|
5
|
+
describe "POST /signup (#signup)" do
|
6
|
+
it "should deliver the signup email" do
|
7
|
+
# expect
|
8
|
+
UserMailer.should_receive(:deliver_signup).with("email@example.com", "Jimmy Bean")
|
9
|
+
# when
|
10
|
+
post :signup, "Email" => "email@example.com", "Name" => "Jimmy Bean"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
# These two example groups are specifying the exact same behavior. However, the documentation style is different
|
4
|
+
# and the value that each one provides is different with various trade-offs. Run these examples with the specdoc
|
5
|
+
# formatter to get an idea of how they differ.
|
6
|
+
|
7
|
+
# Example of documenting the behaviour explicitly and expressing the intent in the example's sentence.
|
8
|
+
describe "Signup Email" do
|
9
|
+
include EmailSpec::Helpers
|
10
|
+
include EmailSpec::Matchers
|
11
|
+
include ActionController::UrlWriter
|
12
|
+
|
13
|
+
before(:all) do
|
14
|
+
@email = UserMailer.create_signup("jojo@yahoo.com", "Jojo Binks")
|
15
|
+
end
|
16
|
+
|
17
|
+
subject { @email }
|
18
|
+
|
19
|
+
it "should be delivered to the email passed in" do
|
20
|
+
should deliver_to("jojo@yahoo.com")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should contain the user's name in the mail body" do
|
24
|
+
@email.should have_body_text(/Jojo Binks/)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should contain a link to the confirmation page" do
|
28
|
+
@email.should have_body_text(/#{confirm_account_url}/)
|
29
|
+
end
|
30
|
+
|
31
|
+
it { should have_subject(/Account confirmation/) }
|
32
|
+
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
# In this example group more of the documentation is placed in the context trying to allow for more concise specs.
|
37
|
+
describe "Signup Email" do
|
38
|
+
include EmailSpec::Helpers
|
39
|
+
include EmailSpec::Matchers
|
40
|
+
include ActionController::UrlWriter
|
41
|
+
|
42
|
+
before(:all) do
|
43
|
+
@email = UserMailer.create_signup("jojo@yahoo.com", "Jojo Binks")
|
44
|
+
end
|
45
|
+
|
46
|
+
subject { @email }
|
47
|
+
|
48
|
+
it { should have_body_text(/#{confirm_account_url}/) }
|
49
|
+
it { should have_subject(/Account confirmation/) }
|
50
|
+
|
51
|
+
describe "sent with email address of 'jojo@yahoo.com', and users name 'Jojo Binks'" do
|
52
|
+
subject { @email }
|
53
|
+
it { should deliver_to("jojo@yahoo.com") }
|
54
|
+
it { should have_body_text(/Jojo Binks/) }
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
|
2
|
+
# from the project root directory.
|
3
|
+
ENV["RAILS_ENV"] = "test"
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
5
|
+
require 'spec'
|
6
|
+
require 'spec/rails'
|
7
|
+
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + "/model_factory.rb")
|
9
|
+
require (Rails.root + '/../../lib/email_spec.rb')
|
10
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
config.include(Fixjour)
|
13
|
+
# If you're not using ActiveRecord you should remove these
|
14
|
+
# lines, delete config/database.yml and disable :active_record
|
15
|
+
# in your config/boot.rb
|
16
|
+
#config.use_transactional_fixtures = true
|
17
|
+
#config.use_instantiated_fixtures = false
|
18
|
+
#config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
|
19
|
+
|
20
|
+
# == Fixtures
|
21
|
+
#
|
22
|
+
# You can declare fixtures for each example_group like this:
|
23
|
+
# describe "...." do
|
24
|
+
# fixtures :table_a, :table_b
|
25
|
+
#
|
26
|
+
# Alternatively, if you prefer to declare them only once, you can
|
27
|
+
# do so right here. Just uncomment the next line and replace the fixture
|
28
|
+
# names with your fixtures.
|
29
|
+
#
|
30
|
+
# config.global_fixtures = :table_a, :table_b
|
31
|
+
#
|
32
|
+
# If you declare global fixtures, be aware that they will be declared
|
33
|
+
# for all of your examples, even those that don't use them.
|
34
|
+
#
|
35
|
+
# You can also declare which fixtures to use (for example fixtures for test/fixtures):
|
36
|
+
#
|
37
|
+
# config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
|
38
|
+
#
|
39
|
+
# == Mock Framework
|
40
|
+
#
|
41
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
42
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
43
|
+
#
|
44
|
+
# config.mock_with :mocha
|
45
|
+
# config.mock_with :flexmock
|
46
|
+
# config.mock_with :rr
|
47
|
+
#
|
48
|
+
# == Notes
|
49
|
+
#
|
50
|
+
# For more information take a look at Spec::Runner::Configuration and Spec::Runner
|
51
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This generator adds email steps to the step definitions directory
|
2
|
+
generator_base = defined?(Rails) ? Rails::Generator::Base : RubiGen::Base
|
3
|
+
class EmailSpecGenerator < generator_base
|
4
|
+
def manifest
|
5
|
+
record do |m|
|
6
|
+
m.directory 'features/step_definitions'
|
7
|
+
m.file 'email_steps.rb', 'features/step_definitions/email_steps.rb'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def banner
|
14
|
+
"Usage: #{$0} email_spec"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
#Commonly used email steps
|
2
|
+
#
|
3
|
+
# To add your own steps make a custom_email_steps.rb
|
4
|
+
# The provided methods are:
|
5
|
+
#
|
6
|
+
# reset_mailer
|
7
|
+
# open_last_email
|
8
|
+
# visit_in_email
|
9
|
+
# unread_emails_for
|
10
|
+
# mailbox_for
|
11
|
+
# current_email
|
12
|
+
# open_email
|
13
|
+
# read_emails_for
|
14
|
+
# find_email
|
15
|
+
|
16
|
+
module EmailHelpers
|
17
|
+
def current_email_address
|
18
|
+
"quentin@example.com" # Replace with your a way to find your current_email. e.g current_user.email
|
19
|
+
end
|
20
|
+
end
|
21
|
+
World(EmailHelpers)
|
22
|
+
|
23
|
+
# Use this step to reset the e-mail queue within a scenario.
|
24
|
+
# This is done automatically before each scenario.
|
25
|
+
Given /^(?:a clear email queue|no emails have been sent)$/ do
|
26
|
+
reset_mailer
|
27
|
+
end
|
28
|
+
|
29
|
+
# Use this step to open the most recently sent e-mail.
|
30
|
+
When /^I open the email$/ do
|
31
|
+
open_email(current_email_address)
|
32
|
+
end
|
33
|
+
|
34
|
+
When /^I follow "(.*)" in the email$/ do |link|
|
35
|
+
visit_in_email(link)
|
36
|
+
end
|
37
|
+
|
38
|
+
Then /^I should receive (an|\d+) emails?$/ do |amount|
|
39
|
+
amount = 1 if amount == "an"
|
40
|
+
unread_emails_for(current_email_address).size.should == amount.to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
Then /^"([^']*?)" should receive (\d+) emails?$/ do |address, n|
|
44
|
+
unread_emails_for(address).size.should == n.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
Then /^"([^']*?)" should have (\d+) emails?$/ do |address, n|
|
48
|
+
mailbox_for(address).size.should == n.to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
Then /^"([^']*?)" should not receive an email$/ do |address|
|
52
|
+
find_email(address).should be_nil
|
53
|
+
end
|
54
|
+
|
55
|
+
Then /^I should see "(.*)" in the subject$/ do |text|
|
56
|
+
current_email.should have_subject(Regexp.new(text))
|
57
|
+
end
|
58
|
+
|
59
|
+
Then /^I should see "(.*)" in the email$/ do |text|
|
60
|
+
current_email.body.should =~ Regexp.new(text)
|
61
|
+
end
|
62
|
+
|
63
|
+
When %r{^"([^']*?)" opens? the email with subject "([^']*?)"$} do |address, subject|
|
64
|
+
open_email(address, :with_subject => subject)
|
65
|
+
end
|
66
|
+
|
67
|
+
When %r{^"([^']*?)" opens? the email with text "([^']*?)"$} do |address, text|
|
68
|
+
open_email(address, :with_text => text)
|
69
|
+
end
|
70
|
+
|
71
|
+
When /^I click the first link in the email$/ do
|
72
|
+
click_first_link_in_email
|
73
|
+
end
|
74
|
+
|
data/install.rb
ADDED
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EmailSpec
|
2
|
+
class AddressConverter
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
attr_accessor :converter
|
6
|
+
|
7
|
+
# The block provided to conversion should convert to an email
|
8
|
+
# address string or return the input untouched. For example:
|
9
|
+
#
|
10
|
+
# EmailSpec::AddressConverter.instance.conversion do |input|
|
11
|
+
# if input.is_a?(User)
|
12
|
+
# input.email
|
13
|
+
# else
|
14
|
+
# input
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
def conversion(&block)
|
19
|
+
self.converter = block
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert(input)
|
23
|
+
return input unless converter
|
24
|
+
converter.call(input)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# require this in your env.rb file after you require cucumber/rails/world
|
2
|
+
|
3
|
+
# Global Setup
|
4
|
+
ActionMailer::Base.delivery_method = :test unless ActionMailer::Base.delivery_method == :activerecord
|
5
|
+
ActionMailer::Base.perform_deliveries = true
|
6
|
+
|
7
|
+
Before do
|
8
|
+
# Scenario setup
|
9
|
+
ActionMailer::Base.deliveries.clear if ActionMailer::Base.delivery_method == :test
|
10
|
+
end
|
11
|
+
|
12
|
+
After do
|
13
|
+
EmailSpec::EmailViewer.save_and_open_all_raw_emails if ENV['SHOW_EMAILS']
|
14
|
+
EmailSpec::EmailViewer.save_and_open_all_html_emails if ENV['SHOW_HTML_EMAILS']
|
15
|
+
EmailSpec::EmailViewer.save_and_open_all_text_emails if ENV['SHOW_TEXT_EMAILS']
|
16
|
+
end
|
17
|
+
|
18
|
+
World(EmailSpec::Helpers)
|
19
|
+
World(EmailSpec::Matchers)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module EmailSpec
|
2
|
+
module TestDeliveries
|
3
|
+
def all_emails
|
4
|
+
ActionMailer::Base.deliveries
|
5
|
+
end
|
6
|
+
|
7
|
+
def last_email_sent
|
8
|
+
ActionMailer::Base.deliveries.last || raise("No email has been sent!")
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset_mailer
|
12
|
+
ActionMailer::Base.deliveries.clear
|
13
|
+
end
|
14
|
+
|
15
|
+
def mailbox_for(address)
|
16
|
+
address = AddressConverter.instance.convert(address)
|
17
|
+
ActionMailer::Base.deliveries.select { |m| m.to.include?(address) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ARMailerDeliveries
|
22
|
+
def all_emails
|
23
|
+
Email.all.map{ |email| parse_to_tmail(email) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def last_email_sent
|
27
|
+
if email = Email.last
|
28
|
+
TMail::Mail.parse(email.mail)
|
29
|
+
else
|
30
|
+
raise("No email has been sent!")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def reset_mailer
|
35
|
+
Email.delete_all
|
36
|
+
end
|
37
|
+
|
38
|
+
def mailbox_for(address)
|
39
|
+
address = AddressConverter.instance.convert(address)
|
40
|
+
Email.all.select { |email| email.to.include?(address) }.map{ |email| parse_to_tmail(email) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse_to_tmail(email)
|
44
|
+
TMail::Mail.parse(email.mail)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module Deliveries
|
49
|
+
if ActionMailer::Base.delivery_method == :activerecord
|
50
|
+
include EmailSpec::ARMailerDeliveries
|
51
|
+
else
|
52
|
+
include EmailSpec::TestDeliveries
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module EmailSpec
|
2
|
+
class EmailViewer
|
3
|
+
extend Deliveries
|
4
|
+
|
5
|
+
def self.save_and_open_all_raw_emails
|
6
|
+
filename = "#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}.txt"
|
7
|
+
|
8
|
+
File.open(filename, "w") do |f|
|
9
|
+
all_emails.each do |m|
|
10
|
+
f.write m.to_s
|
11
|
+
f.write "\n" + '='*80 + "\n"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
open_in_text_editor(filename)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.save_and_open_all_html_emails
|
19
|
+
all_emails.each_with_index do |m, index|
|
20
|
+
if m.multipart? && html_part = m.parts.detect{ |p| p.content_type == 'text/html' }
|
21
|
+
filename = tmp_email_filename("-#{index}.html")
|
22
|
+
File.open(filename, "w") do |f|
|
23
|
+
f.write m.parts[1].body
|
24
|
+
end
|
25
|
+
open_in_browser(filename)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.save_and_open_all_text_emails
|
31
|
+
filename = tmp_email_filename
|
32
|
+
|
33
|
+
File.open(filename, "w") do |f|
|
34
|
+
all_emails.each do |m|
|
35
|
+
if m.multipart? && text_part = m.parts.detect{ |p| p.content_type == 'text/plain' }
|
36
|
+
m.ordered_each{|k,v| f.write "#{k}: #{v}\n" }
|
37
|
+
f.write text_part.body
|
38
|
+
else
|
39
|
+
f.write m.to_s
|
40
|
+
end
|
41
|
+
f.write "\n" + '='*80 + "\n"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
open_in_text_editor(filename)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.save_and_open_email(mail)
|
49
|
+
filename = "#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}.txt"
|
50
|
+
|
51
|
+
File.open(filename, "w") do |f|
|
52
|
+
f.write mail.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
open_in_text_editor(filename)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.open_in_text_editor(filename)
|
59
|
+
`mate #{filename}`
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.open_in_browser(filename)
|
63
|
+
`open #{filename}`
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.tmp_email_filename(extension = '.txt')
|
67
|
+
"#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}#{extension}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|