2016-08-06 13:03:39 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
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
|
2016-08-06 13:03:39 -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|
|
2016-08-06 13:03:39 -04:00
|
|
|
c.assets_dir = "" # To get the tests to pass
|
2010-03-04 04:12:16 -05:00
|
|
|
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)
|
2016-08-06 13:03:39 -04:00
|
|
|
mail(from: "from@example.com", to: "to@example.com", subject: "subject")
|
2014-11-23 16:53:01 -05:00
|
|
|
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
|
2016-08-06 13:03:39 -04:00
|
|
|
OpenStruct.new(route_key: "dummy_model")
|
2014-11-23 16:53:01 -05:00
|
|
|
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
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def encode(text, charset = "UTF-8")
|
|
|
|
quoted_printable(text, charset)
|
2006-08-25 02:48:15 -04:00
|
|
|
end
|
|
|
|
|
2016-10-28 23:05:58 -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)
|
2016-08-06 13:03:39 -04:00
|
|
|
expected = "http://www.basecamphq.com#{expected}" if expected.start_with?("/") && !relative
|
2014-11-23 16:53:01 -05:00
|
|
|
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
|
2016-08-06 13:03:39 -04:00
|
|
|
@recipient = "test@localhost"
|
2006-08-25 02:48:15 -04:00
|
|
|
end
|
|
|
|
|
2014-11-23 16:53:01 -05:00
|
|
|
def test_url_for
|
|
|
|
UrlTestMailer.delivery_method = :test
|
|
|
|
|
|
|
|
AppRoutes.draw do
|
2016-03-30 12:07:43 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 13:03:39 -04:00
|
|
|
get ":controller(/:action(/:id))"
|
2016-10-28 23:05:58 -04:00
|
|
|
get "/welcome" => "foo#bar", as: "welcome"
|
2016-08-06 13:03:39 -04:00
|
|
|
get "/dummy_model" => "foo#baz", as: "dummy_model"
|
2016-03-30 12:07:43 -04:00
|
|
|
end
|
2014-11-23 16:53:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# string
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_url_for "http://foo/", "http://foo/"
|
2014-11-23 16:53:01 -05:00
|
|
|
|
|
|
|
# symbol
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_url_for "/welcome", :welcome
|
2014-11-23 16:53:01 -05:00
|
|
|
|
|
|
|
# hash
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_url_for "/a/b/c", controller: "a", action: "b", id: "c"
|
2016-08-16 03:30:11 -04: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
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_url_for "/dummy_model", DummyModel.new
|
2014-11-23 16:53:01 -05:00
|
|
|
|
|
|
|
# class
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_url_for "/dummy_model", DummyModel
|
2014-11-23 16:53:01 -05:00
|
|
|
|
|
|
|
# array
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_url_for "/dummy_model" , [DummyModel]
|
2014-11-23 16:53:01 -05:00
|
|
|
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
|
2016-03-30 12:07:43 -04:00
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 13:03:39 -04:00
|
|
|
get ":controller(/:action(/:id))"
|
|
|
|
get "/welcome" => "foo#bar", as: "welcome"
|
2016-03-30 12:07:43 -04:00
|
|
|
end
|
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
|
|
|
|
2016-08-06 13:03:39 -04: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
|
|
|
|
2016-08-06 13:03:39 -04: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
|