Skip to content

Commit

Permalink
Improve "I follow the ... link in the email" step (resolves #110)
Browse files Browse the repository at this point in the history
  • Loading branch information
foobear committed Feb 12, 2020
1 parent f34b60f commit 4f961b1
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 11 deletions.
15 changes: 11 additions & 4 deletions lib/spreewald/email_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@
When /^I follow the (first|second|third)? ?link in the e?mail$/ do |index_in_words|
mail = @mail || ActionMailer::Base.deliveries.last
index = { nil => 0, 'first' => 0, 'second' => 1, 'third' => 2 }[index_in_words]
url_pattern = %r{(?:http|https)://[^/]+([^"'\s\\]*)}
mail_body = MailFinder.email_text_body(mail).to_s
only_path = mail_body.scan(url_pattern)[index][0]
visit only_path
url_pattern = %r((?:https?://[^/]+)([^"'\s]+))

paths = if mail.html_part
dom = Nokogiri::HTML(mail.html_part.body.to_s)
(dom / 'a[href]').map { |a| a['href'].match(url_pattern)[1] }
else
mail_body = MailFinder.email_text_body(mail).to_s
mail_body.scan(url_pattern).flatten(1)
end

visit paths[index]
end.overridable

Then /^no e?mail should have been sent$/ do
Expand Down
29 changes: 22 additions & 7 deletions tests/shared/app/controllers/emails_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
class EmailsController < ApplicationController

def do_nothing
render :nothing => true
render nothing: true
end

def send_email
if Rails.version >= "3"
Mailer.email.deliver
deliver :email
render nothing: true
end

def send_html_email_with_links
deliver :html_email_with_links
render nothing: true
end

def send_text_email_with_links
deliver :text_email_with_links
render nothing: true
end

private

def deliver(method_name)
if Rails.version >= '3'
Mailer.public_send(method_name).deliver
else
Mailer.deliver_email
Mailer.public_send("deliver_#{method_name}")
end

render :nothing => true
end

end
8 changes: 8 additions & 0 deletions tests/shared/app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ def visibility
def within
end

def link_target
render :nothing => true
end

def second_link_target
render :nothing => true
end

end
16 changes: 16 additions & 0 deletions tests/shared/app/models/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def email
)
end

def html_email_with_links
email
end

def text_email_with_links
email
end

else

def email
Expand All @@ -34,6 +42,14 @@ def email
body BODY
end

def html_email_with_links
email
end

def text_email_with_links
email
end

end

end
Empty file.
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/shared/app/views/mailer/html_email_with_links.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
!!!
%html(lang="en" xmlns="http://www.w3.org/1999/xhtml")
%body
%img(src="https://www.example.com/image.png")
%a(href="https://www.example.com/static_pages/link_target") Click me
%a(href="https://www.example.com/static_pages/second_link_target") Other link
6 changes: 6 additions & 0 deletions tests/shared/app/views/mailer/text_email_with_links.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Hello!

First link: https://www.example.com/static_pages/link_target
Second link: https://www.example.com/static_pages/second_link_target

Bye!
18 changes: 18 additions & 0 deletions tests/shared/features/shared/email_steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,21 @@ Feature: Test Spreewald's email steps
line
'''
"""


Scenario: /^I follow the (first|second|third)? ?link in the e?mail$/ (HTML e-mail body)
When I go to "/emails/send_html_email_with_links"
And I follow the first link in the email
Then I should be on "/static_pages/link_target"

When I follow the second link in the email
Then I should be on "/static_pages/second_link_target"


Scenario: /^I follow the (first|second|third)? ?link in the e?mail$/ (text e-mail body)
When I go to "/emails/send_text_email_with_links"
And I follow the first link in the email
Then I should be on "/static_pages/link_target"

When I follow the second link in the email
Then I should be on "/static_pages/second_link_target"

0 comments on commit 4f961b1

Please sign in to comment.