##// END OF EJS Templates
Fixed: tests with ruby1.9 broken by r3322 (#4607)....
Jean-Philippe Lang -
r3232:080ad6a16c51
parent child
Show More
@@ -1,348 +1,350
1 # encoding: utf-8
2 #
1 3 # Redmine - project management software
2 4 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 5 #
4 6 # This program is free software; you can redistribute it and/or
5 7 # modify it under the terms of the GNU General Public License
6 8 # as published by the Free Software Foundation; either version 2
7 9 # of the License, or (at your option) any later version.
8 10 #
9 11 # This program is distributed in the hope that it will be useful,
10 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 14 # GNU General Public License for more details.
13 15 #
14 16 # You should have received a copy of the GNU General Public License
15 17 # along with this program; if not, write to the Free Software
16 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 19
18 20 require File.dirname(__FILE__) + '/../test_helper'
19 21
20 22 class MailHandlerTest < ActiveSupport::TestCase
21 23 fixtures :users, :projects,
22 24 :enabled_modules,
23 25 :roles,
24 26 :members,
25 27 :member_roles,
26 28 :issues,
27 29 :issue_statuses,
28 30 :workflows,
29 31 :trackers,
30 32 :projects_trackers,
31 33 :enumerations,
32 34 :issue_categories,
33 35 :custom_fields,
34 36 :custom_fields_trackers,
35 37 :boards,
36 38 :messages
37 39
38 40 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
39 41
40 42 def setup
41 43 ActionMailer::Base.deliveries.clear
42 44 end
43 45
44 46 def test_add_issue
45 47 ActionMailer::Base.deliveries.clear
46 48 # This email contains: 'Project: onlinestore'
47 49 issue = submit_email('ticket_on_given_project.eml')
48 50 assert issue.is_a?(Issue)
49 51 assert !issue.new_record?
50 52 issue.reload
51 53 assert_equal 'New ticket on a given project', issue.subject
52 54 assert_equal User.find_by_login('jsmith'), issue.author
53 55 assert_equal Project.find(2), issue.project
54 56 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
55 57 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
56 58 # keywords should be removed from the email body
57 59 assert !issue.description.match(/^Project:/i)
58 60 assert !issue.description.match(/^Status:/i)
59 61 # Email notification should be sent
60 62 mail = ActionMailer::Base.deliveries.last
61 63 assert_not_nil mail
62 64 assert mail.subject.include?('New ticket on a given project')
63 65 end
64 66
65 67 def test_add_issue_with_status
66 68 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
67 69 issue = submit_email('ticket_on_given_project.eml')
68 70 assert issue.is_a?(Issue)
69 71 assert !issue.new_record?
70 72 issue.reload
71 73 assert_equal Project.find(2), issue.project
72 74 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
73 75 end
74 76
75 77 def test_add_issue_with_attributes_override
76 78 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
77 79 assert issue.is_a?(Issue)
78 80 assert !issue.new_record?
79 81 issue.reload
80 82 assert_equal 'New ticket on a given project', issue.subject
81 83 assert_equal User.find_by_login('jsmith'), issue.author
82 84 assert_equal Project.find(2), issue.project
83 85 assert_equal 'Feature request', issue.tracker.to_s
84 86 assert_equal 'Stock management', issue.category.to_s
85 87 assert_equal 'Urgent', issue.priority.to_s
86 88 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
87 89 end
88 90
89 91 def test_add_issue_with_partial_attributes_override
90 92 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
91 93 assert issue.is_a?(Issue)
92 94 assert !issue.new_record?
93 95 issue.reload
94 96 assert_equal 'New ticket on a given project', issue.subject
95 97 assert_equal User.find_by_login('jsmith'), issue.author
96 98 assert_equal Project.find(2), issue.project
97 99 assert_equal 'Feature request', issue.tracker.to_s
98 100 assert_nil issue.category
99 101 assert_equal 'High', issue.priority.to_s
100 102 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
101 103 end
102 104
103 105 def test_add_issue_with_spaces_between_attribute_and_separator
104 106 issue = submit_email('ticket_with_spaces_between_attribute_and_separator.eml', :allow_override => 'tracker,category,priority')
105 107 assert issue.is_a?(Issue)
106 108 assert !issue.new_record?
107 109 issue.reload
108 110 assert_equal 'New ticket on a given project', issue.subject
109 111 assert_equal User.find_by_login('jsmith'), issue.author
110 112 assert_equal Project.find(2), issue.project
111 113 assert_equal 'Feature request', issue.tracker.to_s
112 114 assert_equal 'Stock management', issue.category.to_s
113 115 assert_equal 'Urgent', issue.priority.to_s
114 116 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
115 117 end
116 118
117 119
118 120 def test_add_issue_with_attachment_to_specific_project
119 121 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
120 122 assert issue.is_a?(Issue)
121 123 assert !issue.new_record?
122 124 issue.reload
123 125 assert_equal 'Ticket created by email with attachment', issue.subject
124 126 assert_equal User.find_by_login('jsmith'), issue.author
125 127 assert_equal Project.find(2), issue.project
126 128 assert_equal 'This is a new ticket with attachments', issue.description
127 129 # Attachment properties
128 130 assert_equal 1, issue.attachments.size
129 131 assert_equal 'Paella.jpg', issue.attachments.first.filename
130 132 assert_equal 'image/jpeg', issue.attachments.first.content_type
131 133 assert_equal 10790, issue.attachments.first.filesize
132 134 end
133 135
134 136 def test_add_issue_with_custom_fields
135 137 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
136 138 assert issue.is_a?(Issue)
137 139 assert !issue.new_record?
138 140 issue.reload
139 141 assert_equal 'New ticket with custom field values', issue.subject
140 142 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
141 143 assert !issue.description.match(/^searchable field:/i)
142 144 end
143 145
144 146 def test_add_issue_with_cc
145 147 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
146 148 assert issue.is_a?(Issue)
147 149 assert !issue.new_record?
148 150 issue.reload
149 151 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
150 152 assert_equal 1, issue.watchers.size
151 153 end
152 154
153 155 def test_add_issue_by_unknown_user
154 156 assert_no_difference 'User.count' do
155 157 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'})
156 158 end
157 159 end
158 160
159 161 def test_add_issue_by_anonymous_user
160 162 Role.anonymous.add_permission!(:add_issues)
161 163 assert_no_difference 'User.count' do
162 164 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
163 165 assert issue.is_a?(Issue)
164 166 assert issue.author.anonymous?
165 167 end
166 168 end
167 169
168 170 def test_add_issue_by_anonymous_user_on_private_project
169 171 Role.anonymous.add_permission!(:add_issues)
170 172 assert_no_difference 'User.count' do
171 173 assert_no_difference 'Issue.count' do
172 174 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :unknown_user => 'accept')
173 175 end
174 176 end
175 177 end
176 178
177 179 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
178 180 assert_no_difference 'User.count' do
179 181 assert_difference 'Issue.count' do
180 182 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :no_permission_check => '1', :unknown_user => 'accept')
181 183 assert issue.is_a?(Issue)
182 184 assert issue.author.anonymous?
183 185 assert !issue.project.is_public?
184 186 end
185 187 end
186 188 end
187 189
188 190 def test_add_issue_by_created_user
189 191 Setting.default_language = 'en'
190 192 assert_difference 'User.count' do
191 193 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
192 194 assert issue.is_a?(Issue)
193 195 assert issue.author.active?
194 196 assert_equal 'john.doe@somenet.foo', issue.author.mail
195 197 assert_equal 'John', issue.author.firstname
196 198 assert_equal 'Doe', issue.author.lastname
197 199
198 200 # account information
199 201 email = ActionMailer::Base.deliveries.first
200 202 assert_not_nil email
201 203 assert email.subject.include?('account activation')
202 204 login = email.body.match(/\* Login: (.*)$/)[1]
203 205 password = email.body.match(/\* Password: (.*)$/)[1]
204 206 assert_equal issue.author, User.try_to_login(login, password)
205 207 end
206 208 end
207 209
208 210 def test_add_issue_without_from_header
209 211 Role.anonymous.add_permission!(:add_issues)
210 212 assert_equal false, submit_email('ticket_without_from_header.eml')
211 213 end
212 214
213 215 def test_add_issue_with_japanese_keywords
214 216 tracker = Tracker.create!(:name => 'ι–‹η™Ί')
215 217 Project.find(1).trackers << tracker
216 218 issue = submit_email('japanese_keywords_iso_2022_jp.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'tracker')
217 219 assert_kind_of Issue, issue
218 220 assert_equal tracker, issue.tracker
219 221 end
220 222
221 223 def test_should_ignore_emails_from_emission_address
222 224 Role.anonymous.add_permission!(:add_issues)
223 225 assert_no_difference 'User.count' do
224 226 assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
225 227 end
226 228 end
227 229
228 230 def test_add_issue_should_send_email_notification
229 231 ActionMailer::Base.deliveries.clear
230 232 # This email contains: 'Project: onlinestore'
231 233 issue = submit_email('ticket_on_given_project.eml')
232 234 assert issue.is_a?(Issue)
233 235 assert_equal 1, ActionMailer::Base.deliveries.size
234 236 end
235 237
236 238 def test_add_issue_note
237 239 journal = submit_email('ticket_reply.eml')
238 240 assert journal.is_a?(Journal)
239 241 assert_equal User.find_by_login('jsmith'), journal.user
240 242 assert_equal Issue.find(2), journal.journalized
241 243 assert_match /This is reply/, journal.notes
242 244 end
243 245
244 246 def test_add_issue_note_with_status_change
245 247 # This email contains: 'Status: Resolved'
246 248 journal = submit_email('ticket_reply_with_status.eml')
247 249 assert journal.is_a?(Journal)
248 250 issue = Issue.find(journal.issue.id)
249 251 assert_equal User.find_by_login('jsmith'), journal.user
250 252 assert_equal Issue.find(2), journal.journalized
251 253 assert_match /This is reply/, journal.notes
252 254 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
253 255 end
254 256
255 257 def test_add_issue_note_should_send_email_notification
256 258 ActionMailer::Base.deliveries.clear
257 259 journal = submit_email('ticket_reply.eml')
258 260 assert journal.is_a?(Journal)
259 261 assert_equal 1, ActionMailer::Base.deliveries.size
260 262 end
261 263
262 264 def test_reply_to_a_message
263 265 m = submit_email('message_reply.eml')
264 266 assert m.is_a?(Message)
265 267 assert !m.new_record?
266 268 m.reload
267 269 assert_equal 'Reply via email', m.subject
268 270 # The email replies to message #2 which is part of the thread of message #1
269 271 assert_equal Message.find(1), m.parent
270 272 end
271 273
272 274 def test_reply_to_a_message_by_subject
273 275 m = submit_email('message_reply_by_subject.eml')
274 276 assert m.is_a?(Message)
275 277 assert !m.new_record?
276 278 m.reload
277 279 assert_equal 'Reply to the first post', m.subject
278 280 assert_equal Message.find(1), m.parent
279 281 end
280 282
281 283 def test_should_strip_tags_of_html_only_emails
282 284 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
283 285 assert issue.is_a?(Issue)
284 286 assert !issue.new_record?
285 287 issue.reload
286 288 assert_equal 'HTML email', issue.subject
287 289 assert_equal 'This is a html-only email.', issue.description
288 290 end
289 291
290 292 context "truncate emails based on the Setting" do
291 293 context "with no setting" do
292 294 setup do
293 295 Setting.mail_handler_body_delimiters = ''
294 296 end
295 297
296 298 should "add the entire email into the issue" do
297 299 issue = submit_email('ticket_on_given_project.eml')
298 300 assert_issue_created(issue)
299 301 assert issue.description.include?('---')
300 302 assert issue.description.include?('This paragraph is after the delimiter')
301 303 end
302 304 end
303 305
304 306 context "with a single string" do
305 307 setup do
306 308 Setting.mail_handler_body_delimiters = '---'
307 309 end
308 310
309 311 should "truncate the email at the delimiter for the issue" do
310 312 issue = submit_email('ticket_on_given_project.eml')
311 313 assert_issue_created(issue)
312 314 assert issue.description.include?('This paragraph is before delimiters')
313 315 assert issue.description.include?('--- This line starts with a delimiter')
314 316 assert !issue.description.match(/^---$/)
315 317 assert !issue.description.include?('This paragraph is after the delimiter')
316 318 end
317 319 end
318 320
319 321 context "with multiple strings" do
320 322 setup do
321 323 Setting.mail_handler_body_delimiters = "---\nBREAK"
322 324 end
323 325
324 326 should "truncate the email at the first delimiter found (BREAK)" do
325 327 issue = submit_email('ticket_on_given_project.eml')
326 328 assert_issue_created(issue)
327 329 assert issue.description.include?('This paragraph is before delimiters')
328 330 assert !issue.description.include?('BREAK')
329 331 assert !issue.description.include?('This paragraph is between delimiters')
330 332 assert !issue.description.match(/^---$/)
331 333 assert !issue.description.include?('This paragraph is after the delimiter')
332 334 end
333 335 end
334 336 end
335 337
336 338 private
337 339
338 340 def submit_email(filename, options={})
339 341 raw = IO.read(File.join(FIXTURES_PATH, filename))
340 342 MailHandler.receive(raw, options)
341 343 end
342 344
343 345 def assert_issue_created(issue)
344 346 assert issue.is_a?(Issue)
345 347 assert !issue.new_record?
346 348 issue.reload
347 349 end
348 350 end
General Comments 0
You need to be logged in to leave comments. Login now