File: markerb_test.rb

package info (click to toggle)
ruby-markerb 1.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 196 kB
  • sloc: ruby: 202; makefile: 6
file content (110 lines) | stat: -rw-r--r-- 3,518 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require "test_helper"

class Notifier < ActionMailer::Base
  self.view_paths = File.expand_path("../views", __FILE__)

  layout false

  def contact(recipient, format_type)
    @recipient = recipient
    mail(:to => @recipient, :from => "john.doe@example.com") do |format|
      format.send(format_type)
    end
  end

  def link(format_type)
    mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
      format.send(format_type)
    end
  end

  def user(format_type)
    mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
      format.send(format_type)
    end
  end

  def multiple_format_contact(recipient)
    @recipient = recipient
    mail(:to => @recipient, :from => "john.doe@example.com", :template => "contact") do |format|
      format.text  { render 'contact' }
      format.html  { render 'contact' }
    end
  end
end

class TestRenderer < Redcarpet::Render::HTML
  attr_accessor :show_text
  def initialize(render_options = {})
    @show_text = render_options.delete(:show_text)
    super(render_options)
  end

  def normal_text(text)
    show_text ? "TEST #{text}" : "TEST"
  end
end

class MarkerbTest < ActiveSupport::TestCase

  setup do
    @original_renderer = Markerb.renderer
    @original_processing_options = Markerb.processing_options
  end

  teardown do
    Markerb.renderer = @original_renderer
    Markerb.processing_options = @original_processing_options
  end

  test "plain text should be sent as a plain text" do
    email = Notifier.contact("you@example.com", :text)
    assert_equal "text/plain", email.mime_type
    assert_equal "Dual templates **rocks**!", email.body.encoded.strip
  end

  test "html should be sent as html" do
    email = Notifier.contact("you@example.com", :html)
    assert_equal "text/html", email.mime_type
    assert_equal "<p>Dual templates <strong>rocks</strong>!</p>", email.body.encoded.strip
  end

  test 'dealing with multipart e-mails' do
    email = Notifier.multiple_format_contact("you@example.com")
    assert_equal 2, email.parts.size
    assert_equal "multipart/alternative", email.mime_type
    assert_equal "text/plain", email.parts[0].mime_type
    assert_equal "Dual templates **rocks**!",
      email.parts[0].body.encoded.strip
    assert_equal "text/html", email.parts[1].mime_type
    assert_equal "<p>Dual templates <strong>rocks</strong>!</p>",
      email.parts[1].body.encoded.strip
  end

  test "with a custom renderer" do
    Markerb.renderer = TestRenderer
    email = Notifier.contact("you@example.com", :html)
    assert_equal "text/html", email.mime_type
    assert_equal "<p>TEST<strong>TEST</strong>TEST</p>", email.body.encoded.strip
  end

  test "with a custom renderer and options" do
    Markerb.renderer = TestRenderer.new(:show_text => true)
    email = Notifier.contact("you@example.com", :html)
    assert_equal "text/html", email.mime_type
    assert_equal "<p>TEST Dual templates <strong>TEST rocks</strong>TEST !</p>", email.body.encoded.strip
  end

  test 'with custom markdown processing options' do
    Markerb.processing_options = {:autolink => true}
    email = Notifier.link(:html)
    assert_equal "text/html", email.mime_type
    assert_equal '<p>Hello from <a href="http://www.fcstpauli.com">http://www.fcstpauli.com</a></p>', email.body.encoded.strip
  end

  test 'with partial' do
    email = Notifier.user(:html)
    assert_equal "text/html", email.mime_type
    assert_equal '<p>woot! <strong>Partial</strong></p>', email.body.encoded.strip
  end
end