##// END OF EJS Templates
Adds unit test for #3645....
Jean-Philippe Lang -
r3213:d55e1ec5b092
parent child
Show More
@@ -1,313 +1,319
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class MailerTest < ActiveSupport::TestCase
21 21 include Redmine::I18n
22 22 include ActionController::Assertions::SelectorAssertions
23 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 24
25 25 def test_generated_links_in_emails
26 26 ActionMailer::Base.deliveries.clear
27 27 Setting.host_name = 'mydomain.foo'
28 28 Setting.protocol = 'https'
29 29
30 30 journal = Journal.find(2)
31 31 assert Mailer.deliver_issue_edit(journal)
32 32
33 33 mail = ActionMailer::Base.deliveries.last
34 34 assert_kind_of TMail::Mail, mail
35 35
36 36 assert_select_email do
37 37 # link to the main ticket
38 38 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
39 39 # link to a referenced ticket
40 40 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
41 41 # link to a changeset
42 42 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"
43 43 end
44 44 end
45 45
46 46 def test_generated_links_with_prefix
47 47 relative_url_root = Redmine::Utils.relative_url_root
48 48 ActionMailer::Base.deliveries.clear
49 49 Setting.host_name = 'mydomain.foo/rdm'
50 50 Setting.protocol = 'http'
51 51 Redmine::Utils.relative_url_root = '/rdm'
52 52
53 53 journal = Journal.find(2)
54 54 assert Mailer.deliver_issue_edit(journal)
55 55
56 56 mail = ActionMailer::Base.deliveries.last
57 57 assert_kind_of TMail::Mail, mail
58 58
59 59 assert_select_email do
60 60 # link to the main ticket
61 61 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
62 62 # link to a referenced ticket
63 63 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
64 64 # link to a changeset
65 65 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"
66 66 end
67 67 ensure
68 68 # restore it
69 69 Redmine::Utils.relative_url_root = relative_url_root
70 70 end
71 71
72 72 def test_generated_links_with_prefix_and_no_relative_url_root
73 73 relative_url_root = Redmine::Utils.relative_url_root
74 74 ActionMailer::Base.deliveries.clear
75 75 Setting.host_name = 'mydomain.foo/rdm'
76 76 Setting.protocol = 'http'
77 77 Redmine::Utils.relative_url_root = nil
78 78
79 79 journal = Journal.find(2)
80 80 assert Mailer.deliver_issue_edit(journal)
81 81
82 82 mail = ActionMailer::Base.deliveries.last
83 83 assert_kind_of TMail::Mail, mail
84 84
85 85 assert_select_email do
86 86 # link to the main ticket
87 87 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
88 88 # link to a referenced ticket
89 89 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
90 90 # link to a changeset
91 91 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"
92 92 end
93 93 ensure
94 94 # restore it
95 95 Redmine::Utils.relative_url_root = relative_url_root
96 96 end
97 97
98 98 def test_email_headers
99 99 ActionMailer::Base.deliveries.clear
100 100 issue = Issue.find(1)
101 101 Mailer.deliver_issue_add(issue)
102 102 mail = ActionMailer::Base.deliveries.last
103 103 assert_not_nil mail
104 104 assert_equal 'bulk', mail.header_string('Precedence')
105 105 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
106 106 end
107 107
108 108 def test_plain_text_mail
109 109 Setting.plain_text_mail = 1
110 110 journal = Journal.find(2)
111 111 Mailer.deliver_issue_edit(journal)
112 112 mail = ActionMailer::Base.deliveries.last
113 113 assert_equal "text/plain", mail.content_type
114 114 assert_equal 0, mail.parts.size
115 115 assert !mail.encoded.include?('href')
116 116 end
117 117
118 118 def test_html_mail
119 119 Setting.plain_text_mail = 0
120 120 journal = Journal.find(2)
121 121 Mailer.deliver_issue_edit(journal)
122 122 mail = ActionMailer::Base.deliveries.last
123 123 assert_equal 2, mail.parts.size
124 124 assert mail.encoded.include?('href')
125 125 end
126 126
127 127 def test_issue_add_message_id
128 128 ActionMailer::Base.deliveries.clear
129 129 issue = Issue.find(1)
130 130 Mailer.deliver_issue_add(issue)
131 131 mail = ActionMailer::Base.deliveries.last
132 132 assert_not_nil mail
133 133 assert_equal Mailer.message_id_for(issue), mail.message_id
134 134 assert_nil mail.references
135 135 end
136 136
137 137 def test_issue_edit_message_id
138 138 ActionMailer::Base.deliveries.clear
139 139 journal = Journal.find(1)
140 140 Mailer.deliver_issue_edit(journal)
141 141 mail = ActionMailer::Base.deliveries.last
142 142 assert_not_nil mail
143 143 assert_equal Mailer.message_id_for(journal), mail.message_id
144 144 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
145 145 end
146 146
147 147 def test_message_posted_message_id
148 148 ActionMailer::Base.deliveries.clear
149 149 message = Message.find(1)
150 150 Mailer.deliver_message_posted(message)
151 151 mail = ActionMailer::Base.deliveries.last
152 152 assert_not_nil mail
153 153 assert_equal Mailer.message_id_for(message), mail.message_id
154 154 assert_nil mail.references
155 155 end
156 156
157 157 def test_reply_posted_message_id
158 158 ActionMailer::Base.deliveries.clear
159 159 message = Message.find(3)
160 160 Mailer.deliver_message_posted(message)
161 161 mail = ActionMailer::Base.deliveries.last
162 162 assert_not_nil mail
163 163 assert_equal Mailer.message_id_for(message), mail.message_id
164 164 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
165 165 end
166 166
167 167 context("#issue_add") do
168 168 setup do
169 169 ActionMailer::Base.deliveries.clear
170 170 Setting.bcc_recipients = '1'
171 171 @issue = Issue.find(1)
172 172 end
173 173
174 174 should "notify project members" do
175 175 assert Mailer.deliver_issue_add(@issue)
176 176 assert last_email.bcc.include?('dlopper@somenet.foo')
177 177 end
178 178
179 179 should "not notify project members that are not allow to view the issue" do
180 180 Role.find(2).remove_permission!(:view_issues)
181 181 assert Mailer.deliver_issue_add(@issue)
182 182 assert !last_email.bcc.include?('dlopper@somenet.foo')
183 183 end
184 184
185 185 should "notify issue watchers" do
186 186 user = User.find(9)
187 # minimal email notification options
188 user.pref[:no_self_notified] = '1'
189 user.pref.save
190 user.mail_notification = false
191 user.save
192
187 193 Watcher.create!(:watchable => @issue, :user => user)
188 194 assert Mailer.deliver_issue_add(@issue)
189 195 assert last_email.bcc.include?(user.mail)
190 196 end
191 197
192 198 should "not notify watchers not allowed to view the issue" do
193 199 user = User.find(9)
194 200 Watcher.create!(:watchable => @issue, :user => user)
195 201 Role.non_member.remove_permission!(:view_issues)
196 202 assert Mailer.deliver_issue_add(@issue)
197 203 assert !last_email.bcc.include?(user.mail)
198 204 end
199 205 end
200 206
201 207 # test mailer methods for each language
202 208 def test_issue_add
203 209 issue = Issue.find(1)
204 210 valid_languages.each do |lang|
205 211 Setting.default_language = lang.to_s
206 212 assert Mailer.deliver_issue_add(issue)
207 213 end
208 214 end
209 215
210 216 def test_issue_edit
211 217 journal = Journal.find(1)
212 218 valid_languages.each do |lang|
213 219 Setting.default_language = lang.to_s
214 220 assert Mailer.deliver_issue_edit(journal)
215 221 end
216 222 end
217 223
218 224 def test_document_added
219 225 document = Document.find(1)
220 226 valid_languages.each do |lang|
221 227 Setting.default_language = lang.to_s
222 228 assert Mailer.deliver_document_added(document)
223 229 end
224 230 end
225 231
226 232 def test_attachments_added
227 233 attachements = [ Attachment.find_by_container_type('Document') ]
228 234 valid_languages.each do |lang|
229 235 Setting.default_language = lang.to_s
230 236 assert Mailer.deliver_attachments_added(attachements)
231 237 end
232 238 end
233 239
234 240 def test_news_added
235 241 news = News.find(:first)
236 242 valid_languages.each do |lang|
237 243 Setting.default_language = lang.to_s
238 244 assert Mailer.deliver_news_added(news)
239 245 end
240 246 end
241 247
242 248 def test_message_posted
243 249 message = Message.find(:first)
244 250 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
245 251 recipients = recipients.compact.uniq
246 252 valid_languages.each do |lang|
247 253 Setting.default_language = lang.to_s
248 254 assert Mailer.deliver_message_posted(message)
249 255 end
250 256 end
251 257
252 258 def test_account_information
253 259 user = User.find(:first)
254 260 valid_languages.each do |lang|
255 261 user.update_attribute :language, lang.to_s
256 262 user.reload
257 263 assert Mailer.deliver_account_information(user, 'pAsswORd')
258 264 end
259 265 end
260 266
261 267 def test_lost_password
262 268 token = Token.find(2)
263 269 valid_languages.each do |lang|
264 270 token.user.update_attribute :language, lang.to_s
265 271 token.reload
266 272 assert Mailer.deliver_lost_password(token)
267 273 end
268 274 end
269 275
270 276 def test_register
271 277 token = Token.find(1)
272 278 Setting.host_name = 'redmine.foo'
273 279 Setting.protocol = 'https'
274 280
275 281 valid_languages.each do |lang|
276 282 token.user.update_attribute :language, lang.to_s
277 283 token.reload
278 284 ActionMailer::Base.deliveries.clear
279 285 assert Mailer.deliver_register(token)
280 286 mail = ActionMailer::Base.deliveries.last
281 287 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
282 288 end
283 289 end
284 290
285 291 def test_reminders
286 292 ActionMailer::Base.deliveries.clear
287 293 Mailer.reminders(:days => 42)
288 294 assert_equal 1, ActionMailer::Base.deliveries.size
289 295 mail = ActionMailer::Base.deliveries.last
290 296 assert mail.bcc.include?('dlopper@somenet.foo')
291 297 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
292 298 end
293 299
294 300 def last_email
295 301 mail = ActionMailer::Base.deliveries.last
296 302 assert_not_nil mail
297 303 mail
298 304 end
299 305
300 306 def test_mailer_should_not_change_locale
301 307 Setting.default_language = 'en'
302 308 # Set current language to italian
303 309 set_language_if_valid 'it'
304 310 # Send an email to a french user
305 311 user = User.find(1)
306 312 user.language = 'fr'
307 313 Mailer.deliver_account_activated(user)
308 314 mail = ActionMailer::Base.deliveries.last
309 315 assert mail.body.include?('Votre compte')
310 316
311 317 assert_equal :it, current_language
312 318 end
313 319 end
General Comments 0
You need to be logged in to leave comments. Login now