##// END OF EJS Templates
Adds a test for not sending email without recipient (#4920)....
Jean-Philippe Lang -
r3404:ae55978515a4
parent child
Show More
@@ -1,333 +1,354
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class MailerTest < ActiveSupport::TestCase
21 21 include Redmine::I18n
22 22 include ActionController::Assertions::SelectorAssertions
23 23 fixtures :projects, :enabled_modules, :issues, :users, :members, :member_roles, :roles, :documents, :attachments, :news, :tokens, :journals, :journal_details, :changesets, :trackers, :issue_statuses, :enumerations, :messages, :boards, :repositories
24 24
25 25 def setup
26 26 ActionMailer::Base.deliveries.clear
27 27 Setting.host_name = 'mydomain.foo'
28 28 Setting.protocol = 'http'
29 29 end
30 30
31 31 def test_generated_links_in_emails
32 32 Setting.host_name = 'mydomain.foo'
33 33 Setting.protocol = 'https'
34 34
35 35 journal = Journal.find(2)
36 36 assert Mailer.deliver_issue_edit(journal)
37 37
38 38 mail = ActionMailer::Base.deliveries.last
39 39 assert_kind_of TMail::Mail, mail
40 40
41 41 assert_select_email do
42 42 # link to the main ticket
43 43 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
44 44 # link to a referenced ticket
45 45 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
46 46 # link to a changeset
47 47 assert_select "a[href=?][title=?]", "https://mydomain.foo/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
48 48 end
49 49 end
50 50
51 51 def test_generated_links_with_prefix
52 52 relative_url_root = Redmine::Utils.relative_url_root
53 53 Setting.host_name = 'mydomain.foo/rdm'
54 54 Setting.protocol = 'http'
55 55 Redmine::Utils.relative_url_root = '/rdm'
56 56
57 57 journal = Journal.find(2)
58 58 assert Mailer.deliver_issue_edit(journal)
59 59
60 60 mail = ActionMailer::Base.deliveries.last
61 61 assert_kind_of TMail::Mail, mail
62 62
63 63 assert_select_email do
64 64 # link to the main ticket
65 65 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
66 66 # link to a referenced ticket
67 67 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
68 68 # link to a changeset
69 69 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
70 70 end
71 71 ensure
72 72 # restore it
73 73 Redmine::Utils.relative_url_root = relative_url_root
74 74 end
75 75
76 76 def test_generated_links_with_prefix_and_no_relative_url_root
77 77 relative_url_root = Redmine::Utils.relative_url_root
78 78 Setting.host_name = 'mydomain.foo/rdm'
79 79 Setting.protocol = 'http'
80 80 Redmine::Utils.relative_url_root = nil
81 81
82 82 journal = Journal.find(2)
83 83 assert Mailer.deliver_issue_edit(journal)
84 84
85 85 mail = ActionMailer::Base.deliveries.last
86 86 assert_kind_of TMail::Mail, mail
87 87
88 88 assert_select_email do
89 89 # link to the main ticket
90 90 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
91 91 # link to a referenced ticket
92 92 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
93 93 # link to a changeset
94 94 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
95 95 end
96 96 ensure
97 97 # restore it
98 98 Redmine::Utils.relative_url_root = relative_url_root
99 99 end
100 100
101 101 def test_email_headers
102 102 issue = Issue.find(1)
103 103 Mailer.deliver_issue_add(issue)
104 104 mail = ActionMailer::Base.deliveries.last
105 105 assert_not_nil mail
106 106 assert_equal 'bulk', mail.header_string('Precedence')
107 107 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
108 108 end
109 109
110 110 def test_plain_text_mail
111 111 Setting.plain_text_mail = 1
112 112 journal = Journal.find(2)
113 113 Mailer.deliver_issue_edit(journal)
114 114 mail = ActionMailer::Base.deliveries.last
115 115 assert_equal "text/plain", mail.content_type
116 116 assert_equal 0, mail.parts.size
117 117 assert !mail.encoded.include?('href')
118 118 end
119 119
120 120 def test_html_mail
121 121 Setting.plain_text_mail = 0
122 122 journal = Journal.find(2)
123 123 Mailer.deliver_issue_edit(journal)
124 124 mail = ActionMailer::Base.deliveries.last
125 125 assert_equal 2, mail.parts.size
126 126 assert mail.encoded.include?('href')
127 127 end
128 128
129 129 def test_mail_from_with_phrase
130 130 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
131 131 Mailer.deliver_test(User.find(1))
132 132 end
133 133 mail = ActionMailer::Base.deliveries.last
134 134 assert_not_nil mail
135 135 assert_equal 'Redmine app', mail.from_addrs.first.name
136 136 end
137 137
138 def test_should_not_send_email_without_recipient
139 news = News.find(:first)
140 user = news.author
141 # Remove members except news author
142 news.project.memberships.each {|m| m.destroy unless m.user == user}
143
144 user.pref[:no_self_notified] = false
145 user.pref.save
146 User.current = user
147 Mailer.deliver_news_added(news.reload)
148 assert_equal 1, last_email.bcc.size
149
150 # nobody to notify
151 user.pref[:no_self_notified] = true
152 user.pref.save
153 User.current = user
154 ActionMailer::Base.deliveries.clear
155 Mailer.deliver_news_added(news.reload)
156 assert ActionMailer::Base.deliveries.empty?
157 end
158
138 159 def test_issue_add_message_id
139 160 issue = Issue.find(1)
140 161 Mailer.deliver_issue_add(issue)
141 162 mail = ActionMailer::Base.deliveries.last
142 163 assert_not_nil mail
143 164 assert_equal Mailer.message_id_for(issue), mail.message_id
144 165 assert_nil mail.references
145 166 end
146 167
147 168 def test_issue_edit_message_id
148 169 journal = Journal.find(1)
149 170 Mailer.deliver_issue_edit(journal)
150 171 mail = ActionMailer::Base.deliveries.last
151 172 assert_not_nil mail
152 173 assert_equal Mailer.message_id_for(journal), mail.message_id
153 174 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
154 175 end
155 176
156 177 def test_message_posted_message_id
157 178 message = Message.find(1)
158 179 Mailer.deliver_message_posted(message)
159 180 mail = ActionMailer::Base.deliveries.last
160 181 assert_not_nil mail
161 182 assert_equal Mailer.message_id_for(message), mail.message_id
162 183 assert_nil mail.references
163 184 assert_select_email do
164 185 # link to the message
165 186 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}", :text => message.subject
166 187 end
167 188 end
168 189
169 190 def test_reply_posted_message_id
170 191 message = Message.find(3)
171 192 Mailer.deliver_message_posted(message)
172 193 mail = ActionMailer::Base.deliveries.last
173 194 assert_not_nil mail
174 195 assert_equal Mailer.message_id_for(message), mail.message_id
175 196 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
176 197 assert_select_email do
177 198 # link to the reply
178 199 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}", :text => message.subject
179 200 end
180 201 end
181 202
182 203 context("#issue_add") do
183 204 setup do
184 205 ActionMailer::Base.deliveries.clear
185 206 Setting.bcc_recipients = '1'
186 207 @issue = Issue.find(1)
187 208 end
188 209
189 210 should "notify project members" do
190 211 assert Mailer.deliver_issue_add(@issue)
191 212 assert last_email.bcc.include?('dlopper@somenet.foo')
192 213 end
193 214
194 215 should "not notify project members that are not allow to view the issue" do
195 216 Role.find(2).remove_permission!(:view_issues)
196 217 assert Mailer.deliver_issue_add(@issue)
197 218 assert !last_email.bcc.include?('dlopper@somenet.foo')
198 219 end
199 220
200 221 should "notify issue watchers" do
201 222 user = User.find(9)
202 223 # minimal email notification options
203 224 user.pref[:no_self_notified] = '1'
204 225 user.pref.save
205 226 user.mail_notification = false
206 227 user.save
207 228
208 229 Watcher.create!(:watchable => @issue, :user => user)
209 230 assert Mailer.deliver_issue_add(@issue)
210 231 assert last_email.bcc.include?(user.mail)
211 232 end
212 233
213 234 should "not notify watchers not allowed to view the issue" do
214 235 user = User.find(9)
215 236 Watcher.create!(:watchable => @issue, :user => user)
216 237 Role.non_member.remove_permission!(:view_issues)
217 238 assert Mailer.deliver_issue_add(@issue)
218 239 assert !last_email.bcc.include?(user.mail)
219 240 end
220 241 end
221 242
222 243 # test mailer methods for each language
223 244 def test_issue_add
224 245 issue = Issue.find(1)
225 246 valid_languages.each do |lang|
226 247 Setting.default_language = lang.to_s
227 248 assert Mailer.deliver_issue_add(issue)
228 249 end
229 250 end
230 251
231 252 def test_issue_edit
232 253 journal = Journal.find(1)
233 254 valid_languages.each do |lang|
234 255 Setting.default_language = lang.to_s
235 256 assert Mailer.deliver_issue_edit(journal)
236 257 end
237 258 end
238 259
239 260 def test_document_added
240 261 document = Document.find(1)
241 262 valid_languages.each do |lang|
242 263 Setting.default_language = lang.to_s
243 264 assert Mailer.deliver_document_added(document)
244 265 end
245 266 end
246 267
247 268 def test_attachments_added
248 269 attachements = [ Attachment.find_by_container_type('Document') ]
249 270 valid_languages.each do |lang|
250 271 Setting.default_language = lang.to_s
251 272 assert Mailer.deliver_attachments_added(attachements)
252 273 end
253 274 end
254 275
255 276 def test_news_added
256 277 news = News.find(:first)
257 278 valid_languages.each do |lang|
258 279 Setting.default_language = lang.to_s
259 280 assert Mailer.deliver_news_added(news)
260 281 end
261 282 end
262 283
263 284 def test_message_posted
264 285 message = Message.find(:first)
265 286 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
266 287 recipients = recipients.compact.uniq
267 288 valid_languages.each do |lang|
268 289 Setting.default_language = lang.to_s
269 290 assert Mailer.deliver_message_posted(message)
270 291 end
271 292 end
272 293
273 294 def test_account_information
274 295 user = User.find(:first)
275 296 valid_languages.each do |lang|
276 297 user.update_attribute :language, lang.to_s
277 298 user.reload
278 299 assert Mailer.deliver_account_information(user, 'pAsswORd')
279 300 end
280 301 end
281 302
282 303 def test_lost_password
283 304 token = Token.find(2)
284 305 valid_languages.each do |lang|
285 306 token.user.update_attribute :language, lang.to_s
286 307 token.reload
287 308 assert Mailer.deliver_lost_password(token)
288 309 end
289 310 end
290 311
291 312 def test_register
292 313 token = Token.find(1)
293 314 Setting.host_name = 'redmine.foo'
294 315 Setting.protocol = 'https'
295 316
296 317 valid_languages.each do |lang|
297 318 token.user.update_attribute :language, lang.to_s
298 319 token.reload
299 320 ActionMailer::Base.deliveries.clear
300 321 assert Mailer.deliver_register(token)
301 322 mail = ActionMailer::Base.deliveries.last
302 323 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
303 324 end
304 325 end
305 326
306 327 def test_reminders
307 328 Mailer.reminders(:days => 42)
308 329 assert_equal 1, ActionMailer::Base.deliveries.size
309 330 mail = ActionMailer::Base.deliveries.last
310 331 assert mail.bcc.include?('dlopper@somenet.foo')
311 332 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
312 333 end
313 334
314 335 def last_email
315 336 mail = ActionMailer::Base.deliveries.last
316 337 assert_not_nil mail
317 338 mail
318 339 end
319 340
320 341 def test_mailer_should_not_change_locale
321 342 Setting.default_language = 'en'
322 343 # Set current language to italian
323 344 set_language_if_valid 'it'
324 345 # Send an email to a french user
325 346 user = User.find(1)
326 347 user.language = 'fr'
327 348 Mailer.deliver_account_activated(user)
328 349 mail = ActionMailer::Base.deliveries.last
329 350 assert mail.body.include?('Votre compte')
330 351
331 352 assert_equal :it, current_language
332 353 end
333 354 end
General Comments 0
You need to be logged in to leave comments. Login now