@@ -1,340 +1,342 | |||
|
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_should_ignore_emails_from_emission_address |
|
214 | 216 | Role.anonymous.add_permission!(:add_issues) |
|
215 | 217 | assert_no_difference 'User.count' do |
|
216 | 218 | assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create') |
|
217 | 219 | end |
|
218 | 220 | end |
|
219 | 221 | |
|
220 | 222 | def test_add_issue_should_send_email_notification |
|
221 | 223 | ActionMailer::Base.deliveries.clear |
|
222 | 224 | # This email contains: 'Project: onlinestore' |
|
223 | 225 | issue = submit_email('ticket_on_given_project.eml') |
|
224 | 226 | assert issue.is_a?(Issue) |
|
225 | 227 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
226 | 228 | end |
|
227 | 229 | |
|
228 | 230 | def test_add_issue_note |
|
229 | 231 | journal = submit_email('ticket_reply.eml') |
|
230 | 232 | assert journal.is_a?(Journal) |
|
231 | 233 | assert_equal User.find_by_login('jsmith'), journal.user |
|
232 | 234 | assert_equal Issue.find(2), journal.journalized |
|
233 | 235 | assert_match /This is reply/, journal.notes |
|
234 | 236 | end |
|
235 | 237 | |
|
236 | 238 | def test_add_issue_note_with_status_change |
|
237 | 239 | # This email contains: 'Status: Resolved' |
|
238 | 240 | journal = submit_email('ticket_reply_with_status.eml') |
|
239 | 241 | assert journal.is_a?(Journal) |
|
240 | 242 | issue = Issue.find(journal.issue.id) |
|
241 | 243 | assert_equal User.find_by_login('jsmith'), journal.user |
|
242 | 244 | assert_equal Issue.find(2), journal.journalized |
|
243 | 245 | assert_match /This is reply/, journal.notes |
|
244 | 246 | assert_equal IssueStatus.find_by_name("Resolved"), issue.status |
|
245 | 247 | end |
|
246 | 248 | |
|
247 | 249 | def test_add_issue_note_should_send_email_notification |
|
248 | 250 | ActionMailer::Base.deliveries.clear |
|
249 | 251 | journal = submit_email('ticket_reply.eml') |
|
250 | 252 | assert journal.is_a?(Journal) |
|
251 | 253 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
252 | 254 | end |
|
253 | 255 | |
|
254 | 256 | def test_reply_to_a_message |
|
255 | 257 | m = submit_email('message_reply.eml') |
|
256 | 258 | assert m.is_a?(Message) |
|
257 | 259 | assert !m.new_record? |
|
258 | 260 | m.reload |
|
259 | 261 | assert_equal 'Reply via email', m.subject |
|
260 | 262 | # The email replies to message #2 which is part of the thread of message #1 |
|
261 | 263 | assert_equal Message.find(1), m.parent |
|
262 | 264 | end |
|
263 | 265 | |
|
264 | 266 | def test_reply_to_a_message_by_subject |
|
265 | 267 | m = submit_email('message_reply_by_subject.eml') |
|
266 | 268 | assert m.is_a?(Message) |
|
267 | 269 | assert !m.new_record? |
|
268 | 270 | m.reload |
|
269 | 271 | assert_equal 'Reply to the first post', m.subject |
|
270 | 272 | assert_equal Message.find(1), m.parent |
|
271 | 273 | end |
|
272 | 274 | |
|
273 | 275 | def test_should_strip_tags_of_html_only_emails |
|
274 | 276 | issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'}) |
|
275 | 277 | assert issue.is_a?(Issue) |
|
276 | 278 | assert !issue.new_record? |
|
277 | 279 | issue.reload |
|
278 | 280 | assert_equal 'HTML email', issue.subject |
|
279 | 281 | assert_equal 'This is a html-only email.', issue.description |
|
280 | 282 | end |
|
281 | 283 | |
|
282 | 284 | context "truncate emails based on the Setting" do |
|
283 | 285 | context "with no setting" do |
|
284 | 286 | setup do |
|
285 | 287 | Setting.mail_handler_body_delimiters = '' |
|
286 | 288 | end |
|
287 | 289 | |
|
288 | 290 | should "add the entire email into the issue" do |
|
289 | 291 | issue = submit_email('ticket_on_given_project.eml') |
|
290 | 292 | assert_issue_created(issue) |
|
291 | 293 | assert issue.description.include?('---') |
|
292 | 294 | assert issue.description.include?('This paragraph is after the delimiter') |
|
293 | 295 | end |
|
294 | 296 | end |
|
295 | 297 | |
|
296 | 298 | context "with a single string" do |
|
297 | 299 | setup do |
|
298 | 300 | Setting.mail_handler_body_delimiters = '---' |
|
299 | 301 | end |
|
300 | 302 | |
|
301 | 303 | should "truncate the email at the delimiter for the issue" do |
|
302 | 304 | issue = submit_email('ticket_on_given_project.eml') |
|
303 | 305 | assert_issue_created(issue) |
|
304 | 306 | assert issue.description.include?('This paragraph is before delimiters') |
|
305 | 307 | assert issue.description.include?('--- This line starts with a delimiter') |
|
306 | 308 | assert !issue.description.match(/^---$/) |
|
307 | 309 | assert !issue.description.include?('This paragraph is after the delimiter') |
|
308 | 310 | end |
|
309 | 311 | end |
|
310 | 312 | |
|
311 | 313 | context "with multiple strings" do |
|
312 | 314 | setup do |
|
313 | 315 | Setting.mail_handler_body_delimiters = "---\nBREAK" |
|
314 | 316 | end |
|
315 | 317 | |
|
316 | 318 | should "truncate the email at the first delimiter found (BREAK)" do |
|
317 | 319 | issue = submit_email('ticket_on_given_project.eml') |
|
318 | 320 | assert_issue_created(issue) |
|
319 | 321 | assert issue.description.include?('This paragraph is before delimiters') |
|
320 | 322 | assert !issue.description.include?('BREAK') |
|
321 | 323 | assert !issue.description.include?('This paragraph is between delimiters') |
|
322 | 324 | assert !issue.description.match(/^---$/) |
|
323 | 325 | assert !issue.description.include?('This paragraph is after the delimiter') |
|
324 | 326 | end |
|
325 | 327 | end |
|
326 | 328 | end |
|
327 | 329 | |
|
328 | 330 | private |
|
329 | 331 | |
|
330 | 332 | def submit_email(filename, options={}) |
|
331 | 333 | raw = IO.read(File.join(FIXTURES_PATH, filename)) |
|
332 | 334 | MailHandler.receive(raw, options) |
|
333 | 335 | end |
|
334 | 336 | |
|
335 | 337 | def assert_issue_created(issue) |
|
336 | 338 | assert issue.is_a?(Issue) |
|
337 | 339 | assert !issue.new_record? |
|
338 | 340 | issue.reload |
|
339 | 341 | end |
|
340 | 342 | end |
General Comments 0
You need to be logged in to leave comments.
Login now