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