Skip to content

Commit

Permalink
implement link following from email_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderpantsGnome authored and ianwhite committed Feb 25, 2010
1 parent 4002dc4 commit 05bcde8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/pickle/email.rb
Expand Up @@ -18,7 +18,17 @@ def email_has_fields?(email, fields)
end
true
end


def visit_in_email(email, link_text)
visit(parse_email_for_link(email, link_text))
end

def click_first_link_in_email(email)
link = links_in_email(email).first
request_uri = URI::parse(link).request_uri
visit request_uri
end

protected
# Saves the emails out to RAILS_ROOT/tmp/ and opens it in the default
# web browser if on OS X. (depends on webrat)
Expand All @@ -32,5 +42,31 @@ def save_and_open_emails
end
open_in_browser(filename)
end

def parse_email_for_link(email, text_or_regex)
url = parse_email_for_explicit_link(email, text_or_regex)
url ||= parse_email_for_anchor_text_link(email, text_or_regex)
raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url
url
end

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

# e.g. Click here in <a href="http://confirm">Click here</a>
def parse_email_for_anchor_text_link(email, link_text)
email.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)
end

def links_in_email(email, protos=['http', 'https'])
URI.extract(email.body, protos)
end

end
end
8 changes: 8 additions & 0 deletions rails_generators/pickle/templates/email_steps.rb
Expand Up @@ -18,6 +18,14 @@
emails.size.should == count.to_i
end

When(/^(?:I|they) follow "([^"]*?)" in #{capture_email}$/) do |link, email_ref|
visit_in_email(email(email_ref), link)
end

When(/^(?:I|they) click the first link in #{capture_email}$/) do
click_first_link_in_email(email(email_ref))
end

Then(/^(\d)+ emails? should be delivered to (.*)$/) do |count, to|
emails("to: \"#{email_for(to)}\"").size.should == count.to_i
end
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/pickle_email_spec.rb
Expand Up @@ -128,4 +128,26 @@
save_and_open_emails
end
end

describe "following links in emails" do
before do
stub!(:open_in_browser)
@email1.stub!(:body).and_return('some text <a href="http://example.com/page">example page</a> more text')
end

it "should find a link for http://example.com/page" do
should_receive(:visit).with('/page')
visit_in_email(@email1, 'http://example.com/page')
end

it "should find a link for \"example page\"" do
should_receive(:visit).with('/page')
visit_in_email(@email1, 'example page')
end

it "should follow the first link in an email" do
should_receive(:visit).with('/page')
click_first_link_in_email(@email1)
end
end
end

0 comments on commit 05bcde8

Please sign in to comment.