annotate test/unit/mailer_test.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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@1295 213 news = News.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@1295 282 test "#issue_add should notify project members" do
Chris@1295 283 issue = Issue.find(1)
Chris@1295 284 assert Mailer.issue_add(issue).deliver
Chris@1295 285 assert last_email.bcc.include?('dlopper@somenet.foo')
Chris@1295 286 end
Chris@909 287
Chris@1295 288 test "#issue_add should not notify project members that are not allow to view the issue" do
Chris@1295 289 issue = Issue.find(1)
Chris@1295 290 Role.find(2).remove_permission!(:view_issues)
Chris@1295 291 assert Mailer.issue_add(issue).deliver
Chris@1295 292 assert !last_email.bcc.include?('dlopper@somenet.foo')
Chris@1295 293 end
Chris@909 294
Chris@1295 295 test "#issue_add should notify issue watchers" do
Chris@1295 296 issue = Issue.find(1)
Chris@1295 297 user = User.find(9)
Chris@1295 298 # minimal email notification options
Chris@1295 299 user.pref[:no_self_notified] = '1'
Chris@1295 300 user.pref.save
Chris@1295 301 user.mail_notification = false
Chris@1295 302 user.save
Chris@909 303
Chris@1295 304 Watcher.create!(:watchable => issue, :user => user)
Chris@1295 305 assert Mailer.issue_add(issue).deliver
Chris@1295 306 assert last_email.bcc.include?(user.mail)
Chris@1295 307 end
Chris@909 308
Chris@1295 309 test "#issue_add should not notify watchers not allowed to view the issue" do
Chris@1295 310 issue = Issue.find(1)
Chris@1295 311 user = User.find(9)
Chris@1295 312 Watcher.create!(:watchable => issue, :user => user)
Chris@1295 313 Role.non_member.remove_permission!(:view_issues)
Chris@1295 314 assert Mailer.issue_add(issue).deliver
Chris@1295 315 assert !last_email.bcc.include?(user.mail)
Chris@0 316 end
Chris@909 317
Chris@0 318 # test mailer methods for each language
Chris@0 319 def test_issue_add
Chris@0 320 issue = Issue.find(1)
Chris@0 321 valid_languages.each do |lang|
Chris@0 322 Setting.default_language = lang.to_s
Chris@1115 323 assert Mailer.issue_add(issue).deliver
Chris@0 324 end
Chris@0 325 end
Chris@0 326
Chris@0 327 def test_issue_edit
Chris@0 328 journal = Journal.find(1)
Chris@0 329 valid_languages.each do |lang|
Chris@0 330 Setting.default_language = lang.to_s
Chris@1115 331 assert Mailer.issue_edit(journal).deliver
Chris@0 332 end
Chris@0 333 end
Chris@909 334
Chris@1115 335 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
Chris@1115 336 journal = Journal.find(1)
Chris@1115 337 journal.private_notes = true
Chris@1115 338 journal.save!
Chris@1115 339
Chris@1115 340 Role.find(2).add_permission! :view_private_notes
Chris@1115 341 Mailer.issue_edit(journal).deliver
Chris@1115 342 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 343
Chris@1115 344 Role.find(2).remove_permission! :view_private_notes
Chris@1115 345 Mailer.issue_edit(journal).deliver
Chris@1115 346 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 347 end
Chris@1115 348
Chris@1115 349 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
Chris@1115 350 Issue.find(1).set_watcher(User.find_by_login('someone'))
Chris@1115 351 journal = Journal.find(1)
Chris@1115 352 journal.private_notes = true
Chris@1115 353 journal.save!
Chris@1115 354
Chris@1115 355 Role.non_member.add_permission! :view_private_notes
Chris@1115 356 Mailer.issue_edit(journal).deliver
Chris@1115 357 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 358
Chris@1115 359 Role.non_member.remove_permission! :view_private_notes
Chris@1115 360 Mailer.issue_edit(journal).deliver
Chris@1115 361 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 362 end
Chris@1115 363
Chris@0 364 def test_document_added
Chris@0 365 document = Document.find(1)
Chris@0 366 valid_languages.each do |lang|
Chris@0 367 Setting.default_language = lang.to_s
Chris@1115 368 assert Mailer.document_added(document).deliver
Chris@0 369 end
Chris@0 370 end
Chris@909 371
Chris@0 372 def test_attachments_added
Chris@0 373 attachements = [ Attachment.find_by_container_type('Document') ]
Chris@0 374 valid_languages.each do |lang|
Chris@0 375 Setting.default_language = lang.to_s
Chris@1115 376 assert Mailer.attachments_added(attachements).deliver
Chris@0 377 end
Chris@0 378 end
Chris@909 379
Chris@0 380 def test_version_file_added
Chris@0 381 attachements = [ Attachment.find_by_container_type('Version') ]
Chris@1115 382 assert Mailer.attachments_added(attachements).deliver
Chris@0 383 assert_not_nil last_email.bcc
Chris@0 384 assert last_email.bcc.any?
Chris@441 385 assert_select_email do
Chris@441 386 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
Chris@441 387 end
Chris@0 388 end
Chris@909 389
Chris@0 390 def test_project_file_added
Chris@0 391 attachements = [ Attachment.find_by_container_type('Project') ]
Chris@1115 392 assert Mailer.attachments_added(attachements).deliver
Chris@0 393 assert_not_nil last_email.bcc
Chris@0 394 assert last_email.bcc.any?
Chris@441 395 assert_select_email do
Chris@441 396 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
Chris@441 397 end
Chris@0 398 end
Chris@909 399
Chris@0 400 def test_news_added
Chris@1295 401 news = News.first
Chris@0 402 valid_languages.each do |lang|
Chris@0 403 Setting.default_language = lang.to_s
Chris@1115 404 assert Mailer.news_added(news).deliver
Chris@0 405 end
Chris@0 406 end
Chris@909 407
Chris@441 408 def test_news_comment_added
Chris@441 409 comment = Comment.find(2)
Chris@441 410 valid_languages.each do |lang|
Chris@441 411 Setting.default_language = lang.to_s
Chris@1115 412 assert Mailer.news_comment_added(comment).deliver
Chris@441 413 end
Chris@441 414 end
Chris@909 415
Chris@0 416 def test_message_posted
Chris@1295 417 message = Message.first
Chris@0 418 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
Chris@0 419 recipients = recipients.compact.uniq
Chris@0 420 valid_languages.each do |lang|
Chris@0 421 Setting.default_language = lang.to_s
Chris@1115 422 assert Mailer.message_posted(message).deliver
Chris@0 423 end
Chris@0 424 end
Chris@909 425
Chris@119 426 def test_wiki_content_added
Chris@929 427 content = WikiContent.find(1)
Chris@119 428 valid_languages.each do |lang|
Chris@119 429 Setting.default_language = lang.to_s
Chris@119 430 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 431 assert Mailer.wiki_content_added(content).deliver
Chris@1115 432 assert_select_email do
Chris@1115 433 assert_select 'a[href=?]',
Chris@1115 434 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
Chris@1115 435 :text => 'CookBook documentation'
Chris@1115 436 end
Chris@119 437 end
Chris@119 438 end
Chris@119 439 end
Chris@909 440
Chris@119 441 def test_wiki_content_updated
Chris@929 442 content = WikiContent.find(1)
Chris@119 443 valid_languages.each do |lang|
Chris@119 444 Setting.default_language = lang.to_s
Chris@119 445 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 446 assert Mailer.wiki_content_updated(content).deliver
Chris@1115 447 assert_select_email do
Chris@1115 448 assert_select 'a[href=?]',
Chris@1115 449 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
Chris@1115 450 :text => 'CookBook documentation'
Chris@1115 451 end
Chris@119 452 end
Chris@119 453 end
Chris@119 454 end
Chris@909 455
Chris@0 456 def test_account_information
Chris@0 457 user = User.find(2)
Chris@0 458 valid_languages.each do |lang|
Chris@0 459 user.update_attribute :language, lang.to_s
Chris@0 460 user.reload
Chris@1115 461 assert Mailer.account_information(user, 'pAsswORd').deliver
Chris@0 462 end
Chris@0 463 end
Chris@0 464
Chris@0 465 def test_lost_password
Chris@0 466 token = Token.find(2)
Chris@0 467 valid_languages.each do |lang|
Chris@0 468 token.user.update_attribute :language, lang.to_s
Chris@0 469 token.reload
Chris@1115 470 assert Mailer.lost_password(token).deliver
Chris@0 471 end
Chris@0 472 end
Chris@0 473
Chris@0 474 def test_register
Chris@0 475 token = Token.find(1)
Chris@0 476 Setting.host_name = 'redmine.foo'
Chris@0 477 Setting.protocol = 'https'
Chris@909 478
Chris@0 479 valid_languages.each do |lang|
Chris@0 480 token.user.update_attribute :language, lang.to_s
Chris@0 481 token.reload
Chris@0 482 ActionMailer::Base.deliveries.clear
Chris@1115 483 assert Mailer.register(token).deliver
Chris@1115 484 mail = last_email
Chris@1115 485 assert_select_email do
Chris@1115 486 assert_select "a[href=?]",
Chris@1115 487 "https://redmine.foo/account/activate?token=#{token.value}",
Chris@1115 488 :text => "https://redmine.foo/account/activate?token=#{token.value}"
Chris@1115 489 end
Chris@0 490 end
Chris@0 491 end
Chris@909 492
Chris@0 493 def test_test
Chris@0 494 user = User.find(1)
Chris@0 495 valid_languages.each do |lang|
Chris@0 496 user.update_attribute :language, lang.to_s
Chris@1115 497 assert Mailer.test_email(user).deliver
Chris@0 498 end
Chris@0 499 end
Chris@909 500
Chris@0 501 def test_reminders
Chris@0 502 Mailer.reminders(:days => 42)
Chris@0 503 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@1115 504 mail = last_email
Chris@0 505 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 506 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
Chris@14 507 assert_equal '1 issue(s) due in the next 42 days', mail.subject
Chris@0 508 end
Chris@909 509
Chris@1115 510 def test_reminders_should_not_include_closed_issues
Chris@1115 511 with_settings :default_language => 'en' do
Chris@1115 512 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
Chris@1115 513 :subject => 'Closed issue', :assigned_to_id => 3,
Chris@1115 514 :due_date => 5.days.from_now,
Chris@1115 515 :author_id => 2)
Chris@1115 516 ActionMailer::Base.deliveries.clear
Chris@1115 517
Chris@1115 518 Mailer.reminders(:days => 42)
Chris@1115 519 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@1115 520 mail = last_email
Chris@1115 521 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 522 assert_mail_body_no_match 'Closed issue', mail
Chris@1115 523 end
Chris@1115 524 end
Chris@1115 525
chris@22 526 def test_reminders_for_users
chris@22 527 Mailer.reminders(:days => 42, :users => ['5'])
chris@22 528 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
chris@22 529 Mailer.reminders(:days => 42, :users => ['3'])
chris@22 530 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
Chris@1115 531 mail = last_email
chris@22 532 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 533 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
chris@22 534 end
Chris@909 535
Chris@1115 536 def test_reminder_should_include_issues_assigned_to_groups
Chris@1115 537 with_settings :default_language => 'en' do
Chris@1115 538 group = Group.generate!
Chris@1115 539 group.users << User.find(2)
Chris@1115 540 group.users << User.find(3)
Chris@1115 541
Chris@1115 542 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
Chris@1115 543 :subject => 'Assigned to group', :assigned_to => group,
Chris@1115 544 :due_date => 5.days.from_now,
Chris@1115 545 :author_id => 2)
Chris@1115 546 ActionMailer::Base.deliveries.clear
Chris@1115 547
Chris@1115 548 Mailer.reminders(:days => 7)
Chris@1115 549 assert_equal 2, ActionMailer::Base.deliveries.size
Chris@1115 550 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
Chris@1115 551 ActionMailer::Base.deliveries.each do |mail|
Chris@1115 552 assert_mail_body_match 'Assigned to group', mail
Chris@1115 553 end
Chris@1115 554 end
Chris@0 555 end
Chris@909 556
Chris@0 557 def test_mailer_should_not_change_locale
Chris@0 558 Setting.default_language = 'en'
Chris@0 559 # Set current language to italian
Chris@0 560 set_language_if_valid 'it'
Chris@0 561 # Send an email to a french user
Chris@0 562 user = User.find(1)
Chris@0 563 user.language = 'fr'
Chris@1115 564 Mailer.account_activated(user).deliver
Chris@1115 565 mail = last_email
Chris@1115 566 assert_mail_body_match 'Votre compte', mail
Chris@909 567
Chris@0 568 assert_equal :it, current_language
Chris@0 569 end
Chris@909 570
Chris@0 571 def test_with_deliveries_off
Chris@0 572 Mailer.with_deliveries false do
Chris@1115 573 Mailer.test_email(User.find(1)).deliver
Chris@0 574 end
Chris@0 575 assert ActionMailer::Base.deliveries.empty?
Chris@0 576 # should restore perform_deliveries
Chris@0 577 assert ActionMailer::Base.perform_deliveries
Chris@0 578 end
chris@37 579
Chris@1115 580 def test_layout_should_include_the_emails_header
Chris@1115 581 with_settings :emails_header => "*Header content*" do
Chris@1115 582 assert Mailer.test_email(User.find(1)).deliver
Chris@1115 583 assert_select_email do
Chris@1115 584 assert_select ".header" do
Chris@1115 585 assert_select "strong", :text => "Header content"
chris@37 586 end
chris@37 587 end
chris@37 588 end
chris@37 589 end
Chris@1115 590
Chris@1115 591 def test_should_escape_html_templates_only
Chris@1115 592 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
Chris@1115 593 mail = last_email
Chris@1115 594 assert_equal 2, mail.parts.size
Chris@1115 595 assert_include '<tag>', text_part.body.encoded
Chris@1115 596 assert_include '&lt;tag&gt;', html_part.body.encoded
Chris@1115 597 end
Chris@1115 598
Chris@1115 599 private
Chris@1115 600
Chris@1115 601 def last_email
Chris@1115 602 mail = ActionMailer::Base.deliveries.last
Chris@1115 603 assert_not_nil mail
Chris@1115 604 mail
Chris@1115 605 end
Chris@1115 606
Chris@1115 607 def text_part
Chris@1115 608 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
Chris@1115 609 end
Chris@1115 610
Chris@1115 611 def html_part
Chris@1115 612 last_email.parts.detect {|part| part.content_type.include?('text/html')}
Chris@1115 613 end
Chris@0 614 end