comment_notifier_test.rb
3.04 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
require File.dirname(__FILE__) + '/../test_helper'
class CommentNotifierTest < Test::Unit::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 = []
@profile = create_user('user_comment_test').person
@article = fast_create(Article, :name => 'Article test', :profile_id => @profile.id, :notify_comments => true)
end
should 'deliver mail after make aarticle commment' do
assert_difference ActionMailer::Base.deliveries, :size do
@article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!')
end
end
should 'deliver mail to owner of article' do
@article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!')
sent = ActionMailer::Base.deliveries.first
assert_equal [@profile.email], sent.to
end
should 'display author name in delivered mail' do
@article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!')
sent = ActionMailer::Base.deliveries.first
assert_match /user_comment_test/, sent.body
end
should 'display unauthenticated author name and email in delivered mail' do
@article.comments << Comment.new(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!')
sent = ActionMailer::Base.deliveries.first
assert_match /flatline/, sent.body
assert_match /flatline@invalid.com/, sent.body
end
should 'not deliver mail if notify comments is false' do
@article.update_attribute(:notify_comments, false)
assert_no_difference ActionMailer::Base.deliveries, :size do
@article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!')
end
end
should 'include comment title in the e-mail' do
@article.comments << Comment.new(:author => @profile, :title => 'comment title', :body => 'comment title')
sent = ActionMailer::Base.deliveries.first
assert_match /comment title/, sent.body
end
should 'include comment text in the e-mail' do
@article.comments << Comment.new(:author => @profile, :title => 'comment title', :body => 'comment body')
sent = ActionMailer::Base.deliveries.first
assert_match /comment body/, sent.body
end
should 'not deliver mail if has no notification emails' do
community = fast_create(Community)
assert_equal [], community.notification_emails
article = fast_create(Article, :name => 'Article test', :profile_id => community.id, :notify_comments => true)
assert_no_difference ActionMailer::Base.deliveries, :size do
article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'there is no addresses to send notification')
end
end
private
def read_fixture(action)
IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}")
end
def encode(subject)
quoted_printable(subject, CHARSET)
end
end