@@ -1,280 +1,280 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | class MailHandler < ActionMailer::Base |
|
19 | 19 | include ActionView::Helpers::SanitizeHelper |
|
20 | 20 | |
|
21 | 21 | class UnauthorizedAction < StandardError; end |
|
22 | 22 | class MissingInformation < StandardError; end |
|
23 | 23 | |
|
24 | 24 | attr_reader :email, :user |
|
25 | 25 | |
|
26 | 26 | def self.receive(email, options={}) |
|
27 | 27 | @@handler_options = options.dup |
|
28 | 28 | |
|
29 | 29 | @@handler_options[:issue] ||= {} |
|
30 | 30 | |
|
31 | 31 | @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String) |
|
32 | 32 | @@handler_options[:allow_override] ||= [] |
|
33 | 33 | # Project needs to be overridable if not specified |
|
34 | 34 | @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) |
|
35 | 35 | # Status overridable by default |
|
36 | 36 | @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) |
|
37 | 37 | super email |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | # Processes incoming emails |
|
41 | 41 | # Returns the created object (eg. an issue, a message) or false |
|
42 | 42 | def receive(email) |
|
43 | 43 | @email = email |
|
44 | 44 | @user = User.find_by_mail(email.from.to_a.first.to_s.strip) |
|
45 | 45 | if @user && !@user.active? |
|
46 | 46 | logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info |
|
47 | 47 | return false |
|
48 | 48 | end |
|
49 | 49 | if @user.nil? |
|
50 | 50 | # Email was submitted by an unknown user |
|
51 | 51 | case @@handler_options[:unknown_user] |
|
52 | 52 | when 'accept' |
|
53 | 53 | @user = User.anonymous |
|
54 | 54 | when 'create' |
|
55 | 55 | @user = MailHandler.create_user_from_email(email) |
|
56 | 56 | if @user |
|
57 | 57 | logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info |
|
58 | 58 | Mailer.deliver_account_information(@user, @user.password) |
|
59 | 59 | else |
|
60 | 60 | logger.error "MailHandler: could not create account for [#{email.from.first}]" if logger && logger.error |
|
61 | 61 | return false |
|
62 | 62 | end |
|
63 | 63 | else |
|
64 | 64 | # Default behaviour, emails from unknown users are ignored |
|
65 | 65 | logger.info "MailHandler: ignoring email from unknown user [#{email.from.first}]" if logger && logger.info |
|
66 | 66 | return false |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | User.current = @user |
|
70 | 70 | dispatch |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | private |
|
74 | 74 | |
|
75 | 75 | MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} |
|
76 | 76 | ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]+#(\d+)\]} |
|
77 | 77 | MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]+msg(\d+)\]} |
|
78 | 78 | |
|
79 | 79 | def dispatch |
|
80 | 80 | headers = [email.in_reply_to, email.references].flatten.compact |
|
81 | 81 | if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} |
|
82 | 82 | klass, object_id = $1, $2.to_i |
|
83 | 83 | method_name = "receive_#{klass}_reply" |
|
84 | 84 | if self.class.private_instance_methods.include?(method_name) |
|
85 | 85 | send method_name, object_id |
|
86 | 86 | else |
|
87 | 87 | # ignoring it |
|
88 | 88 | end |
|
89 | 89 | elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE) |
|
90 | 90 | receive_issue_reply(m[1].to_i) |
|
91 | 91 | elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE) |
|
92 | 92 | receive_message_reply(m[1].to_i) |
|
93 | 93 | else |
|
94 | 94 | receive_issue |
|
95 | 95 | end |
|
96 | 96 | rescue ActiveRecord::RecordInvalid => e |
|
97 | 97 | # TODO: send a email to the user |
|
98 | 98 | logger.error e.message if logger |
|
99 | 99 | false |
|
100 | 100 | rescue MissingInformation => e |
|
101 | 101 | logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger |
|
102 | 102 | false |
|
103 | 103 | rescue UnauthorizedAction => e |
|
104 | 104 | logger.error "MailHandler: unauthorized attempt from #{user}" if logger |
|
105 | 105 | false |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | # Creates a new issue |
|
109 | 109 | def receive_issue |
|
110 | 110 | project = target_project |
|
111 | 111 | tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first) |
|
112 | 112 | category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category))) |
|
113 | 113 | priority = (get_keyword(:priority) && IssuePriority.find_by_name(get_keyword(:priority))) |
|
114 | 114 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) |
|
115 | 115 | |
|
116 | 116 | # check permission |
|
117 | 117 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) |
|
118 | 118 | issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority) |
|
119 | 119 | # check workflow |
|
120 | 120 | if status && issue.new_statuses_allowed_to(user).include?(status) |
|
121 | 121 | issue.status = status |
|
122 | 122 | end |
|
123 | 123 | issue.subject = email.subject.chomp.toutf8 |
|
124 | 124 | issue.description = plain_text_body |
|
125 | 125 | # custom fields |
|
126 | 126 | issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c| |
|
127 | 127 | if value = get_keyword(c.name, :override => true) |
|
128 | 128 | h[c.id] = value |
|
129 | 129 | end |
|
130 | 130 | h |
|
131 | 131 | end |
|
132 | 132 | # add To and Cc as watchers before saving so the watchers can reply to Redmine |
|
133 | 133 | add_watchers(issue) |
|
134 | 134 | issue.save! |
|
135 | 135 | add_attachments(issue) |
|
136 | 136 | logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info |
|
137 | 137 | issue |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | def target_project |
|
141 | 141 | # TODO: other ways to specify project: |
|
142 | 142 | # * parse the email To field |
|
143 | 143 | # * specific project (eg. Setting.mail_handler_target_project) |
|
144 | 144 | target = Project.find_by_identifier(get_keyword(:project)) |
|
145 | 145 | raise MissingInformation.new('Unable to determine target project') if target.nil? |
|
146 | 146 | target |
|
147 | 147 | end |
|
148 | 148 | |
|
149 | 149 | # Adds a note to an existing issue |
|
150 | 150 | def receive_issue_reply(issue_id) |
|
151 | 151 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) |
|
152 | 152 | |
|
153 | 153 | issue = Issue.find_by_id(issue_id) |
|
154 | 154 | return unless issue |
|
155 | 155 | # check permission |
|
156 | 156 | raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project) |
|
157 | 157 | raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project) |
|
158 | 158 | |
|
159 | 159 | # add the note |
|
160 | 160 | journal = issue.init_journal(user, plain_text_body) |
|
161 | 161 | add_attachments(issue) |
|
162 | 162 | # check workflow |
|
163 | 163 | if status && issue.new_statuses_allowed_to(user).include?(status) |
|
164 | 164 | issue.status = status |
|
165 | 165 | end |
|
166 | 166 | issue.save! |
|
167 | 167 | logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info |
|
168 | 168 | journal |
|
169 | 169 | end |
|
170 | 170 | |
|
171 | 171 | # Reply will be added to the issue |
|
172 | 172 | def receive_journal_reply(journal_id) |
|
173 | 173 | journal = Journal.find_by_id(journal_id) |
|
174 | 174 | if journal && journal.journalized_type == 'Issue' |
|
175 | 175 | receive_issue_reply(journal.journalized_id) |
|
176 | 176 | end |
|
177 | 177 | end |
|
178 | 178 | |
|
179 | 179 | # Receives a reply to a forum message |
|
180 | 180 | def receive_message_reply(message_id) |
|
181 | 181 | message = Message.find_by_id(message_id) |
|
182 | 182 | if message |
|
183 | 183 | message = message.root |
|
184 | 184 | if user.allowed_to?(:add_messages, message.project) && !message.locked? |
|
185 | 185 | reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip, |
|
186 | 186 | :content => plain_text_body) |
|
187 | 187 | reply.author = user |
|
188 | 188 | reply.board = message.board |
|
189 | 189 | message.children << reply |
|
190 | 190 | add_attachments(reply) |
|
191 | 191 | reply |
|
192 | 192 | else |
|
193 | 193 | raise UnauthorizedAction |
|
194 | 194 | end |
|
195 | 195 | end |
|
196 | 196 | end |
|
197 | 197 | |
|
198 | 198 | def add_attachments(obj) |
|
199 | 199 | if email.has_attachments? |
|
200 | 200 | email.attachments.each do |attachment| |
|
201 | 201 | Attachment.create(:container => obj, |
|
202 | 202 | :file => attachment, |
|
203 | 203 | :author => user, |
|
204 | 204 | :content_type => attachment.content_type) |
|
205 | 205 | end |
|
206 | 206 | end |
|
207 | 207 | end |
|
208 | 208 | |
|
209 | 209 | # Adds To and Cc as watchers of the given object if the sender has the |
|
210 | 210 | # appropriate permission |
|
211 | 211 | def add_watchers(obj) |
|
212 | 212 | if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) |
|
213 | 213 | addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} |
|
214 | 214 | unless addresses.empty? |
|
215 | 215 | watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses]) |
|
216 | 216 | watchers.each {|w| obj.add_watcher(w)} |
|
217 | 217 | end |
|
218 | 218 | end |
|
219 | 219 | end |
|
220 | 220 | |
|
221 | 221 | def get_keyword(attr, options={}) |
|
222 | 222 | @keywords ||= {} |
|
223 | 223 | if @keywords.has_key?(attr) |
|
224 | 224 | @keywords[attr] |
|
225 | 225 | else |
|
226 | 226 | @keywords[attr] = begin |
|
227 | if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}:[ \t]*(.+)\s*$/i, '') | |
|
227 | if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}[ \t]*:[ \t]*(.+)\s*$/i, '') | |
|
228 | 228 | $1.strip |
|
229 | 229 | elsif !@@handler_options[:issue][attr].blank? |
|
230 | 230 | @@handler_options[:issue][attr] |
|
231 | 231 | end |
|
232 | 232 | end |
|
233 | 233 | end |
|
234 | 234 | end |
|
235 | 235 | |
|
236 | 236 | # Returns the text/plain part of the email |
|
237 | 237 | # If not found (eg. HTML-only email), returns the body with tags removed |
|
238 | 238 | def plain_text_body |
|
239 | 239 | return @plain_text_body unless @plain_text_body.nil? |
|
240 | 240 | parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten |
|
241 | 241 | if parts.empty? |
|
242 | 242 | parts << @email |
|
243 | 243 | end |
|
244 | 244 | plain_text_part = parts.detect {|p| p.content_type == 'text/plain'} |
|
245 | 245 | if plain_text_part.nil? |
|
246 | 246 | # no text/plain part found, assuming html-only email |
|
247 | 247 | # strip html tags and remove doctype directive |
|
248 | 248 | @plain_text_body = strip_tags(@email.body.to_s) |
|
249 | 249 | @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, '' |
|
250 | 250 | else |
|
251 | 251 | @plain_text_body = plain_text_part.body.to_s |
|
252 | 252 | end |
|
253 | 253 | @plain_text_body.strip! |
|
254 | 254 | @plain_text_body |
|
255 | 255 | end |
|
256 | 256 | |
|
257 | 257 | |
|
258 | 258 | def self.full_sanitizer |
|
259 | 259 | @full_sanitizer ||= HTML::FullSanitizer.new |
|
260 | 260 | end |
|
261 | 261 | |
|
262 | 262 | # Creates a user account for the +email+ sender |
|
263 | 263 | def self.create_user_from_email(email) |
|
264 | 264 | addr = email.from_addrs.to_a.first |
|
265 | 265 | if addr && !addr.spec.blank? |
|
266 | 266 | user = User.new |
|
267 | 267 | user.mail = addr.spec |
|
268 | 268 | |
|
269 | 269 | names = addr.name.blank? ? addr.spec.gsub(/@.*$/, '').split('.') : addr.name.split |
|
270 | 270 | user.firstname = names.shift |
|
271 | 271 | user.lastname = names.join(' ') |
|
272 | 272 | user.lastname = '-' if user.lastname.blank? |
|
273 | 273 | |
|
274 | 274 | user.login = user.mail |
|
275 | 275 | user.password = ActiveSupport::SecureRandom.hex(5) |
|
276 | 276 | user.language = Setting.default_language |
|
277 | 277 | user.save ? user : nil |
|
278 | 278 | end |
|
279 | 279 | end |
|
280 | 280 | end |
@@ -1,241 +1,256 | |||
|
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 < Test::Unit::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 | 45 | # This email contains: 'Project: onlinestore' |
|
46 | 46 | issue = submit_email('ticket_on_given_project.eml') |
|
47 | 47 | assert issue.is_a?(Issue) |
|
48 | 48 | assert !issue.new_record? |
|
49 | 49 | issue.reload |
|
50 | 50 | assert_equal 'New ticket on a given project', issue.subject |
|
51 | 51 | assert_equal User.find_by_login('jsmith'), issue.author |
|
52 | 52 | assert_equal Project.find(2), issue.project |
|
53 | 53 | assert_equal IssueStatus.find_by_name('Resolved'), issue.status |
|
54 | 54 | assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') |
|
55 | 55 | # keywords should be removed from the email body |
|
56 | 56 | assert !issue.description.match(/^Project:/i) |
|
57 | 57 | assert !issue.description.match(/^Status:/i) |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | def test_add_issue_with_status |
|
61 | 61 | # This email contains: 'Project: onlinestore' and 'Status: Resolved' |
|
62 | 62 | issue = submit_email('ticket_on_given_project.eml') |
|
63 | 63 | assert issue.is_a?(Issue) |
|
64 | 64 | assert !issue.new_record? |
|
65 | 65 | issue.reload |
|
66 | 66 | assert_equal Project.find(2), issue.project |
|
67 | 67 | assert_equal IssueStatus.find_by_name("Resolved"), issue.status |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def test_add_issue_with_attributes_override |
|
71 | 71 | issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority') |
|
72 | 72 | assert issue.is_a?(Issue) |
|
73 | 73 | assert !issue.new_record? |
|
74 | 74 | issue.reload |
|
75 | 75 | assert_equal 'New ticket on a given project', issue.subject |
|
76 | 76 | assert_equal User.find_by_login('jsmith'), issue.author |
|
77 | 77 | assert_equal Project.find(2), issue.project |
|
78 | 78 | assert_equal 'Feature request', issue.tracker.to_s |
|
79 | 79 | assert_equal 'Stock management', issue.category.to_s |
|
80 | 80 | assert_equal 'Urgent', issue.priority.to_s |
|
81 | 81 | assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def test_add_issue_with_partial_attributes_override |
|
85 | 85 | issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker']) |
|
86 | 86 | assert issue.is_a?(Issue) |
|
87 | 87 | assert !issue.new_record? |
|
88 | 88 | issue.reload |
|
89 | 89 | assert_equal 'New ticket on a given project', issue.subject |
|
90 | 90 | assert_equal User.find_by_login('jsmith'), issue.author |
|
91 | 91 | assert_equal Project.find(2), issue.project |
|
92 | 92 | assert_equal 'Feature request', issue.tracker.to_s |
|
93 | 93 | assert_nil issue.category |
|
94 | 94 | assert_equal 'High', issue.priority.to_s |
|
95 | 95 | assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | def test_add_issue_with_spaces_between_attribute_and_separator | |
|
99 | issue = submit_email('ticket_with_spaces_between_attribute_and_separator.eml', :allow_override => 'tracker,category,priority') | |
|
100 | assert issue.is_a?(Issue) | |
|
101 | assert !issue.new_record? | |
|
102 | issue.reload | |
|
103 | assert_equal 'New ticket on a given project', issue.subject | |
|
104 | assert_equal User.find_by_login('jsmith'), issue.author | |
|
105 | assert_equal Project.find(2), issue.project | |
|
106 | assert_equal 'Feature request', issue.tracker.to_s | |
|
107 | assert_equal 'Stock management', issue.category.to_s | |
|
108 | assert_equal 'Urgent', issue.priority.to_s | |
|
109 | assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') | |
|
110 | end | |
|
111 | ||
|
112 | ||
|
98 | 113 | def test_add_issue_with_attachment_to_specific_project |
|
99 | 114 | issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'}) |
|
100 | 115 | assert issue.is_a?(Issue) |
|
101 | 116 | assert !issue.new_record? |
|
102 | 117 | issue.reload |
|
103 | 118 | assert_equal 'Ticket created by email with attachment', issue.subject |
|
104 | 119 | assert_equal User.find_by_login('jsmith'), issue.author |
|
105 | 120 | assert_equal Project.find(2), issue.project |
|
106 | 121 | assert_equal 'This is a new ticket with attachments', issue.description |
|
107 | 122 | # Attachment properties |
|
108 | 123 | assert_equal 1, issue.attachments.size |
|
109 | 124 | assert_equal 'Paella.jpg', issue.attachments.first.filename |
|
110 | 125 | assert_equal 'image/jpeg', issue.attachments.first.content_type |
|
111 | 126 | assert_equal 10790, issue.attachments.first.filesize |
|
112 | 127 | end |
|
113 | 128 | |
|
114 | 129 | def test_add_issue_with_custom_fields |
|
115 | 130 | issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'}) |
|
116 | 131 | assert issue.is_a?(Issue) |
|
117 | 132 | assert !issue.new_record? |
|
118 | 133 | issue.reload |
|
119 | 134 | assert_equal 'New ticket with custom field values', issue.subject |
|
120 | 135 | assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value |
|
121 | 136 | assert !issue.description.match(/^searchable field:/i) |
|
122 | 137 | end |
|
123 | 138 | |
|
124 | 139 | def test_add_issue_with_cc |
|
125 | 140 | issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'}) |
|
126 | 141 | assert issue.is_a?(Issue) |
|
127 | 142 | assert !issue.new_record? |
|
128 | 143 | issue.reload |
|
129 | 144 | assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo')) |
|
130 | 145 | assert_equal 1, issue.watchers.size |
|
131 | 146 | end |
|
132 | 147 | |
|
133 | 148 | def test_add_issue_by_unknown_user |
|
134 | 149 | assert_no_difference 'User.count' do |
|
135 | 150 | assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}) |
|
136 | 151 | end |
|
137 | 152 | end |
|
138 | 153 | |
|
139 | 154 | def test_add_issue_by_anonymous_user |
|
140 | 155 | Role.anonymous.add_permission!(:add_issues) |
|
141 | 156 | assert_no_difference 'User.count' do |
|
142 | 157 | issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept') |
|
143 | 158 | assert issue.is_a?(Issue) |
|
144 | 159 | assert issue.author.anonymous? |
|
145 | 160 | end |
|
146 | 161 | end |
|
147 | 162 | |
|
148 | 163 | def test_add_issue_by_created_user |
|
149 | 164 | Setting.default_language = 'en' |
|
150 | 165 | assert_difference 'User.count' do |
|
151 | 166 | issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create') |
|
152 | 167 | assert issue.is_a?(Issue) |
|
153 | 168 | assert issue.author.active? |
|
154 | 169 | assert_equal 'john.doe@somenet.foo', issue.author.mail |
|
155 | 170 | assert_equal 'John', issue.author.firstname |
|
156 | 171 | assert_equal 'Doe', issue.author.lastname |
|
157 | 172 | |
|
158 | 173 | # account information |
|
159 | 174 | email = ActionMailer::Base.deliveries.first |
|
160 | 175 | assert_not_nil email |
|
161 | 176 | assert email.subject.include?('account activation') |
|
162 | 177 | login = email.body.match(/\* Login: (.*)$/)[1] |
|
163 | 178 | password = email.body.match(/\* Password: (.*)$/)[1] |
|
164 | 179 | assert_equal issue.author, User.try_to_login(login, password) |
|
165 | 180 | end |
|
166 | 181 | end |
|
167 | 182 | |
|
168 | 183 | def test_add_issue_without_from_header |
|
169 | 184 | Role.anonymous.add_permission!(:add_issues) |
|
170 | 185 | assert_equal false, submit_email('ticket_without_from_header.eml') |
|
171 | 186 | end |
|
172 | 187 | |
|
173 | 188 | def test_add_issue_should_send_email_notification |
|
174 | 189 | ActionMailer::Base.deliveries.clear |
|
175 | 190 | # This email contains: 'Project: onlinestore' |
|
176 | 191 | issue = submit_email('ticket_on_given_project.eml') |
|
177 | 192 | assert issue.is_a?(Issue) |
|
178 | 193 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
179 | 194 | end |
|
180 | 195 | |
|
181 | 196 | def test_add_issue_note |
|
182 | 197 | journal = submit_email('ticket_reply.eml') |
|
183 | 198 | assert journal.is_a?(Journal) |
|
184 | 199 | assert_equal User.find_by_login('jsmith'), journal.user |
|
185 | 200 | assert_equal Issue.find(2), journal.journalized |
|
186 | 201 | assert_match /This is reply/, journal.notes |
|
187 | 202 | end |
|
188 | 203 | |
|
189 | 204 | def test_add_issue_note_with_status_change |
|
190 | 205 | # This email contains: 'Status: Resolved' |
|
191 | 206 | journal = submit_email('ticket_reply_with_status.eml') |
|
192 | 207 | assert journal.is_a?(Journal) |
|
193 | 208 | issue = Issue.find(journal.issue.id) |
|
194 | 209 | assert_equal User.find_by_login('jsmith'), journal.user |
|
195 | 210 | assert_equal Issue.find(2), journal.journalized |
|
196 | 211 | assert_match /This is reply/, journal.notes |
|
197 | 212 | assert_equal IssueStatus.find_by_name("Resolved"), issue.status |
|
198 | 213 | end |
|
199 | 214 | |
|
200 | 215 | def test_add_issue_note_should_send_email_notification |
|
201 | 216 | ActionMailer::Base.deliveries.clear |
|
202 | 217 | journal = submit_email('ticket_reply.eml') |
|
203 | 218 | assert journal.is_a?(Journal) |
|
204 | 219 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
205 | 220 | end |
|
206 | 221 | |
|
207 | 222 | def test_reply_to_a_message |
|
208 | 223 | m = submit_email('message_reply.eml') |
|
209 | 224 | assert m.is_a?(Message) |
|
210 | 225 | assert !m.new_record? |
|
211 | 226 | m.reload |
|
212 | 227 | assert_equal 'Reply via email', m.subject |
|
213 | 228 | # The email replies to message #2 which is part of the thread of message #1 |
|
214 | 229 | assert_equal Message.find(1), m.parent |
|
215 | 230 | end |
|
216 | 231 | |
|
217 | 232 | def test_reply_to_a_message_by_subject |
|
218 | 233 | m = submit_email('message_reply_by_subject.eml') |
|
219 | 234 | assert m.is_a?(Message) |
|
220 | 235 | assert !m.new_record? |
|
221 | 236 | m.reload |
|
222 | 237 | assert_equal 'Reply to the first post', m.subject |
|
223 | 238 | assert_equal Message.find(1), m.parent |
|
224 | 239 | end |
|
225 | 240 | |
|
226 | 241 | def test_should_strip_tags_of_html_only_emails |
|
227 | 242 | issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'}) |
|
228 | 243 | assert issue.is_a?(Issue) |
|
229 | 244 | assert !issue.new_record? |
|
230 | 245 | issue.reload |
|
231 | 246 | assert_equal 'HTML email', issue.subject |
|
232 | 247 | assert_equal 'This is a html-only email.', issue.description |
|
233 | 248 | end |
|
234 | 249 | |
|
235 | 250 | private |
|
236 | 251 | |
|
237 | 252 | def submit_email(filename, options={}) |
|
238 | 253 | raw = IO.read(File.join(FIXTURES_PATH, filename)) |
|
239 | 254 | MailHandler.receive(raw, options) |
|
240 | 255 | end |
|
241 | 256 | end |
General Comments 0
You need to be logged in to leave comments.
Login now