2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
|
2018-09-24 19:12:44 -04:00
|
|
|
AppRoutes.draw do
|
|
|
|
get "/welcome" => "foo#bar", as: "welcome"
|
|
|
|
get "/dummy_model" => "foo#baz", as: "dummy_model"
|
|
|
|
get "/welcome/greeting", to: "welcome#greeting"
|
|
|
|
get "/a/b(/:id)", to: "a#b"
|
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
|
2018-09-24 19:12:44 -04:00
|
|
|
include AppRoutes.url_helpers
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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
|
2017-12-12 06:00:50 -05: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
|
|
|
|
2009-11-29 19:28:45 -05:00
|
|
|
expected = new_mail
|
|
|
|
expected.to = @recipient
|
|
|
|
expected.subject = "[Signed up] Welcome #{@recipient}"
|
2017-08-16 14:34:02 -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 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
|