##// END OF EJS Templates
Fixed that the mail method should return a Mail::Message (#15113)....
Jean-Philippe Lang -
r11980:176ce785740c
parent child
Show More
@@ -1,489 +1,494
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 class Mailer < ActionMailer::Base
19 19 layout 'mailer'
20 20 helper :application
21 21 helper :issues
22 22 helper :custom_fields
23 23
24 24 include Redmine::I18n
25 25
26 26 def self.default_url_options
27 27 { :host => Setting.host_name, :protocol => Setting.protocol }
28 28 end
29 29
30 30 # Builds a mail for notifying to_users and cc_users about a new issue
31 31 def issue_add(issue, to_users, cc_users)
32 32 redmine_headers 'Project' => issue.project.identifier,
33 33 'Issue-Id' => issue.id,
34 34 'Issue-Author' => issue.author.login
35 35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
36 36 message_id issue
37 37 references issue
38 38 @author = issue.author
39 39 @issue = issue
40 40 @users = to_users + cc_users
41 41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
42 42 mail :to => to_users.map(&:mail),
43 43 :cc => cc_users.map(&:mail),
44 44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
45 45 end
46 46
47 47 # Notifies users about a new issue
48 48 def self.deliver_issue_add(issue)
49 49 to = issue.notified_users
50 50 cc = issue.notified_watchers - to
51 51 issue.each_notification(to + cc) do |users|
52 52 Mailer.issue_add(issue, to & users, cc & users).deliver
53 53 end
54 54 end
55 55
56 56 # Builds a mail for notifying to_users and cc_users about an issue update
57 57 def issue_edit(journal, to_users, cc_users)
58 58 issue = journal.journalized
59 59 redmine_headers 'Project' => issue.project.identifier,
60 60 'Issue-Id' => issue.id,
61 61 'Issue-Author' => issue.author.login
62 62 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
63 63 message_id journal
64 64 references issue
65 65 @author = journal.user
66 66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
67 67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
68 68 s << issue.subject
69 69 @issue = issue
70 70 @users = to_users + cc_users
71 71 @journal = journal
72 72 @journal_details = journal.visible_details(@users.first)
73 73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
74 74 mail :to => to_users.map(&:mail),
75 75 :cc => cc_users.map(&:mail),
76 76 :subject => s
77 77 end
78 78
79 79 # Notifies users about an issue update
80 80 def self.deliver_issue_edit(journal)
81 81 issue = journal.journalized.reload
82 82 to = journal.notified_users
83 83 cc = journal.notified_watchers
84 84 journal.each_notification(to + cc) do |users|
85 85 issue.each_notification(users) do |users2|
86 86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
87 87 end
88 88 end
89 89 end
90 90
91 91 def reminder(user, issues, days)
92 92 set_language_if_valid user.language
93 93 @issues = issues
94 94 @days = days
95 95 @issues_url = url_for(:controller => 'issues', :action => 'index',
96 96 :set_filter => 1, :assigned_to_id => user.id,
97 97 :sort => 'due_date:asc')
98 98 mail :to => user.mail,
99 99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
100 100 end
101 101
102 102 # Builds a Mail::Message object used to email users belonging to the added document's project.
103 103 #
104 104 # Example:
105 105 # document_added(document) => Mail::Message object
106 106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
107 107 def document_added(document)
108 108 redmine_headers 'Project' => document.project.identifier
109 109 @author = User.current
110 110 @document = document
111 111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
112 112 mail :to => document.recipients,
113 113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
114 114 end
115 115
116 116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
117 117 #
118 118 # Example:
119 119 # attachments_added(attachments) => Mail::Message object
120 120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
121 121 def attachments_added(attachments)
122 122 container = attachments.first.container
123 123 added_to = ''
124 124 added_to_url = ''
125 125 @author = attachments.first.author
126 126 case container.class.name
127 127 when 'Project'
128 128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
129 129 added_to = "#{l(:label_project)}: #{container}"
130 130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
131 131 when 'Version'
132 132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
133 133 added_to = "#{l(:label_version)}: #{container.name}"
134 134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
135 135 when 'Document'
136 136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
137 137 added_to = "#{l(:label_document)}: #{container.title}"
138 138 recipients = container.recipients
139 139 end
140 140 redmine_headers 'Project' => container.project.identifier
141 141 @attachments = attachments
142 142 @added_to = added_to
143 143 @added_to_url = added_to_url
144 144 mail :to => recipients,
145 145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
146 146 end
147 147
148 148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
149 149 #
150 150 # Example:
151 151 # news_added(news) => Mail::Message object
152 152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
153 153 def news_added(news)
154 154 redmine_headers 'Project' => news.project.identifier
155 155 @author = news.author
156 156 message_id news
157 157 references news
158 158 @news = news
159 159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
160 160 mail :to => news.recipients,
161 161 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
162 162 end
163 163
164 164 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
165 165 #
166 166 # Example:
167 167 # news_comment_added(comment) => Mail::Message object
168 168 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
169 169 def news_comment_added(comment)
170 170 news = comment.commented
171 171 redmine_headers 'Project' => news.project.identifier
172 172 @author = comment.author
173 173 message_id comment
174 174 references news
175 175 @news = news
176 176 @comment = comment
177 177 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
178 178 mail :to => news.recipients,
179 179 :cc => news.watcher_recipients,
180 180 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
181 181 end
182 182
183 183 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
184 184 #
185 185 # Example:
186 186 # message_posted(message) => Mail::Message object
187 187 # Mailer.message_posted(message).deliver => sends an email to the recipients
188 188 def message_posted(message)
189 189 redmine_headers 'Project' => message.project.identifier,
190 190 'Topic-Id' => (message.parent_id || message.id)
191 191 @author = message.author
192 192 message_id message
193 193 references message.root
194 194 recipients = message.recipients
195 195 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
196 196 @message = message
197 197 @message_url = url_for(message.event_url)
198 198 mail :to => recipients,
199 199 :cc => cc,
200 200 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
201 201 end
202 202
203 203 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
204 204 #
205 205 # Example:
206 206 # wiki_content_added(wiki_content) => Mail::Message object
207 207 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
208 208 def wiki_content_added(wiki_content)
209 209 redmine_headers 'Project' => wiki_content.project.identifier,
210 210 'Wiki-Page-Id' => wiki_content.page.id
211 211 @author = wiki_content.author
212 212 message_id wiki_content
213 213 recipients = wiki_content.recipients
214 214 cc = wiki_content.page.wiki.watcher_recipients - recipients
215 215 @wiki_content = wiki_content
216 216 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
217 217 :project_id => wiki_content.project,
218 218 :id => wiki_content.page.title)
219 219 mail :to => recipients,
220 220 :cc => cc,
221 221 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
222 222 end
223 223
224 224 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
225 225 #
226 226 # Example:
227 227 # wiki_content_updated(wiki_content) => Mail::Message object
228 228 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
229 229 def wiki_content_updated(wiki_content)
230 230 redmine_headers 'Project' => wiki_content.project.identifier,
231 231 'Wiki-Page-Id' => wiki_content.page.id
232 232 @author = wiki_content.author
233 233 message_id wiki_content
234 234 recipients = wiki_content.recipients
235 235 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
236 236 @wiki_content = wiki_content
237 237 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
238 238 :project_id => wiki_content.project,
239 239 :id => wiki_content.page.title)
240 240 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
241 241 :project_id => wiki_content.project, :id => wiki_content.page.title,
242 242 :version => wiki_content.version)
243 243 mail :to => recipients,
244 244 :cc => cc,
245 245 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
246 246 end
247 247
248 248 # Builds a Mail::Message object used to email the specified user their account information.
249 249 #
250 250 # Example:
251 251 # account_information(user, password) => Mail::Message object
252 252 # Mailer.account_information(user, password).deliver => sends account information to the user
253 253 def account_information(user, password)
254 254 set_language_if_valid user.language
255 255 @user = user
256 256 @password = password
257 257 @login_url = url_for(:controller => 'account', :action => 'login')
258 258 mail :to => user.mail,
259 259 :subject => l(:mail_subject_register, Setting.app_title)
260 260 end
261 261
262 262 # Builds a Mail::Message object used to email all active administrators of an account activation request.
263 263 #
264 264 # Example:
265 265 # account_activation_request(user) => Mail::Message object
266 266 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
267 267 def account_activation_request(user)
268 268 # Send the email to all active administrators
269 269 recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact
270 270 @user = user
271 271 @url = url_for(:controller => 'users', :action => 'index',
272 272 :status => User::STATUS_REGISTERED,
273 273 :sort_key => 'created_on', :sort_order => 'desc')
274 274 mail :to => recipients,
275 275 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
276 276 end
277 277
278 278 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
279 279 #
280 280 # Example:
281 281 # account_activated(user) => Mail::Message object
282 282 # Mailer.account_activated(user).deliver => sends an email to the registered user
283 283 def account_activated(user)
284 284 set_language_if_valid user.language
285 285 @user = user
286 286 @login_url = url_for(:controller => 'account', :action => 'login')
287 287 mail :to => user.mail,
288 288 :subject => l(:mail_subject_register, Setting.app_title)
289 289 end
290 290
291 291 def lost_password(token)
292 292 set_language_if_valid(token.user.language)
293 293 @token = token
294 294 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
295 295 mail :to => token.user.mail,
296 296 :subject => l(:mail_subject_lost_password, Setting.app_title)
297 297 end
298 298
299 299 def register(token)
300 300 set_language_if_valid(token.user.language)
301 301 @token = token
302 302 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
303 303 mail :to => token.user.mail,
304 304 :subject => l(:mail_subject_register, Setting.app_title)
305 305 end
306 306
307 307 def test_email(user)
308 308 set_language_if_valid(user.language)
309 309 @url = url_for(:controller => 'welcome')
310 310 mail :to => user.mail,
311 311 :subject => 'Redmine test'
312 312 end
313 313
314 314 # Sends reminders to issue assignees
315 315 # Available options:
316 316 # * :days => how many days in the future to remind about (defaults to 7)
317 317 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
318 318 # * :project => id or identifier of project to process (defaults to all projects)
319 319 # * :users => array of user/group ids who should be reminded
320 320 def self.reminders(options={})
321 321 days = options[:days] || 7
322 322 project = options[:project] ? Project.find(options[:project]) : nil
323 323 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
324 324 user_ids = options[:users]
325 325
326 326 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
327 327 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
328 328 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
329 329 )
330 330 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
331 331 scope = scope.where(:project_id => project.id) if project
332 332 scope = scope.where(:tracker_id => tracker.id) if tracker
333 333
334 334 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to)
335 335 issues_by_assignee.keys.each do |assignee|
336 336 if assignee.is_a?(Group)
337 337 assignee.users.each do |user|
338 338 issues_by_assignee[user] ||= []
339 339 issues_by_assignee[user] += issues_by_assignee[assignee]
340 340 end
341 341 end
342 342 end
343 343
344 344 issues_by_assignee.each do |assignee, issues|
345 345 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
346 346 end
347 347 end
348 348
349 349 # Activates/desactivates email deliveries during +block+
350 350 def self.with_deliveries(enabled = true, &block)
351 351 was_enabled = ActionMailer::Base.perform_deliveries
352 352 ActionMailer::Base.perform_deliveries = !!enabled
353 353 yield
354 354 ensure
355 355 ActionMailer::Base.perform_deliveries = was_enabled
356 356 end
357 357
358 358 # Sends emails synchronously in the given block
359 359 def self.with_synched_deliveries(&block)
360 360 saved_method = ActionMailer::Base.delivery_method
361 361 if m = saved_method.to_s.match(%r{^async_(.+)$})
362 362 synched_method = m[1]
363 363 ActionMailer::Base.delivery_method = synched_method.to_sym
364 364 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
365 365 end
366 366 yield
367 367 ensure
368 368 ActionMailer::Base.delivery_method = saved_method
369 369 end
370 370
371 def mail(headers={})
371 def mail(headers={}, &block)
372 372 headers.merge! 'X-Mailer' => 'Redmine',
373 373 'X-Redmine-Host' => Setting.host_name,
374 374 'X-Redmine-Site' => Setting.app_title,
375 375 'X-Auto-Response-Suppress' => 'OOF',
376 376 'Auto-Submitted' => 'auto-generated',
377 377 'From' => Setting.mail_from,
378 378 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
379 379
380 380 # Removes the author from the recipients and cc
381 381 # if the author does not want to receive notifications
382 382 # about what the author do
383 383 if @author && @author.logged? && @author.pref.no_self_notified
384 384 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
385 385 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
386 386 end
387 387
388 388 if @author && @author.logged?
389 389 redmine_headers 'Sender' => @author.login
390 390 end
391 391
392 392 # Blind carbon copy recipients
393 393 if Setting.bcc_recipients?
394 394 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
395 395 headers[:to] = nil
396 396 headers[:cc] = nil
397 397 end
398 398
399 399 if @message_id_object
400 400 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
401 401 end
402 402 if @references_objects
403 403 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
404 404 end
405 405
406 super headers do |format|
407 format.text
408 format.html unless Setting.plain_text_mail?
406 m = if block_given?
407 super headers, &block
408 else
409 super headers do |format|
410 format.text
411 format.html unless Setting.plain_text_mail?
412 end
409 413 end
410
411 414 set_language_if_valid @initial_language
415
416 m
412 417 end
413 418
414 419 def initialize(*args)
415 420 @initial_language = current_language
416 421 set_language_if_valid Setting.default_language
417 422 super
418 423 end
419 424
420 425 def self.deliver_mail(mail)
421 426 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
422 427 begin
423 428 # Log errors when raise_delivery_errors is set to false, Rails does not
424 429 mail.raise_delivery_errors = true
425 430 super
426 431 rescue Exception => e
427 432 if ActionMailer::Base.raise_delivery_errors
428 433 raise e
429 434 else
430 435 Rails.logger.error "Email delivery error: #{e.message}"
431 436 end
432 437 end
433 438 end
434 439
435 440 def self.method_missing(method, *args, &block)
436 441 if m = method.to_s.match(%r{^deliver_(.+)$})
437 442 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
438 443 send(m[1], *args).deliver
439 444 else
440 445 super
441 446 end
442 447 end
443 448
444 449 private
445 450
446 451 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
447 452 def redmine_headers(h)
448 453 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
449 454 end
450 455
451 456 def self.token_for(object, rand=true)
452 457 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
453 458 hash = [
454 459 "redmine",
455 460 "#{object.class.name.demodulize.underscore}-#{object.id}",
456 461 timestamp.strftime("%Y%m%d%H%M%S")
457 462 ]
458 463 if rand
459 464 hash << Redmine::Utils.random_hex(8)
460 465 end
461 466 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
462 467 host = "#{::Socket.gethostname}.redmine" if host.empty?
463 468 "#{hash.join('.')}@#{host}"
464 469 end
465 470
466 471 # Returns a Message-Id for the given object
467 472 def self.message_id_for(object)
468 473 token_for(object, true)
469 474 end
470 475
471 476 # Returns a uniq token for a given object referenced by all notifications
472 477 # related to this object
473 478 def self.references_for(object)
474 479 token_for(object, false)
475 480 end
476 481
477 482 def message_id(object)
478 483 @message_id_object = object
479 484 end
480 485
481 486 def references(object)
482 487 @references_objects ||= []
483 488 @references_objects << object
484 489 end
485 490
486 491 def mylogger
487 492 Rails.logger
488 493 end
489 494 end
@@ -1,743 +1,747
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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.expand_path('../../test_helper', __FILE__)
19 19
20 20 class MailerTest < ActiveSupport::TestCase
21 21 include Redmine::I18n
22 22 include ActionDispatch::Assertions::SelectorAssertions
23 23 fixtures :projects, :enabled_modules, :issues, :users, :members,
24 24 :member_roles, :roles, :documents, :attachments, :news,
25 25 :tokens, :journals, :journal_details, :changesets,
26 26 :trackers, :projects_trackers,
27 27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
28 28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
29 29 :versions,
30 30 :comments
31 31
32 32 def setup
33 33 ActionMailer::Base.deliveries.clear
34 34 Setting.host_name = 'mydomain.foo'
35 35 Setting.protocol = 'http'
36 36 Setting.plain_text_mail = '0'
37 37 end
38 38
39 39 def test_generated_links_in_emails
40 40 Setting.default_language = 'en'
41 41 Setting.host_name = 'mydomain.foo'
42 42 Setting.protocol = 'https'
43 43
44 44 journal = Journal.find(3)
45 45 assert Mailer.deliver_issue_edit(journal)
46 46
47 47 mail = last_email
48 48 assert_not_nil mail
49 49
50 50 assert_select_email do
51 51 # link to the main ticket
52 52 assert_select 'a[href=?]',
53 53 'https://mydomain.foo/issues/2#change-3',
54 54 :text => 'Feature request #2: Add ingredients categories'
55 55 # link to a referenced ticket
56 56 assert_select 'a[href=?][title=?]',
57 57 'https://mydomain.foo/issues/1',
58 58 'Can&#x27;t print recipes (New)',
59 59 :text => '#1'
60 60 # link to a changeset
61 61 assert_select 'a[href=?][title=?]',
62 62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
63 63 'This commit fixes #1, #2 and references #1 &amp; #3',
64 64 :text => 'r2'
65 65 # link to a description diff
66 66 assert_select 'a[href=?][title=?]',
67 67 'https://mydomain.foo/journals/diff/3?detail_id=4',
68 68 'View differences',
69 69 :text => 'diff'
70 70 # link to an attachment
71 71 assert_select 'a[href=?]',
72 72 'https://mydomain.foo/attachments/download/4/source.rb',
73 73 :text => 'source.rb'
74 74 end
75 75 end
76 76
77 77 def test_generated_links_with_prefix
78 78 Setting.default_language = 'en'
79 79 relative_url_root = Redmine::Utils.relative_url_root
80 80 Setting.host_name = 'mydomain.foo/rdm'
81 81 Setting.protocol = 'http'
82 82
83 83 journal = Journal.find(3)
84 84 assert Mailer.deliver_issue_edit(journal)
85 85
86 86 mail = last_email
87 87 assert_not_nil mail
88 88
89 89 assert_select_email do
90 90 # link to the main ticket
91 91 assert_select 'a[href=?]',
92 92 'http://mydomain.foo/rdm/issues/2#change-3',
93 93 :text => 'Feature request #2: Add ingredients categories'
94 94 # link to a referenced ticket
95 95 assert_select 'a[href=?][title=?]',
96 96 'http://mydomain.foo/rdm/issues/1',
97 97 'Can&#x27;t print recipes (New)',
98 98 :text => '#1'
99 99 # link to a changeset
100 100 assert_select 'a[href=?][title=?]',
101 101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
102 102 'This commit fixes #1, #2 and references #1 &amp; #3',
103 103 :text => 'r2'
104 104 # link to a description diff
105 105 assert_select 'a[href=?][title=?]',
106 106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
107 107 'View differences',
108 108 :text => 'diff'
109 109 # link to an attachment
110 110 assert_select 'a[href=?]',
111 111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
112 112 :text => 'source.rb'
113 113 end
114 114 end
115 115
116 116 def test_generated_links_with_prefix_and_no_relative_url_root
117 117 Setting.default_language = 'en'
118 118 relative_url_root = Redmine::Utils.relative_url_root
119 119 Setting.host_name = 'mydomain.foo/rdm'
120 120 Setting.protocol = 'http'
121 121 Redmine::Utils.relative_url_root = nil
122 122
123 123 journal = Journal.find(3)
124 124 assert Mailer.deliver_issue_edit(journal)
125 125
126 126 mail = last_email
127 127 assert_not_nil mail
128 128
129 129 assert_select_email do
130 130 # link to the main ticket
131 131 assert_select 'a[href=?]',
132 132 'http://mydomain.foo/rdm/issues/2#change-3',
133 133 :text => 'Feature request #2: Add ingredients categories'
134 134 # link to a referenced ticket
135 135 assert_select 'a[href=?][title=?]',
136 136 'http://mydomain.foo/rdm/issues/1',
137 137 'Can&#x27;t print recipes (New)',
138 138 :text => '#1'
139 139 # link to a changeset
140 140 assert_select 'a[href=?][title=?]',
141 141 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
142 142 'This commit fixes #1, #2 and references #1 &amp; #3',
143 143 :text => 'r2'
144 144 # link to a description diff
145 145 assert_select 'a[href=?][title=?]',
146 146 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
147 147 'View differences',
148 148 :text => 'diff'
149 149 # link to an attachment
150 150 assert_select 'a[href=?]',
151 151 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
152 152 :text => 'source.rb'
153 153 end
154 154 ensure
155 155 # restore it
156 156 Redmine::Utils.relative_url_root = relative_url_root
157 157 end
158 158
159 159 def test_email_headers
160 160 issue = Issue.find(1)
161 161 Mailer.deliver_issue_add(issue)
162 162 mail = last_email
163 163 assert_not_nil mail
164 164 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
165 165 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
166 166 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
167 167 end
168 168
169 169 def test_email_headers_should_include_sender
170 170 issue = Issue.find(1)
171 171 Mailer.deliver_issue_add(issue)
172 172 mail = last_email
173 173 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
174 174 end
175 175
176 176 def test_plain_text_mail
177 177 Setting.plain_text_mail = 1
178 178 journal = Journal.find(2)
179 179 Mailer.deliver_issue_edit(journal)
180 180 mail = last_email
181 181 assert_equal "text/plain; charset=UTF-8", mail.content_type
182 182 assert_equal 0, mail.parts.size
183 183 assert !mail.encoded.include?('href')
184 184 end
185 185
186 186 def test_html_mail
187 187 Setting.plain_text_mail = 0
188 188 journal = Journal.find(2)
189 189 Mailer.deliver_issue_edit(journal)
190 190 mail = last_email
191 191 assert_equal 2, mail.parts.size
192 192 assert mail.encoded.include?('href')
193 193 end
194 194
195 195 def test_from_header
196 196 with_settings :mail_from => 'redmine@example.net' do
197 197 Mailer.test_email(User.find(1)).deliver
198 198 end
199 199 mail = last_email
200 200 assert_equal 'redmine@example.net', mail.from_addrs.first
201 201 end
202 202
203 203 def test_from_header_with_phrase
204 204 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
205 205 Mailer.test_email(User.find(1)).deliver
206 206 end
207 207 mail = last_email
208 208 assert_equal 'redmine@example.net', mail.from_addrs.first
209 209 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
210 210 end
211 211
212 212 def test_should_not_send_email_without_recipient
213 213 news = News.first
214 214 user = news.author
215 215 # Remove members except news author
216 216 news.project.memberships.each {|m| m.destroy unless m.user == user}
217 217
218 218 user.pref.no_self_notified = false
219 219 user.pref.save
220 220 User.current = user
221 221 Mailer.news_added(news.reload).deliver
222 222 assert_equal 1, last_email.bcc.size
223 223
224 224 # nobody to notify
225 225 user.pref.no_self_notified = true
226 226 user.pref.save
227 227 User.current = user
228 228 ActionMailer::Base.deliveries.clear
229 229 Mailer.news_added(news.reload).deliver
230 230 assert ActionMailer::Base.deliveries.empty?
231 231 end
232 232
233 233 def test_issue_add_message_id
234 234 issue = Issue.find(2)
235 235 Mailer.deliver_issue_add(issue)
236 236 mail = last_email
237 237 assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
238 238 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
239 239 end
240 240
241 241 def test_issue_edit_message_id
242 242 journal = Journal.find(3)
243 243 journal.issue = Issue.find(2)
244 244
245 245 Mailer.deliver_issue_edit(journal)
246 246 mail = last_email
247 247 assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
248 248 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
249 249 assert_select_email do
250 250 # link to the update
251 251 assert_select "a[href=?]",
252 252 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
253 253 end
254 254 end
255 255
256 256 def test_message_posted_message_id
257 257 message = Message.find(1)
258 258 Mailer.message_posted(message).deliver
259 259 mail = last_email
260 260 assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
261 261 assert_include "redmine.message-1.20070512151532@example.net", mail.references
262 262 assert_select_email do
263 263 # link to the message
264 264 assert_select "a[href=?]",
265 265 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
266 266 :text => message.subject
267 267 end
268 268 end
269 269
270 270 def test_reply_posted_message_id
271 271 message = Message.find(3)
272 272 Mailer.message_posted(message).deliver
273 273 mail = last_email
274 274 assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
275 275 assert_include "redmine.message-1.20070512151532@example.net", mail.references
276 276 assert_select_email do
277 277 # link to the reply
278 278 assert_select "a[href=?]",
279 279 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
280 280 :text => message.subject
281 281 end
282 282 end
283 283
284 284 test "#issue_add should notify project members" do
285 285 issue = Issue.find(1)
286 286 assert Mailer.deliver_issue_add(issue)
287 287 assert last_email.bcc.include?('dlopper@somenet.foo')
288 288 end
289 289
290 290 test "#issue_add should not notify project members that are not allow to view the issue" do
291 291 issue = Issue.find(1)
292 292 Role.find(2).remove_permission!(:view_issues)
293 293 assert Mailer.deliver_issue_add(issue)
294 294 assert !last_email.bcc.include?('dlopper@somenet.foo')
295 295 end
296 296
297 297 test "#issue_add should notify issue watchers" do
298 298 issue = Issue.find(1)
299 299 user = User.find(9)
300 300 # minimal email notification options
301 301 user.pref.no_self_notified = '1'
302 302 user.pref.save
303 303 user.mail_notification = false
304 304 user.save
305 305
306 306 Watcher.create!(:watchable => issue, :user => user)
307 307 assert Mailer.deliver_issue_add(issue)
308 308 assert last_email.bcc.include?(user.mail)
309 309 end
310 310
311 311 test "#issue_add should not notify watchers not allowed to view the issue" do
312 312 issue = Issue.find(1)
313 313 user = User.find(9)
314 314 Watcher.create!(:watchable => issue, :user => user)
315 315 Role.non_member.remove_permission!(:view_issues)
316 316 assert Mailer.deliver_issue_add(issue)
317 317 assert !last_email.bcc.include?(user.mail)
318 318 end
319 319
320 320 def test_issue_add_should_include_enabled_fields
321 321 Setting.default_language = 'en'
322 322 issue = Issue.find(2)
323 323 assert Mailer.deliver_issue_add(issue)
324 324 assert_mail_body_match '* Target version: 1.0', last_email
325 325 assert_select_email do
326 326 assert_select 'li', :text => 'Target version: 1.0'
327 327 end
328 328 end
329 329
330 330 def test_issue_add_should_not_include_disabled_fields
331 331 Setting.default_language = 'en'
332 332 issue = Issue.find(2)
333 333 tracker = issue.tracker
334 334 tracker.core_fields -= ['fixed_version_id']
335 335 tracker.save!
336 336 assert Mailer.deliver_issue_add(issue)
337 337 assert_mail_body_no_match 'Target version', last_email
338 338 assert_select_email do
339 339 assert_select 'li', :text => /Target version/, :count => 0
340 340 end
341 341 end
342 342
343 343 # test mailer methods for each language
344 344 def test_issue_add
345 345 issue = Issue.find(1)
346 346 valid_languages.each do |lang|
347 347 Setting.default_language = lang.to_s
348 348 assert Mailer.deliver_issue_add(issue)
349 349 end
350 350 end
351 351
352 352 def test_issue_edit
353 353 journal = Journal.find(1)
354 354 valid_languages.each do |lang|
355 355 Setting.default_language = lang.to_s
356 356 assert Mailer.deliver_issue_edit(journal)
357 357 end
358 358 end
359 359
360 360 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
361 361 journal = Journal.find(1)
362 362 journal.private_notes = true
363 363 journal.save!
364 364
365 365 Role.find(2).add_permission! :view_private_notes
366 366 Mailer.deliver_issue_edit(journal)
367 367 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
368 368
369 369 Role.find(2).remove_permission! :view_private_notes
370 370 Mailer.deliver_issue_edit(journal)
371 371 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
372 372 end
373 373
374 374 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
375 375 Issue.find(1).set_watcher(User.find_by_login('someone'))
376 376 journal = Journal.find(1)
377 377 journal.private_notes = true
378 378 journal.save!
379 379
380 380 Role.non_member.add_permission! :view_private_notes
381 381 Mailer.deliver_issue_edit(journal)
382 382 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
383 383
384 384 Role.non_member.remove_permission! :view_private_notes
385 385 Mailer.deliver_issue_edit(journal)
386 386 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
387 387 end
388 388
389 389 def test_issue_edit_should_mark_private_notes
390 390 journal = Journal.find(2)
391 391 journal.private_notes = true
392 392 journal.save!
393 393
394 394 with_settings :default_language => 'en' do
395 395 Mailer.deliver_issue_edit(journal)
396 396 end
397 397 assert_mail_body_match '(Private notes)', last_email
398 398 end
399 399
400 400 def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
401 401 issue = Issue.generate!
402 402 private_issue = Issue.generate!(:is_private => true)
403 403 IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
404 404 issue.reload
405 405 assert_equal 1, issue.journals.size
406 406 journal = issue.journals.first
407 407 ActionMailer::Base.deliveries.clear
408 408
409 409 Mailer.deliver_issue_edit(journal)
410 410 last_email.bcc.each do |email|
411 411 user = User.find_by_mail(email)
412 412 assert private_issue.visible?(user), "Issue was not visible to #{user}"
413 413 end
414 414 end
415 415
416 416 def test_document_added
417 417 document = Document.find(1)
418 418 valid_languages.each do |lang|
419 419 Setting.default_language = lang.to_s
420 420 assert Mailer.document_added(document).deliver
421 421 end
422 422 end
423 423
424 424 def test_attachments_added
425 425 attachements = [ Attachment.find_by_container_type('Document') ]
426 426 valid_languages.each do |lang|
427 427 Setting.default_language = lang.to_s
428 428 assert Mailer.attachments_added(attachements).deliver
429 429 end
430 430 end
431 431
432 432 def test_version_file_added
433 433 attachements = [ Attachment.find_by_container_type('Version') ]
434 434 assert Mailer.attachments_added(attachements).deliver
435 435 assert_not_nil last_email.bcc
436 436 assert last_email.bcc.any?
437 437 assert_select_email do
438 438 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
439 439 end
440 440 end
441 441
442 442 def test_project_file_added
443 443 attachements = [ Attachment.find_by_container_type('Project') ]
444 444 assert Mailer.attachments_added(attachements).deliver
445 445 assert_not_nil last_email.bcc
446 446 assert last_email.bcc.any?
447 447 assert_select_email do
448 448 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
449 449 end
450 450 end
451 451
452 452 def test_news_added
453 453 news = News.first
454 454 valid_languages.each do |lang|
455 455 Setting.default_language = lang.to_s
456 456 assert Mailer.news_added(news).deliver
457 457 end
458 458 end
459 459
460 460 def test_news_comment_added
461 461 comment = Comment.find(2)
462 462 valid_languages.each do |lang|
463 463 Setting.default_language = lang.to_s
464 464 assert Mailer.news_comment_added(comment).deliver
465 465 end
466 466 end
467 467
468 468 def test_message_posted
469 469 message = Message.first
470 470 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
471 471 recipients = recipients.compact.uniq
472 472 valid_languages.each do |lang|
473 473 Setting.default_language = lang.to_s
474 474 assert Mailer.message_posted(message).deliver
475 475 end
476 476 end
477 477
478 478 def test_wiki_content_added
479 479 content = WikiContent.find(1)
480 480 valid_languages.each do |lang|
481 481 Setting.default_language = lang.to_s
482 482 assert_difference 'ActionMailer::Base.deliveries.size' do
483 483 assert Mailer.wiki_content_added(content).deliver
484 484 assert_select_email do
485 485 assert_select 'a[href=?]',
486 486 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
487 487 :text => 'CookBook documentation'
488 488 end
489 489 end
490 490 end
491 491 end
492 492
493 493 def test_wiki_content_updated
494 494 content = WikiContent.find(1)
495 495 valid_languages.each do |lang|
496 496 Setting.default_language = lang.to_s
497 497 assert_difference 'ActionMailer::Base.deliveries.size' do
498 498 assert Mailer.wiki_content_updated(content).deliver
499 499 assert_select_email do
500 500 assert_select 'a[href=?]',
501 501 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
502 502 :text => 'CookBook documentation'
503 503 end
504 504 end
505 505 end
506 506 end
507 507
508 508 def test_account_information
509 509 user = User.find(2)
510 510 valid_languages.each do |lang|
511 511 user.update_attribute :language, lang.to_s
512 512 user.reload
513 513 assert Mailer.account_information(user, 'pAsswORd').deliver
514 514 end
515 515 end
516 516
517 517 def test_lost_password
518 518 token = Token.find(2)
519 519 valid_languages.each do |lang|
520 520 token.user.update_attribute :language, lang.to_s
521 521 token.reload
522 522 assert Mailer.lost_password(token).deliver
523 523 end
524 524 end
525 525
526 526 def test_register
527 527 token = Token.find(1)
528 528 Setting.host_name = 'redmine.foo'
529 529 Setting.protocol = 'https'
530 530
531 531 valid_languages.each do |lang|
532 532 token.user.update_attribute :language, lang.to_s
533 533 token.reload
534 534 ActionMailer::Base.deliveries.clear
535 535 assert Mailer.register(token).deliver
536 536 mail = last_email
537 537 assert_select_email do
538 538 assert_select "a[href=?]",
539 539 "https://redmine.foo/account/activate?token=#{token.value}",
540 540 :text => "https://redmine.foo/account/activate?token=#{token.value}"
541 541 end
542 542 end
543 543 end
544 544
545 545 def test_test
546 546 user = User.find(1)
547 547 valid_languages.each do |lang|
548 548 user.update_attribute :language, lang.to_s
549 549 assert Mailer.test_email(user).deliver
550 550 end
551 551 end
552 552
553 553 def test_reminders
554 554 Mailer.reminders(:days => 42)
555 555 assert_equal 1, ActionMailer::Base.deliveries.size
556 556 mail = last_email
557 557 assert mail.bcc.include?('dlopper@somenet.foo')
558 558 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
559 559 assert_equal '1 issue(s) due in the next 42 days', mail.subject
560 560 end
561 561
562 562 def test_reminders_should_not_include_closed_issues
563 563 with_settings :default_language => 'en' do
564 564 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
565 565 :subject => 'Closed issue', :assigned_to_id => 3,
566 566 :due_date => 5.days.from_now,
567 567 :author_id => 2)
568 568 ActionMailer::Base.deliveries.clear
569 569
570 570 Mailer.reminders(:days => 42)
571 571 assert_equal 1, ActionMailer::Base.deliveries.size
572 572 mail = last_email
573 573 assert mail.bcc.include?('dlopper@somenet.foo')
574 574 assert_mail_body_no_match 'Closed issue', mail
575 575 end
576 576 end
577 577
578 578 def test_reminders_for_users
579 579 Mailer.reminders(:days => 42, :users => ['5'])
580 580 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
581 581 Mailer.reminders(:days => 42, :users => ['3'])
582 582 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
583 583 mail = last_email
584 584 assert mail.bcc.include?('dlopper@somenet.foo')
585 585 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
586 586 end
587 587
588 588 def test_reminder_should_include_issues_assigned_to_groups
589 589 with_settings :default_language => 'en' do
590 590 group = Group.generate!
591 591 group.users << User.find(2)
592 592 group.users << User.find(3)
593 593
594 594 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
595 595 :subject => 'Assigned to group', :assigned_to => group,
596 596 :due_date => 5.days.from_now,
597 597 :author_id => 2)
598 598 ActionMailer::Base.deliveries.clear
599 599
600 600 Mailer.reminders(:days => 7)
601 601 assert_equal 2, ActionMailer::Base.deliveries.size
602 602 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
603 603 ActionMailer::Base.deliveries.each do |mail|
604 604 assert_mail_body_match 'Assigned to group', mail
605 605 end
606 606 end
607 607 end
608 608
609 609 def test_mailer_should_not_change_locale
610 610 Setting.default_language = 'en'
611 611 # Set current language to italian
612 612 set_language_if_valid 'it'
613 613 # Send an email to a french user
614 614 user = User.find(1)
615 615 user.language = 'fr'
616 616 Mailer.account_activated(user).deliver
617 617 mail = last_email
618 618 assert_mail_body_match 'Votre compte', mail
619 619
620 620 assert_equal :it, current_language
621 621 end
622 622
623 623 def test_with_deliveries_off
624 624 Mailer.with_deliveries false do
625 625 Mailer.test_email(User.find(1)).deliver
626 626 end
627 627 assert ActionMailer::Base.deliveries.empty?
628 628 # should restore perform_deliveries
629 629 assert ActionMailer::Base.perform_deliveries
630 630 end
631 631
632 632 def test_layout_should_include_the_emails_header
633 633 with_settings :emails_header => "*Header content*" do
634 634 with_settings :plain_text_mail => 0 do
635 635 assert Mailer.test_email(User.find(1)).deliver
636 636 assert_select_email do
637 637 assert_select ".header" do
638 638 assert_select "strong", :text => "Header content"
639 639 end
640 640 end
641 641 end
642 642 with_settings :plain_text_mail => 1 do
643 643 assert Mailer.test_email(User.find(1)).deliver
644 644 mail = last_email
645 645 assert_not_nil mail
646 646 assert_include "*Header content*", mail.body.decoded
647 647 end
648 648 end
649 649 end
650 650
651 651 def test_layout_should_not_include_empty_emails_header
652 652 with_settings :emails_header => "", :plain_text_mail => 0 do
653 653 assert Mailer.test_email(User.find(1)).deliver
654 654 assert_select_email do
655 655 assert_select ".header", false
656 656 end
657 657 end
658 658 end
659 659
660 660 def test_layout_should_include_the_emails_footer
661 661 with_settings :emails_footer => "*Footer content*" do
662 662 with_settings :plain_text_mail => 0 do
663 663 assert Mailer.test_email(User.find(1)).deliver
664 664 assert_select_email do
665 665 assert_select ".footer" do
666 666 assert_select "strong", :text => "Footer content"
667 667 end
668 668 end
669 669 end
670 670 with_settings :plain_text_mail => 1 do
671 671 assert Mailer.test_email(User.find(1)).deliver
672 672 mail = last_email
673 673 assert_not_nil mail
674 674 assert_include "\n-- \n", mail.body.decoded
675 675 assert_include "*Footer content*", mail.body.decoded
676 676 end
677 677 end
678 678 end
679 679
680 680 def test_layout_should_not_include_empty_emails_footer
681 681 with_settings :emails_footer => "" do
682 682 with_settings :plain_text_mail => 0 do
683 683 assert Mailer.test_email(User.find(1)).deliver
684 684 assert_select_email do
685 685 assert_select ".footer", false
686 686 end
687 687 end
688 688 with_settings :plain_text_mail => 1 do
689 689 assert Mailer.test_email(User.find(1)).deliver
690 690 mail = last_email
691 691 assert_not_nil mail
692 692 assert_not_include "\n-- \n", mail.body.decoded
693 693 end
694 694 end
695 695 end
696 696
697 697 def test_should_escape_html_templates_only
698 698 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
699 699 mail = last_email
700 700 assert_equal 2, mail.parts.size
701 701 assert_include '<tag>', text_part.body.encoded
702 702 assert_include '&lt;tag&gt;', html_part.body.encoded
703 703 end
704 704
705 705 def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
706 706 mail = Mailer.test_email(User.find(1))
707 707 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
708 708
709 709 ActionMailer::Base.raise_delivery_errors = true
710 710 assert_raise Exception, "delivery error" do
711 711 mail.deliver
712 712 end
713 713 ensure
714 714 ActionMailer::Base.raise_delivery_errors = false
715 715 end
716 716
717 717 def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
718 718 mail = Mailer.test_email(User.find(1))
719 719 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
720 720
721 721 Rails.logger.expects(:error).with("Email delivery error: delivery error")
722 722 ActionMailer::Base.raise_delivery_errors = false
723 723 assert_nothing_raised do
724 724 mail.deliver
725 725 end
726 726 end
727 727
728 def test_mail_should_return_a_mail_message
729 assert_kind_of ::Mail::Message, Mailer.test_email(User.find(1))
730 end
731
728 732 private
729 733
730 734 def last_email
731 735 mail = ActionMailer::Base.deliveries.last
732 736 assert_not_nil mail
733 737 mail
734 738 end
735 739
736 740 def text_part
737 741 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
738 742 end
739 743
740 744 def html_part
741 745 last_email.parts.detect {|part| part.content_type.include?('text/html')}
742 746 end
743 747 end
General Comments 0
You need to be logged in to leave comments. Login now