glennr-email_spec 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/History.txt +135 -0
  2. data/MIT-LICENSE.txt +19 -0
  3. data/README.rdoc +125 -0
  4. data/Rakefile +75 -0
  5. data/examples/rails_root/Rakefile +13 -0
  6. data/examples/rails_root/app/controllers/application_controller.rb +15 -0
  7. data/examples/rails_root/app/controllers/welcome_controller.rb +12 -0
  8. data/examples/rails_root/app/helpers/application_helper.rb +3 -0
  9. data/examples/rails_root/app/helpers/welcome_helper.rb +2 -0
  10. data/examples/rails_root/app/models/user.rb +2 -0
  11. data/examples/rails_root/app/models/user_mailer.rb +18 -0
  12. data/examples/rails_root/app/views/user_mailer/newsletter.erb +8 -0
  13. data/examples/rails_root/app/views/user_mailer/signup.erb +3 -0
  14. data/examples/rails_root/app/views/welcome/confirm.html.erb +1 -0
  15. data/examples/rails_root/app/views/welcome/index.html.erb +8 -0
  16. data/examples/rails_root/app/views/welcome/newsletter.html.erb +0 -0
  17. data/examples/rails_root/app/views/welcome/signup.html.erb +1 -0
  18. data/examples/rails_root/config/boot.rb +110 -0
  19. data/examples/rails_root/config/database.yml +22 -0
  20. data/examples/rails_root/config/environment.rb +22 -0
  21. data/examples/rails_root/config/environments/development.rb +17 -0
  22. data/examples/rails_root/config/environments/production.rb +24 -0
  23. data/examples/rails_root/config/environments/test.rb +30 -0
  24. data/examples/rails_root/config/initializers/inflections.rb +10 -0
  25. data/examples/rails_root/config/initializers/mime_types.rb +5 -0
  26. data/examples/rails_root/config/initializers/new_rails_defaults.rb +17 -0
  27. data/examples/rails_root/config/routes.rb +44 -0
  28. data/examples/rails_root/db/migrate/20090125013728_create_users.rb +11 -0
  29. data/examples/rails_root/db/migrate/20090908054656_create_delayed_jobs.rb +20 -0
  30. data/examples/rails_root/db/schema.rb +32 -0
  31. data/examples/rails_root/doc/README_FOR_APP +5 -0
  32. data/examples/rails_root/features/delayed_job.feature +13 -0
  33. data/examples/rails_root/features/errors.feature +32 -0
  34. data/examples/rails_root/features/example.feature +62 -0
  35. data/examples/rails_root/features/step_definitions/user_steps.rb +26 -0
  36. data/examples/rails_root/features/step_definitions/webrat_steps.rb +137 -0
  37. data/examples/rails_root/features/support/env.rb +32 -0
  38. data/examples/rails_root/features/support/paths.rb +24 -0
  39. data/examples/rails_root/public/404.html +30 -0
  40. data/examples/rails_root/public/422.html +30 -0
  41. data/examples/rails_root/public/500.html +33 -0
  42. data/examples/rails_root/public/dispatch.rb +10 -0
  43. data/examples/rails_root/public/favicon.ico +0 -0
  44. data/examples/rails_root/public/images/rails.png +0 -0
  45. data/examples/rails_root/public/javascripts/application.js +2 -0
  46. data/examples/rails_root/public/javascripts/controls.js +963 -0
  47. data/examples/rails_root/public/javascripts/dragdrop.js +973 -0
  48. data/examples/rails_root/public/javascripts/effects.js +1128 -0
  49. data/examples/rails_root/public/javascripts/prototype.js +4320 -0
  50. data/examples/rails_root/public/robots.txt +5 -0
  51. data/examples/rails_root/script/about +4 -0
  52. data/examples/rails_root/script/autospec +5 -0
  53. data/examples/rails_root/script/console +3 -0
  54. data/examples/rails_root/script/cucumber +9 -0
  55. data/examples/rails_root/script/dbconsole +3 -0
  56. data/examples/rails_root/script/delayed_job +5 -0
  57. data/examples/rails_root/script/destroy +3 -0
  58. data/examples/rails_root/script/generate +3 -0
  59. data/examples/rails_root/script/performance/benchmarker +3 -0
  60. data/examples/rails_root/script/performance/profiler +3 -0
  61. data/examples/rails_root/script/performance/request +3 -0
  62. data/examples/rails_root/script/plugin +3 -0
  63. data/examples/rails_root/script/process/inspector +3 -0
  64. data/examples/rails_root/script/process/reaper +3 -0
  65. data/examples/rails_root/script/process/spawner +3 -0
  66. data/examples/rails_root/script/runner +3 -0
  67. data/examples/rails_root/script/server +3 -0
  68. data/examples/rails_root/script/spec +5 -0
  69. data/examples/rails_root/script/spec_server +125 -0
  70. data/examples/rails_root/spec/controllers/welcome_controller_spec.rb +15 -0
  71. data/examples/rails_root/spec/model_factory.rb +6 -0
  72. data/examples/rails_root/spec/models/user_mailer_spec.rb +58 -0
  73. data/examples/rails_root/spec/models/user_spec.rb +5 -0
  74. data/examples/rails_root/spec/rcov.opts +2 -0
  75. data/examples/rails_root/spec/spec.opts +4 -0
  76. data/examples/rails_root/spec/spec_helper.rb +51 -0
  77. data/examples/sinatra/app.rb +33 -0
  78. data/examples/sinatra/features/errors.feature +32 -0
  79. data/examples/sinatra/features/example.feature +62 -0
  80. data/examples/sinatra/features/step_definitions/user_steps.rb +26 -0
  81. data/examples/sinatra/features/step_definitions/webrat_steps.rb +137 -0
  82. data/examples/sinatra/features/support/env.rb +33 -0
  83. data/examples/sinatra/features/support/paths.rb +24 -0
  84. data/install.rb +0 -0
  85. data/lib/email-spec.rb +1 -0
  86. data/lib/email_spec.rb +15 -0
  87. data/lib/email_spec/address_converter.rb +29 -0
  88. data/lib/email_spec/background_processes.rb +31 -0
  89. data/lib/email_spec/cucumber.rb +21 -0
  90. data/lib/email_spec/deliveries.rb +71 -0
  91. data/lib/email_spec/email_viewer.rb +70 -0
  92. data/lib/email_spec/helpers.rb +149 -0
  93. data/lib/email_spec/matchers.rb +186 -0
  94. data/rails_generators/email_spec/email_spec_generator.rb +17 -0
  95. data/rails_generators/email_spec/templates/email_steps.rb +122 -0
  96. data/spec/email_spec/matchers_spec.rb +213 -0
  97. data/spec/spec.opts +4 -0
  98. data/spec/spec_helper.rb +4 -0
  99. metadata +154 -0
