annotate test/unit/mailer_test.rb @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class MailerTest < ActiveSupport::TestCase
Chris@0 21 include Redmine::I18n
Chris@1115 22 include ActionDispatch::Assertions::SelectorAssertions
Chris@909 23 fixtures :projects, :enabled_modules, :issues, :users, :members,
Chris@909 24 :member_roles, :roles, :documents, :attachments, :news,
Chris@1115 25 :tokens, :journals, :journal_details, :changesets,
Chris@1115 26 :trackers, :projects_trackers,
Chris@909 27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
Chris@909 28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
Chris@909 29 :versions,
Chris@909 30 :comments
Chris@909 31
Chris@0 32 def setup
Chris@0 33 ActionMailer::Base.deliveries.clear
Chris@0 34 Setting.host_name = 'mydomain.foo'
Chris@0 35 Setting.protocol = 'http'
Chris@441 36 Setting.plain_text_mail = '0'
Chris@0 37 end
Chris@909 38
Chris@0 39 def test_generated_links_in_emails
Chris@1115 40 Setting.default_language = 'en'
Chris@0 41 Setting.host_name = 'mydomain.foo'
Chris@0 42 Setting.protocol = 'https'
Chris@909 43
Chris@1115 44 journal = Journal.find(3)
Chris@1115 45 assert Mailer.issue_edit(journal).deliver
Chris@909 46
Chris@1115 47 mail = last_email
Chris@1115 48 assert_not_nil mail
Chris@909 49
Chris@0 50 assert_select_email do
Chris@0 51 # link to the main ticket
Chris@1115 52 assert_select 'a[href=?]',
Chris@1115 53 'https://mydomain.foo/issues/2#change-3',
Chris@1115 54 :text => 'Feature request #2: Add ingredients categories'
Chris@0 55 # link to a referenced ticket
Chris@1115 56 assert_select 'a[href=?][title=?]',
Chris@1115 57 'https://mydomain.foo/issues/1',
Chris@1115 58 'Can&#x27;t print recipes (New)',
Chris@1115 59 :text => '#1'
Chris@0 60 # link to a changeset
Chris@1115 61 assert_select 'a[href=?][title=?]',
Chris@1115 62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
Chris@1115 63 'This commit fixes #1, #2 and references #1 &amp; #3',
Chris@1115 64 :text => 'r2'
Chris@1115 65 # link to a description diff
Chris@1115 66 assert_select 'a[href=?][title=?]',
Chris@1115 67 'https://mydomain.foo/journals/diff/3?detail_id=4',
Chris@1115 68 'View differences',
Chris@1115 69 :text => 'diff'
Chris@1115 70 # link to an attachment
Chris@1115 71 assert_select 'a[href=?]',
Chris@1115 72 'https://mydomain.foo/attachments/download/4/source.rb',
Chris@1115 73 :text => 'source.rb'
Chris@0 74 end
Chris@0 75 end
Chris@909 76
Chris@0 77 def test_generated_links_with_prefix
Chris@1115 78 Setting.default_language = 'en'
Chris@0 79 relative_url_root = Redmine::Utils.relative_url_root
Chris@0 80 Setting.host_name = 'mydomain.foo/rdm'
Chris@0 81 Setting.protocol = 'http'
Chris@909 82
Chris@1115 83 journal = Journal.find(3)
Chris@1115 84 assert Mailer.issue_edit(journal).deliver
Chris@909 85
Chris@1115 86 mail = last_email
Chris@1115 87 assert_not_nil mail
Chris@0 88
Chris@0 89 assert_select_email do
Chris@0 90 # link to the main ticket
Chris@1115 91 assert_select 'a[href=?]',
Chris@1115 92 'http://mydomain.foo/rdm/issues/2#change-3',
Chris@1115 93 :text => 'Feature request #2: Add ingredients categories'
Chris@0 94 # link to a referenced ticket
Chris@1115 95 assert_select 'a[href=?][title=?]',
Chris@1115 96 'http://mydomain.foo/rdm/issues/1',
Chris@1115 97 'Can&#x27;t print recipes (New)',
Chris@1115 98 :text => '#1'
Chris@0 99 # link to a changeset
Chris@1115 100 assert_select 'a[href=?][title=?]',
Chris@1115 101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
Chris@1115 102 'This commit fixes #1, #2 and references #1 &amp; #3',
Chris@1115 103 :text => 'r2'
Chris@1115 104 # link to a description diff
Chris@1115 105 assert_select 'a[href=?][title=?]',
Chris@1115 106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
Chris@1115 107 'View differences',
Chris@1115 108 :text => 'diff'
Chris@1115 109 # link to an attachment
Chris@1115 110 assert_select 'a[href=?]',
Chris@1115 111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
Chris@1115 112 :text => 'source.rb'
Chris@0 113 end
Chris@0 114 end
Chris@909 115
Chris@0 116 def test_generated_links_with_prefix_and_no_relative_url_root
Chris@1115 117 Setting.default_language = 'en'
Chris@0 118 relative_url_root = Redmine::Utils.relative_url_root
Chris@0 119 Setting.host_name = 'mydomain.foo/rdm'
Chris@0 120 Setting.protocol = 'http'
Chris@0 121 Redmine::Utils.relative_url_root = nil
Chris@909 122
Chris@1115 123 journal = Journal.find(3)
Chris@1115 124 assert Mailer.issue_edit(journal).deliver
Chris@909 125
Chris@1115 126 mail = last_email
Chris@1115 127 assert_not_nil mail
Chris@0 128
Chris@0 129 assert_select_email do
Chris@0 130 # link to the main ticket
Chris@1115 131 assert_select 'a[href=?]',
Chris@1115 132 'http://mydomain.foo/rdm/issues/2#change-3',
Chris@1115 133 :text => 'Feature request #2: Add ingredients categories'
Chris@0 134 # link to a referenced ticket
Chris@1115 135 assert_select 'a[href=?][title=?]',
Chris@1115 136 'http://mydomain.foo/rdm/issues/1',
Chris@1115 137 'Can&#x27;t print recipes (New)',
Chris@1115 138 :text => '#1'
Chris@0 139 # link to a changeset
Chris@1115 140 assert_select 'a[href=?][title=?]',
Chris@1115 141 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
Chris@1115 142 'This commit fixes #1, #2 and references #1 &amp; #3',
Chris@1115 143 :text => 'r2'
Chris@1115 144 # link to a description diff
Chris@1115 145 assert_select 'a[href=?][title=?]',
Chris@1115 146 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
Chris@1115 147 'View differences',
Chris@1115 148 :text => 'diff'
Chris@1115 149 # link to an attachment
Chris@1115 150 assert_select 'a[href=?]',
Chris@1115 151 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
Chris@1115 152 :text => 'source.rb'
Chris@0 153 end
Chris@0 154 ensure
Chris@0 155 # restore it
Chris@0 156 Redmine::Utils.relative_url_root = relative_url_root
Chris@0 157 end
Chris@909 158
Chris@0 159 def test_email_headers
Chris@0 160 issue = Issue.find(1)
Chris@1115 161 Mailer.issue_add(issue).deliver
Chris@1115 162 mail = last_email
Chris@0 163 assert_not_nil mail
Chris@1115 164 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
Chris@1115 165 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
Chris@1115 166 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
Chris@1115 167 end
Chris@1115 168
Chris@1115 169 def test_email_headers_should_include_sender
Chris@1115 170 issue = Issue.find(1)
Chris@1115 171 Mailer.issue_add(issue).deliver
Chris@1115 172 mail = last_email
Chris@1115 173 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
Chris@0 174 end
Chris@0 175
Chris@0 176 def test_plain_text_mail
Chris@0 177 Setting.plain_text_mail = 1
Chris@0 178 journal = Journal.find(2)
Chris@1115 179 Mailer.issue_edit(journal).deliver
Chris@1115 180 mail = last_email
Chris@1115 181 assert_equal "text/plain; charset=UTF-8", mail.content_type
Chris@0 182 assert_equal 0, mail.parts.size
Chris@0 183 assert !mail.encoded.include?('href')
Chris@0 184 end
Chris@0 185
Chris@0 186 def test_html_mail
Chris@0 187 Setting.plain_text_mail = 0
Chris@0 188 journal = Journal.find(2)
Chris@1115 189 Mailer.issue_edit(journal).deliver
Chris@1115 190 mail = last_email
Chris@0 191 assert_equal 2, mail.parts.size
Chris@0 192 assert mail.encoded.include?('href')
Chris@0 193 end
Chris@909 194
Chris@909 195 def test_from_header
Chris@909 196 with_settings :mail_from => 'redmine@example.net' do
Chris@1115 197 Mailer.test_email(User.find(1)).deliver
Chris@909 198 end
Chris@1115 199 mail = last_email
Chris@1115 200 assert_equal 'redmine@example.net', mail.from_addrs.first
Chris@909 201 end
Chris@909 202
Chris@909 203 def test_from_header_with_phrase
Chris@0 204 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
Chris@1115 205 Mailer.test_email(User.find(1)).deliver
Chris@0 206 end
Chris@1115 207 mail = last_email
Chris@1115 208 assert_equal 'redmine@example.net', mail.from_addrs.first
Chris@1115 209 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
Chris@0 210 end
Chris@909 211
Chris@0 212 def test_should_not_send_email_without_recipient
Chris@0 213 news = News.find(:first)
Chris@0 214 user = news.author
Chris@0 215 # Remove members except news author
Chris@0 216 news.project.memberships.each {|m| m.destroy unless m.user == user}
Chris@909 217
Chris@0 218 user.pref[:no_self_notified] = false
Chris@0 219 user.pref.save
Chris@0 220 User.current = user
Chris@1115 221 Mailer.news_added(news.reload).deliver
Chris@0 222 assert_equal 1, last_email.bcc.size
Chris@0 223
Chris@0 224 # nobody to notify
Chris@0 225 user.pref[:no_self_notified] = true
Chris@0 226 user.pref.save
Chris@0 227 User.current = user
Chris@0 228 ActionMailer::Base.deliveries.clear
Chris@1115 229 Mailer.news_added(news.reload).deliver
Chris@0 230 assert ActionMailer::Base.deliveries.empty?
Chris@0 231 end
Chris@0 232
Chris@0 233 def test_issue_add_message_id
Chris@0 234 issue = Issue.find(1)
Chris@1115 235 Mailer.issue_add(issue).deliver
Chris@1115 236 mail = last_email
Chris@0 237 assert_equal Mailer.message_id_for(issue), mail.message_id
Chris@0 238 assert_nil mail.references
Chris@0 239 end
Chris@909 240
Chris@0 241 def test_issue_edit_message_id
Chris@0 242 journal = Journal.find(1)
Chris@1115 243 Mailer.issue_edit(journal).deliver
Chris@1115 244 mail = last_email
Chris@0 245 assert_equal Mailer.message_id_for(journal), mail.message_id
Chris@1115 246 assert_include Mailer.message_id_for(journal.issue), mail.references
Chris@909 247 assert_select_email do
Chris@909 248 # link to the update
Chris@909 249 assert_select "a[href=?]",
Chris@909 250 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
Chris@909 251 end
Chris@0 252 end
Chris@909 253
Chris@0 254 def test_message_posted_message_id
Chris@0 255 message = Message.find(1)
Chris@1115 256 Mailer.message_posted(message).deliver
Chris@1115 257 mail = last_email
Chris@0 258 assert_equal Mailer.message_id_for(message), mail.message_id
Chris@0 259 assert_nil mail.references
Chris@0 260 assert_select_email do
Chris@0 261 # link to the message
Chris@909 262 assert_select "a[href=?]",
Chris@909 263 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
Chris@909 264 :text => message.subject
Chris@0 265 end
Chris@0 266 end
Chris@909 267
Chris@0 268 def test_reply_posted_message_id
Chris@0 269 message = Message.find(3)
Chris@1115 270 Mailer.message_posted(message).deliver
Chris@1115 271 mail = last_email
Chris@0 272 assert_equal Mailer.message_id_for(message), mail.message_id
Chris@1115 273 assert_include Mailer.message_id_for(message.parent), mail.references
Chris@0 274 assert_select_email do
Chris@0 275 # link to the reply
Chris@909 276 assert_select "a[href=?]",
Chris@909 277 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
Chris@909 278 :text => message.subject
Chris@0 279 end
Chris@0 280 end
Chris@909 281
Chris@0 282 context("#issue_add") do
Chris@0 283 setup do
Chris@0 284 ActionMailer::Base.deliveries.clear
Chris@0 285 Setting.bcc_recipients = '1'
Chris@909 286 @issue = Issue.find(1)
Chris@0 287 end
Chris@909 288
Chris@0 289 should "notify project members" do
Chris@1115 290 assert Mailer.issue_add(@issue).deliver
Chris@0 291 assert last_email.bcc.include?('dlopper@somenet.foo')
Chris@0 292 end
Chris@909 293
Chris@0 294 should "not notify project members that are not allow to view the issue" do
Chris@0 295 Role.find(2).remove_permission!(:view_issues)
Chris@1115 296 assert Mailer.issue_add(@issue).deliver
Chris@0 297 assert !last_email.bcc.include?('dlopper@somenet.foo')
Chris@0 298 end
Chris@909 299
Chris@0 300 should "notify issue watchers" do
Chris@0 301 user = User.find(9)
Chris@0 302 # minimal email notification options
Chris@0 303 user.pref[:no_self_notified] = '1'
Chris@0 304 user.pref.save
Chris@0 305 user.mail_notification = false
Chris@0 306 user.save
Chris@909 307
Chris@0 308 Watcher.create!(:watchable => @issue, :user => user)
Chris@1115 309 assert Mailer.issue_add(@issue).deliver
Chris@0 310 assert last_email.bcc.include?(user.mail)
Chris@0 311 end
Chris@909 312
Chris@0 313 should "not notify watchers not allowed to view the issue" do
Chris@0 314 user = User.find(9)
Chris@0 315 Watcher.create!(:watchable => @issue, :user => user)
Chris@0 316 Role.non_member.remove_permission!(:view_issues)
Chris@1115 317 assert Mailer.issue_add(@issue).deliver
Chris@0 318 assert !last_email.bcc.include?(user.mail)
Chris@0 319 end
Chris@0 320 end
Chris@909 321
Chris@0 322 # test mailer methods for each language
Chris@0 323 def test_issue_add
Chris@0 324 issue = Issue.find(1)
Chris@0 325 valid_languages.each do |lang|
Chris@0 326 Setting.default_language = lang.to_s
Chris@1115 327 assert Mailer.issue_add(issue).deliver
Chris@0 328 end
Chris@0 329 end
Chris@0 330
Chris@0 331 def test_issue_edit
Chris@0 332 journal = Journal.find(1)
Chris@0 333 valid_languages.each do |lang|
Chris@0 334 Setting.default_language = lang.to_s
Chris@1115 335 assert Mailer.issue_edit(journal).deliver
Chris@0 336 end
Chris@0 337 end
Chris@909 338
Chris@1115 339 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
Chris@1115 340 journal = Journal.find(1)
Chris@1115 341 journal.private_notes = true
Chris@1115 342 journal.save!
Chris@1115 343
Chris@1115 344 Role.find(2).add_permission! :view_private_notes
Chris@1115 345 Mailer.issue_edit(journal).deliver
Chris@1115 346 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 347
Chris@1115 348 Role.find(2).remove_permission! :view_private_notes
Chris@1115 349 Mailer.issue_edit(journal).deliver
Chris@1115 350 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 351 end
Chris@1115 352
Chris@1115 353 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
Chris@1115 354 Issue.find(1).set_watcher(User.find_by_login('someone'))
Chris@1115 355 journal = Journal.find(1)
Chris@1115 356 journal.private_notes = true
Chris@1115 357 journal.save!
Chris@1115 358
Chris@1115 359 Role.non_member.add_permission! :view_private_notes
Chris@1115 360 Mailer.issue_edit(journal).deliver
Chris@1115 361 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 362
Chris@1115 363 Role.non_member.remove_permission! :view_private_notes
Chris@1115 364 Mailer.issue_edit(journal).deliver
Chris@1115 365 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 366 end
Chris@1115 367
Chris@0 368 def test_document_added
Chris@0 369 document = Document.find(1)
Chris@0 370 valid_languages.each do |lang|
Chris@0 371 Setting.default_language = lang.to_s
Chris@1115 372 assert Mailer.document_added(document).deliver
Chris@0 373 end
Chris@0 374 end
Chris@909 375
Chris@0 376 def test_attachments_added
Chris@0 377 attachements = [ Attachment.find_by_container_type('Document') ]
Chris@0 378 valid_languages.each do |lang|
Chris@0 379 Setting.default_language = lang.to_s
Chris@1115 380 assert Mailer.attachments_added(attachements).deliver
Chris@0 381 end
Chris@0 382 end
Chris@909 383
Chris@0 384 def test_version_file_added
Chris@0 385 attachements = [ Attachment.find_by_container_type('Version') ]
Chris@1115 386 assert Mailer.attachments_added(attachements).deliver
Chris@0 387 assert_not_nil last_email.bcc
Chris@0 388 assert last_email.bcc.any?
Chris@441 389 assert_select_email do
Chris@441 390 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
Chris@441 391 end
Chris@0 392 end
Chris@909 393
Chris@0 394 def test_project_file_added
Chris@0 395 attachements = [ Attachment.find_by_container_type('Project') ]
Chris@1115 396 assert Mailer.attachments_added(attachements).deliver
Chris@0 397 assert_not_nil last_email.bcc
Chris@0 398 assert last_email.bcc.any?
Chris@441 399 assert_select_email do
Chris@441 400 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
Chris@441 401 end
Chris@0 402 end
Chris@909 403
Chris@0 404 def test_news_added
Chris@0 405 news = News.find(:first)
Chris@0 406 valid_languages.each do |lang|
Chris@0 407 Setting.default_language = lang.to_s
Chris@1115 408 assert Mailer.news_added(news).deliver
Chris@0 409 end
Chris@0 410 end
Chris@909 411
Chris@441 412 def test_news_comment_added
Chris@441 413 comment = Comment.find(2)
Chris@441 414 valid_languages.each do |lang|
Chris@441 415 Setting.default_language = lang.to_s
Chris@1115 416 assert Mailer.news_comment_added(comment).deliver
Chris@441 417 end
Chris@441 418 end
Chris@909 419
Chris@0 420 def test_message_posted
Chris@0 421 message = Message.find(:first)
Chris@0 422 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
Chris@0 423 recipients = recipients.compact.uniq
Chris@0 424 valid_languages.each do |lang|
Chris@0 425 Setting.default_language = lang.to_s
Chris@1115 426 assert Mailer.message_posted(message).deliver
Chris@0 427 end
Chris@0 428 end
Chris@909 429
Chris@119 430 def test_wiki_content_added
Chris@929 431 content = WikiContent.find(1)
Chris@119 432 valid_languages.each do |lang|
Chris@119 433 Setting.default_language = lang.to_s
Chris@119 434 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 435 assert Mailer.wiki_content_added(content).deliver
Chris@1115 436 assert_select_email do
Chris@1115 437 assert_select 'a[href=?]',
Chris@1115 438 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
Chris@1115 439 :text => 'CookBook documentation'
Chris@1115 440 end
Chris@119 441 end
Chris@119 442 end
Chris@119 443 end
Chris@909 444
Chris@119 445 def test_wiki_content_updated
Chris@929 446 content = WikiContent.find(1)
Chris@119 447 valid_languages.each do |lang|
Chris@119 448 Setting.default_language = lang.to_s
Chris@119 449 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 450 assert Mailer.wiki_content_updated(content).deliver
Chris@1115 451 assert_select_email do
Chris@1115 452 assert_select 'a[href=?]',
Chris@1115 453 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
Chris@1115 454 :text => 'CookBook documentation'
Chris@1115 455 end
Chris@119 456 end
Chris@119 457 end
Chris@119 458 end
Chris@909 459
Chris@0 460 def test_account_information
Chris@0 461 user = User.find(2)
Chris@0 462 valid_languages.each do |lang|
Chris@0 463 user.update_attribute :language, lang.to_s
Chris@0 464 user.reload
Chris@1115 465 assert Mailer.account_information(user, 'pAsswORd').deliver
Chris@0 466 end
Chris@0 467 end
Chris@0 468
Chris@0 469 def test_lost_password
Chris@0 470 token = Token.find(2)
Chris@0 471 valid_languages.each do |lang|
Chris@0 472 token.user.update_attribute :language, lang.to_s
Chris@0 473 token.reload
Chris@1115 474 assert Mailer.lost_password(token).deliver
Chris@0 475 end
Chris@0 476 end
Chris@0 477
Chris@0 478 def test_register
Chris@0 479 token = Token.find(1)
Chris@0 480 Setting.host_name = 'redmine.foo'
Chris@0 481 Setting.protocol = 'https'
Chris@909 482
Chris@0 483 valid_languages.each do |lang|
Chris@0 484 token.user.update_attribute :language, lang.to_s
Chris@0 485 token.reload
Chris@0 486 ActionMailer::Base.deliveries.clear
Chris@1115 487 assert Mailer.register(token).deliver
Chris@1115 488 mail = last_email
Chris@1115 489 assert_select_email do
Chris@1115 490 assert_select "a[href=?]",
Chris@1115 491 "https://redmine.foo/account/activate?token=#{token.value}",
Chris@1115 492 :text => "https://redmine.foo/account/activate?token=#{token.value}"
Chris@1115 493 end
Chris@0 494 end
Chris@0 495 end
Chris@909 496
Chris@0 497 def test_test
Chris@0 498 user = User.find(1)
Chris@0 499 valid_languages.each do |lang|
Chris@0 500 user.update_attribute :language, lang.to_s
Chris@1115 501 assert Mailer.test_email(user).deliver
Chris@0 502 end
Chris@0 503 end
Chris@909 504
Chris@0 505 def test_reminders
Chris@0 506 Mailer.reminders(:days => 42)
Chris@0 507 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@1115 508 mail = last_email
Chris@0 509 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 510 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
Chris@14 511 assert_equal '1 issue(s) due in the next 42 days', mail.subject
Chris@0 512 end
Chris@909 513
Chris@1115 514 def test_reminders_should_not_include_closed_issues
Chris@1115 515 with_settings :default_language => 'en' do
Chris@1115 516 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
Chris@1115 517 :subject => 'Closed issue', :assigned_to_id => 3,
Chris@1115 518 :due_date => 5.days.from_now,
Chris@1115 519 :author_id => 2)
Chris@1115 520 ActionMailer::Base.deliveries.clear
Chris@1115 521
Chris@1115 522 Mailer.reminders(:days => 42)
Chris@1115 523 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@1115 524 mail = last_email
Chris@1115 525 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 526 assert_mail_body_no_match 'Closed issue', mail
Chris@1115 527 end
Chris@1115 528 end
Chris@1115 529
chris@22 530 def test_reminders_for_users
chris@22 531 Mailer.reminders(:days => 42, :users => ['5'])
chris@22 532 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
chris@22 533 Mailer.reminders(:days => 42, :users => ['3'])
chris@22 534 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
Chris@1115 535 mail = last_email
chris@22 536 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 537 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
chris@22 538 end
Chris@909 539
Chris@1115 540 def test_reminder_should_include_issues_assigned_to_groups
Chris@1115 541 with_settings :default_language => 'en' do
Chris@1115 542 group = Group.generate!
Chris@1115 543 group.users << User.find(2)
Chris@1115 544 group.users << User.find(3)
Chris@1115 545
Chris@1115 546 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
Chris@1115 547 :subject => 'Assigned to group', :assigned_to => group,
Chris@1115 548 :due_date => 5.days.from_now,
Chris@1115 549 :author_id => 2)
Chris@1115 550 ActionMailer::Base.deliveries.clear
Chris@1115 551
Chris@1115 552 Mailer.reminders(:days => 7)
Chris@1115 553 assert_equal 2, ActionMailer::Base.deliveries.size
Chris@1115 554 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
Chris@1115 555 ActionMailer::Base.deliveries.each do |mail|
Chris@1115 556 assert_mail_body_match 'Assigned to group', mail
Chris@1115 557 end
Chris@1115 558 end
Chris@0 559 end
Chris@909 560
Chris@0 561 def test_mailer_should_not_change_locale
Chris@0 562 Setting.default_language = 'en'
Chris@0 563 # Set current language to italian
Chris@0 564 set_language_if_valid 'it'
Chris@0 565 # Send an email to a french user
Chris@0 566 user = User.find(1)
Chris@0 567 user.language = 'fr'
Chris@1115 568 Mailer.account_activated(user).deliver
Chris@1115 569 mail = last_email
Chris@1115 570 assert_mail_body_match 'Votre compte', mail
Chris@909 571
Chris@0 572 assert_equal :it, current_language
Chris@0 573 end
Chris@909 574
Chris@0 575 def test_with_deliveries_off
Chris@0 576 Mailer.with_deliveries false do
Chris@1115 577 Mailer.test_email(User.find(1)).deliver
Chris@0 578 end
Chris@0 579 assert ActionMailer::Base.deliveries.empty?
Chris@0 580 # should restore perform_deliveries
Chris@0 581 assert ActionMailer::Base.perform_deliveries
Chris@0 582 end
chris@37 583
Chris@1115 584 def test_layout_should_include_the_emails_header
Chris@1115 585 with_settings :emails_header => "*Header content*" do
Chris@1115 586 assert Mailer.test_email(User.find(1)).deliver
Chris@1115 587 assert_select_email do
Chris@1115 588 assert_select ".header" do
Chris@1115 589 assert_select "strong", :text => "Header content"
chris@37 590 end
chris@37 591 end
chris@37 592 end
chris@37 593 end
Chris@1115 594
Chris@1115 595 def test_should_escape_html_templates_only
Chris@1115 596 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
Chris@1115 597 mail = last_email
Chris@1115 598 assert_equal 2, mail.parts.size
Chris@1115 599 assert_include '<tag>', text_part.body.encoded
Chris@1115 600 assert_include '&lt;tag&gt;', html_part.body.encoded
Chris@1115 601 end
Chris@1115 602
Chris@1115 603 private
Chris@1115 604
Chris@1115 605 def last_email
Chris@1115 606 mail = ActionMailer::Base.deliveries.last
Chris@1115 607 assert_not_nil mail
Chris@1115 608 mail
Chris@1115 609 end
Chris@1115 610
Chris@1115 611 def text_part
Chris@1115 612 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
Chris@1115 613 end
Chris@1115 614
Chris@1115 615 def html_part
Chris@1115 616 last_email.parts.detect {|part| part.content_type.include?('text/html')}
Chris@1115 617 end
Chris@0 618 end