##// END OF EJS Templates
code clean up MailHandlerTest unit test....
Toshi MARUYAMA -
r5439:d5b97d49c610
parent child
Show More
@@ -1,466 +1,457
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2009 Jean-Philippe Lang
4 # Copyright (C) 2006-2009 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,
23 fixtures :users, :projects,
24 :enabled_modules,
24 :enabled_modules,
25 :roles,
25 :roles,
26 :members,
26 :members,
27 :member_roles,
27 :member_roles,
28 :users,
28 :users,
29 :issues,
29 :issues,
30 :issue_statuses,
30 :issue_statuses,
31 :workflows,
31 :workflows,
32 :trackers,
32 :trackers,
33 :projects_trackers,
33 :projects_trackers,
34 :versions,
34 :versions,
35 :enumerations,
35 :enumerations,
36 :issue_categories,
36 :issue_categories,
37 :custom_fields,
37 :custom_fields,
38 :custom_fields_trackers,
38 :custom_fields_trackers,
39 :custom_fields_projects,
39 :custom_fields_projects,
40 :boards,
40 :boards,
41 :messages
41 :messages
42
42
43 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
43 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
44
44
45 def setup
45 def setup
46 ActionMailer::Base.deliveries.clear
46 ActionMailer::Base.deliveries.clear
47 Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
47 Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
48 end
48 end
49
49
50 def test_add_issue
50 def test_add_issue
51 ActionMailer::Base.deliveries.clear
51 ActionMailer::Base.deliveries.clear
52 # This email contains: 'Project: onlinestore'
52 # This email contains: 'Project: onlinestore'
53 issue = submit_email('ticket_on_given_project.eml')
53 issue = submit_email('ticket_on_given_project.eml')
54 assert issue.is_a?(Issue)
54 assert issue.is_a?(Issue)
55 assert !issue.new_record?
55 assert !issue.new_record?
56 issue.reload
56 issue.reload
57 assert_equal Project.find(2), issue.project
57 assert_equal Project.find(2), issue.project
58 assert_equal issue.project.trackers.first, issue.tracker
58 assert_equal issue.project.trackers.first, issue.tracker
59 assert_equal 'New ticket on a given project', issue.subject
59 assert_equal 'New ticket on a given project', issue.subject
60 assert_equal User.find_by_login('jsmith'), issue.author
60 assert_equal User.find_by_login('jsmith'), issue.author
61 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
61 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
62 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
62 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
63 assert_equal '2010-01-01', issue.start_date.to_s
63 assert_equal '2010-01-01', issue.start_date.to_s
64 assert_equal '2010-12-31', issue.due_date.to_s
64 assert_equal '2010-12-31', issue.due_date.to_s
65 assert_equal User.find_by_login('jsmith'), issue.assigned_to
65 assert_equal User.find_by_login('jsmith'), issue.assigned_to
66 assert_equal Version.find_by_name('alpha'), issue.fixed_version
66 assert_equal Version.find_by_name('alpha'), issue.fixed_version
67 assert_equal 2.5, issue.estimated_hours
67 assert_equal 2.5, issue.estimated_hours
68 assert_equal 30, issue.done_ratio
68 assert_equal 30, issue.done_ratio
69 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
69 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
70 # keywords should be removed from the email body
70 # keywords should be removed from the email body
71 assert !issue.description.match(/^Project:/i)
71 assert !issue.description.match(/^Project:/i)
72 assert !issue.description.match(/^Status:/i)
72 assert !issue.description.match(/^Status:/i)
73 assert !issue.description.match(/^Start Date:/i)
73 assert !issue.description.match(/^Start Date:/i)
74 # Email notification should be sent
74 # Email notification should be sent
75 mail = ActionMailer::Base.deliveries.last
75 mail = ActionMailer::Base.deliveries.last
76 assert_not_nil mail
76 assert_not_nil mail
77 assert mail.subject.include?('New ticket on a given project')
77 assert mail.subject.include?('New ticket on a given project')
78 end
78 end
79
79
80 def test_add_issue_with_default_tracker
80 def test_add_issue_with_default_tracker
81 # This email contains: 'Project: onlinestore'
81 # This email contains: 'Project: onlinestore'
82 issue = submit_email('ticket_on_given_project.eml', :issue => {:tracker => 'Support request'})
82 issue = submit_email('ticket_on_given_project.eml', :issue => {:tracker => 'Support request'})
83 assert issue.is_a?(Issue)
83 assert issue.is_a?(Issue)
84 assert !issue.new_record?
84 assert !issue.new_record?
85 issue.reload
85 issue.reload
86 assert_equal 'Support request', issue.tracker.name
86 assert_equal 'Support request', issue.tracker.name
87 end
87 end
88
88
89 def test_add_issue_with_status
89 def test_add_issue_with_status
90 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
90 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
91 issue = submit_email('ticket_on_given_project.eml')
91 issue = submit_email('ticket_on_given_project.eml')
92 assert issue.is_a?(Issue)
92 assert issue.is_a?(Issue)
93 assert !issue.new_record?
93 assert !issue.new_record?
94 issue.reload
94 issue.reload
95 assert_equal Project.find(2), issue.project
95 assert_equal Project.find(2), issue.project
96 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
96 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
97 end
97 end
98
98
99 def test_add_issue_with_attributes_override
99 def test_add_issue_with_attributes_override
100 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
100 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
101 assert issue.is_a?(Issue)
101 assert issue.is_a?(Issue)
102 assert !issue.new_record?
102 assert !issue.new_record?
103 issue.reload
103 issue.reload
104 assert_equal 'New ticket on a given project', issue.subject
104 assert_equal 'New ticket on a given project', issue.subject
105 assert_equal User.find_by_login('jsmith'), issue.author
105 assert_equal User.find_by_login('jsmith'), issue.author
106 assert_equal Project.find(2), issue.project
106 assert_equal Project.find(2), issue.project
107 assert_equal 'Feature request', issue.tracker.to_s
107 assert_equal 'Feature request', issue.tracker.to_s
108 assert_equal 'Stock management', issue.category.to_s
108 assert_equal 'Stock management', issue.category.to_s
109 assert_equal 'Urgent', issue.priority.to_s
109 assert_equal 'Urgent', issue.priority.to_s
110 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
110 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
111 end
111 end
112
112
113 def test_add_issue_with_partial_attributes_override
113 def test_add_issue_with_partial_attributes_override
114 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
114 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
115 assert issue.is_a?(Issue)
115 assert issue.is_a?(Issue)
116 assert !issue.new_record?
116 assert !issue.new_record?
117 issue.reload
117 issue.reload
118 assert_equal 'New ticket on a given project', issue.subject
118 assert_equal 'New ticket on a given project', issue.subject
119 assert_equal User.find_by_login('jsmith'), issue.author
119 assert_equal User.find_by_login('jsmith'), issue.author
120 assert_equal Project.find(2), issue.project
120 assert_equal Project.find(2), issue.project
121 assert_equal 'Feature request', issue.tracker.to_s
121 assert_equal 'Feature request', issue.tracker.to_s
122 assert_nil issue.category
122 assert_nil issue.category
123 assert_equal 'High', issue.priority.to_s
123 assert_equal 'High', issue.priority.to_s
124 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
124 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
125 end
125 end
126
126
127 def test_add_issue_with_spaces_between_attribute_and_separator
127 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')
128 issue = submit_email('ticket_with_spaces_between_attribute_and_separator.eml', :allow_override => 'tracker,category,priority')
129 assert issue.is_a?(Issue)
129 assert issue.is_a?(Issue)
130 assert !issue.new_record?
130 assert !issue.new_record?
131 issue.reload
131 issue.reload
132 assert_equal 'New ticket on a given project', issue.subject
132 assert_equal 'New ticket on a given project', issue.subject
133 assert_equal User.find_by_login('jsmith'), issue.author
133 assert_equal User.find_by_login('jsmith'), issue.author
134 assert_equal Project.find(2), issue.project
134 assert_equal Project.find(2), issue.project
135 assert_equal 'Feature request', issue.tracker.to_s
135 assert_equal 'Feature request', issue.tracker.to_s
136 assert_equal 'Stock management', issue.category.to_s
136 assert_equal 'Stock management', issue.category.to_s
137 assert_equal 'Urgent', issue.priority.to_s
137 assert_equal 'Urgent', issue.priority.to_s
138 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
138 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
139 end
139 end
140
140
141
142 def test_add_issue_with_attachment_to_specific_project
141 def test_add_issue_with_attachment_to_specific_project
143 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
142 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
144 assert issue.is_a?(Issue)
143 assert issue.is_a?(Issue)
145 assert !issue.new_record?
144 assert !issue.new_record?
146 issue.reload
145 issue.reload
147 assert_equal 'Ticket created by email with attachment', issue.subject
146 assert_equal 'Ticket created by email with attachment', issue.subject
148 assert_equal User.find_by_login('jsmith'), issue.author
147 assert_equal User.find_by_login('jsmith'), issue.author
149 assert_equal Project.find(2), issue.project
148 assert_equal Project.find(2), issue.project
150 assert_equal 'This is a new ticket with attachments', issue.description
149 assert_equal 'This is a new ticket with attachments', issue.description
151 # Attachment properties
150 # Attachment properties
152 assert_equal 1, issue.attachments.size
151 assert_equal 1, issue.attachments.size
153 assert_equal 'Paella.jpg', issue.attachments.first.filename
152 assert_equal 'Paella.jpg', issue.attachments.first.filename
154 assert_equal 'image/jpeg', issue.attachments.first.content_type
153 assert_equal 'image/jpeg', issue.attachments.first.content_type
155 assert_equal 10790, issue.attachments.first.filesize
154 assert_equal 10790, issue.attachments.first.filesize
156 end
155 end
157
156
158 def test_add_issue_with_custom_fields
157 def test_add_issue_with_custom_fields
159 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
158 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
160 assert issue.is_a?(Issue)
159 assert issue.is_a?(Issue)
161 assert !issue.new_record?
160 assert !issue.new_record?
162 issue.reload
161 issue.reload
163 assert_equal 'New ticket with custom field values', issue.subject
162 assert_equal 'New ticket with custom field values', issue.subject
164 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
163 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
165 assert !issue.description.match(/^searchable field:/i)
164 assert !issue.description.match(/^searchable field:/i)
166 end
165 end
167
166
168 def test_add_issue_with_cc
167 def test_add_issue_with_cc
169 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
168 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
170 assert issue.is_a?(Issue)
169 assert issue.is_a?(Issue)
171 assert !issue.new_record?
170 assert !issue.new_record?
172 issue.reload
171 issue.reload
173 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
172 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
174 assert_equal 1, issue.watcher_user_ids.size
173 assert_equal 1, issue.watcher_user_ids.size
175 end
174 end
176
175
177 def test_add_issue_by_unknown_user
176 def test_add_issue_by_unknown_user
178 assert_no_difference 'User.count' do
177 assert_no_difference 'User.count' do
179 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'})
178 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'})
180 end
179 end
181 end
180 end
182
181
183 def test_add_issue_by_anonymous_user
182 def test_add_issue_by_anonymous_user
184 Role.anonymous.add_permission!(:add_issues)
183 Role.anonymous.add_permission!(:add_issues)
185 assert_no_difference 'User.count' do
184 assert_no_difference 'User.count' do
186 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
185 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
187 assert issue.is_a?(Issue)
186 assert issue.is_a?(Issue)
188 assert issue.author.anonymous?
187 assert issue.author.anonymous?
189 end
188 end
190 end
189 end
191
190
192 def test_add_issue_by_anonymous_user_with_no_from_address
191 def test_add_issue_by_anonymous_user_with_no_from_address
193 Role.anonymous.add_permission!(:add_issues)
192 Role.anonymous.add_permission!(:add_issues)
194 assert_no_difference 'User.count' do
193 assert_no_difference 'User.count' do
195 issue = submit_email('ticket_by_empty_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
194 issue = submit_email('ticket_by_empty_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
196 assert issue.is_a?(Issue)
195 assert issue.is_a?(Issue)
197 assert issue.author.anonymous?
196 assert issue.author.anonymous?
198 end
197 end
199 end
198 end
200
199
201 def test_add_issue_by_anonymous_user_on_private_project
200 def test_add_issue_by_anonymous_user_on_private_project
202 Role.anonymous.add_permission!(:add_issues)
201 Role.anonymous.add_permission!(:add_issues)
203 assert_no_difference 'User.count' do
202 assert_no_difference 'User.count' do
204 assert_no_difference 'Issue.count' do
203 assert_no_difference 'Issue.count' do
205 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :unknown_user => 'accept')
204 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :unknown_user => 'accept')
206 end
205 end
207 end
206 end
208 end
207 end
209
208
210 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
209 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
211 assert_no_difference 'User.count' do
210 assert_no_difference 'User.count' do
212 assert_difference 'Issue.count' do
211 assert_difference 'Issue.count' do
213 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :no_permission_check => '1', :unknown_user => 'accept')
212 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :no_permission_check => '1', :unknown_user => 'accept')
214 assert issue.is_a?(Issue)
213 assert issue.is_a?(Issue)
215 assert issue.author.anonymous?
214 assert issue.author.anonymous?
216 assert !issue.project.is_public?
215 assert !issue.project.is_public?
217 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
216 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
218 end
217 end
219 end
218 end
220 end
219 end
221
220
222 def test_add_issue_by_created_user
221 def test_add_issue_by_created_user
223 Setting.default_language = 'en'
222 Setting.default_language = 'en'
224 assert_difference 'User.count' do
223 assert_difference 'User.count' do
225 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
224 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
226 assert issue.is_a?(Issue)
225 assert issue.is_a?(Issue)
227 assert issue.author.active?
226 assert issue.author.active?
228 assert_equal 'john.doe@somenet.foo', issue.author.mail
227 assert_equal 'john.doe@somenet.foo', issue.author.mail
229 assert_equal 'John', issue.author.firstname
228 assert_equal 'John', issue.author.firstname
230 assert_equal 'Doe', issue.author.lastname
229 assert_equal 'Doe', issue.author.lastname
231
230
232 # account information
231 # account information
233 email = ActionMailer::Base.deliveries.first
232 email = ActionMailer::Base.deliveries.first
234 assert_not_nil email
233 assert_not_nil email
235 assert email.subject.include?('account activation')
234 assert email.subject.include?('account activation')
236 login = email.body.match(/\* Login: (.*)$/)[1]
235 login = email.body.match(/\* Login: (.*)$/)[1]
237 password = email.body.match(/\* Password: (.*)$/)[1]
236 password = email.body.match(/\* Password: (.*)$/)[1]
238 assert_equal issue.author, User.try_to_login(login, password)
237 assert_equal issue.author, User.try_to_login(login, password)
239 end
238 end
240 end
239 end
241
240
242 def test_add_issue_without_from_header
241 def test_add_issue_without_from_header
243 Role.anonymous.add_permission!(:add_issues)
242 Role.anonymous.add_permission!(:add_issues)
244 assert_equal false, submit_email('ticket_without_from_header.eml')
243 assert_equal false, submit_email('ticket_without_from_header.eml')
245 end
244 end
246
245
247 def test_add_issue_with_invalid_attributes
246 def test_add_issue_with_invalid_attributes
248 issue = submit_email('ticket_with_invalid_attributes.eml', :allow_override => 'tracker,category,priority')
247 issue = submit_email('ticket_with_invalid_attributes.eml', :allow_override => 'tracker,category,priority')
249 assert issue.is_a?(Issue)
248 assert issue.is_a?(Issue)
250 assert !issue.new_record?
249 assert !issue.new_record?
251 issue.reload
250 issue.reload
252 assert_nil issue.assigned_to
251 assert_nil issue.assigned_to
253 assert_nil issue.start_date
252 assert_nil issue.start_date
254 assert_nil issue.due_date
253 assert_nil issue.due_date
255 assert_equal 0, issue.done_ratio
254 assert_equal 0, issue.done_ratio
256 assert_equal 'Normal', issue.priority.to_s
255 assert_equal 'Normal', issue.priority.to_s
257 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
256 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
258 end
257 end
259
258
260 def test_add_issue_with_localized_attributes
259 def test_add_issue_with_localized_attributes
261 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
260 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
262 issue = submit_email('ticket_with_localized_attributes.eml', :allow_override => 'tracker,category,priority')
261 issue = submit_email('ticket_with_localized_attributes.eml', :allow_override => 'tracker,category,priority')
263 assert issue.is_a?(Issue)
262 assert issue.is_a?(Issue)
264 assert !issue.new_record?
263 assert !issue.new_record?
265 issue.reload
264 issue.reload
266 assert_equal 'New ticket on a given project', issue.subject
265 assert_equal 'New ticket on a given project', issue.subject
267 assert_equal User.find_by_login('jsmith'), issue.author
266 assert_equal User.find_by_login('jsmith'), issue.author
268 assert_equal Project.find(2), issue.project
267 assert_equal Project.find(2), issue.project
269 assert_equal 'Feature request', issue.tracker.to_s
268 assert_equal 'Feature request', issue.tracker.to_s
270 assert_equal 'Stock management', issue.category.to_s
269 assert_equal 'Stock management', issue.category.to_s
271 assert_equal 'Urgent', issue.priority.to_s
270 assert_equal 'Urgent', issue.priority.to_s
272 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
271 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
273 end
272 end
274
273
275 def test_add_issue_with_japanese_keywords
274 def test_add_issue_with_japanese_keywords
276 tracker = Tracker.create!(:name => 'ι–‹η™Ί')
275 tracker = Tracker.create!(:name => 'ι–‹η™Ί')
277 Project.find(1).trackers << tracker
276 Project.find(1).trackers << tracker
278 issue = submit_email('japanese_keywords_iso_2022_jp.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'tracker')
277 issue = submit_email('japanese_keywords_iso_2022_jp.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'tracker')
279 assert_kind_of Issue, issue
278 assert_kind_of Issue, issue
280 assert_equal tracker, issue.tracker
279 assert_equal tracker, issue.tracker
281 end
280 end
282
281
283 def test_should_ignore_emails_from_emission_address
282 def test_should_ignore_emails_from_emission_address
284 Role.anonymous.add_permission!(:add_issues)
283 Role.anonymous.add_permission!(:add_issues)
285 assert_no_difference 'User.count' do
284 assert_no_difference 'User.count' do
286 assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
285 assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
287 end
286 end
288 end
287 end
289
288
290 def test_add_issue_should_send_email_notification
289 def test_add_issue_should_send_email_notification
291 Setting.notified_events = ['issue_added']
290 Setting.notified_events = ['issue_added']
292 ActionMailer::Base.deliveries.clear
291 ActionMailer::Base.deliveries.clear
293 # This email contains: 'Project: onlinestore'
292 # This email contains: 'Project: onlinestore'
294 issue = submit_email('ticket_on_given_project.eml')
293 issue = submit_email('ticket_on_given_project.eml')
295 assert issue.is_a?(Issue)
294 assert issue.is_a?(Issue)
296 assert_equal 1, ActionMailer::Base.deliveries.size
295 assert_equal 1, ActionMailer::Base.deliveries.size
297 end
296 end
298
297
299 def test_add_issue_note
298 def test_add_issue_note
300 journal = submit_email('ticket_reply.eml')
299 journal = submit_email('ticket_reply.eml')
301 assert journal.is_a?(Journal)
300 assert journal.is_a?(Journal)
302 assert_equal User.find_by_login('jsmith'), journal.user
301 assert_equal User.find_by_login('jsmith'), journal.user
303 assert_equal Issue.find(2), journal.journalized
302 assert_equal Issue.find(2), journal.journalized
304 assert_match /This is reply/, journal.notes
303 assert_match /This is reply/, journal.notes
305 assert_equal 'Feature request', journal.issue.tracker.name
304 assert_equal 'Feature request', journal.issue.tracker.name
306 end
305 end
307
306
308 def test_add_issue_note_with_attribute_changes
307 def test_add_issue_note_with_attribute_changes
309 # This email contains: 'Status: Resolved'
308 # This email contains: 'Status: Resolved'
310 journal = submit_email('ticket_reply_with_status.eml')
309 journal = submit_email('ticket_reply_with_status.eml')
311 assert journal.is_a?(Journal)
310 assert journal.is_a?(Journal)
312 issue = Issue.find(journal.issue.id)
311 issue = Issue.find(journal.issue.id)
313 assert_equal User.find_by_login('jsmith'), journal.user
312 assert_equal User.find_by_login('jsmith'), journal.user
314 assert_equal Issue.find(2), journal.journalized
313 assert_equal Issue.find(2), journal.journalized
315 assert_match /This is reply/, journal.notes
314 assert_match /This is reply/, journal.notes
316 assert_equal 'Feature request', journal.issue.tracker.name
315 assert_equal 'Feature request', journal.issue.tracker.name
317 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
316 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
318 assert_equal '2010-01-01', issue.start_date.to_s
317 assert_equal '2010-01-01', issue.start_date.to_s
319 assert_equal '2010-12-31', issue.due_date.to_s
318 assert_equal '2010-12-31', issue.due_date.to_s
320 assert_equal User.find_by_login('jsmith'), issue.assigned_to
319 assert_equal User.find_by_login('jsmith'), issue.assigned_to
321 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
320 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
322 # keywords should be removed from the email body
321 # keywords should be removed from the email body
323 assert !journal.notes.match(/^Status:/i)
322 assert !journal.notes.match(/^Status:/i)
324 assert !journal.notes.match(/^Start Date:/i)
323 assert !journal.notes.match(/^Start Date:/i)
325 end
324 end
326
325
327 def test_add_issue_note_should_send_email_notification
326 def test_add_issue_note_should_send_email_notification
328 ActionMailer::Base.deliveries.clear
327 ActionMailer::Base.deliveries.clear
329 journal = submit_email('ticket_reply.eml')
328 journal = submit_email('ticket_reply.eml')
330 assert journal.is_a?(Journal)
329 assert journal.is_a?(Journal)
331 assert_equal 1, ActionMailer::Base.deliveries.size
330 assert_equal 1, ActionMailer::Base.deliveries.size
332 end
331 end
333
332
334 def test_add_issue_note_should_not_set_defaults
333 def test_add_issue_note_should_not_set_defaults
335 journal = submit_email('ticket_reply.eml', :issue => {:tracker => 'Support request', :priority => 'High'})
334 journal = submit_email('ticket_reply.eml', :issue => {:tracker => 'Support request', :priority => 'High'})
336 assert journal.is_a?(Journal)
335 assert journal.is_a?(Journal)
337 assert_match /This is reply/, journal.notes
336 assert_match /This is reply/, journal.notes
338 assert_equal 'Feature request', journal.issue.tracker.name
337 assert_equal 'Feature request', journal.issue.tracker.name
339 assert_equal 'Normal', journal.issue.priority.name
338 assert_equal 'Normal', journal.issue.priority.name
340 end
339 end
341
340
342 def test_reply_to_a_message
341 def test_reply_to_a_message
343 m = submit_email('message_reply.eml')
342 m = submit_email('message_reply.eml')
344 assert m.is_a?(Message)
343 assert m.is_a?(Message)
345 assert !m.new_record?
344 assert !m.new_record?
346 m.reload
345 m.reload
347 assert_equal 'Reply via email', m.subject
346 assert_equal 'Reply via email', m.subject
348 # The email replies to message #2 which is part of the thread of message #1
347 # The email replies to message #2 which is part of the thread of message #1
349 assert_equal Message.find(1), m.parent
348 assert_equal Message.find(1), m.parent
350 end
349 end
351
350
352 def test_reply_to_a_message_by_subject
351 def test_reply_to_a_message_by_subject
353 m = submit_email('message_reply_by_subject.eml')
352 m = submit_email('message_reply_by_subject.eml')
354 assert m.is_a?(Message)
353 assert m.is_a?(Message)
355 assert !m.new_record?
354 assert !m.new_record?
356 m.reload
355 m.reload
357 assert_equal 'Reply to the first post', m.subject
356 assert_equal 'Reply to the first post', m.subject
358 assert_equal Message.find(1), m.parent
357 assert_equal Message.find(1), m.parent
359 end
358 end
360
359
361 def test_should_strip_tags_of_html_only_emails
360 def test_should_strip_tags_of_html_only_emails
362 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
361 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
363 assert issue.is_a?(Issue)
362 assert issue.is_a?(Issue)
364 assert !issue.new_record?
363 assert !issue.new_record?
365 issue.reload
364 issue.reload
366 assert_equal 'HTML email', issue.subject
365 assert_equal 'HTML email', issue.subject
367 assert_equal 'This is a html-only email.', issue.description
366 assert_equal 'This is a html-only email.', issue.description
368 end
367 end
369
368
370 context "truncate emails based on the Setting" do
369 context "truncate emails based on the Setting" do
371 context "with no setting" do
370 context "with no setting" do
372 setup do
371 setup do
373 Setting.mail_handler_body_delimiters = ''
372 Setting.mail_handler_body_delimiters = ''
374 end
373 end
375
374
376 should "add the entire email into the issue" do
375 should "add the entire email into the issue" do
377 issue = submit_email('ticket_on_given_project.eml')
376 issue = submit_email('ticket_on_given_project.eml')
378 assert_issue_created(issue)
377 assert_issue_created(issue)
379 assert issue.description.include?('---')
378 assert issue.description.include?('---')
380 assert issue.description.include?('This paragraph is after the delimiter')
379 assert issue.description.include?('This paragraph is after the delimiter')
381 end
380 end
382 end
381 end
383
382
384 context "with a single string" do
383 context "with a single string" do
385 setup do
384 setup do
386 Setting.mail_handler_body_delimiters = '---'
385 Setting.mail_handler_body_delimiters = '---'
387 end
386 end
388
389 should "truncate the email at the delimiter for the issue" do
387 should "truncate the email at the delimiter for the issue" do
390 issue = submit_email('ticket_on_given_project.eml')
388 issue = submit_email('ticket_on_given_project.eml')
391 assert_issue_created(issue)
389 assert_issue_created(issue)
392 assert issue.description.include?('This paragraph is before delimiters')
390 assert issue.description.include?('This paragraph is before delimiters')
393 assert issue.description.include?('--- This line starts with a delimiter')
391 assert issue.description.include?('--- This line starts with a delimiter')
394 assert !issue.description.match(/^---$/)
392 assert !issue.description.match(/^---$/)
395 assert !issue.description.include?('This paragraph is after the delimiter')
393 assert !issue.description.include?('This paragraph is after the delimiter')
396 end
394 end
397 end
395 end
398
396
399 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
397 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
400 setup do
398 setup do
401 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
399 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
402 end
400 end
403
404 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
401 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
405 journal = submit_email('issue_update_with_quoted_reply_above.eml')
402 journal = submit_email('issue_update_with_quoted_reply_above.eml')
406 assert journal.is_a?(Journal)
403 assert journal.is_a?(Journal)
407 assert journal.notes.include?('An update to the issue by the sender.')
404 assert journal.notes.include?('An update to the issue by the sender.')
408 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
405 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
409 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
406 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
410
411 end
407 end
412
413 end
408 end
414
409
415 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
410 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
416 setup do
411 setup do
417 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
412 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
418 end
413 end
419
420 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
414 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
421 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
415 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
422 assert journal.is_a?(Journal)
416 assert journal.is_a?(Journal)
423 assert journal.notes.include?('An update to the issue by the sender.')
417 assert journal.notes.include?('An update to the issue by the sender.')
424 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
418 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
425 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
419 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
426
427 end
420 end
428
429 end
421 end
430
422
431 context "with multiple strings" do
423 context "with multiple strings" do
432 setup do
424 setup do
433 Setting.mail_handler_body_delimiters = "---\nBREAK"
425 Setting.mail_handler_body_delimiters = "---\nBREAK"
434 end
426 end
435
436 should "truncate the email at the first delimiter found (BREAK)" do
427 should "truncate the email at the first delimiter found (BREAK)" do
437 issue = submit_email('ticket_on_given_project.eml')
428 issue = submit_email('ticket_on_given_project.eml')
438 assert_issue_created(issue)
429 assert_issue_created(issue)
439 assert issue.description.include?('This paragraph is before delimiters')
430 assert issue.description.include?('This paragraph is before delimiters')
440 assert !issue.description.include?('BREAK')
431 assert !issue.description.include?('BREAK')
441 assert !issue.description.include?('This paragraph is between delimiters')
432 assert !issue.description.include?('This paragraph is between delimiters')
442 assert !issue.description.match(/^---$/)
433 assert !issue.description.match(/^---$/)
443 assert !issue.description.include?('This paragraph is after the delimiter')
434 assert !issue.description.include?('This paragraph is after the delimiter')
444 end
435 end
445 end
436 end
446 end
437 end
447
438
448 def test_email_with_long_subject_line
439 def test_email_with_long_subject_line
449 issue = submit_email('ticket_with_long_subject.eml')
440 issue = submit_email('ticket_with_long_subject.eml')
450 assert issue.is_a?(Issue)
441 assert issue.is_a?(Issue)
451 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]
442 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]
452 end
443 end
453
444
454 private
445 private
455
446
456 def submit_email(filename, options={})
447 def submit_email(filename, options={})
457 raw = IO.read(File.join(FIXTURES_PATH, filename))
448 raw = IO.read(File.join(FIXTURES_PATH, filename))
458 MailHandler.receive(raw, options)
449 MailHandler.receive(raw, options)
459 end
450 end
460
451
461 def assert_issue_created(issue)
452 def assert_issue_created(issue)
462 assert issue.is_a?(Issue)
453 assert issue.is_a?(Issue)
463 assert !issue.new_record?
454 assert !issue.new_record?
464 issue.reload
455 issue.reload
465 end
456 end
466 end
457 end
General Comments 0
You need to be logged in to leave comments. Login now