@@ -0,0 +1,62 @@
1
+ Feature: EmailSpec Example -- Prevent Bots from creating accounts
2
+
3
+ In order to help alleviate email testing in apps
4
+ As an email-spec contributor I want new users of the library
5
+ to easily adopt email-spec in their app by following this example
6
+
7
+ In order to prevent bots from setting up new accounts
8
+ As a site manager I want new users
9
+ to verify their email address with a confirmation link
10
+
11
+ Background:
12
+ Given no emails have been sent
13
+ And I am a real person wanting to sign up for an account
14
+ And I am on the homepage
15
+ And I submit my registration information
16
+
17
+ Scenario: First person signup (as myself) with two ways of opening email
18
+ Then I should receive an email
19
+ And I should have 1 email
20
+
21
+ # Opening email #1
22
+ When I open the email
23
+ Then I should see "Account confirmation" in the email subject
24
+ And I should see "Joe Someone" in the email body
25
+ And I should see "confirm" in the email body
26
+
27
+ # Opening email #2
28
+ When I open the email with subject "Account confirmation"
29
+ Then I should see "Account confirmation" in the email subject
30
+ And I should see "Joe Someone" in the email body
31
+ And I should see "confirm" in the email body
32
+
33
+ When I follow "Click here to confirm your account!" in the email
34
+ Then I should see "Confirm your new account"
35
+
36
+ And "foo@bar.com" should have no emails
37
+
38
+ Scenario: Third person signup (emails sent to others) with two ways of opening email
39
+ Then "foo@bar.com" should have no emails
40
+ And "example@example.com" should receive an email
41
+ And "example@example.com" should have 1 email
42
+
43
+ # Opening email #1
44
+ When they open the email
45
+ Then they should see "Account confirmation" in the email subject
46
+ And they should see "Joe Someone" in the email body
47
+ And they should see "confirm" in the email body
48
+
49
+ # Opening email #2
50
+ When "example@example.com" opens the email with subject "Account confirmation"
51
+ Then they should see "Account confirmation" in the email subject
52
+ And they should see "Joe Someone" in the email body
53
+ And they should see "confirm" in the email body
54
+
55
+ When they follow "Click here to confirm your account!" in the email
56
+ Then they should see "Confirm your new account"
57
+
58
+ Scenario: Declarative First Person signup
59
+ Then I should receive an email with a link to a confirmation page
60
+
61
+ Scenario: Declarative First Person signup
62
+ Then they should receive an email with a link to a confirmation page
@@ -0,0 +1,26 @@
1
+ Given "I am a real person wanting to sign up for an account" do
2
+ # no-op.. for documentation purposes only!
3
+ end
4
+
5
+ When /^I submit my registration information$/ do
6
+ fill_in "Name", :with => 'Joe Someone'
7
+ fill_in "Email", :with => 'example@example.com'
8
+ click_button
9
+ end
10
+
11
+ Then /^(?:I|they) should receive an email with a link to a confirmation page$/ do
12
+ unread_emails_for(current_email_address).size.should == 1
13
+
14
+ # this call will store the email and you can access it with current_email
15
+ open_last_email_for(last_email_address)
16
+ current_email.should have_subject(/Account confirmation/)
17
+ current_email.should have_body_text('Joe Someone')
18
+
19
+ click_email_link_matching /confirm/
20
+ response_body.should contain("Confirm your new account")
21
+ end
22
+
23
+ # Basically aliases "I should see [text]", but for third person
24
+ Then /^they should see "([^\"]*)"$/ do |text|
25
+ Then "I should see \"#{text}\""
26
+ end
@@ -0,0 +1,137 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
2
+
3
+ # Commonly used webrat steps
4
+ # http://github.com/brynary/webrat
5
+
6
+ Given /^I am on (.+)$/ do |page_name|
7
+ visit path_to(page_name)
8
+ end
9
+
10
+ When /^I go to (.+)$/ do |page_name|
11
+ visit path_to(page_name)
12
+ end
13
+
14
+ When /^I press "([^\"]*)"$/ do |button|
15
+ click_button(button)
16
+ end
17
+
18
+ When /^I follow "([^\"]*)"$/ do |link|
19
+ click_link(link)
20
+ end
21
+
22
+ When /^I follow "([^\"]*)" within "([^\"]*)"$/ do |link, parent|
23
+ click_link_within(parent, link)
24
+ end
25
+
26
+ When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
27
+ fill_in(field, :with => value)
28
+ end
29
+
30
+ When /^I select "([^\"]*)" from "([^\"]*)"$/ do |value, field|
31
+ select(value, :from => field)
32
+ end
33
+
34
+ # Use this step in conjunction with Rail's datetime_select helper. For example:
35
+ # When I select "December 25, 2008 10:00" as the date and time
36
+ When /^I select "([^\"]*)" as the date and time$/ do |time|
37
+ select_datetime(time)
38
+ end
39
+
40
+ # Use this step when using multiple datetime_select helpers on a page or
41
+ # you want to specify which datetime to select. Given the following view:
42
+ # <%%= f.label :preferred %><br />
43
+ # <%%= f.datetime_select :preferred %>
44
+ # <%%= f.label :alternative %><br />
45
+ # <%%= f.datetime_select :alternative %>
46
+ # The following steps would fill out the form:
47
+ # When I select "November 23, 2004 11:20" as the "Preferred" date and time
48
+ # And I select "November 25, 2004 10:30" as the "Alternative" date and time
49
+ When /^I select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, datetime_label|
50
+ select_datetime(datetime, :from => datetime_label)
51
+ end
52
+
53
+ # Use this step in conjunction with Rail's time_select helper. For example:
54
+ # When I select "2:20PM" as the time
55
+ # Note: Rail's default time helper provides 24-hour time-- not 12 hour time. Webrat
56
+ # will convert the 2:20PM to 14:20 and then select it.
57
+ When /^I select "([^\"]*)" as the time$/ do |time|
58
+ select_time(time)
59
+ end
60
+
61
+ # Use this step when using multiple time_select helpers on a page or you want to
62
+ # specify the name of the time on the form. For example:
63
+ # When I select "7:30AM" as the "Gym" time
64
+ When /^I select "([^\"]*)" as the "([^\"]*)" time$/ do |time, time_label|
65
+ select_time(time, :from => time_label)
66
+ end
67
+
68
+ # Use this step in conjunction with Rail's date_select helper. For example:
69
+ # When I select "February 20, 1981" as the date
70
+ When /^I select "([^\"]*)" as the date$/ do |date|
71
+ select_date(date)
72
+ end
73
+
74
+ # Use this step when using multiple date_select helpers on one page or
75
+ # you want to specify the name of the date on the form. For example:
76
+ # When I select "April 26, 1982" as the "Date of Birth" date
77
+ When /^I select "([^\"]*)" as the "([^\"]*)" date$/ do |date, date_label|
78
+ select_date(date, :from => date_label)
79
+ end
80
+
81
+ When /^I check "([^\"]*)"$/ do |field|
82
+ check(field)
83
+ end
84
+
85
+ When /^I uncheck "([^\"]*)"$/ do |field|
86
+ uncheck(field)
87
+ end
88
+
89
+ When /^I choose "([^\"]*)"$/ do |field|
90
+ choose(field)
91
+ end
92
+
93
+ When /^I attach the file at "([^\"]*)" to "([^\"]*)"$/ do |path, field|
94
+ attach_file(field, path)
95
+ end
96
+
97
+ Then /^I should see "([^\"]*)"$/ do |text|
98
+ response_body.should contain(text)
99
+ end
100
+
101
+ Then /^I should see \/([^\/]*)\/$/ do |regexp|
102
+ regexp = Regexp.new(regexp)
103
+ response_body.should contain(regexp)
104
+ end
105
+
106
+ Then /^I should not see "([^\"]*)"$/ do |text|
107
+ response_body.should_not contain(text)
108
+ end
109
+
110
+ Then /^I should not see \/([^\/]*)\/$/ do |regexp|
111
+ regexp = Regexp.new(regexp)
112
+ response_body.should_not contain(regexp)
113
+ end
114
+
115
+ Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
116
+ field_labeled(field).value.should =~ /#{value}/
117
+ end
118
+
119
+ Then /^the "([^\"]*)" field should not contain "([^\"]*)"$/ do |field, value|
120
+ field_labeled(field).value.should_not =~ /#{value}/
121
+ end
122
+
123
+ Then /^the "([^\"]*)" checkbox should be checked$/ do |label|
124
+ field_labeled(label).should be_checked
125
+ end
126
+
127
+ Then /^the "([^\"]*)" checkbox should not be checked$/ do |label|
128
+ field_labeled(label).should_not be_checked
129
+ end
130
+
131
+ Then /^I should be on (.+)$/ do |page_name|
132
+ URI.parse(current_url).path.should == path_to(page_name)
133
+ end
134
+
135
+ Then /^show me the page$/ do
136
+ save_and_open_page
137
+ end
@@ -0,0 +1,33 @@
1
+ # Sets up the Rails environment for Cucumber
2
+ ENV["RACK_ENV"] ||= "test"
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../app.rb')
4
+
5
+ # Comment out the next line if you don't want Cucumber Unicode support
6
+ require 'cucumber/formatter/unicode'
7
+
8
+ require 'rack/test'
9
+ require 'webrat'
10
+ require 'cucumber/webrat/table_locator' # Lets you do table.diff!(table_at('#my_table').to_a)
11
+
12
+ Webrat.configure do |config|
13
+ config.mode = :rack
14
+ end
15
+
16
+ require 'webrat/core/matchers'
17
+
18
+ # email testing in cucumber
19
+ require 'activesupport'
20
+ require File.expand_path(File.dirname(__FILE__) + '../../../../../lib/email_spec')
21
+ require 'email_spec/cucumber'
22
+
23
+ class AppWorld
24
+ include Rack::Test::Methods
25
+ include Webrat::Methods
26
+ include Webrat::Matchers
27
+
28
+ def app
29
+ Sinatra::Application.new
30
+ end
31
+ end
32
+
33
+ World { AppWorld.new }
@@ -0,0 +1,24 @@
1
+ module NavigationHelpers
2
+ # Maps a name to a path. Used by the
3
+ #
4
+ # When /^I go to (.+)$/ do |page_name|
5
+ #
6
+ # step definition in webrat_steps.rb
7
+ #
8
+ def path_to(page_name)
9
+ case page_name
10
+
11
+ when /the homepage/
12
+ '/'
13
+
14
+ when /request a newsletter/
15
+ "/newsletter/Joe Someone/example@example.com"
16
+
17
+ else
18
+ raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
19
+ "Now, go and add a mapping in #{__FILE__}"
20
+ end
21
+ end
22
+ end
23
+
24
+ World(NavigationHelpers)
File without changes
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'email_spec'))
@@ -0,0 +1,15 @@
1
+ unless defined?(Pony) or defined?(ActionMailer)
2
+ Kernel.warn("Neither Pony nor ActionMailer appear to be loaded so email-spec is requiring ActionMailer.")
3
+ require 'actionmailer'
4
+ end
5
+
6
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
7
+
8
+ require 'email_spec/background_processes'
9
+ require 'email_spec/deliveries'
10
+ require 'email_spec/address_converter'
11
+ require 'email_spec/email_viewer'
12
+ require 'email_spec/helpers'
13
+ require 'email_spec/matchers'
14
+
15
+
@@ -0,0 +1,29 @@
1
+ require 'singleton'
2
+
3
+ module EmailSpec
4
+ class AddressConverter
5
+ include Singleton
6
+
7
+ attr_accessor :converter
8
+
9
+ # The block provided to conversion should convert to an email
10
+ # address string or return the input untouched. For example:
11
+ #
12
+ # EmailSpec::AddressConverter.instance.conversion do |input|
13
+ # if input.is_a?(User)
14
+ # input.email
15
+ # else
16
+ # input
17
+ # end
18
+ # end
19
+ #
20
+ def conversion(&block)
21
+ self.converter = block
22
+ end
23
+
24
+ def convert(input)
25
+ return input unless converter
26
+ converter.call(input)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module EmailSpec
2
+ module BackgroundProcesses
3
+ module DelayedJob
4
+ def all_emails
5
+ Delayed::Job.work_off
6
+ super
7
+ end
8
+
9
+ def last_email_sent
10
+ Delayed::Job.work_off
11
+ super
12
+ end
13
+
14
+ def reset_mailer
15
+ Delayed::Job.work_off
16
+ super
17
+ end
18
+
19
+ def mailbox_for(address)
20
+ Delayed::Job.work_off
21
+ super
22
+ end
23
+ end
24
+
25
+ module Compatibility
26
+ if defined?(Delayed)
27
+ include EmailSpec::BackgroundProcesses::DelayedJob
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ # require this in your env.rb file after you require cucumber/rails/world
2
+
3
+ # Global Setup
4
+ if defined?(ActionMailer)
5
+ ActionMailer::Base.delivery_method = :test unless ActionMailer::Base.delivery_method == :activerecord
6
+ ActionMailer::Base.perform_deliveries = true
7
+
8
+ Before do
9
+ # Scenario setup
10
+ ActionMailer::Base.deliveries.clear if ActionMailer::Base.delivery_method == :test
11
+ end
12
+ end
13
+
14
+ After do
15
+ EmailSpec::EmailViewer.save_and_open_all_raw_emails if ENV['SHOW_EMAILS']
16
+ EmailSpec::EmailViewer.save_and_open_all_html_emails if ENV['SHOW_HTML_EMAILS']
17
+ EmailSpec::EmailViewer.save_and_open_all_text_emails if ENV['SHOW_TEXT_EMAILS']
18
+ end
19
+
20
+ World(EmailSpec::Helpers)
21
+ World(EmailSpec::Matchers)
@@ -0,0 +1,71 @@
1
+ module EmailSpec
2
+ module MailerDeliveries
3
+ def all_emails
4
+ mailer.deliveries
5
+ end
6
+
7
+ def last_email_sent
8
+ mailer.deliveries.last || raise("No email has been sent!")
9
+ end
10
+
11
+ def reset_mailer
12
+ mailer.deliveries.clear
13
+ end
14
+
15
+ def mailbox_for(address)
16
+ mailer.deliveries.select { |m| m.to.include?(address) || (m.bcc && m.bcc.include?(address)) || (m.cc && m.cc.include?(address)) }
17
+ end
18
+ end
19
+
20
+ module ARMailerDeliveries
21
+ def all_emails
22
+ Email.all.map{ |email| parse_to_tmail(email) }
23
+ end
24
+
25
+ def last_email_sent
26
+ if email = Email.last
27
+ TMail::Mail.parse(email.mail)
28
+ else
29
+ raise("No email has been sent!")
30
+ end
31
+ end
32
+
33
+ def reset_mailer
34
+ Email.delete_all
35
+ end
36
+
37
+ def mailbox_for(address)
38
+ Email.all.select { |email| email.to.include?(address) || (email.bcc && email.bcc.include?(address)) || (email.cc && email.cc.include?(address)) }.map{ |email| parse_to_tmail(email) }
39
+ end
40
+
41
+ def parse_to_tmail(email)
42
+ TMail::Mail.parse(email.mail)
43
+ end
44
+ end
45
+
46
+ if defined?(Pony)
47
+ module ::Pony
48
+ def self.deliveries
49
+ @deliveries ||= []
50
+ end
51
+
52
+ def self.mail(options)
53
+ deliveries << build_tmail(options)
54
+ end
55
+ end
56
+ end
57
+
58
+ module Deliveries
59
+ if defined?(Pony)
60
+ def mailer; Pony; end
61
+ include EmailSpec::MailerDeliveries
62
+ elsif ActionMailer::Base.delivery_method == :activerecord
63
+ include EmailSpec::ARMailerDeliveries
64
+ else
65
+ def mailer; ActionMailer::Base; end
66
+ include EmailSpec::MailerDeliveries
67
+ end
68
+ include EmailSpec::BackgroundProcesses::Compatibility
69
+ end
70
+ end
71
+