cirandas.net

ref: master

plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb


 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
require 'test_helper'

def base_setup
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    environment = Environment.default
    environment.noreply_email = 'noreply@example.com'
    environment.save!
end

def run_common_tests
  should 'not deliver emails via GET requests' do
    get :deliver, @extra_args
    assert_response 403 # forbidden
  end

  should 'deliver emails only via POST requests' do
    post :deliver, @extra_args
    assert_response :success
  end

  should 'render fail template if could not deliver mail' do
    post :deliver, @extra_args
    assert_template 'fail'
  end

  should 'render success template after deliver mail' do
    SendEmailPlugin::Mail.any_instance.stubs(:valid?).returns(true)
    post :deliver, @extra_args.merge(:to => 'john@example.com', :message => 'Hi john')
    assert_template 'success'
  end

  should 'render dialog error if could not deliver mail by ajax request' do
    xhr :post, :deliver, @extra_args
    assert_template '_dialog_error_messages'
  end

  should 'render success message after deliver mail by ajax request' do
    SendEmailPlugin::Mail.any_instance.stubs(:valid?).returns(true)
    xhr :post, :deliver, @extra_args.merge(:to => 'john@example.com', :message => 'Hi john')
    assert_equal 'Message sent', @response.body
  end

  should 'deliver mail' do
    Environment.any_instance.stubs(:send_email_plugin_allow_to).returns('john@example.com')
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      post :deliver, @extra_args.merge(:to => 'john@example.com', :message => 'Hi john')
    end
  end

  should 'deliver mail with nondefault subject' do
    Environment.any_instance.stubs(:send_email_plugin_allow_to).returns('john@example.com')
    post :deliver, @extra_args.merge(:to => 'john@example.com', :message => 'Hi john', :subject => 'Hello john')
    assert_equal '[Colivre.net] Hello john', ActionMailer::Base.deliveries.first.subject
  end

  should 'deliver mail with message from view' do
    Environment.any_instance.stubs(:send_email_plugin_allow_to).returns('john@example.com')
    post :deliver, @extra_args.merge(:to => 'john@example.com', :message => 'Hi john', :subject => 'Hello john')
    assert_match /Contact from/, ActionMailer::Base.deliveries.first.body.to_s
  end

end

class SendEmailPluginProfileControllerTest < ActionController::TestCase
  def setup
    base_setup
    community = fast_create(Community)
    @extra_args = {:profile => community.identifier}
  end

  run_common_tests
end

class SendEmailPluginControllerTest < ActionController::TestCase
  def setup
    base_setup
    @extra_args = {}
  end

  run_common_tests
end