##// END OF EJS Templates
Adds a test for emission address with phrase (#4855)....
Jean-Philippe Lang -
r3329:b733accfe336
parent child
Show More
@@ -1,319 +1,329
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
127 def test_mail_from_with_phrase
128 ActionMailer::Base.deliveries.clear
129 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
130 Mailer.deliver_test(User.find(1))
131 end
132 mail = ActionMailer::Base.deliveries.last
133 assert_not_nil mail
134 assert_equal 'Redmine app', mail.from_addrs.first.name
135 end
126 136
127 137 def test_issue_add_message_id
128 138 ActionMailer::Base.deliveries.clear
129 139 issue = Issue.find(1)
130 140 Mailer.deliver_issue_add(issue)
131 141 mail = ActionMailer::Base.deliveries.last
132 142 assert_not_nil mail
133 143 assert_equal Mailer.message_id_for(issue), mail.message_id
134 144 assert_nil mail.references
135 145 end
136 146
137 147 def test_issue_edit_message_id
138 148 ActionMailer::Base.deliveries.clear
139 149 journal = Journal.find(1)
140 150 Mailer.deliver_issue_edit(journal)
141 151 mail = ActionMailer::Base.deliveries.last
142 152 assert_not_nil mail
143 153 assert_equal Mailer.message_id_for(journal), mail.message_id
144 154 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
145 155 end
146 156
147 157 def test_message_posted_message_id
148 158 ActionMailer::Base.deliveries.clear
149 159 message = Message.find(1)
150 160 Mailer.deliver_message_posted(message)
151 161 mail = ActionMailer::Base.deliveries.last
152 162 assert_not_nil mail
153 163 assert_equal Mailer.message_id_for(message), mail.message_id
154 164 assert_nil mail.references
155 165 end
156 166
157 167 def test_reply_posted_message_id
158 168 ActionMailer::Base.deliveries.clear
159 169 message = Message.find(3)
160 170 Mailer.deliver_message_posted(message)
161 171 mail = ActionMailer::Base.deliveries.last
162 172 assert_not_nil mail
163 173 assert_equal Mailer.message_id_for(message), mail.message_id
164 174 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
165 175 end
166 176
167 177 context("#issue_add") do
168 178 setup do
169 179 ActionMailer::Base.deliveries.clear
170 180 Setting.bcc_recipients = '1'
171 181 @issue = Issue.find(1)
172 182 end
173 183
174 184 should "notify project members" do
175 185 assert Mailer.deliver_issue_add(@issue)
176 186 assert last_email.bcc.include?('dlopper@somenet.foo')
177 187 end
178 188
179 189 should "not notify project members that are not allow to view the issue" do
180 190 Role.find(2).remove_permission!(:view_issues)
181 191 assert Mailer.deliver_issue_add(@issue)
182 192 assert !last_email.bcc.include?('dlopper@somenet.foo')
183 193 end
184 194
185 195 should "notify issue watchers" do
186 196 user = User.find(9)
187 197 # minimal email notification options
188 198 user.pref[:no_self_notified] = '1'
189 199 user.pref.save
190 200 user.mail_notification = false
191 201 user.save
192 202
193 203 Watcher.create!(:watchable => @issue, :user => user)
194 204 assert Mailer.deliver_issue_add(@issue)
195 205 assert last_email.bcc.include?(user.mail)
196 206 end
197 207
198 208 should "not notify watchers not allowed to view the issue" do
199 209 user = User.find(9)
200 210 Watcher.create!(:watchable => @issue, :user => user)
201 211 Role.non_member.remove_permission!(:view_issues)
202 212 assert Mailer.deliver_issue_add(@issue)
203 213 assert !last_email.bcc.include?(user.mail)
204 214 end
205 215 end
206 216
207 217 # test mailer methods for each language
208 218 def test_issue_add
209 219 issue = Issue.find(1)
210 220 valid_languages.each do |lang|
211 221 Setting.default_language = lang.to_s
212 222 assert Mailer.deliver_issue_add(issue)
213 223 end
214 224 end
215 225
216 226 def test_issue_edit
217 227 journal = Journal.find(1)
218 228 valid_languages.each do |lang|
219 229 Setting.default_language = lang.to_s
220 230 assert Mailer.deliver_issue_edit(journal)
221 231 end
222 232 end
223 233
224 234 def test_document_added
225 235 document = Document.find(1)
226 236 valid_languages.each do |lang|
227 237 Setting.default_language = lang.to_s
228 238 assert Mailer.deliver_document_added(document)
229 239 end
230 240 end
231 241
232 242 def test_attachments_added
233 243 attachements = [ Attachment.find_by_container_type('Document') ]
234 244 valid_languages.each do |lang|
235 245 Setting.default_language = lang.to_s
236 246 assert Mailer.deliver_attachments_added(attachements)
237 247 end
238 248 end
239 249
240 250 def test_news_added
241 251 news = News.find(:first)
242 252 valid_languages.each do |lang|
243 253 Setting.default_language = lang.to_s
244 254 assert Mailer.deliver_news_added(news)
245 255 end
246 256 end
247 257
248 258 def test_message_posted
249 259 message = Message.find(:first)
250 260 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
251 261 recipients = recipients.compact.uniq
252 262 valid_languages.each do |lang|
253 263 Setting.default_language = lang.to_s
254 264 assert Mailer.deliver_message_posted(message)
255 265 end
256 266 end
257 267
258 268 def test_account_information
259 269 user = User.find(:first)
260 270 valid_languages.each do |lang|
261 271 user.update_attribute :language, lang.to_s
262 272 user.reload
263 273 assert Mailer.deliver_account_information(user, 'pAsswORd')
264 274 end
265 275 end
266 276
267 277 def test_lost_password
268 278 token = Token.find(2)
269 279 valid_languages.each do |lang|
270 280 token.user.update_attribute :language, lang.to_s
271 281 token.reload
272 282 assert Mailer.deliver_lost_password(token)
273 283 end
274 284 end
275 285
276 286 def test_register
277 287 token = Token.find(1)
278 288 Setting.host_name = 'redmine.foo'
279 289 Setting.protocol = 'https'
280 290
281 291 valid_languages.each do |lang|
282 292 token.user.update_attribute :language, lang.to_s
283 293 token.reload
284 294 ActionMailer::Base.deliveries.clear
285 295 assert Mailer.deliver_register(token)
286 296 mail = ActionMailer::Base.deliveries.last
287 297 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
288 298 end
289 299 end
290 300
291 301 def test_reminders
292 302 ActionMailer::Base.deliveries.clear
293 303 Mailer.reminders(:days => 42)
294 304 assert_equal 1, ActionMailer::Base.deliveries.size
295 305 mail = ActionMailer::Base.deliveries.last
296 306 assert mail.bcc.include?('dlopper@somenet.foo')
297 307 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
298 308 end
299 309
300 310 def last_email
301 311 mail = ActionMailer::Base.deliveries.last
302 312 assert_not_nil mail
303 313 mail
304 314 end
305 315
306 316 def test_mailer_should_not_change_locale
307 317 Setting.default_language = 'en'
308 318 # Set current language to italian
309 319 set_language_if_valid 'it'
310 320 # Send an email to a french user
311 321 user = User.find(1)
312 322 user.language = 'fr'
313 323 Mailer.deliver_account_activated(user)
314 324 mail = ActionMailer::Base.deliveries.last
315 325 assert mail.body.include?('Votre compte')
316 326
317 327 assert_equal :it, current_language
318 328 end
319 329 end
General Comments 0
You need to be logged in to leave comments. Login now