##// END OF EJS Templates
code layout clean up test/unit/mail_handler_test.rb...
Toshi MARUYAMA -
r8621:65a1f54f71d7
parent child
Show More
@@ -1,534 +1,595
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require File.expand_path('../../test_helper', __FILE__)
20 require File.expand_path('../../test_helper', __FILE__)
21
21
22 class MailHandlerTest < ActiveSupport::TestCase
22 class MailHandlerTest < ActiveSupport::TestCase
23 fixtures :users, :projects, :enabled_modules, :roles,
23 fixtures :users, :projects, :enabled_modules, :roles,
24 :members, :member_roles, :users,
24 :members, :member_roles, :users,
25 :issues, :issue_statuses,
25 :issues, :issue_statuses,
26 :workflows, :trackers, :projects_trackers,
26 :workflows, :trackers, :projects_trackers,
27 :versions, :enumerations, :issue_categories,
27 :versions, :enumerations, :issue_categories,
28 :custom_fields, :custom_fields_trackers, :custom_fields_projects,
28 :custom_fields, :custom_fields_trackers, :custom_fields_projects,
29 :boards, :messages
29 :boards, :messages
30
30
31 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
31 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
32
32
33 def setup
33 def setup
34 ActionMailer::Base.deliveries.clear
34 ActionMailer::Base.deliveries.clear
35 Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
35 Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
36 end
36 end
37
37
38 def test_add_issue
38 def test_add_issue
39 ActionMailer::Base.deliveries.clear
39 ActionMailer::Base.deliveries.clear
40 # This email contains: 'Project: onlinestore'
40 # This email contains: 'Project: onlinestore'
41 issue = submit_email('ticket_on_given_project.eml')
41 issue = submit_email('ticket_on_given_project.eml')
42 assert issue.is_a?(Issue)
42 assert issue.is_a?(Issue)
43 assert !issue.new_record?
43 assert !issue.new_record?
44 issue.reload
44 issue.reload
45 assert_equal Project.find(2), issue.project
45 assert_equal Project.find(2), issue.project
46 assert_equal issue.project.trackers.first, issue.tracker
46 assert_equal issue.project.trackers.first, issue.tracker
47 assert_equal 'New ticket on a given project', issue.subject
47 assert_equal 'New ticket on a given project', issue.subject
48 assert_equal User.find_by_login('jsmith'), issue.author
48 assert_equal User.find_by_login('jsmith'), issue.author
49 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
49 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
50 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
50 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
51 assert_equal '2010-01-01', issue.start_date.to_s
51 assert_equal '2010-01-01', issue.start_date.to_s
52 assert_equal '2010-12-31', issue.due_date.to_s
52 assert_equal '2010-12-31', issue.due_date.to_s
53 assert_equal User.find_by_login('jsmith'), issue.assigned_to
53 assert_equal User.find_by_login('jsmith'), issue.assigned_to
54 assert_equal Version.find_by_name('Alpha'), issue.fixed_version
54 assert_equal Version.find_by_name('Alpha'), issue.fixed_version
55 assert_equal 2.5, issue.estimated_hours
55 assert_equal 2.5, issue.estimated_hours
56 assert_equal 30, issue.done_ratio
56 assert_equal 30, issue.done_ratio
57 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
57 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
58 # keywords should be removed from the email body
58 # keywords should be removed from the email body
59 assert !issue.description.match(/^Project:/i)
59 assert !issue.description.match(/^Project:/i)
60 assert !issue.description.match(/^Status:/i)
60 assert !issue.description.match(/^Status:/i)
61 assert !issue.description.match(/^Start Date:/i)
61 assert !issue.description.match(/^Start Date:/i)
62 # Email notification should be sent
62 # Email notification should be sent
63 mail = ActionMailer::Base.deliveries.last
63 mail = ActionMailer::Base.deliveries.last
64 assert_not_nil mail
64 assert_not_nil mail
65 assert mail.subject.include?('New ticket on a given project')
65 assert mail.subject.include?('New ticket on a given project')
66 end
66 end
67
67
68 def test_add_issue_with_default_tracker
68 def test_add_issue_with_default_tracker
69 # This email contains: 'Project: onlinestore'
69 # This email contains: 'Project: onlinestore'
70 issue = submit_email('ticket_on_given_project.eml', :issue => {:tracker => 'Support request'})
70 issue = submit_email(
71 'ticket_on_given_project.eml',
72 :issue => {:tracker => 'Support request'}
73 )
71 assert issue.is_a?(Issue)
74 assert issue.is_a?(Issue)
72 assert !issue.new_record?
75 assert !issue.new_record?
73 issue.reload
76 issue.reload
74 assert_equal 'Support request', issue.tracker.name
77 assert_equal 'Support request', issue.tracker.name
75 end
78 end
76
79
77 def test_add_issue_with_status
80 def test_add_issue_with_status
78 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
81 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
79 issue = submit_email('ticket_on_given_project.eml')
82 issue = submit_email('ticket_on_given_project.eml')
80 assert issue.is_a?(Issue)
83 assert issue.is_a?(Issue)
81 assert !issue.new_record?
84 assert !issue.new_record?
82 issue.reload
85 issue.reload
83 assert_equal Project.find(2), issue.project
86 assert_equal Project.find(2), issue.project
84 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
87 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
85 end
88 end
86
89
87 def test_add_issue_with_attributes_override
90 def test_add_issue_with_attributes_override
88 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
91 issue = submit_email(
92 'ticket_with_attributes.eml',
93 :allow_override => 'tracker,category,priority'
94 )
89 assert issue.is_a?(Issue)
95 assert issue.is_a?(Issue)
90 assert !issue.new_record?
96 assert !issue.new_record?
91 issue.reload
97 issue.reload
92 assert_equal 'New ticket on a given project', issue.subject
98 assert_equal 'New ticket on a given project', issue.subject
93 assert_equal User.find_by_login('jsmith'), issue.author
99 assert_equal User.find_by_login('jsmith'), issue.author
94 assert_equal Project.find(2), issue.project
100 assert_equal Project.find(2), issue.project
95 assert_equal 'Feature request', issue.tracker.to_s
101 assert_equal 'Feature request', issue.tracker.to_s
96 assert_equal 'Stock management', issue.category.to_s
102 assert_equal 'Stock management', issue.category.to_s
97 assert_equal 'Urgent', issue.priority.to_s
103 assert_equal 'Urgent', issue.priority.to_s
98 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
104 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
99 end
105 end
100
106
101 def test_add_issue_with_group_assignment
107 def test_add_issue_with_group_assignment
102 with_settings :issue_group_assignment => '1' do
108 with_settings :issue_group_assignment => '1' do
103 issue = submit_email('ticket_on_given_project.eml') do |email|
109 issue = submit_email('ticket_on_given_project.eml') do |email|
104 email.gsub!('Assigned to: John Smith', 'Assigned to: B Team')
110 email.gsub!('Assigned to: John Smith', 'Assigned to: B Team')
105 end
111 end
106 assert issue.is_a?(Issue)
112 assert issue.is_a?(Issue)
107 assert !issue.new_record?
113 assert !issue.new_record?
108 issue.reload
114 issue.reload
109 assert_equal Group.find(11), issue.assigned_to
115 assert_equal Group.find(11), issue.assigned_to
110 end
116 end
111 end
117 end
112
118
113 def test_add_issue_with_partial_attributes_override
119 def test_add_issue_with_partial_attributes_override
114 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
120 issue = submit_email(
121 'ticket_with_attributes.eml',
122 :issue => {:priority => 'High'},
123 :allow_override => ['tracker']
124 )
115 assert issue.is_a?(Issue)
125 assert issue.is_a?(Issue)
116 assert !issue.new_record?
126 assert !issue.new_record?
117 issue.reload
127 issue.reload
118 assert_equal 'New ticket on a given project', issue.subject
128 assert_equal 'New ticket on a given project', issue.subject
119 assert_equal User.find_by_login('jsmith'), issue.author
129 assert_equal User.find_by_login('jsmith'), issue.author
120 assert_equal Project.find(2), issue.project
130 assert_equal Project.find(2), issue.project
121 assert_equal 'Feature request', issue.tracker.to_s
131 assert_equal 'Feature request', issue.tracker.to_s
122 assert_nil issue.category
132 assert_nil issue.category
123 assert_equal 'High', issue.priority.to_s
133 assert_equal 'High', issue.priority.to_s
124 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
134 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
125 end
135 end
126
136
127 def test_add_issue_with_spaces_between_attribute_and_separator
137 def test_add_issue_with_spaces_between_attribute_and_separator
128 issue = submit_email('ticket_with_spaces_between_attribute_and_separator.eml', :allow_override => 'tracker,category,priority')
138 issue = submit_email(
139 'ticket_with_spaces_between_attribute_and_separator.eml',
140 :allow_override => 'tracker,category,priority'
141 )
129 assert issue.is_a?(Issue)
142 assert issue.is_a?(Issue)
130 assert !issue.new_record?
143 assert !issue.new_record?
131 issue.reload
144 issue.reload
132 assert_equal 'New ticket on a given project', issue.subject
145 assert_equal 'New ticket on a given project', issue.subject
133 assert_equal User.find_by_login('jsmith'), issue.author
146 assert_equal User.find_by_login('jsmith'), issue.author
134 assert_equal Project.find(2), issue.project
147 assert_equal Project.find(2), issue.project
135 assert_equal 'Feature request', issue.tracker.to_s
148 assert_equal 'Feature request', issue.tracker.to_s
136 assert_equal 'Stock management', issue.category.to_s
149 assert_equal 'Stock management', issue.category.to_s
137 assert_equal 'Urgent', issue.priority.to_s
150 assert_equal 'Urgent', issue.priority.to_s
138 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
151 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
139 end
152 end
140
153
141 def test_add_issue_with_attachment_to_specific_project
154 def test_add_issue_with_attachment_to_specific_project
142 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
155 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
143 assert issue.is_a?(Issue)
156 assert issue.is_a?(Issue)
144 assert !issue.new_record?
157 assert !issue.new_record?
145 issue.reload
158 issue.reload
146 assert_equal 'Ticket created by email with attachment', issue.subject
159 assert_equal 'Ticket created by email with attachment', issue.subject
147 assert_equal User.find_by_login('jsmith'), issue.author
160 assert_equal User.find_by_login('jsmith'), issue.author
148 assert_equal Project.find(2), issue.project
161 assert_equal Project.find(2), issue.project
149 assert_equal 'This is a new ticket with attachments', issue.description
162 assert_equal 'This is a new ticket with attachments', issue.description
150 # Attachment properties
163 # Attachment properties
151 assert_equal 1, issue.attachments.size
164 assert_equal 1, issue.attachments.size
152 assert_equal 'Paella.jpg', issue.attachments.first.filename
165 assert_equal 'Paella.jpg', issue.attachments.first.filename
153 assert_equal 'image/jpeg', issue.attachments.first.content_type
166 assert_equal 'image/jpeg', issue.attachments.first.content_type
154 assert_equal 10790, issue.attachments.first.filesize
167 assert_equal 10790, issue.attachments.first.filesize
155 end
168 end
156
169
157 def test_add_issue_with_custom_fields
170 def test_add_issue_with_custom_fields
158 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
171 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
159 assert issue.is_a?(Issue)
172 assert issue.is_a?(Issue)
160 assert !issue.new_record?
173 assert !issue.new_record?
161 issue.reload
174 issue.reload
162 assert_equal 'New ticket with custom field values', issue.subject
175 assert_equal 'New ticket with custom field values', issue.subject
163 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
176 assert_equal 'Value for a custom field',
177 issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
164 assert !issue.description.match(/^searchable field:/i)
178 assert !issue.description.match(/^searchable field:/i)
165 end
179 end
166
180
167 def test_add_issue_with_cc
181 def test_add_issue_with_cc
168 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
182 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
169 assert issue.is_a?(Issue)
183 assert issue.is_a?(Issue)
170 assert !issue.new_record?
184 assert !issue.new_record?
171 issue.reload
185 issue.reload
172 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
186 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
173 assert_equal 1, issue.watcher_user_ids.size
187 assert_equal 1, issue.watcher_user_ids.size
174 end
188 end
175
189
176 def test_add_issue_by_unknown_user
190 def test_add_issue_by_unknown_user
177 assert_no_difference 'User.count' do
191 assert_no_difference 'User.count' do
178 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'})
192 assert_equal false,
193 submit_email(
194 'ticket_by_unknown_user.eml',
195 :issue => {:project => 'ecookbook'}
196 )
179 end
197 end
180 end
198 end
181
199
182 def test_add_issue_by_anonymous_user
200 def test_add_issue_by_anonymous_user
183 Role.anonymous.add_permission!(:add_issues)
201 Role.anonymous.add_permission!(:add_issues)
184 assert_no_difference 'User.count' do
202 assert_no_difference 'User.count' do
185 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
203 issue = submit_email(
204 'ticket_by_unknown_user.eml',
205 :issue => {:project => 'ecookbook'},
206 :unknown_user => 'accept'
207 )
186 assert issue.is_a?(Issue)
208 assert issue.is_a?(Issue)
187 assert issue.author.anonymous?
209 assert issue.author.anonymous?
188 end
210 end
189 end
211 end
190
212
191 def test_add_issue_by_anonymous_user_with_no_from_address
213 def test_add_issue_by_anonymous_user_with_no_from_address
192 Role.anonymous.add_permission!(:add_issues)
214 Role.anonymous.add_permission!(:add_issues)
193 assert_no_difference 'User.count' do
215 assert_no_difference 'User.count' do
194 issue = submit_email('ticket_by_empty_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
216 issue = submit_email(
217 'ticket_by_empty_user.eml',
218 :issue => {:project => 'ecookbook'},
219 :unknown_user => 'accept'
220 )
195 assert issue.is_a?(Issue)
221 assert issue.is_a?(Issue)
196 assert issue.author.anonymous?
222 assert issue.author.anonymous?
197 end
223 end
198 end
224 end
199
225
200 def test_add_issue_by_anonymous_user_on_private_project
226 def test_add_issue_by_anonymous_user_on_private_project
201 Role.anonymous.add_permission!(:add_issues)
227 Role.anonymous.add_permission!(:add_issues)
202 assert_no_difference 'User.count' do
228 assert_no_difference 'User.count' do
203 assert_no_difference 'Issue.count' do
229 assert_no_difference 'Issue.count' do
204 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :unknown_user => 'accept')
230 assert_equal false,
231 submit_email(
232 'ticket_by_unknown_user.eml',
233 :issue => {:project => 'onlinestore'},
234 :unknown_user => 'accept'
235 )
205 end
236 end
206 end
237 end
207 end
238 end
208
239
209 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
240 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
210 assert_no_difference 'User.count' do
241 assert_no_difference 'User.count' do
211 assert_difference 'Issue.count' do
242 assert_difference 'Issue.count' do
212 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :no_permission_check => '1', :unknown_user => 'accept')
243 issue = submit_email(
244 'ticket_by_unknown_user.eml',
245 :issue => {:project => 'onlinestore'},
246 :no_permission_check => '1',
247 :unknown_user => 'accept'
248 )
213 assert issue.is_a?(Issue)
249 assert issue.is_a?(Issue)
214 assert issue.author.anonymous?
250 assert issue.author.anonymous?
215 assert !issue.project.is_public?
251 assert !issue.project.is_public?
216 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
252 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
217 end
253 end
218 end
254 end
219 end
255 end
220
256
221 def test_add_issue_by_created_user
257 def test_add_issue_by_created_user
222 Setting.default_language = 'en'
258 Setting.default_language = 'en'
223 assert_difference 'User.count' do
259 assert_difference 'User.count' do
224 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
260 issue = submit_email(
261 'ticket_by_unknown_user.eml',
262 :issue => {:project => 'ecookbook'},
263 :unknown_user => 'create'
264 )
225 assert issue.is_a?(Issue)
265 assert issue.is_a?(Issue)
226 assert issue.author.active?
266 assert issue.author.active?
227 assert_equal 'john.doe@somenet.foo', issue.author.mail
267 assert_equal 'john.doe@somenet.foo', issue.author.mail
228 assert_equal 'John', issue.author.firstname
268 assert_equal 'John', issue.author.firstname
229 assert_equal 'Doe', issue.author.lastname
269 assert_equal 'Doe', issue.author.lastname
230
270
231 # account information
271 # account information
232 email = ActionMailer::Base.deliveries.first
272 email = ActionMailer::Base.deliveries.first
233 assert_not_nil email
273 assert_not_nil email
234 assert email.subject.include?('account activation')
274 assert email.subject.include?('account activation')
235 login = email.body.match(/\* Login: (.*)$/)[1]
275 login = email.body.match(/\* Login: (.*)$/)[1]
236 password = email.body.match(/\* Password: (.*)$/)[1]
276 password = email.body.match(/\* Password: (.*)$/)[1]
237 assert_equal issue.author, User.try_to_login(login, password)
277 assert_equal issue.author, User.try_to_login(login, password)
238 end
278 end
239 end
279 end
240
280
241 def test_add_issue_without_from_header
281 def test_add_issue_without_from_header
242 Role.anonymous.add_permission!(:add_issues)
282 Role.anonymous.add_permission!(:add_issues)
243 assert_equal false, submit_email('ticket_without_from_header.eml')
283 assert_equal false, submit_email('ticket_without_from_header.eml')
244 end
284 end
245
285
246 def test_add_issue_with_invalid_attributes
286 def test_add_issue_with_invalid_attributes
247 issue = submit_email('ticket_with_invalid_attributes.eml', :allow_override => 'tracker,category,priority')
287 issue = submit_email(
288 'ticket_with_invalid_attributes.eml',
289 :allow_override => 'tracker,category,priority'
290 )
248 assert issue.is_a?(Issue)
291 assert issue.is_a?(Issue)
249 assert !issue.new_record?
292 assert !issue.new_record?
250 issue.reload
293 issue.reload
251 assert_nil issue.assigned_to
294 assert_nil issue.assigned_to
252 assert_nil issue.start_date
295 assert_nil issue.start_date
253 assert_nil issue.due_date
296 assert_nil issue.due_date
254 assert_equal 0, issue.done_ratio
297 assert_equal 0, issue.done_ratio
255 assert_equal 'Normal', issue.priority.to_s
298 assert_equal 'Normal', issue.priority.to_s
256 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
299 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
257 end
300 end
258
301
259 def test_add_issue_with_localized_attributes
302 def test_add_issue_with_localized_attributes
260 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
303 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
261 issue = submit_email('ticket_with_localized_attributes.eml', :allow_override => 'tracker,category,priority')
304 issue = submit_email(
305 'ticket_with_localized_attributes.eml',
306 :allow_override => 'tracker,category,priority'
307 )
262 assert issue.is_a?(Issue)
308 assert issue.is_a?(Issue)
263 assert !issue.new_record?
309 assert !issue.new_record?
264 issue.reload
310 issue.reload
265 assert_equal 'New ticket on a given project', issue.subject
311 assert_equal 'New ticket on a given project', issue.subject
266 assert_equal User.find_by_login('jsmith'), issue.author
312 assert_equal User.find_by_login('jsmith'), issue.author
267 assert_equal Project.find(2), issue.project
313 assert_equal Project.find(2), issue.project
268 assert_equal 'Feature request', issue.tracker.to_s
314 assert_equal 'Feature request', issue.tracker.to_s
269 assert_equal 'Stock management', issue.category.to_s
315 assert_equal 'Stock management', issue.category.to_s
270 assert_equal 'Urgent', issue.priority.to_s
316 assert_equal 'Urgent', issue.priority.to_s
271 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
317 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
272 end
318 end
273
319
274 def test_add_issue_with_japanese_keywords
320 def test_add_issue_with_japanese_keywords
275 ja_dev = "\xe9\x96\x8b\xe7\x99\xba"
321 ja_dev = "\xe9\x96\x8b\xe7\x99\xba"
276 ja_dev.force_encoding('UTF-8') if ja_dev.respond_to?(:force_encoding)
322 ja_dev.force_encoding('UTF-8') if ja_dev.respond_to?(:force_encoding)
277 tracker = Tracker.create!(:name => ja_dev)
323 tracker = Tracker.create!(:name => ja_dev)
278 Project.find(1).trackers << tracker
324 Project.find(1).trackers << tracker
279 issue = submit_email('japanese_keywords_iso_2022_jp.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'tracker')
325 issue = submit_email(
326 'japanese_keywords_iso_2022_jp.eml',
327 :issue => {:project => 'ecookbook'},
328 :allow_override => 'tracker'
329 )
280 assert_kind_of Issue, issue
330 assert_kind_of Issue, issue
281 assert_equal tracker, issue.tracker
331 assert_equal tracker, issue.tracker
282 end
332 end
283
333
284 def test_add_issue_from_apple_mail
334 def test_add_issue_from_apple_mail
285 issue = submit_email('apple_mail_with_attachment.eml', :issue => {:project => 'ecookbook'})
335 issue = submit_email(
336 'apple_mail_with_attachment.eml',
337 :issue => {:project => 'ecookbook'}
338 )
286 assert_kind_of Issue, issue
339 assert_kind_of Issue, issue
287 assert_equal 1, issue.attachments.size
340 assert_equal 1, issue.attachments.size
288
341
289 attachment = issue.attachments.first
342 attachment = issue.attachments.first
290 assert_equal 'paella.jpg', attachment.filename
343 assert_equal 'paella.jpg', attachment.filename
291 assert_equal 10790, attachment.filesize
344 assert_equal 10790, attachment.filesize
292 assert File.exist?(attachment.diskfile)
345 assert File.exist?(attachment.diskfile)
293 assert_equal 10790, File.size(attachment.diskfile)
346 assert_equal 10790, File.size(attachment.diskfile)
294 assert_equal 'caaf384198bcbc9563ab5c058acd73cd', attachment.digest
347 assert_equal 'caaf384198bcbc9563ab5c058acd73cd', attachment.digest
295 end
348 end
296
349
297 def test_should_ignore_emails_from_emission_address
350 def test_should_ignore_emails_from_emission_address
298 Role.anonymous.add_permission!(:add_issues)
351 Role.anonymous.add_permission!(:add_issues)
299 assert_no_difference 'User.count' do
352 assert_no_difference 'User.count' do
300 assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
353 assert_equal false,
354 submit_email(
355 'ticket_from_emission_address.eml',
356 :issue => {:project => 'ecookbook'},
357 :unknown_user => 'create'
358 )
301 end
359 end
302 end
360 end
303
361
304 def test_add_issue_should_send_email_notification
362 def test_add_issue_should_send_email_notification
305 Setting.notified_events = ['issue_added']
363 Setting.notified_events = ['issue_added']
306 ActionMailer::Base.deliveries.clear
364 ActionMailer::Base.deliveries.clear
307 # This email contains: 'Project: onlinestore'
365 # This email contains: 'Project: onlinestore'
308 issue = submit_email('ticket_on_given_project.eml')
366 issue = submit_email('ticket_on_given_project.eml')
309 assert issue.is_a?(Issue)
367 assert issue.is_a?(Issue)
310 assert_equal 1, ActionMailer::Base.deliveries.size
368 assert_equal 1, ActionMailer::Base.deliveries.size
311 end
369 end
312
370
313 def test_update_issue
371 def test_update_issue
314 journal = submit_email('ticket_reply.eml')
372 journal = submit_email('ticket_reply.eml')
315 assert journal.is_a?(Journal)
373 assert journal.is_a?(Journal)
316 assert_equal User.find_by_login('jsmith'), journal.user
374 assert_equal User.find_by_login('jsmith'), journal.user
317 assert_equal Issue.find(2), journal.journalized
375 assert_equal Issue.find(2), journal.journalized
318 assert_match /This is reply/, journal.notes
376 assert_match /This is reply/, journal.notes
319 assert_equal 'Feature request', journal.issue.tracker.name
377 assert_equal 'Feature request', journal.issue.tracker.name
320 end
378 end
321
379
322 def test_update_issue_with_attribute_changes
380 def test_update_issue_with_attribute_changes
323 # This email contains: 'Status: Resolved'
381 # This email contains: 'Status: Resolved'
324 journal = submit_email('ticket_reply_with_status.eml')
382 journal = submit_email('ticket_reply_with_status.eml')
325 assert journal.is_a?(Journal)
383 assert journal.is_a?(Journal)
326 issue = Issue.find(journal.issue.id)
384 issue = Issue.find(journal.issue.id)
327 assert_equal User.find_by_login('jsmith'), journal.user
385 assert_equal User.find_by_login('jsmith'), journal.user
328 assert_equal Issue.find(2), journal.journalized
386 assert_equal Issue.find(2), journal.journalized
329 assert_match /This is reply/, journal.notes
387 assert_match /This is reply/, journal.notes
330 assert_equal 'Feature request', journal.issue.tracker.name
388 assert_equal 'Feature request', journal.issue.tracker.name
331 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
389 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
332 assert_equal '2010-01-01', issue.start_date.to_s
390 assert_equal '2010-01-01', issue.start_date.to_s
333 assert_equal '2010-12-31', issue.due_date.to_s
391 assert_equal '2010-12-31', issue.due_date.to_s
334 assert_equal User.find_by_login('jsmith'), issue.assigned_to
392 assert_equal User.find_by_login('jsmith'), issue.assigned_to
335 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
393 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
336 # keywords should be removed from the email body
394 # keywords should be removed from the email body
337 assert !journal.notes.match(/^Status:/i)
395 assert !journal.notes.match(/^Status:/i)
338 assert !journal.notes.match(/^Start Date:/i)
396 assert !journal.notes.match(/^Start Date:/i)
339 end
397 end
340
398
341 def test_update_issue_with_attachment
399 def test_update_issue_with_attachment
342 assert_difference 'Journal.count' do
400 assert_difference 'Journal.count' do
343 assert_difference 'JournalDetail.count' do
401 assert_difference 'JournalDetail.count' do
344 assert_difference 'Attachment.count' do
402 assert_difference 'Attachment.count' do
345 assert_no_difference 'Issue.count' do
403 assert_no_difference 'Issue.count' do
346 journal = submit_email('ticket_with_attachment.eml') do |raw|
404 journal = submit_email('ticket_with_attachment.eml') do |raw|
347 raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
405 raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
348 end
406 end
349 end
407 end
350 end
408 end
351 end
409 end
352 end
410 end
353 journal = Journal.first(:order => 'id DESC')
411 journal = Journal.first(:order => 'id DESC')
354 assert_equal Issue.find(2), journal.journalized
412 assert_equal Issue.find(2), journal.journalized
355 assert_equal 1, journal.details.size
413 assert_equal 1, journal.details.size
356
414
357 detail = journal.details.first
415 detail = journal.details.first
358 assert_equal 'attachment', detail.property
416 assert_equal 'attachment', detail.property
359 assert_equal 'Paella.jpg', detail.value
417 assert_equal 'Paella.jpg', detail.value
360 end
418 end
361
419
362 def test_update_issue_should_send_email_notification
420 def test_update_issue_should_send_email_notification
363 ActionMailer::Base.deliveries.clear
421 ActionMailer::Base.deliveries.clear
364 journal = submit_email('ticket_reply.eml')
422 journal = submit_email('ticket_reply.eml')
365 assert journal.is_a?(Journal)
423 assert journal.is_a?(Journal)
366 assert_equal 1, ActionMailer::Base.deliveries.size
424 assert_equal 1, ActionMailer::Base.deliveries.size
367 end
425 end
368
426
369 def test_update_issue_should_not_set_defaults
427 def test_update_issue_should_not_set_defaults
370 journal = submit_email('ticket_reply.eml', :issue => {:tracker => 'Support request', :priority => 'High'})
428 journal = submit_email(
429 'ticket_reply.eml',
430 :issue => {:tracker => 'Support request', :priority => 'High'}
431 )
371 assert journal.is_a?(Journal)
432 assert journal.is_a?(Journal)
372 assert_match /This is reply/, journal.notes
433 assert_match /This is reply/, journal.notes
373 assert_equal 'Feature request', journal.issue.tracker.name
434 assert_equal 'Feature request', journal.issue.tracker.name
374 assert_equal 'Normal', journal.issue.priority.name
435 assert_equal 'Normal', journal.issue.priority.name
375 end
436 end
376
437
377 def test_reply_to_a_message
438 def test_reply_to_a_message
378 m = submit_email('message_reply.eml')
439 m = submit_email('message_reply.eml')
379 assert m.is_a?(Message)
440 assert m.is_a?(Message)
380 assert !m.new_record?
441 assert !m.new_record?
381 m.reload
442 m.reload
382 assert_equal 'Reply via email', m.subject
443 assert_equal 'Reply via email', m.subject
383 # The email replies to message #2 which is part of the thread of message #1
444 # The email replies to message #2 which is part of the thread of message #1
384 assert_equal Message.find(1), m.parent
445 assert_equal Message.find(1), m.parent
385 end
446 end
386
447
387 def test_reply_to_a_message_by_subject
448 def test_reply_to_a_message_by_subject
388 m = submit_email('message_reply_by_subject.eml')
449 m = submit_email('message_reply_by_subject.eml')
389 assert m.is_a?(Message)
450 assert m.is_a?(Message)
390 assert !m.new_record?
451 assert !m.new_record?
391 m.reload
452 m.reload
392 assert_equal 'Reply to the first post', m.subject
453 assert_equal 'Reply to the first post', m.subject
393 assert_equal Message.find(1), m.parent
454 assert_equal Message.find(1), m.parent
394 end
455 end
395
456
396 def test_should_strip_tags_of_html_only_emails
457 def test_should_strip_tags_of_html_only_emails
397 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
458 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
398 assert issue.is_a?(Issue)
459 assert issue.is_a?(Issue)
399 assert !issue.new_record?
460 assert !issue.new_record?
400 issue.reload
461 issue.reload
401 assert_equal 'HTML email', issue.subject
462 assert_equal 'HTML email', issue.subject
402 assert_equal 'This is a html-only email.', issue.description
463 assert_equal 'This is a html-only email.', issue.description
403 end
464 end
404
465
405 context "truncate emails based on the Setting" do
466 context "truncate emails based on the Setting" do
406 context "with no setting" do
467 context "with no setting" do
407 setup do
468 setup do
408 Setting.mail_handler_body_delimiters = ''
469 Setting.mail_handler_body_delimiters = ''
409 end
470 end
410
471
411 should "add the entire email into the issue" do
472 should "add the entire email into the issue" do
412 issue = submit_email('ticket_on_given_project.eml')
473 issue = submit_email('ticket_on_given_project.eml')
413 assert_issue_created(issue)
474 assert_issue_created(issue)
414 assert issue.description.include?('---')
475 assert issue.description.include?('---')
415 assert issue.description.include?('This paragraph is after the delimiter')
476 assert issue.description.include?('This paragraph is after the delimiter')
416 end
477 end
417 end
478 end
418
479
419 context "with a single string" do
480 context "with a single string" do
420 setup do
481 setup do
421 Setting.mail_handler_body_delimiters = '---'
482 Setting.mail_handler_body_delimiters = '---'
422 end
483 end
423 should "truncate the email at the delimiter for the issue" do
484 should "truncate the email at the delimiter for the issue" do
424 issue = submit_email('ticket_on_given_project.eml')
485 issue = submit_email('ticket_on_given_project.eml')
425 assert_issue_created(issue)
486 assert_issue_created(issue)
426 assert issue.description.include?('This paragraph is before delimiters')
487 assert issue.description.include?('This paragraph is before delimiters')
427 assert issue.description.include?('--- This line starts with a delimiter')
488 assert issue.description.include?('--- This line starts with a delimiter')
428 assert !issue.description.match(/^---$/)
489 assert !issue.description.match(/^---$/)
429 assert !issue.description.include?('This paragraph is after the delimiter')
490 assert !issue.description.include?('This paragraph is after the delimiter')
430 end
491 end
431 end
492 end
432
493
433 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
494 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
434 setup do
495 setup do
435 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
496 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
436 end
497 end
437 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
498 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
438 journal = submit_email('issue_update_with_quoted_reply_above.eml')
499 journal = submit_email('issue_update_with_quoted_reply_above.eml')
439 assert journal.is_a?(Journal)
500 assert journal.is_a?(Journal)
440 assert journal.notes.include?('An update to the issue by the sender.')
501 assert journal.notes.include?('An update to the issue by the sender.')
441 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
502 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
442 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
503 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
443 end
504 end
444 end
505 end
445
506
446 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
507 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
447 setup do
508 setup do
448 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
509 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
449 end
510 end
450 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
511 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
451 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
512 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
452 assert journal.is_a?(Journal)
513 assert journal.is_a?(Journal)
453 assert journal.notes.include?('An update to the issue by the sender.')
514 assert journal.notes.include?('An update to the issue by the sender.')
454 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
515 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
455 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
516 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
456 end
517 end
457 end
518 end
458
519
459 context "with multiple strings" do
520 context "with multiple strings" do
460 setup do
521 setup do
461 Setting.mail_handler_body_delimiters = "---\nBREAK"
522 Setting.mail_handler_body_delimiters = "---\nBREAK"
462 end
523 end
463 should "truncate the email at the first delimiter found (BREAK)" do
524 should "truncate the email at the first delimiter found (BREAK)" do
464 issue = submit_email('ticket_on_given_project.eml')
525 issue = submit_email('ticket_on_given_project.eml')
465 assert_issue_created(issue)
526 assert_issue_created(issue)
466 assert issue.description.include?('This paragraph is before delimiters')
527 assert issue.description.include?('This paragraph is before delimiters')
467 assert !issue.description.include?('BREAK')
528 assert !issue.description.include?('BREAK')
468 assert !issue.description.include?('This paragraph is between delimiters')
529 assert !issue.description.include?('This paragraph is between delimiters')
469 assert !issue.description.match(/^---$/)
530 assert !issue.description.match(/^---$/)
470 assert !issue.description.include?('This paragraph is after the delimiter')
531 assert !issue.description.include?('This paragraph is after the delimiter')
471 end
532 end
472 end
533 end
473 end
534 end
474
535
475 def test_email_with_long_subject_line
536 def test_email_with_long_subject_line
476 issue = submit_email('ticket_with_long_subject.eml')
537 issue = submit_email('ticket_with_long_subject.eml')
477 assert issue.is_a?(Issue)
538 assert issue.is_a?(Issue)
478 assert_equal issue.subject, 'New ticket on a given project with a very long subject line which exceeds 255 chars and should not be ignored but chopped off. And if the subject line is still not long enough, we just add more text. And more text. Wow, this is really annoying. Especially, if you have nothing to say...'[0,255]
539 assert_equal issue.subject, 'New ticket on a given project with a very long subject line which exceeds 255 chars and should not be ignored but chopped off. And if the subject line is still not long enough, we just add more text. And more text. Wow, this is really annoying. Especially, if you have nothing to say...'[0,255]
479 end
540 end
480
541
481 def test_new_user_from_attributes_should_return_valid_user
542 def test_new_user_from_attributes_should_return_valid_user
482 to_test = {
543 to_test = {
483 # [address, name] => [login, firstname, lastname]
544 # [address, name] => [login, firstname, lastname]
484 ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
545 ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
485 ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
546 ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
486 ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
547 ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
487 ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
548 ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
488 ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
549 ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
489 ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh'],
550 ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh'],
490 ['alongemailaddressthatexceedsloginlength@example.net', 'John Smith'] => ['alongemailaddressthatexceedslo', 'John', 'Smith']
551 ['alongemailaddressthatexceedsloginlength@example.net', 'John Smith'] => ['alongemailaddressthatexceedslo', 'John', 'Smith']
491 }
552 }
492
553
493 to_test.each do |attrs, expected|
554 to_test.each do |attrs, expected|
494 user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
555 user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
495
556
496 assert user.valid?
557 assert user.valid?
497 assert_equal attrs.first, user.mail
558 assert_equal attrs.first, user.mail
498 assert_equal expected[0], user.login
559 assert_equal expected[0], user.login
499 assert_equal expected[1], user.firstname
560 assert_equal expected[1], user.firstname
500 assert_equal expected[2], user.lastname
561 assert_equal expected[2], user.lastname
501 end
562 end
502 end
563 end
503
564
504 def test_new_user_from_attributes_should_respect_minimum_password_length
565 def test_new_user_from_attributes_should_respect_minimum_password_length
505 with_settings :password_min_length => 15 do
566 with_settings :password_min_length => 15 do
506 user = MailHandler.new_user_from_attributes('jsmith@example.net')
567 user = MailHandler.new_user_from_attributes('jsmith@example.net')
507 assert user.valid?
568 assert user.valid?
508 assert user.password.length >= 15
569 assert user.password.length >= 15
509 end
570 end
510 end
571 end
511
572
512 def test_new_user_from_attributes_should_use_default_login_if_invalid
573 def test_new_user_from_attributes_should_use_default_login_if_invalid
513 MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-1@example.net').save!
574 MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-1@example.net').save!
514
575
515 # another long address that would result in duplicate login
576 # another long address that would result in duplicate login
516 user = MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-2@example.net')
577 user = MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-2@example.net')
517 assert user.valid?
578 assert user.valid?
518 assert user.login =~ /^user[a-f0-9]+$/
579 assert user.login =~ /^user[a-f0-9]+$/
519 end
580 end
520
581
521 private
582 private
522
583
523 def submit_email(filename, options={})
584 def submit_email(filename, options={})
524 raw = IO.read(File.join(FIXTURES_PATH, filename))
585 raw = IO.read(File.join(FIXTURES_PATH, filename))
525 yield raw if block_given?
586 yield raw if block_given?
526 MailHandler.receive(raw, options)
587 MailHandler.receive(raw, options)
527 end
588 end
528
589
529 def assert_issue_created(issue)
590 def assert_issue_created(issue)
530 assert issue.is_a?(Issue)
591 assert issue.is_a?(Issue)
531 assert !issue.new_record?
592 assert !issue.new_record?
532 issue.reload
593 issue.reload
533 end
594 end
534 end
595 end
General Comments 0
You need to be logged in to leave comments. Login now