comparison test/unit/.svn/text-base/mailer_test.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children 1d32c0a0efbf
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19
20 class MailerTest < ActiveSupport::TestCase
21 include Redmine::I18n
22 include ActionController::Assertions::SelectorAssertions
23 fixtures :projects, :enabled_modules, :issues, :users, :members, :member_roles, :roles, :documents, :attachments, :news, :tokens, :journals, :journal_details, :changesets, :trackers, :issue_statuses, :enumerations, :messages, :boards, :repositories
24
25 def setup
26 ActionMailer::Base.deliveries.clear
27 Setting.host_name = 'mydomain.foo'
28 Setting.protocol = 'http'
29 end
30
31 def test_generated_links_in_emails
32 Setting.host_name = 'mydomain.foo'
33 Setting.protocol = 'https'
34
35 journal = Journal.find(2)
36 assert Mailer.deliver_issue_edit(journal)
37
38 mail = ActionMailer::Base.deliveries.last
39 assert_kind_of TMail::Mail, mail
40
41 assert_select_email do
42 # link to the main ticket
43 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
44 # link to a referenced ticket
45 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
46 # link to a changeset
47 assert_select "a[href=?][title=?]", "https://mydomain.foo/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
48 end
49 end
50
51 def test_generated_links_with_prefix
52 relative_url_root = Redmine::Utils.relative_url_root
53 Setting.host_name = 'mydomain.foo/rdm'
54 Setting.protocol = 'http'
55 Redmine::Utils.relative_url_root = '/rdm'
56
57 journal = Journal.find(2)
58 assert Mailer.deliver_issue_edit(journal)
59
60 mail = ActionMailer::Base.deliveries.last
61 assert_kind_of TMail::Mail, mail
62
63 assert_select_email do
64 # link to the main ticket
65 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
66 # link to a referenced ticket
67 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
68 # link to a changeset
69 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
70 end
71 ensure
72 # restore it
73 Redmine::Utils.relative_url_root = relative_url_root
74 end
75
76 def test_generated_links_with_prefix_and_no_relative_url_root
77 relative_url_root = Redmine::Utils.relative_url_root
78 Setting.host_name = 'mydomain.foo/rdm'
79 Setting.protocol = 'http'
80 Redmine::Utils.relative_url_root = nil
81
82 journal = Journal.find(2)
83 assert Mailer.deliver_issue_edit(journal)
84
85 mail = ActionMailer::Base.deliveries.last
86 assert_kind_of TMail::Mail, mail
87
88 assert_select_email do
89 # link to the main ticket
90 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
91 # link to a referenced ticket
92 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
93 # link to a changeset
94 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
95 end
96 ensure
97 # restore it
98 Redmine::Utils.relative_url_root = relative_url_root
99 end
100
101 def test_email_headers
102 issue = Issue.find(1)
103 Mailer.deliver_issue_add(issue)
104 mail = ActionMailer::Base.deliveries.last
105 assert_not_nil mail
106 assert_equal 'bulk', mail.header_string('Precedence')
107 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
108 end
109
110 def test_plain_text_mail
111 Setting.plain_text_mail = 1
112 journal = Journal.find(2)
113 Mailer.deliver_issue_edit(journal)
114 mail = ActionMailer::Base.deliveries.last
115 assert_equal "text/plain", mail.content_type
116 assert_equal 0, mail.parts.size
117 assert !mail.encoded.include?('href')
118 end
119
120 def test_html_mail
121 Setting.plain_text_mail = 0
122 journal = Journal.find(2)
123 Mailer.deliver_issue_edit(journal)
124 mail = ActionMailer::Base.deliveries.last
125 assert_equal 2, mail.parts.size
126 assert mail.encoded.include?('href')
127 end
128
129 def test_mail_from_with_phrase
130 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
131 Mailer.deliver_test(User.find(1))
132 end
133 mail = ActionMailer::Base.deliveries.last
134 assert_not_nil mail
135 assert_equal 'Redmine app', mail.from_addrs.first.name
136 end
137
138 def test_should_not_send_email_without_recipient
139 news = News.find(:first)
140 user = news.author
141 # Remove members except news author
142 news.project.memberships.each {|m| m.destroy unless m.user == user}
143
144 user.pref[:no_self_notified] = false
145 user.pref.save
146 User.current = user
147 Mailer.deliver_news_added(news.reload)
148 assert_equal 1, last_email.bcc.size
149
150 # nobody to notify
151 user.pref[:no_self_notified] = true
152 user.pref.save
153 User.current = user
154 ActionMailer::Base.deliveries.clear
155 Mailer.deliver_news_added(news.reload)
156 assert ActionMailer::Base.deliveries.empty?
157 end
158
159 def test_issue_add_message_id
160 issue = Issue.find(1)
161 Mailer.deliver_issue_add(issue)
162 mail = ActionMailer::Base.deliveries.last
163 assert_not_nil mail
164 assert_equal Mailer.message_id_for(issue), mail.message_id
165 assert_nil mail.references
166 end
167
168 def test_issue_edit_message_id
169 journal = Journal.find(1)
170 Mailer.deliver_issue_edit(journal)
171 mail = ActionMailer::Base.deliveries.last
172 assert_not_nil mail
173 assert_equal Mailer.message_id_for(journal), mail.message_id
174 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
175 end
176
177 def test_message_posted_message_id
178 message = Message.find(1)
179 Mailer.deliver_message_posted(message)
180 mail = ActionMailer::Base.deliveries.last
181 assert_not_nil mail
182 assert_equal Mailer.message_id_for(message), mail.message_id
183 assert_nil mail.references
184 assert_select_email do
185 # link to the message
186 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}", :text => message.subject
187 end
188 end
189
190 def test_reply_posted_message_id
191 message = Message.find(3)
192 Mailer.deliver_message_posted(message)
193 mail = ActionMailer::Base.deliveries.last
194 assert_not_nil mail
195 assert_equal Mailer.message_id_for(message), mail.message_id
196 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
197 assert_select_email do
198 # link to the reply
199 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}", :text => message.subject
200 end
201 end
202
203 context("#issue_add") do
204 setup do
205 ActionMailer::Base.deliveries.clear
206 Setting.bcc_recipients = '1'
207 @issue = Issue.find(1)
208 end
209
210 should "notify project members" do
211 assert Mailer.deliver_issue_add(@issue)
212 assert last_email.bcc.include?('dlopper@somenet.foo')
213 end
214
215 should "not notify project members that are not allow to view the issue" do
216 Role.find(2).remove_permission!(:view_issues)
217 assert Mailer.deliver_issue_add(@issue)
218 assert !last_email.bcc.include?('dlopper@somenet.foo')
219 end
220
221 should "notify issue watchers" do
222 user = User.find(9)
223 # minimal email notification options
224 user.pref[:no_self_notified] = '1'
225 user.pref.save
226 user.mail_notification = false
227 user.save
228
229 Watcher.create!(:watchable => @issue, :user => user)
230 assert Mailer.deliver_issue_add(@issue)
231 assert last_email.bcc.include?(user.mail)
232 end
233
234 should "not notify watchers not allowed to view the issue" do
235 user = User.find(9)
236 Watcher.create!(:watchable => @issue, :user => user)
237 Role.non_member.remove_permission!(:view_issues)
238 assert Mailer.deliver_issue_add(@issue)
239 assert !last_email.bcc.include?(user.mail)
240 end
241 end
242
243 # test mailer methods for each language
244 def test_issue_add
245 issue = Issue.find(1)
246 valid_languages.each do |lang|
247 Setting.default_language = lang.to_s
248 assert Mailer.deliver_issue_add(issue)
249 end
250 end
251
252 def test_issue_edit
253 journal = Journal.find(1)
254 valid_languages.each do |lang|
255 Setting.default_language = lang.to_s
256 assert Mailer.deliver_issue_edit(journal)
257 end
258 end
259
260 def test_document_added
261 document = Document.find(1)
262 valid_languages.each do |lang|
263 Setting.default_language = lang.to_s
264 assert Mailer.deliver_document_added(document)
265 end
266 end
267
268 def test_attachments_added
269 attachements = [ Attachment.find_by_container_type('Document') ]
270 valid_languages.each do |lang|
271 Setting.default_language = lang.to_s
272 assert Mailer.deliver_attachments_added(attachements)
273 end
274 end
275
276 def test_version_file_added
277 attachements = [ Attachment.find_by_container_type('Version') ]
278 assert Mailer.deliver_attachments_added(attachements)
279 assert_not_nil last_email.bcc
280 assert last_email.bcc.any?
281 end
282
283 def test_project_file_added
284 attachements = [ Attachment.find_by_container_type('Project') ]
285 assert Mailer.deliver_attachments_added(attachements)
286 assert_not_nil last_email.bcc
287 assert last_email.bcc.any?
288 end
289
290 def test_news_added
291 news = News.find(:first)
292 valid_languages.each do |lang|
293 Setting.default_language = lang.to_s
294 assert Mailer.deliver_news_added(news)
295 end
296 end
297
298 def test_message_posted
299 message = Message.find(:first)
300 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
301 recipients = recipients.compact.uniq
302 valid_languages.each do |lang|
303 Setting.default_language = lang.to_s
304 assert Mailer.deliver_message_posted(message)
305 end
306 end
307
308 def test_account_information
309 user = User.find(2)
310 valid_languages.each do |lang|
311 user.update_attribute :language, lang.to_s
312 user.reload
313 assert Mailer.deliver_account_information(user, 'pAsswORd')
314 end
315 end
316
317 def test_lost_password
318 token = Token.find(2)
319 valid_languages.each do |lang|
320 token.user.update_attribute :language, lang.to_s
321 token.reload
322 assert Mailer.deliver_lost_password(token)
323 end
324 end
325
326 def test_register
327 token = Token.find(1)
328 Setting.host_name = 'redmine.foo'
329 Setting.protocol = 'https'
330
331 valid_languages.each do |lang|
332 token.user.update_attribute :language, lang.to_s
333 token.reload
334 ActionMailer::Base.deliveries.clear
335 assert Mailer.deliver_register(token)
336 mail = ActionMailer::Base.deliveries.last
337 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
338 end
339 end
340
341 def test_test
342 user = User.find(1)
343 valid_languages.each do |lang|
344 user.update_attribute :language, lang.to_s
345 assert Mailer.deliver_test(user)
346 end
347 end
348
349 def test_reminders
350 Mailer.reminders(:days => 42)
351 assert_equal 1, ActionMailer::Base.deliveries.size
352 mail = ActionMailer::Base.deliveries.last
353 assert mail.bcc.include?('dlopper@somenet.foo')
354 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
355 end
356
357 def last_email
358 mail = ActionMailer::Base.deliveries.last
359 assert_not_nil mail
360 mail
361 end
362
363 def test_mailer_should_not_change_locale
364 Setting.default_language = 'en'
365 # Set current language to italian
366 set_language_if_valid 'it'
367 # Send an email to a french user
368 user = User.find(1)
369 user.language = 'fr'
370 Mailer.deliver_account_activated(user)
371 mail = ActionMailer::Base.deliveries.last
372 assert mail.body.include?('Votre compte')
373
374 assert_equal :it, current_language
375 end
376
377 def test_with_deliveries_off
378 Mailer.with_deliveries false do
379 Mailer.deliver_test(User.find(1))
380 end
381 assert ActionMailer::Base.deliveries.empty?
382 # should restore perform_deliveries
383 assert ActionMailer::Base.perform_deliveries
384 end
385 end