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