annotate .svn/pristine/f2/f2f32f727029afdbb52c620936d00b4beda9e3f1.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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