annotate test/unit/mailer_test.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1464 45 assert Mailer.deliver_issue_edit(journal)
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@1517 58 "#{ESCAPED_UCANT} 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@1464 84 assert Mailer.deliver_issue_edit(journal)
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@1517 97 "#{ESCAPED_UCANT} 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@1464 116 def test_issue_edit_should_generate_url_with_hostname_for_relations
Chris@1464 117 journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now)
Chris@1464 118 journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
Chris@1464 119 Mailer.deliver_issue_edit(journal)
Chris@1464 120 assert_not_nil last_email
Chris@1464 121 assert_select_email do
Chris@1464 122 assert_select 'a[href=?]', 'http://mydomain.foo/issues/2', :text => 'Feature request #2'
Chris@1464 123 end
Chris@1464 124 end
Chris@1464 125
Chris@0 126 def test_generated_links_with_prefix_and_no_relative_url_root
Chris@1115 127 Setting.default_language = 'en'
Chris@0 128 relative_url_root = Redmine::Utils.relative_url_root
Chris@0 129 Setting.host_name = 'mydomain.foo/rdm'
Chris@0 130 Setting.protocol = 'http'
Chris@0 131 Redmine::Utils.relative_url_root = nil
Chris@909 132
Chris@1115 133 journal = Journal.find(3)
Chris@1464 134 assert Mailer.deliver_issue_edit(journal)
Chris@909 135
Chris@1115 136 mail = last_email
Chris@1115 137 assert_not_nil mail
Chris@0 138
Chris@0 139 assert_select_email do
Chris@0 140 # link to the main ticket
Chris@1115 141 assert_select 'a[href=?]',
Chris@1115 142 'http://mydomain.foo/rdm/issues/2#change-3',
Chris@1115 143 :text => 'Feature request #2: Add ingredients categories'
Chris@0 144 # link to a referenced ticket
Chris@1115 145 assert_select 'a[href=?][title=?]',
Chris@1115 146 'http://mydomain.foo/rdm/issues/1',
Chris@1517 147 "#{ESCAPED_UCANT} print recipes (New)",
Chris@1115 148 :text => '#1'
Chris@0 149 # link to a changeset
Chris@1115 150 assert_select 'a[href=?][title=?]',
Chris@1115 151 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
Chris@1115 152 'This commit fixes #1, #2 and references #1 &amp; #3',
Chris@1115 153 :text => 'r2'
Chris@1115 154 # link to a description diff
Chris@1115 155 assert_select 'a[href=?][title=?]',
Chris@1115 156 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
Chris@1115 157 'View differences',
Chris@1115 158 :text => 'diff'
Chris@1115 159 # link to an attachment
Chris@1115 160 assert_select 'a[href=?]',
Chris@1115 161 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
Chris@1115 162 :text => 'source.rb'
Chris@0 163 end
Chris@0 164 ensure
Chris@0 165 # restore it
Chris@0 166 Redmine::Utils.relative_url_root = relative_url_root
Chris@0 167 end
Chris@909 168
Chris@0 169 def test_email_headers
Chris@0 170 issue = Issue.find(1)
Chris@1464 171 Mailer.deliver_issue_add(issue)
Chris@1115 172 mail = last_email
Chris@0 173 assert_not_nil mail
Chris@1115 174 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
Chris@1115 175 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
Chris@1115 176 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
Chris@1115 177 end
Chris@1115 178
Chris@1115 179 def test_email_headers_should_include_sender
Chris@1115 180 issue = Issue.find(1)
Chris@1464 181 Mailer.deliver_issue_add(issue)
Chris@1115 182 mail = last_email
Chris@1115 183 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
Chris@0 184 end
Chris@0 185
Chris@0 186 def test_plain_text_mail
Chris@0 187 Setting.plain_text_mail = 1
Chris@0 188 journal = Journal.find(2)
Chris@1464 189 Mailer.deliver_issue_edit(journal)
Chris@1115 190 mail = last_email
Chris@1115 191 assert_equal "text/plain; charset=UTF-8", mail.content_type
Chris@0 192 assert_equal 0, mail.parts.size
Chris@0 193 assert !mail.encoded.include?('href')
Chris@0 194 end
Chris@0 195
Chris@0 196 def test_html_mail
Chris@0 197 Setting.plain_text_mail = 0
Chris@0 198 journal = Journal.find(2)
Chris@1464 199 Mailer.deliver_issue_edit(journal)
Chris@1115 200 mail = last_email
Chris@0 201 assert_equal 2, mail.parts.size
Chris@0 202 assert mail.encoded.include?('href')
Chris@0 203 end
Chris@909 204
Chris@909 205 def test_from_header
Chris@909 206 with_settings :mail_from => 'redmine@example.net' do
Chris@1115 207 Mailer.test_email(User.find(1)).deliver
Chris@909 208 end
Chris@1115 209 mail = last_email
Chris@1115 210 assert_equal 'redmine@example.net', mail.from_addrs.first
Chris@909 211 end
Chris@909 212
Chris@909 213 def test_from_header_with_phrase
Chris@0 214 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
Chris@1115 215 Mailer.test_email(User.find(1)).deliver
Chris@0 216 end
Chris@1115 217 mail = last_email
Chris@1115 218 assert_equal 'redmine@example.net', mail.from_addrs.first
Chris@1115 219 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
Chris@0 220 end
Chris@909 221
Chris@0 222 def test_should_not_send_email_without_recipient
Chris@1464 223 news = News.first
Chris@0 224 user = news.author
Chris@0 225 # Remove members except news author
Chris@0 226 news.project.memberships.each {|m| m.destroy unless m.user == user}
Chris@909 227
Chris@1464 228 user.pref.no_self_notified = false
Chris@0 229 user.pref.save
Chris@0 230 User.current = user
Chris@1115 231 Mailer.news_added(news.reload).deliver
Chris@0 232 assert_equal 1, last_email.bcc.size
Chris@0 233
Chris@0 234 # nobody to notify
Chris@1464 235 user.pref.no_self_notified = true
Chris@0 236 user.pref.save
Chris@0 237 User.current = user
Chris@0 238 ActionMailer::Base.deliveries.clear
Chris@1115 239 Mailer.news_added(news.reload).deliver
Chris@0 240 assert ActionMailer::Base.deliveries.empty?
Chris@0 241 end
Chris@0 242
Chris@0 243 def test_issue_add_message_id
Chris@1464 244 issue = Issue.find(2)
Chris@1464 245 Mailer.deliver_issue_add(issue)
Chris@1115 246 mail = last_email
Chris@1464 247 assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
Chris@1464 248 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
Chris@0 249 end
Chris@909 250
Chris@0 251 def test_issue_edit_message_id
Chris@1464 252 journal = Journal.find(3)
Chris@1464 253 journal.issue = Issue.find(2)
Chris@1464 254
Chris@1464 255 Mailer.deliver_issue_edit(journal)
Chris@1115 256 mail = last_email
Chris@1464 257 assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
Chris@1464 258 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
Chris@909 259 assert_select_email do
Chris@909 260 # link to the update
Chris@909 261 assert_select "a[href=?]",
Chris@909 262 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
Chris@909 263 end
Chris@0 264 end
Chris@909 265
Chris@0 266 def test_message_posted_message_id
Chris@0 267 message = Message.find(1)
Chris@1115 268 Mailer.message_posted(message).deliver
Chris@1115 269 mail = last_email
Chris@1464 270 assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
Chris@1464 271 assert_include "redmine.message-1.20070512151532@example.net", mail.references
Chris@0 272 assert_select_email do
Chris@0 273 # link to the message
Chris@909 274 assert_select "a[href=?]",
Chris@909 275 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
Chris@909 276 :text => message.subject
Chris@0 277 end
Chris@0 278 end
Chris@909 279
Chris@0 280 def test_reply_posted_message_id
Chris@0 281 message = Message.find(3)
Chris@1115 282 Mailer.message_posted(message).deliver
Chris@1115 283 mail = last_email
Chris@1464 284 assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
Chris@1464 285 assert_include "redmine.message-1.20070512151532@example.net", mail.references
Chris@0 286 assert_select_email do
Chris@0 287 # link to the reply
Chris@909 288 assert_select "a[href=?]",
Chris@909 289 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
Chris@909 290 :text => message.subject
Chris@0 291 end
Chris@0 292 end
Chris@909 293
Chris@1464 294 test "#issue_add should notify project members" do
Chris@1464 295 issue = Issue.find(1)
Chris@1464 296 assert Mailer.deliver_issue_add(issue)
Chris@1464 297 assert last_email.bcc.include?('dlopper@somenet.foo')
Chris@1464 298 end
Chris@1464 299
Chris@1464 300 test "#issue_add should not notify project members that are not allow to view the issue" do
Chris@1464 301 issue = Issue.find(1)
Chris@1464 302 Role.find(2).remove_permission!(:view_issues)
Chris@1464 303 assert Mailer.deliver_issue_add(issue)
Chris@1464 304 assert !last_email.bcc.include?('dlopper@somenet.foo')
Chris@1464 305 end
Chris@1464 306
Chris@1464 307 test "#issue_add should notify issue watchers" do
Chris@1464 308 issue = Issue.find(1)
Chris@1464 309 user = User.find(9)
Chris@1464 310 # minimal email notification options
Chris@1464 311 user.pref.no_self_notified = '1'
Chris@1464 312 user.pref.save
Chris@1464 313 user.mail_notification = false
Chris@1464 314 user.save
Chris@1464 315
Chris@1464 316 Watcher.create!(:watchable => issue, :user => user)
Chris@1464 317 assert Mailer.deliver_issue_add(issue)
Chris@1464 318 assert last_email.bcc.include?(user.mail)
Chris@1464 319 end
Chris@1464 320
Chris@1464 321 test "#issue_add should not notify watchers not allowed to view the issue" do
Chris@1464 322 issue = Issue.find(1)
Chris@1464 323 user = User.find(9)
Chris@1464 324 Watcher.create!(:watchable => issue, :user => user)
Chris@1464 325 Role.non_member.remove_permission!(:view_issues)
Chris@1464 326 assert Mailer.deliver_issue_add(issue)
Chris@1464 327 assert !last_email.bcc.include?(user.mail)
Chris@1464 328 end
Chris@1464 329
Chris@1464 330 def test_issue_add_should_include_enabled_fields
Chris@1464 331 Setting.default_language = 'en'
Chris@1464 332 issue = Issue.find(2)
Chris@1464 333 assert Mailer.deliver_issue_add(issue)
Chris@1464 334 assert_mail_body_match '* Target version: 1.0', last_email
Chris@1464 335 assert_select_email do
Chris@1464 336 assert_select 'li', :text => 'Target version: 1.0'
Chris@0 337 end
Chris@1464 338 end
Chris@909 339
Chris@1464 340 def test_issue_add_should_not_include_disabled_fields
Chris@1464 341 Setting.default_language = 'en'
Chris@1464 342 issue = Issue.find(2)
Chris@1464 343 tracker = issue.tracker
Chris@1464 344 tracker.core_fields -= ['fixed_version_id']
Chris@1464 345 tracker.save!
Chris@1464 346 assert Mailer.deliver_issue_add(issue)
Chris@1464 347 assert_mail_body_no_match 'Target version', last_email
Chris@1464 348 assert_select_email do
Chris@1464 349 assert_select 'li', :text => /Target version/, :count => 0
Chris@0 350 end
Chris@0 351 end
Chris@909 352
Chris@0 353 # test mailer methods for each language
Chris@0 354 def test_issue_add
Chris@0 355 issue = Issue.find(1)
Chris@0 356 valid_languages.each do |lang|
Chris@0 357 Setting.default_language = lang.to_s
Chris@1464 358 assert Mailer.deliver_issue_add(issue)
Chris@0 359 end
Chris@0 360 end
Chris@0 361
Chris@0 362 def test_issue_edit
Chris@0 363 journal = Journal.find(1)
Chris@0 364 valid_languages.each do |lang|
Chris@0 365 Setting.default_language = lang.to_s
Chris@1464 366 assert Mailer.deliver_issue_edit(journal)
Chris@0 367 end
Chris@0 368 end
Chris@909 369
Chris@1115 370 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
Chris@1115 371 journal = Journal.find(1)
Chris@1115 372 journal.private_notes = true
Chris@1115 373 journal.save!
Chris@1115 374
Chris@1115 375 Role.find(2).add_permission! :view_private_notes
Chris@1464 376 Mailer.deliver_issue_edit(journal)
Chris@1115 377 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 378
Chris@1115 379 Role.find(2).remove_permission! :view_private_notes
Chris@1464 380 Mailer.deliver_issue_edit(journal)
Chris@1115 381 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 382 end
Chris@1115 383
Chris@1115 384 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
Chris@1115 385 Issue.find(1).set_watcher(User.find_by_login('someone'))
Chris@1115 386 journal = Journal.find(1)
Chris@1115 387 journal.private_notes = true
Chris@1115 388 journal.save!
Chris@1115 389
Chris@1115 390 Role.non_member.add_permission! :view_private_notes
Chris@1464 391 Mailer.deliver_issue_edit(journal)
Chris@1115 392 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 393
Chris@1115 394 Role.non_member.remove_permission! :view_private_notes
Chris@1464 395 Mailer.deliver_issue_edit(journal)
Chris@1115 396 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
Chris@1115 397 end
Chris@1115 398
Chris@1464 399 def test_issue_edit_should_mark_private_notes
Chris@1464 400 journal = Journal.find(2)
Chris@1464 401 journal.private_notes = true
Chris@1464 402 journal.save!
Chris@1464 403
Chris@1464 404 with_settings :default_language => 'en' do
Chris@1464 405 Mailer.deliver_issue_edit(journal)
Chris@1464 406 end
Chris@1464 407 assert_mail_body_match '(Private notes)', last_email
Chris@1464 408 end
Chris@1464 409
Chris@1464 410 def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
Chris@1464 411 issue = Issue.generate!
Chris@1464 412 private_issue = Issue.generate!(:is_private => true)
Chris@1464 413 IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
Chris@1464 414 issue.reload
Chris@1464 415 assert_equal 1, issue.journals.size
Chris@1464 416 journal = issue.journals.first
Chris@1464 417 ActionMailer::Base.deliveries.clear
Chris@1464 418
Chris@1464 419 Mailer.deliver_issue_edit(journal)
Chris@1464 420 last_email.bcc.each do |email|
Chris@1464 421 user = User.find_by_mail(email)
Chris@1464 422 assert private_issue.visible?(user), "Issue was not visible to #{user}"
Chris@1464 423 end
Chris@1464 424 end
Chris@1464 425
Chris@0 426 def test_document_added
Chris@0 427 document = Document.find(1)
Chris@0 428 valid_languages.each do |lang|
Chris@0 429 Setting.default_language = lang.to_s
Chris@1115 430 assert Mailer.document_added(document).deliver
Chris@0 431 end
Chris@0 432 end
Chris@909 433
Chris@0 434 def test_attachments_added
Chris@0 435 attachements = [ Attachment.find_by_container_type('Document') ]
Chris@0 436 valid_languages.each do |lang|
Chris@0 437 Setting.default_language = lang.to_s
Chris@1115 438 assert Mailer.attachments_added(attachements).deliver
Chris@0 439 end
Chris@0 440 end
Chris@909 441
Chris@0 442 def test_version_file_added
Chris@0 443 attachements = [ Attachment.find_by_container_type('Version') ]
Chris@1115 444 assert Mailer.attachments_added(attachements).deliver
Chris@0 445 assert_not_nil last_email.bcc
Chris@0 446 assert last_email.bcc.any?
Chris@441 447 assert_select_email do
Chris@441 448 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
Chris@441 449 end
Chris@0 450 end
Chris@909 451
Chris@0 452 def test_project_file_added
Chris@0 453 attachements = [ Attachment.find_by_container_type('Project') ]
Chris@1115 454 assert Mailer.attachments_added(attachements).deliver
Chris@0 455 assert_not_nil last_email.bcc
Chris@0 456 assert last_email.bcc.any?
Chris@441 457 assert_select_email do
Chris@441 458 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
Chris@441 459 end
Chris@0 460 end
Chris@909 461
Chris@0 462 def test_news_added
Chris@1464 463 news = News.first
Chris@0 464 valid_languages.each do |lang|
Chris@0 465 Setting.default_language = lang.to_s
Chris@1115 466 assert Mailer.news_added(news).deliver
Chris@0 467 end
Chris@0 468 end
Chris@909 469
Chris@1517 470 def test_news_added_should_notify_project_news_watchers
Chris@1517 471 user1 = User.generate!
Chris@1517 472 user2 = User.generate!
Chris@1517 473 news = News.find(1)
Chris@1517 474 news.project.enabled_module('news').add_watcher(user1)
Chris@1517 475
Chris@1517 476 Mailer.news_added(news).deliver
Chris@1517 477 assert_include user1.mail, last_email.bcc
Chris@1517 478 assert_not_include user2.mail, last_email.bcc
Chris@1517 479 end
Chris@1517 480
Chris@441 481 def test_news_comment_added
Chris@441 482 comment = Comment.find(2)
Chris@441 483 valid_languages.each do |lang|
Chris@441 484 Setting.default_language = lang.to_s
Chris@1115 485 assert Mailer.news_comment_added(comment).deliver
Chris@441 486 end
Chris@441 487 end
Chris@909 488
Chris@0 489 def test_message_posted
Chris@1464 490 message = Message.first
Chris@0 491 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
Chris@0 492 recipients = recipients.compact.uniq
Chris@0 493 valid_languages.each do |lang|
Chris@0 494 Setting.default_language = lang.to_s
Chris@1115 495 assert Mailer.message_posted(message).deliver
Chris@0 496 end
Chris@0 497 end
Chris@909 498
Chris@119 499 def test_wiki_content_added
Chris@929 500 content = WikiContent.find(1)
Chris@119 501 valid_languages.each do |lang|
Chris@119 502 Setting.default_language = lang.to_s
Chris@119 503 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 504 assert Mailer.wiki_content_added(content).deliver
Chris@1115 505 assert_select_email do
Chris@1115 506 assert_select 'a[href=?]',
Chris@1115 507 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
Chris@1115 508 :text => 'CookBook documentation'
Chris@1115 509 end
Chris@119 510 end
Chris@119 511 end
Chris@119 512 end
Chris@909 513
Chris@119 514 def test_wiki_content_updated
Chris@929 515 content = WikiContent.find(1)
Chris@119 516 valid_languages.each do |lang|
Chris@119 517 Setting.default_language = lang.to_s
Chris@119 518 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 519 assert Mailer.wiki_content_updated(content).deliver
Chris@1115 520 assert_select_email do
Chris@1115 521 assert_select 'a[href=?]',
Chris@1115 522 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
Chris@1115 523 :text => 'CookBook documentation'
Chris@1115 524 end
Chris@119 525 end
Chris@119 526 end
Chris@119 527 end
Chris@909 528
Chris@0 529 def test_account_information
Chris@0 530 user = User.find(2)
Chris@0 531 valid_languages.each do |lang|
Chris@0 532 user.update_attribute :language, lang.to_s
Chris@0 533 user.reload
Chris@1115 534 assert Mailer.account_information(user, 'pAsswORd').deliver
Chris@0 535 end
Chris@0 536 end
Chris@0 537
Chris@0 538 def test_lost_password
Chris@0 539 token = Token.find(2)
Chris@0 540 valid_languages.each do |lang|
Chris@0 541 token.user.update_attribute :language, lang.to_s
Chris@0 542 token.reload
Chris@1115 543 assert Mailer.lost_password(token).deliver
Chris@0 544 end
Chris@0 545 end
Chris@0 546
Chris@0 547 def test_register
Chris@0 548 token = Token.find(1)
Chris@0 549 Setting.host_name = 'redmine.foo'
Chris@0 550 Setting.protocol = 'https'
Chris@909 551
Chris@0 552 valid_languages.each do |lang|
Chris@0 553 token.user.update_attribute :language, lang.to_s
Chris@0 554 token.reload
Chris@0 555 ActionMailer::Base.deliveries.clear
Chris@1115 556 assert Mailer.register(token).deliver
Chris@1115 557 mail = last_email
Chris@1115 558 assert_select_email do
Chris@1115 559 assert_select "a[href=?]",
Chris@1115 560 "https://redmine.foo/account/activate?token=#{token.value}",
Chris@1115 561 :text => "https://redmine.foo/account/activate?token=#{token.value}"
Chris@1115 562 end
Chris@0 563 end
Chris@0 564 end
Chris@909 565
Chris@0 566 def test_test
Chris@0 567 user = User.find(1)
Chris@0 568 valid_languages.each do |lang|
Chris@0 569 user.update_attribute :language, lang.to_s
Chris@1115 570 assert Mailer.test_email(user).deliver
Chris@0 571 end
Chris@0 572 end
Chris@909 573
Chris@0 574 def test_reminders
Chris@0 575 Mailer.reminders(:days => 42)
Chris@0 576 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@1115 577 mail = last_email
Chris@0 578 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 579 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
Chris@14 580 assert_equal '1 issue(s) due in the next 42 days', mail.subject
Chris@0 581 end
Chris@909 582
Chris@1115 583 def test_reminders_should_not_include_closed_issues
Chris@1115 584 with_settings :default_language => 'en' do
Chris@1115 585 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
Chris@1115 586 :subject => 'Closed issue', :assigned_to_id => 3,
Chris@1115 587 :due_date => 5.days.from_now,
Chris@1115 588 :author_id => 2)
Chris@1115 589 ActionMailer::Base.deliveries.clear
Chris@1115 590
Chris@1115 591 Mailer.reminders(:days => 42)
Chris@1115 592 assert_equal 1, ActionMailer::Base.deliveries.size
Chris@1115 593 mail = last_email
Chris@1115 594 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 595 assert_mail_body_no_match 'Closed issue', mail
Chris@1115 596 end
Chris@1115 597 end
Chris@1115 598
chris@22 599 def test_reminders_for_users
chris@22 600 Mailer.reminders(:days => 42, :users => ['5'])
chris@22 601 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
chris@22 602 Mailer.reminders(:days => 42, :users => ['3'])
chris@22 603 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
Chris@1115 604 mail = last_email
chris@22 605 assert mail.bcc.include?('dlopper@somenet.foo')
Chris@1115 606 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
chris@22 607 end
Chris@909 608
Chris@1115 609 def test_reminder_should_include_issues_assigned_to_groups
Chris@1115 610 with_settings :default_language => 'en' do
Chris@1115 611 group = Group.generate!
Chris@1115 612 group.users << User.find(2)
Chris@1115 613 group.users << User.find(3)
Chris@1115 614
Chris@1115 615 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
Chris@1115 616 :subject => 'Assigned to group', :assigned_to => group,
Chris@1115 617 :due_date => 5.days.from_now,
Chris@1115 618 :author_id => 2)
Chris@1115 619 ActionMailer::Base.deliveries.clear
Chris@1115 620
Chris@1115 621 Mailer.reminders(:days => 7)
Chris@1115 622 assert_equal 2, ActionMailer::Base.deliveries.size
Chris@1115 623 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
Chris@1115 624 ActionMailer::Base.deliveries.each do |mail|
Chris@1115 625 assert_mail_body_match 'Assigned to group', mail
Chris@1115 626 end
Chris@1115 627 end
Chris@0 628 end
Chris@909 629
Chris@0 630 def test_mailer_should_not_change_locale
Chris@0 631 Setting.default_language = 'en'
Chris@0 632 # Set current language to italian
Chris@0 633 set_language_if_valid 'it'
Chris@0 634 # Send an email to a french user
Chris@0 635 user = User.find(1)
Chris@0 636 user.language = 'fr'
Chris@1115 637 Mailer.account_activated(user).deliver
Chris@1115 638 mail = last_email
Chris@1115 639 assert_mail_body_match 'Votre compte', mail
Chris@909 640
Chris@0 641 assert_equal :it, current_language
Chris@0 642 end
Chris@909 643
Chris@0 644 def test_with_deliveries_off
Chris@0 645 Mailer.with_deliveries false do
Chris@1115 646 Mailer.test_email(User.find(1)).deliver
Chris@0 647 end
Chris@0 648 assert ActionMailer::Base.deliveries.empty?
Chris@0 649 # should restore perform_deliveries
Chris@0 650 assert ActionMailer::Base.perform_deliveries
Chris@0 651 end
chris@37 652
Chris@1517 653 def test_token_for_should_strip_trailing_gt_from_address_with_full_name
Chris@1517 654 with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
Chris@1517 655 assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
Chris@1517 656 end
Chris@1517 657 end
Chris@1517 658
Chris@1115 659 def test_layout_should_include_the_emails_header
Chris@1115 660 with_settings :emails_header => "*Header content*" do
Chris@1464 661 with_settings :plain_text_mail => 0 do
Chris@1464 662 assert Mailer.test_email(User.find(1)).deliver
Chris@1464 663 assert_select_email do
Chris@1464 664 assert_select ".header" do
Chris@1464 665 assert_select "strong", :text => "Header content"
Chris@1464 666 end
Chris@1464 667 end
Chris@1464 668 end
Chris@1464 669 with_settings :plain_text_mail => 1 do
Chris@1464 670 assert Mailer.test_email(User.find(1)).deliver
Chris@1464 671 mail = last_email
Chris@1464 672 assert_not_nil mail
Chris@1464 673 assert_include "*Header content*", mail.body.decoded
Chris@1464 674 end
Chris@1464 675 end
Chris@1464 676 end
Chris@1464 677
Chris@1464 678 def test_layout_should_not_include_empty_emails_header
Chris@1464 679 with_settings :emails_header => "", :plain_text_mail => 0 do
Chris@1115 680 assert Mailer.test_email(User.find(1)).deliver
Chris@1115 681 assert_select_email do
Chris@1464 682 assert_select ".header", false
Chris@1464 683 end
Chris@1464 684 end
Chris@1464 685 end
Chris@1464 686
Chris@1464 687 def test_layout_should_include_the_emails_footer
Chris@1464 688 with_settings :emails_footer => "*Footer content*" do
Chris@1464 689 with_settings :plain_text_mail => 0 do
Chris@1464 690 assert Mailer.test_email(User.find(1)).deliver
Chris@1464 691 assert_select_email do
Chris@1464 692 assert_select ".footer" do
Chris@1464 693 assert_select "strong", :text => "Footer content"
Chris@1464 694 end
chris@37 695 end
chris@37 696 end
Chris@1464 697 with_settings :plain_text_mail => 1 do
Chris@1464 698 assert Mailer.test_email(User.find(1)).deliver
Chris@1464 699 mail = last_email
Chris@1464 700 assert_not_nil mail
Chris@1464 701 assert_include "\n-- \n", mail.body.decoded
Chris@1464 702 assert_include "*Footer content*", mail.body.decoded
Chris@1464 703 end
Chris@1464 704 end
Chris@1464 705 end
Chris@1464 706
Chris@1464 707 def test_layout_should_not_include_empty_emails_footer
Chris@1464 708 with_settings :emails_footer => "" do
Chris@1464 709 with_settings :plain_text_mail => 0 do
Chris@1464 710 assert Mailer.test_email(User.find(1)).deliver
Chris@1464 711 assert_select_email do
Chris@1464 712 assert_select ".footer", false
Chris@1464 713 end
Chris@1464 714 end
Chris@1464 715 with_settings :plain_text_mail => 1 do
Chris@1464 716 assert Mailer.test_email(User.find(1)).deliver
Chris@1464 717 mail = last_email
Chris@1464 718 assert_not_nil mail
Chris@1464 719 assert_not_include "\n-- \n", mail.body.decoded
Chris@1464 720 end
chris@37 721 end
chris@37 722 end
Chris@1115 723
Chris@1115 724 def test_should_escape_html_templates_only
Chris@1115 725 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
Chris@1115 726 mail = last_email
Chris@1115 727 assert_equal 2, mail.parts.size
Chris@1115 728 assert_include '<tag>', text_part.body.encoded
Chris@1115 729 assert_include '&lt;tag&gt;', html_part.body.encoded
Chris@1115 730 end
Chris@1115 731
Chris@1464 732 def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
Chris@1464 733 mail = Mailer.test_email(User.find(1))
Chris@1464 734 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
Chris@1464 735
Chris@1464 736 ActionMailer::Base.raise_delivery_errors = true
Chris@1464 737 assert_raise Exception, "delivery error" do
Chris@1464 738 mail.deliver
Chris@1464 739 end
Chris@1464 740 ensure
Chris@1464 741 ActionMailer::Base.raise_delivery_errors = false
Chris@1464 742 end
Chris@1464 743
Chris@1464 744 def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
Chris@1464 745 mail = Mailer.test_email(User.find(1))
Chris@1464 746 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
Chris@1464 747
Chris@1464 748 Rails.logger.expects(:error).with("Email delivery error: delivery error")
Chris@1464 749 ActionMailer::Base.raise_delivery_errors = false
Chris@1464 750 assert_nothing_raised do
Chris@1464 751 mail.deliver
Chris@1464 752 end
Chris@1464 753 end
Chris@1464 754
Chris@1464 755 def test_mail_should_return_a_mail_message
Chris@1464 756 assert_kind_of ::Mail::Message, Mailer.test_email(User.find(1))
Chris@1464 757 end
Chris@1464 758
Chris@1115 759 private
Chris@1115 760
Chris@1115 761 def last_email
Chris@1115 762 mail = ActionMailer::Base.deliveries.last
Chris@1115 763 assert_not_nil mail
Chris@1115 764 mail
Chris@1115 765 end
Chris@1115 766
Chris@1115 767 def text_part
Chris@1115 768 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
Chris@1115 769 end
Chris@1115 770
Chris@1115 771 def html_part
Chris@1115 772 last_email.parts.detect {|part| part.content_type.include?('text/html')}
Chris@1115 773 end
Chris@0 774 end