PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/features/step_definitions/email_steps.rb

https://bitbucket.org/denjell/loomio_gv
Ruby | 219 lines | 121 code | 44 blank | 54 comment | 11 complexity | 20c78b256327387c5096c6673f9f469a MD5 | raw file
Possible License(s): AGPL-3.0
  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. # last_email_address
  7. # reset_mailer
  8. # open_last_email
  9. # visit_in_email
  10. # unread_emails_for
  11. # mailbox_for
  12. # current_email
  13. # open_email
  14. # read_emails_for
  15. # find_email
  16. #
  17. # General form for email scenarios are:
  18. # - clear the email queue (done automatically by email_spec)
  19. # - execute steps that sends an email
  20. # - check the user received an/no/[0-9] emails
  21. # - open the email
  22. # - inspect the email contents
  23. # - interact with the email (e.g. click links)
  24. #
  25. # The Cucumber steps below are setup in this order.
  26. module EmailHelpers
  27. def current_email_address
  28. # Replace with your a way to find your current email. e.g @current_user.email
  29. # last_email_address will return the last email address used by email spec to find an email.
  30. # Note that last_email_address will be reset after each Scenario.
  31. # last_email_address || "example@example.com"
  32. last_email_address || @current_user.email
  33. end
  34. end
  35. World(EmailHelpers)
  36. #
  37. # Reset the e-mail queue within a scenario.
  38. # This is done automatically before each scenario.
  39. #
  40. Given /^(?:a clear email queue|no emails have been sent)$/ do
  41. reset_mailer
  42. end
  43. #
  44. # Check how many emails have been sent/received
  45. #
  46. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
  47. unread_emails_for(address).size.should == parse_email_count(amount)
  48. end
  49. Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
  50. mailbox_for(address).size.should == parse_email_count(amount)
  51. end
  52. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
  53. unread_emails_for(address).select { |m| m.subject =~ Regexp.new(Regexp.escape(subject)) }.size.should == parse_email_count(amount)
  54. end
  55. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject \/([^"]*?)\/$/ do |address, amount, subject|
  56. unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
  57. end
  58. Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
  59. open_email(address, :with_text => expected_body)
  60. end
  61. #
  62. # Accessing emails
  63. #
  64. # Opens the most recently received email
  65. When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
  66. open_email(address)
  67. end
  68. When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
  69. open_email(address, :with_subject => subject)
  70. end
  71. When /^(?:I|they|"([^"]*?)") opens? the email with subject \/([^"]*?)\/$/ do |address, subject|
  72. open_email(address, :with_subject => Regexp.new(subject))
  73. end
  74. When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
  75. open_email(address, :with_text => text)
  76. end
  77. When /^(?:I|they|"([^"]*?)") opens? the email with text \/([^"]*?)\/$/ do |address, text|
  78. open_email(address, :with_text => Regexp.new(text))
  79. end
  80. #
  81. # Inspect the Email Contents
  82. #
  83. Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
  84. current_email.should have_subject(text)
  85. end
  86. Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
  87. current_email.should have_subject(Regexp.new(text))
  88. end
  89. Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
  90. current_email.default_part_body.to_s.should include(text)
  91. end
  92. Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
  93. current_email.default_part_body.to_s.should =~ Regexp.new(text)
  94. end
  95. Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
  96. current_email.should be_delivered_from(text)
  97. end
  98. Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
  99. current_email.should have_header(name, text)
  100. end
  101. Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
  102. current_email.should have_header(name, Regexp.new(text))
  103. end
  104. Then /^I should see it is a multi\-part email$/ do
  105. current_email.should be_multipart
  106. end
  107. Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
  108. current_email.html_part.body.to_s.should include(text)
  109. end
  110. Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
  111. current_email.text_part.body.to_s.should include(text)
  112. end
  113. #
  114. # Inspect the Email Attachments
  115. #
  116. Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
  117. current_email_attachments.size.should == parse_email_count(amount)
  118. end
  119. Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
  120. current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
  121. end
  122. Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
  123. current_email_attachments[(index.to_i - 1)].filename.should == filename
  124. end
  125. Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
  126. current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
  127. end
  128. Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
  129. current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
  130. end
  131. Then /^all attachments should not be blank$/ do
  132. current_email_attachments.each do |attachment|
  133. attachment.read.size.should_not == 0
  134. end
  135. end
  136. Then /^show me a list of email attachments$/ do
  137. EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
  138. end
  139. #
  140. # Interact with Email Contents
  141. #
  142. When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
  143. visit_in_email(link)
  144. end
  145. When /^(?:I|they) click the first link in the email$/ do
  146. click_first_link_in_email
  147. end
  148. When /^(?:I|they) click the second link in the email$/ do
  149. link = links_in_email(current_email)[1]
  150. request_uri = URI::parse(link).request_uri
  151. visit request_uri
  152. end
  153. When /^(?:I|they) click the third link in the email$/ do
  154. link = links_in_email(current_email)[2]
  155. request_uri = URI::parse(link).request_uri
  156. visit request_uri
  157. end
  158. #
  159. # Debugging
  160. # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
  161. # Patches accepted. ;)
  162. #
  163. Then /^save and open current email$/ do
  164. EmailSpec::EmailViewer::save_and_open_email(current_email)
  165. end
  166. Then /^save and open all text emails$/ do
  167. EmailSpec::EmailViewer::save_and_open_all_text_emails
  168. end
  169. Then /^save and open all html emails$/ do
  170. EmailSpec::EmailViewer::save_and_open_all_html_emails
  171. end
  172. Then /^save and open all raw emails$/ do
  173. EmailSpec::EmailViewer::save_and_open_all_raw_emails
  174. end