task_mailer_test.rb
4.65 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
require_relative "../test_helper"
class TaskMailerTest < ActiveSupport::TestCase
FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
CHARSET = "utf-8"
def setup
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
@environment = Environment.default
@environment.noreply_email = 'noreply@example.com'
@environment.stubs(:default_hostname).returns('example.com')
@environment.name = 'example'
end
attr_reader :environment
should 'be able to send a "task finished" message' do
task = Task.new
task.expects(:task_finished_message).returns('the message')
task.expects(:target_notification_description).returns('the task')
task.target = task.requestor = person = Person.new
person.environment = environment
person.name = 'my name'
person.stubs(:contact_email).returns('requestor@example.com')
person.stubs(:public_profile_url).returns('requestor_path')
task.send(:send_notification, :finished).deliver
refute ActionMailer::Base.deliveries.empty?
end
should 'be able to send a "task cancelled" message' do
task = Task.new
task.expects(:task_cancelled_message).returns('the message')
task.expects(:target_notification_description).returns('the task')
task.target = task.requestor = person = Person.new
person.environment = environment
person.name = 'my name'
person.stubs(:contact_email).returns('requestor@example.com')
person.stubs(:public_profile_url).returns('requestor_path')
task.send(:send_notification, :cancelled).deliver
refute ActionMailer::Base.deliveries.empty?
end
should 'be able to send a "task created" message' do
task = Task.new
task.expects(:task_created_message).returns('the message')
task.expects(:target_notification_description).returns('the task')
task.target = task.requestor = person = Person.new
person.environment = environment
person.name = 'my name'
person.stubs(:contact_email).returns('requestor@example.com')
person.stubs(:public_profile_url).returns('requestor_path')
task.send(:send_notification, :created).deliver
refute ActionMailer::Base.deliveries.empty?
end
should 'be able to send a "target notification" message' do
requestor = fast_create(Person)
requestor.expects(:notification_emails).returns(['requestor@example.com'])
task = Task.new(:target => requestor)
task.expects(:target_notification_description).returns('the task')
TaskMailer.target_notification(task, 'the message').deliver
refute ActionMailer::Base.deliveries.empty?
end
should 'be able to send a "invitation notification" message' do
task = InviteFriend.new
task.expects(:code).returns('123456')
task.target = task.requestor = person = Person.new
person.environment = environment
person.name = 'my name'
person.stubs(:public_profile_url).returns('requestor_path')
task.stubs(:message).returns('Hello <friend>, <user> invite you, please follow this link: <url>')
task.expects(:friend_email).returns('friend@exemple.com')
task.expects(:friend_name).returns('friend name').at_least_once
mail = TaskMailer.invitation_notification(task)
assert_match(/#{task.target_notification_description}/, mail.subject)
assert_equal "Hello friend name, my name invite you, please follow this link: http://example.com/account/signup?invitation_code=123456", mail.body.to_s
mail.deliver
refute ActionMailer::Base.deliveries.empty?
end
should 'use environment name and no-reply email' do
task = mock
task.expects(:environment).returns(environment).at_least_once
assert_equal "#{environment.name} <#{environment.noreply_email}>", TaskMailer.generate_from(task)
end
should 'return the email with the subdirectory defined' do
Noosfero.stubs(:root).returns('/subdir')
task = InviteFriend.new
task.expects(:code).returns('123456')
task.target = task.requestor = person = Person.new
person.environment = environment
person.name = 'my name'
person.stubs(:public_profile_url).returns('requestor_path')
task.stubs(:message).returns('Hello <friend>, <user> invite you, please follow this link: <url>')
task.expects(:friend_email).returns('friend@exemple.com')
task.expects(:friend_name).returns('friend name').at_least_once
mail = TaskMailer.invitation_notification(task)
url_to_compare = "/subdir/account/signup"
assert_match(/#{url_to_compare}/, mail.body.to_s)
end
private
def read_fixture(action)
IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}")
end
def encode(subject)
quoted_printable(subject, CHARSET)
end
end