Skip to content

Commit

Permalink
issue#4 feature, add :path_only option to visit_in_email
Browse files Browse the repository at this point in the history
  • Loading branch information
23inhouse committed Jan 17, 2011
1 parent 37711a2 commit 87d0109
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/email_spec/helpers.rb
Expand Up @@ -6,8 +6,8 @@ module EmailSpec
module Helpers
include Deliveries

def visit_in_email(link_text)
visit(parse_email_for_link(current_email, link_text))
def visit_in_email(link_text, opts={})
visit(parse_email_for_link(current_email, link_text, opts))
end

def click_email_link_matching(regex, email = current_email)
Expand All @@ -16,9 +16,9 @@ def click_email_link_matching(regex, email = current_email)
visit request_uri(url)
end

def click_first_link_in_email(email = current_email)
def click_first_link_in_email(email = current_email, opts={})
link = links_in_email(email).first
visit request_uri(link)
visit request_uri(link, opts)
end

def open_email(address, opts={})
Expand Down Expand Up @@ -93,34 +93,38 @@ def set_current_email(email)
email_spec_hash[:current_email] = email
end

def parse_email_for_link(email, text_or_regex)
def parse_email_for_link(email, text_or_regex, opts={})
email.should have_body_text(text_or_regex)

url = parse_email_for_explicit_link(email, text_or_regex)
url ||= parse_email_for_anchor_text_link(email, text_or_regex)
url = parse_email_for_explicit_link(email, text_or_regex, opts)
url ||= parse_email_for_anchor_text_link(email, text_or_regex, opts)

raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url
url
end

def request_uri(link)
def request_uri(link, opts={})
return unless link
url = URI::parse(link)
url.fragment ? (url.request_uri + "#" + url.fragment) : url.request_uri
# url.fragment ? (url.request_uri + "#" + url.fragment) : url.request_uri
path = url.fragment ? (url.request_uri + "#" + url.fragment) : url.request_uri
opts[:path_only] != false ? path : url.to_s
end

# e.g. confirm in http://confirm
def parse_email_for_explicit_link(email, regex)
def parse_email_for_explicit_link(email, regex, opts={})
regex = /#{Regexp.escape(regex)}/ unless regex.is_a?(Regexp)
url = links_in_email(email).detect { |link| link =~ regex }
request_uri(url)
request_uri(url, opts)
end

# e.g. Click here in <a href="http://confirm">Click here</a>
def parse_email_for_anchor_text_link(email, link_text)
def parse_email_for_anchor_text_link(email, link_text, opts={})
if textify_images(email.default_part_body) =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>}
URI.split($1)[5..-1].compact!.join("?").gsub("&amp;", "&")
# sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
url = $1.to_s
path = URI.split(url)[5..-1].compact!.join("?").gsub("&amp;", "&")
# sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002) dead link now, found it here http://webcache.googleusercontent.com/search?q=cache:TF7WyF2lHG0J:dev.rubyonrails.org/ticket/4002+%22dev.rubyonrails.org/ticket/4002%22&cd=1&hl=en&ct=clnk&gl=us
opts[:path_only] != false ? path : url
else
return nil
end
Expand Down
8 changes: 8 additions & 0 deletions lib/generators/email_spec/steps/templates/email_steps.rb
Expand Up @@ -171,6 +171,14 @@ def current_email_address
click_first_link_in_email
end

When /^(?:I|they) follow the full "([^"]*?)" link in the email$/ do |link|
visit_in_email(link, :path_only => false)
end

When /^(?:I|they) click the first full link in the email$/ do
click_first_link_in_email(:path_only => false)
end

#
# Debugging
# These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
Expand Down
8 changes: 8 additions & 0 deletions rails_generators/email_spec/templates/email_steps.rb
Expand Up @@ -159,6 +159,14 @@ def current_email_address
click_first_link_in_email
end

When /^(?:I|they) follow the full "([^"]*?)" link in the email$/ do |link|
visit_in_email(link, :path_only => false)
end

When /^(?:I|they) click the first full link in the email$/ do
click_first_link_in_email(:path_only => false)
end

#
# Debugging
# These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
Expand Down
20 changes: 20 additions & 0 deletions spec/email_spec/helpers_spec.rb
Expand Up @@ -8,11 +8,31 @@
parse_email_for_link(email, "Click Here").should == "/path/to/page"
end

it "properly finds explicit links with text" do
email = Mail.new(:body => %(http://host.com/path/to/page))
parse_email_for_link(email, "path/to").should == "/path/to/page"
end

it "recognizes img alt properties as text" do
email = Mail.new(:body => %(<a href="/path/to/page"><img src="http://host.com/images/image.gif" alt="an image" /></a>))
parse_email_for_link(email, "an image").should == "/path/to/page"
end

it "properly finds full url links with text" do
email = Mail.new(:body => %(<a href="http://host.com/path/to/page">Click Here</a>))
parse_email_for_link(email, "Click Here", :path_only => false).should == "http://host.com/path/to/page"
end

it "properly finds explicit full url links with text" do
email = Mail.new(:body => %(http://host.com/path/to/page))
parse_email_for_link(email, "path/to", :path_only => false).should == "http://host.com/path/to/page"
end

it "recognizes full url img alt properties as text" do
email = Mail.new(:body => %(<a href="http://host.com/path/to/page"><img src="http://host.com/images/image.gif" alt="an image" /></a>))
parse_email_for_link(email, "an image", :path_only => false).should == "http://host.com/path/to/page"
end

it "causes a spec to fail if the body doesn't contain the text specified to click" do
email = Mail.new(:body => "")
lambda { parse_email_for_link(email, "non-existent text") }.should raise_error( RSpec::Expectations::ExpectationNotMetError)
Expand Down

0 comments on commit 87d0109

Please sign in to comment.