2008-01-05 08:32:22 -05:00
|
|
|
require 'abstract_unit'
|
2010-01-29 10:16:01 -05:00
|
|
|
require 'action_controller'
|
2006-08-25 02:48:15 -04:00
|
|
|
|
2009-11-29 19:28:45 -05:00
|
|
|
class WelcomeController < ActionController::Base
|
|
|
|
end
|
2009-10-18 11:39:21 -04:00
|
|
|
|
2010-02-24 19:21:21 -05:00
|
|
|
AppRoutes = ActionDispatch::Routing::RouteSet.new
|
|
|
|
|
2010-01-29 11:41:10 -05:00
|
|
|
class ActionMailer::Base
|
2010-02-26 18:00:33 -05:00
|
|
|
include AppRoutes.url_helpers
|
2010-01-29 11:41:10 -05:00
|
|
|
end
|
2010-01-29 10:16:01 -05:00
|
|
|
|
2010-03-18 01:27:34 -04:00
|
|
|
class UrlTestMailer < ActionMailer::Base
|
2007-09-22 15:20:06 -04:00
|
|
|
default_url_options[:host] = 'www.basecamphq.com'
|
2009-10-18 11:39:21 -04:00
|
|
|
|
2010-03-04 04:12:16 -05:00
|
|
|
configure do |c|
|
|
|
|
c.assets_dir = '' # To get the tests to pass
|
|
|
|
end
|
|
|
|
|
2006-08-25 02:48:15 -04:00
|
|
|
def signed_up_with_url(recipient)
|
2009-12-22 14:17:27 -05:00
|
|
|
@recipient = recipient
|
2012-10-07 13:54:14 -04:00
|
|
|
@welcome_url = url_for host: "example.com", controller: "welcome", action: "greeting"
|
|
|
|
mail(to: recipient, subject: "[Signed up] Welcome #{recipient}",
|
|
|
|
from: "system@loudthinking.com", date: Time.local(2004, 12, 12))
|
2006-08-25 02:48:15 -04:00
|
|
|
end
|
2014-11-23 16:53:01 -05:00
|
|
|
|
|
|
|
def exercise_url_for(options)
|
|
|
|
@options = options
|
|
|
|
@url = url_for(@options)
|
|
|
|
mail(from: 'from@example.com', to: 'to@example.com', subject: 'subject')
|
|
|
|
end
|
2006-08-25 02:48:15 -04:00
|
|
|
end
|
|
|
|
|
2010-07-03 20:12:49 -04:00
|
|
|
class ActionMailerUrlTest < ActionMailer::TestCase
|
2014-11-23 16:53:01 -05:00
|
|
|
class DummyModel
|
|
|
|
def self.model_name
|
|
|
|
OpenStruct.new(route_key: 'dummy_model')
|
|
|
|
end
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_name
|
|
|
|
self.class.model_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_model
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
2006-08-25 02:48:15 -04:00
|
|
|
|
2010-04-07 22:47:17 -04:00
|
|
|
def encode( text, charset="UTF-8" )
|
2006-08-25 02:48:15 -04:00
|
|
|
quoted_printable( text, charset )
|
|
|
|
end
|
|
|
|
|
2010-04-07 22:47:17 -04:00
|
|
|
def new_mail( charset="UTF-8" )
|
2009-11-12 00:08:50 -05:00
|
|
|
mail = Mail.new
|
2006-09-09 17:56:38 -04:00
|
|
|
mail.mime_version = "1.0"
|
2006-08-25 02:48:15 -04:00
|
|
|
if charset
|
2009-11-19 22:10:57 -05:00
|
|
|
mail.content_type ["text", "plain", { "charset" => charset }]
|
2006-08-25 02:48:15 -04:00
|
|
|
end
|
|
|
|
mail
|
|
|
|
end
|
|
|
|
|
2014-11-25 01:02:49 -05:00
|
|
|
def assert_url_for(expected, options, relative = false)
|
2014-11-23 16:53:01 -05:00
|
|
|
expected = "http://www.basecamphq.com#{expected}" if expected.start_with?('/') && !relative
|
|
|
|
urls = UrlTestMailer.exercise_url_for(options).body.to_s.chomp.split
|
|
|
|
|
|
|
|
assert_equal expected, urls.first
|
|
|
|
assert_equal expected, urls.second
|
|
|
|
end
|
|
|
|
|
2006-08-25 02:48:15 -04:00
|
|
|
def setup
|
|
|
|
@recipient = 'test@localhost'
|
|
|
|
end
|
|
|
|
|
2014-11-23 16:53:01 -05:00
|
|
|
def test_url_for
|
|
|
|
UrlTestMailer.delivery_method = :test
|
|
|
|
|
|
|
|
AppRoutes.draw do
|
|
|
|
get ':controller(/:action(/:id))'
|
|
|
|
get '/welcome' => 'foo#bar', as: 'welcome'
|
|
|
|
get '/dummy_model' => 'foo#baz', as: 'dummy_model'
|
|
|
|
end
|
|
|
|
|
|
|
|
# string
|
|
|
|
assert_url_for 'http://foo/', 'http://foo/'
|
|
|
|
|
|
|
|
# symbol
|
|
|
|
assert_url_for '/welcome', :welcome
|
|
|
|
|
|
|
|
# hash
|
|
|
|
assert_url_for '/a/b/c', controller: 'a', action: 'b', id: 'c'
|
2014-11-25 01:02:49 -05:00
|
|
|
assert_url_for '/a/b/c', {controller: 'a', action: 'b', id: 'c', only_path: true}, true
|
2014-11-23 16:53:01 -05:00
|
|
|
|
|
|
|
# model
|
|
|
|
assert_url_for '/dummy_model', DummyModel.new
|
|
|
|
|
|
|
|
# class
|
|
|
|
assert_url_for '/dummy_model', DummyModel
|
|
|
|
|
|
|
|
# array
|
|
|
|
assert_url_for '/dummy_model' , [DummyModel]
|
|
|
|
end
|
|
|
|
|
2006-08-25 02:48:15 -04:00
|
|
|
def test_signed_up_with_url
|
2010-03-18 01:27:34 -04:00
|
|
|
UrlTestMailer.delivery_method = :test
|
2010-07-03 20:12:49 -04:00
|
|
|
|
2010-09-04 07:36:35 -04:00
|
|
|
AppRoutes.draw do
|
2012-04-25 00:55:39 -04:00
|
|
|
get ':controller(/:action(/:id))'
|
2012-10-07 13:54:14 -04:00
|
|
|
get '/welcome' => "foo#bar", as: "welcome"
|
2009-10-18 11:39:21 -04:00
|
|
|
end
|
2009-11-29 19:28:45 -05:00
|
|
|
|
|
|
|
expected = new_mail
|
|
|
|
expected.to = @recipient
|
|
|
|
expected.subject = "[Signed up] Welcome #{@recipient}"
|
2010-08-14 04:58:34 -04:00
|
|
|
expected.body = "Hello there,\n\nMr. #{@recipient}. Please see our greeting at http://example.com/welcome/greeting http://www.basecamphq.com/welcome\n\n<img alt=\"Somelogo\" src=\"/images/somelogo.png\" />"
|
2009-11-29 19:28:45 -05:00
|
|
|
expected.from = "system@loudthinking.com"
|
|
|
|
expected.date = Time.local(2004, 12, 12)
|
2010-08-29 19:42:09 -04:00
|
|
|
expected.content_type = "text/html"
|
2009-11-29 19:28:45 -05:00
|
|
|
|
|
|
|
created = nil
|
2010-03-18 01:27:34 -04:00
|
|
|
assert_nothing_raised { created = UrlTestMailer.signed_up_with_url(@recipient) }
|
2009-11-29 19:28:45 -05:00
|
|
|
assert_not_nil created
|
2009-12-16 20:00:32 -05:00
|
|
|
|
|
|
|
expected.message_id = '<123@456>'
|
|
|
|
created.message_id = '<123@456>'
|
2014-10-07 18:40:59 -04:00
|
|
|
assert_dom_equal expected.encoded, created.encoded
|
2009-11-29 19:28:45 -05:00
|
|
|
|
2014-08-20 04:34:57 -04:00
|
|
|
assert_nothing_raised { UrlTestMailer.signed_up_with_url(@recipient).deliver_now }
|
2009-11-29 19:28:45 -05:00
|
|
|
assert_not_nil ActionMailer::Base.deliveries.first
|
2009-12-16 20:00:32 -05:00
|
|
|
delivered = ActionMailer::Base.deliveries.first
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-16 20:00:32 -05:00
|
|
|
delivered.message_id = '<123@456>'
|
2014-10-07 18:40:59 -04:00
|
|
|
assert_dom_equal expected.encoded, delivered.encoded
|
2006-08-25 02:48:15 -04:00
|
|
|
end
|
2008-01-05 08:32:22 -05:00
|
|
|
end
|