Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 144 lines (115 sloc) 4.553 kb
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
# coding: utf-8

require 'test_helper'

class MailFormNotifierTest < ActiveSupport::TestCase

  def setup
    @form = ContactForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => 'Cool')

    @request = ActionController::TestRequest.new
    @valid_attributes = { :name => 'José', :email => 'my.email@my.domain.com', :message => "Cool\nno?" }
    @advanced = AdvancedForm.new(@valid_attributes)
    @advanced.request = @request

    test_file = Rack::Test::UploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
    @with_file = FileForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => "Cool", :file => test_file)

    ActionMailer::Base.deliveries = []
  end

  def test_email_is_sent
    @form.deliver
    assert_equal 1, ActionMailer::Base.deliveries.size
  end

  def test_subject_defaults_to_human_class_name
    @form.deliver
    assert_equal 'Contact form', first_delivery.subject
  end

  def test_body_contains_subject
    @form.deliver
    assert_match %r[Contact form], first_delivery.body.to_s
  end

  def test_body_contains_attributes_values
    @form.deliver
    assert_match %r[José], first_delivery.body.to_s
    assert_match %r[my.email@my.domain.com], first_delivery.body.to_s
    assert_match %r[Cool], first_delivery.body.to_s
  end

  def test_body_contains_attributes_names
    @form.deliver
    assert_match %r[Name:], first_delivery.body.to_s
    assert_match %r[Email:], first_delivery.body.to_s
    assert_match %r[Message:], first_delivery.body.to_s
  end

  def test_body_contains_localized_attributes_names
    I18n.backend.store_translations(:en, :mail_form => { :attributes => { :contact_form => { :message => 'Sent message' } } })
    @form.deliver
    assert_match %r[Sent message:], first_delivery.body.to_s
    assert_no_match %r[Message:], first_delivery.body.to_s
  end

  def test_body_mail_format_messages_with_break_line
    @form.deliver
    assert_no_match %r[<p>Cool], first_delivery.body.to_s

    @advanced.deliver
    assert_match %r[<p>Cool], last_delivery.body.to_s
  end

  def test_body_mail_format_dates_with_i18n
    @form.deliver
    assert_no_match %r[I18n.l(Date.today)], first_delivery.body.to_s
  end

  def test_body_does_not_append_request_if_append_is_not_called
    @form.deliver
    assert_no_match %r[Request information], first_delivery.body.to_s
  end

  def test_body_does_append_request_if_append_is_called
    @advanced.deliver
    assert_match %r[Request information], last_delivery.body.to_s
  end

  def test_request_title_is_localized
    I18n.backend.store_translations(:en, :mail_form => { :request => { :title => 'Information about the request' } })
    @advanced.deliver
    assert_no_match %r[Request information], last_delivery.body.to_s
    assert_match %r[Information about the request], last_delivery.body.to_s
  end

  def test_request_info_attributes_are_printed
    @advanced.deliver
    assert_match %r[Remote ip], last_delivery.body.to_s
    assert_match %r[User agent], last_delivery.body.to_s
  end

  def test_request_info_attributes_are_localized
    I18n.backend.store_translations(:en, :mail_form => { :request => { :remote_ip => 'IP Address' } })
    @advanced.deliver
    assert_match %r[IP Address], last_delivery.body.to_s
    assert_no_match %r[Remote ip], last_delivery.body.to_s
  end

  def test_request_info_values_are_printed
    @advanced.deliver
    assert_match %r[0\.0\.0\.0], last_delivery.body.to_s
    assert_match %r[Rails Testing], last_delivery.body.to_s
  end

  def test_request_info_hashes_are_print_inside_lis
    @request.session = { :my => :session, :user => "data" }
    @advanced.deliver
    assert_match %r[<ul], last_delivery.body.to_s
    assert_match %r[<li>my: :session<\/li>], last_delivery.body.to_s
    assert_match %r[<li>user: &quot;data&quot;<\/li>], last_delivery.body.to_s
  end

  def test_error_is_raised_when_append_is_given_but_no_request_is_given
    assert_raise ScriptError do
      @advanced.request = nil
      @advanced.deliver
    end
  end

  def test_form_with_file_includes_an_attachment
    @with_file.deliver
    assert_equal 1, first_delivery.attachments.size
  end

  def test_form_with_file_does_not_output_attachment_as_attribute
    @with_file.deliver
    assert_no_match %r[File:], first_delivery.body.to_s
  end

  protected

    def first_delivery
      ActionMailer::Base.deliveries.first
    end

    def last_delivery
      ActionMailer::Base.deliveries.last
    end

    def teardown
      I18n.reload!
    end
end
Something went wrong with that request. Please try again.