| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
class TestMailer < ActionMailer::Base |
|---|
| 4 |
|
|---|
| 5 |
default_url_options[:host] = 'www.basecamphq.com' |
|---|
| 6 |
|
|---|
| 7 |
def signed_up_with_url(recipient) |
|---|
| 8 |
@recipients = recipient |
|---|
| 9 |
@subject = "[Signed up] Welcome #{recipient}" |
|---|
| 10 |
@from = "system@loudthinking.com" |
|---|
| 11 |
@sent_on = Time.local(2004, 12, 12) |
|---|
| 12 |
|
|---|
| 13 |
@body["recipient"] = recipient |
|---|
| 14 |
@body["welcome_url"] = url_for :host => "example.com", :controller => "welcome", :action => "greeting" |
|---|
| 15 |
end |
|---|
| 16 |
|
|---|
| 17 |
class <<self |
|---|
| 18 |
attr_accessor :received_body |
|---|
| 19 |
end |
|---|
| 20 |
|
|---|
| 21 |
def receive(mail) |
|---|
| 22 |
self.class.received_body = mail.body |
|---|
| 23 |
end |
|---|
| 24 |
end |
|---|
| 25 |
|
|---|
| 26 |
class ActionMailerUrlTest < Test::Unit::TestCase |
|---|
| 27 |
include ActionMailer::Quoting |
|---|
| 28 |
|
|---|
| 29 |
def encode( text, charset="utf-8" ) |
|---|
| 30 |
quoted_printable( text, charset ) |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
def new_mail( charset="utf-8" ) |
|---|
| 34 |
mail = TMail::Mail.new |
|---|
| 35 |
mail.mime_version = "1.0" |
|---|
| 36 |
if charset |
|---|
| 37 |
mail.set_content_type "text", "plain", { "charset" => charset } |
|---|
| 38 |
end |
|---|
| 39 |
mail |
|---|
| 40 |
end |
|---|
| 41 |
|
|---|
| 42 |
def setup |
|---|
| 43 |
set_delivery_method :test |
|---|
| 44 |
ActionMailer::Base.perform_deliveries = true |
|---|
| 45 |
ActionMailer::Base.deliveries = [] |
|---|
| 46 |
|
|---|
| 47 |
@recipient = 'test@localhost' |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
def teardown |
|---|
| 51 |
restore_delivery_method |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
def test_signed_up_with_url |
|---|
| 55 |
ActionController::Routing::Routes.draw do |map| |
|---|
| 56 |
map.connect ':controller/:action/:id' |
|---|
| 57 |
map.welcome 'welcome', :controller=>"foo", :action=>"bar" |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
expected = new_mail |
|---|
| 61 |
expected.to = @recipient |
|---|
| 62 |
expected.subject = "[Signed up] Welcome #{@recipient}" |
|---|
| 63 |
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\" />" |
|---|
| 64 |
expected.from = "system@loudthinking.com" |
|---|
| 65 |
expected.date = Time.local(2004, 12, 12) |
|---|
| 66 |
|
|---|
| 67 |
created = nil |
|---|
| 68 |
assert_nothing_raised { created = TestMailer.create_signed_up_with_url(@recipient) } |
|---|
| 69 |
assert_not_nil created |
|---|
| 70 |
assert_equal expected.encoded, created.encoded |
|---|
| 71 |
|
|---|
| 72 |
assert_nothing_raised { TestMailer.deliver_signed_up_with_url(@recipient) } |
|---|
| 73 |
assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 74 |
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|