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