##// END OF EJS Templates
Mailer.token_for generates invalid message_id when using from address with full name (#16619)....
Jean-Philippe Lang -
r12851:53deb6ca880d
parent child
Show More
@@ -1,495 +1,495
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Mailer < ActionMailer::Base
18 class Mailer < ActionMailer::Base
19 layout 'mailer'
19 layout 'mailer'
20 helper :application
20 helper :application
21 helper :issues
21 helper :issues
22 helper :custom_fields
22 helper :custom_fields
23
23
24 include Redmine::I18n
24 include Redmine::I18n
25
25
26 def self.default_url_options
26 def self.default_url_options
27 { :host => Setting.host_name, :protocol => Setting.protocol }
27 { :host => Setting.host_name, :protocol => Setting.protocol }
28 end
28 end
29
29
30 # Builds a mail for notifying to_users and cc_users about a new issue
30 # Builds a mail for notifying to_users and cc_users about a new issue
31 def issue_add(issue, to_users, cc_users)
31 def issue_add(issue, to_users, cc_users)
32 redmine_headers 'Project' => issue.project.identifier,
32 redmine_headers 'Project' => issue.project.identifier,
33 'Issue-Id' => issue.id,
33 'Issue-Id' => issue.id,
34 'Issue-Author' => issue.author.login
34 'Issue-Author' => issue.author.login
35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
36 message_id issue
36 message_id issue
37 references issue
37 references issue
38 @author = issue.author
38 @author = issue.author
39 @issue = issue
39 @issue = issue
40 @users = to_users + cc_users
40 @users = to_users + cc_users
41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
42 mail :to => to_users.map(&:mail),
42 mail :to => to_users.map(&:mail),
43 :cc => cc_users.map(&:mail),
43 :cc => cc_users.map(&:mail),
44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
45 end
45 end
46
46
47 # Notifies users about a new issue
47 # Notifies users about a new issue
48 def self.deliver_issue_add(issue)
48 def self.deliver_issue_add(issue)
49 to = issue.notified_users
49 to = issue.notified_users
50 cc = issue.notified_watchers - to
50 cc = issue.notified_watchers - to
51 issue.each_notification(to + cc) do |users|
51 issue.each_notification(to + cc) do |users|
52 Mailer.issue_add(issue, to & users, cc & users).deliver
52 Mailer.issue_add(issue, to & users, cc & users).deliver
53 end
53 end
54 end
54 end
55
55
56 # Builds a mail for notifying to_users and cc_users about an issue update
56 # Builds a mail for notifying to_users and cc_users about an issue update
57 def issue_edit(journal, to_users, cc_users)
57 def issue_edit(journal, to_users, cc_users)
58 issue = journal.journalized
58 issue = journal.journalized
59 redmine_headers 'Project' => issue.project.identifier,
59 redmine_headers 'Project' => issue.project.identifier,
60 'Issue-Id' => issue.id,
60 'Issue-Id' => issue.id,
61 'Issue-Author' => issue.author.login
61 'Issue-Author' => issue.author.login
62 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
62 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
63 message_id journal
63 message_id journal
64 references issue
64 references issue
65 @author = journal.user
65 @author = journal.user
66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
68 s << issue.subject
68 s << issue.subject
69 @issue = issue
69 @issue = issue
70 @users = to_users + cc_users
70 @users = to_users + cc_users
71 @journal = journal
71 @journal = journal
72 @journal_details = journal.visible_details(@users.first)
72 @journal_details = journal.visible_details(@users.first)
73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
74 mail :to => to_users.map(&:mail),
74 mail :to => to_users.map(&:mail),
75 :cc => cc_users.map(&:mail),
75 :cc => cc_users.map(&:mail),
76 :subject => s
76 :subject => s
77 end
77 end
78
78
79 # Notifies users about an issue update
79 # Notifies users about an issue update
80 def self.deliver_issue_edit(journal)
80 def self.deliver_issue_edit(journal)
81 issue = journal.journalized.reload
81 issue = journal.journalized.reload
82 to = journal.notified_users
82 to = journal.notified_users
83 cc = journal.notified_watchers - to
83 cc = journal.notified_watchers - to
84 journal.each_notification(to + cc) do |users|
84 journal.each_notification(to + cc) do |users|
85 issue.each_notification(users) do |users2|
85 issue.each_notification(users) do |users2|
86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
87 end
87 end
88 end
88 end
89 end
89 end
90
90
91 def reminder(user, issues, days)
91 def reminder(user, issues, days)
92 set_language_if_valid user.language
92 set_language_if_valid user.language
93 @issues = issues
93 @issues = issues
94 @days = days
94 @days = days
95 @issues_url = url_for(:controller => 'issues', :action => 'index',
95 @issues_url = url_for(:controller => 'issues', :action => 'index',
96 :set_filter => 1, :assigned_to_id => user.id,
96 :set_filter => 1, :assigned_to_id => user.id,
97 :sort => 'due_date:asc')
97 :sort => 'due_date:asc')
98 mail :to => user.mail,
98 mail :to => user.mail,
99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
100 end
100 end
101
101
102 # Builds a Mail::Message object used to email users belonging to the added document's project.
102 # Builds a Mail::Message object used to email users belonging to the added document's project.
103 #
103 #
104 # Example:
104 # Example:
105 # document_added(document) => Mail::Message object
105 # document_added(document) => Mail::Message object
106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
107 def document_added(document)
107 def document_added(document)
108 redmine_headers 'Project' => document.project.identifier
108 redmine_headers 'Project' => document.project.identifier
109 @author = User.current
109 @author = User.current
110 @document = document
110 @document = document
111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
112 mail :to => document.recipients,
112 mail :to => document.recipients,
113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
114 end
114 end
115
115
116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
117 #
117 #
118 # Example:
118 # Example:
119 # attachments_added(attachments) => Mail::Message object
119 # attachments_added(attachments) => Mail::Message object
120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
121 def attachments_added(attachments)
121 def attachments_added(attachments)
122 container = attachments.first.container
122 container = attachments.first.container
123 added_to = ''
123 added_to = ''
124 added_to_url = ''
124 added_to_url = ''
125 @author = attachments.first.author
125 @author = attachments.first.author
126 case container.class.name
126 case container.class.name
127 when 'Project'
127 when 'Project'
128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
129 added_to = "#{l(:label_project)}: #{container}"
129 added_to = "#{l(:label_project)}: #{container}"
130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
131 when 'Version'
131 when 'Version'
132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
133 added_to = "#{l(:label_version)}: #{container.name}"
133 added_to = "#{l(:label_version)}: #{container.name}"
134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
135 when 'Document'
135 when 'Document'
136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
137 added_to = "#{l(:label_document)}: #{container.title}"
137 added_to = "#{l(:label_document)}: #{container.title}"
138 recipients = container.recipients
138 recipients = container.recipients
139 end
139 end
140 redmine_headers 'Project' => container.project.identifier
140 redmine_headers 'Project' => container.project.identifier
141 @attachments = attachments
141 @attachments = attachments
142 @added_to = added_to
142 @added_to = added_to
143 @added_to_url = added_to_url
143 @added_to_url = added_to_url
144 mail :to => recipients,
144 mail :to => recipients,
145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
146 end
146 end
147
147
148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
149 #
149 #
150 # Example:
150 # Example:
151 # news_added(news) => Mail::Message object
151 # news_added(news) => Mail::Message object
152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
153 def news_added(news)
153 def news_added(news)
154 redmine_headers 'Project' => news.project.identifier
154 redmine_headers 'Project' => news.project.identifier
155 @author = news.author
155 @author = news.author
156 message_id news
156 message_id news
157 references news
157 references news
158 @news = news
158 @news = news
159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
160 mail :to => news.recipients,
160 mail :to => news.recipients,
161 :cc => news.cc_for_added_news,
161 :cc => news.cc_for_added_news,
162 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
162 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
163 end
163 end
164
164
165 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
165 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
166 #
166 #
167 # Example:
167 # Example:
168 # news_comment_added(comment) => Mail::Message object
168 # news_comment_added(comment) => Mail::Message object
169 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
169 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
170 def news_comment_added(comment)
170 def news_comment_added(comment)
171 news = comment.commented
171 news = comment.commented
172 redmine_headers 'Project' => news.project.identifier
172 redmine_headers 'Project' => news.project.identifier
173 @author = comment.author
173 @author = comment.author
174 message_id comment
174 message_id comment
175 references news
175 references news
176 @news = news
176 @news = news
177 @comment = comment
177 @comment = comment
178 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
178 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
179 mail :to => news.recipients,
179 mail :to => news.recipients,
180 :cc => news.watcher_recipients,
180 :cc => news.watcher_recipients,
181 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
181 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
182 end
182 end
183
183
184 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
184 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
185 #
185 #
186 # Example:
186 # Example:
187 # message_posted(message) => Mail::Message object
187 # message_posted(message) => Mail::Message object
188 # Mailer.message_posted(message).deliver => sends an email to the recipients
188 # Mailer.message_posted(message).deliver => sends an email to the recipients
189 def message_posted(message)
189 def message_posted(message)
190 redmine_headers 'Project' => message.project.identifier,
190 redmine_headers 'Project' => message.project.identifier,
191 'Topic-Id' => (message.parent_id || message.id)
191 'Topic-Id' => (message.parent_id || message.id)
192 @author = message.author
192 @author = message.author
193 message_id message
193 message_id message
194 references message.root
194 references message.root
195 recipients = message.recipients
195 recipients = message.recipients
196 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
196 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
197 @message = message
197 @message = message
198 @message_url = url_for(message.event_url)
198 @message_url = url_for(message.event_url)
199 mail :to => recipients,
199 mail :to => recipients,
200 :cc => cc,
200 :cc => cc,
201 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
201 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
202 end
202 end
203
203
204 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
204 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
205 #
205 #
206 # Example:
206 # Example:
207 # wiki_content_added(wiki_content) => Mail::Message object
207 # wiki_content_added(wiki_content) => Mail::Message object
208 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
208 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
209 def wiki_content_added(wiki_content)
209 def wiki_content_added(wiki_content)
210 redmine_headers 'Project' => wiki_content.project.identifier,
210 redmine_headers 'Project' => wiki_content.project.identifier,
211 'Wiki-Page-Id' => wiki_content.page.id
211 'Wiki-Page-Id' => wiki_content.page.id
212 @author = wiki_content.author
212 @author = wiki_content.author
213 message_id wiki_content
213 message_id wiki_content
214 recipients = wiki_content.recipients
214 recipients = wiki_content.recipients
215 cc = wiki_content.page.wiki.watcher_recipients - recipients
215 cc = wiki_content.page.wiki.watcher_recipients - recipients
216 @wiki_content = wiki_content
216 @wiki_content = wiki_content
217 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
217 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
218 :project_id => wiki_content.project,
218 :project_id => wiki_content.project,
219 :id => wiki_content.page.title)
219 :id => wiki_content.page.title)
220 mail :to => recipients,
220 mail :to => recipients,
221 :cc => cc,
221 :cc => cc,
222 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
222 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
223 end
223 end
224
224
225 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
225 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
226 #
226 #
227 # Example:
227 # Example:
228 # wiki_content_updated(wiki_content) => Mail::Message object
228 # wiki_content_updated(wiki_content) => Mail::Message object
229 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
229 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
230 def wiki_content_updated(wiki_content)
230 def wiki_content_updated(wiki_content)
231 redmine_headers 'Project' => wiki_content.project.identifier,
231 redmine_headers 'Project' => wiki_content.project.identifier,
232 'Wiki-Page-Id' => wiki_content.page.id
232 'Wiki-Page-Id' => wiki_content.page.id
233 @author = wiki_content.author
233 @author = wiki_content.author
234 message_id wiki_content
234 message_id wiki_content
235 recipients = wiki_content.recipients
235 recipients = wiki_content.recipients
236 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
236 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
237 @wiki_content = wiki_content
237 @wiki_content = wiki_content
238 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
238 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
239 :project_id => wiki_content.project,
239 :project_id => wiki_content.project,
240 :id => wiki_content.page.title)
240 :id => wiki_content.page.title)
241 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
241 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
242 :project_id => wiki_content.project, :id => wiki_content.page.title,
242 :project_id => wiki_content.project, :id => wiki_content.page.title,
243 :version => wiki_content.version)
243 :version => wiki_content.version)
244 mail :to => recipients,
244 mail :to => recipients,
245 :cc => cc,
245 :cc => cc,
246 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
246 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
247 end
247 end
248
248
249 # Builds a Mail::Message object used to email the specified user their account information.
249 # Builds a Mail::Message object used to email the specified user their account information.
250 #
250 #
251 # Example:
251 # Example:
252 # account_information(user, password) => Mail::Message object
252 # account_information(user, password) => Mail::Message object
253 # Mailer.account_information(user, password).deliver => sends account information to the user
253 # Mailer.account_information(user, password).deliver => sends account information to the user
254 def account_information(user, password)
254 def account_information(user, password)
255 set_language_if_valid user.language
255 set_language_if_valid user.language
256 @user = user
256 @user = user
257 @password = password
257 @password = password
258 @login_url = url_for(:controller => 'account', :action => 'login')
258 @login_url = url_for(:controller => 'account', :action => 'login')
259 mail :to => user.mail,
259 mail :to => user.mail,
260 :subject => l(:mail_subject_register, Setting.app_title)
260 :subject => l(:mail_subject_register, Setting.app_title)
261 end
261 end
262
262
263 # Builds a Mail::Message object used to email all active administrators of an account activation request.
263 # Builds a Mail::Message object used to email all active administrators of an account activation request.
264 #
264 #
265 # Example:
265 # Example:
266 # account_activation_request(user) => Mail::Message object
266 # account_activation_request(user) => Mail::Message object
267 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
267 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
268 def account_activation_request(user)
268 def account_activation_request(user)
269 # Send the email to all active administrators
269 # Send the email to all active administrators
270 recipients = User.active.where(:admin => true).collect { |u| u.mail }.compact
270 recipients = User.active.where(:admin => true).collect { |u| u.mail }.compact
271 @user = user
271 @user = user
272 @url = url_for(:controller => 'users', :action => 'index',
272 @url = url_for(:controller => 'users', :action => 'index',
273 :status => User::STATUS_REGISTERED,
273 :status => User::STATUS_REGISTERED,
274 :sort_key => 'created_on', :sort_order => 'desc')
274 :sort_key => 'created_on', :sort_order => 'desc')
275 mail :to => recipients,
275 mail :to => recipients,
276 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
276 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
277 end
277 end
278
278
279 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
279 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
280 #
280 #
281 # Example:
281 # Example:
282 # account_activated(user) => Mail::Message object
282 # account_activated(user) => Mail::Message object
283 # Mailer.account_activated(user).deliver => sends an email to the registered user
283 # Mailer.account_activated(user).deliver => sends an email to the registered user
284 def account_activated(user)
284 def account_activated(user)
285 set_language_if_valid user.language
285 set_language_if_valid user.language
286 @user = user
286 @user = user
287 @login_url = url_for(:controller => 'account', :action => 'login')
287 @login_url = url_for(:controller => 'account', :action => 'login')
288 mail :to => user.mail,
288 mail :to => user.mail,
289 :subject => l(:mail_subject_register, Setting.app_title)
289 :subject => l(:mail_subject_register, Setting.app_title)
290 end
290 end
291
291
292 def lost_password(token)
292 def lost_password(token)
293 set_language_if_valid(token.user.language)
293 set_language_if_valid(token.user.language)
294 @token = token
294 @token = token
295 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
295 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
296 mail :to => token.user.mail,
296 mail :to => token.user.mail,
297 :subject => l(:mail_subject_lost_password, Setting.app_title)
297 :subject => l(:mail_subject_lost_password, Setting.app_title)
298 end
298 end
299
299
300 def register(token)
300 def register(token)
301 set_language_if_valid(token.user.language)
301 set_language_if_valid(token.user.language)
302 @token = token
302 @token = token
303 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
303 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
304 mail :to => token.user.mail,
304 mail :to => token.user.mail,
305 :subject => l(:mail_subject_register, Setting.app_title)
305 :subject => l(:mail_subject_register, Setting.app_title)
306 end
306 end
307
307
308 def test_email(user)
308 def test_email(user)
309 set_language_if_valid(user.language)
309 set_language_if_valid(user.language)
310 @url = url_for(:controller => 'welcome')
310 @url = url_for(:controller => 'welcome')
311 mail :to => user.mail,
311 mail :to => user.mail,
312 :subject => 'Redmine test'
312 :subject => 'Redmine test'
313 end
313 end
314
314
315 # Sends reminders to issue assignees
315 # Sends reminders to issue assignees
316 # Available options:
316 # Available options:
317 # * :days => how many days in the future to remind about (defaults to 7)
317 # * :days => how many days in the future to remind about (defaults to 7)
318 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
318 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
319 # * :project => id or identifier of project to process (defaults to all projects)
319 # * :project => id or identifier of project to process (defaults to all projects)
320 # * :users => array of user/group ids who should be reminded
320 # * :users => array of user/group ids who should be reminded
321 def self.reminders(options={})
321 def self.reminders(options={})
322 days = options[:days] || 7
322 days = options[:days] || 7
323 project = options[:project] ? Project.find(options[:project]) : nil
323 project = options[:project] ? Project.find(options[:project]) : nil
324 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
324 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
325 user_ids = options[:users]
325 user_ids = options[:users]
326
326
327 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
327 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
328 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
328 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
329 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
329 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
330 )
330 )
331 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
331 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
332 scope = scope.where(:project_id => project.id) if project
332 scope = scope.where(:project_id => project.id) if project
333 scope = scope.where(:tracker_id => tracker.id) if tracker
333 scope = scope.where(:tracker_id => tracker.id) if tracker
334 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).
334 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).
335 group_by(&:assigned_to)
335 group_by(&:assigned_to)
336 issues_by_assignee.keys.each do |assignee|
336 issues_by_assignee.keys.each do |assignee|
337 if assignee.is_a?(Group)
337 if assignee.is_a?(Group)
338 assignee.users.each do |user|
338 assignee.users.each do |user|
339 issues_by_assignee[user] ||= []
339 issues_by_assignee[user] ||= []
340 issues_by_assignee[user] += issues_by_assignee[assignee]
340 issues_by_assignee[user] += issues_by_assignee[assignee]
341 end
341 end
342 end
342 end
343 end
343 end
344
344
345 issues_by_assignee.each do |assignee, issues|
345 issues_by_assignee.each do |assignee, issues|
346 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
346 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
347 end
347 end
348 end
348 end
349
349
350 # Activates/desactivates email deliveries during +block+
350 # Activates/desactivates email deliveries during +block+
351 def self.with_deliveries(enabled = true, &block)
351 def self.with_deliveries(enabled = true, &block)
352 was_enabled = ActionMailer::Base.perform_deliveries
352 was_enabled = ActionMailer::Base.perform_deliveries
353 ActionMailer::Base.perform_deliveries = !!enabled
353 ActionMailer::Base.perform_deliveries = !!enabled
354 yield
354 yield
355 ensure
355 ensure
356 ActionMailer::Base.perform_deliveries = was_enabled
356 ActionMailer::Base.perform_deliveries = was_enabled
357 end
357 end
358
358
359 # Sends emails synchronously in the given block
359 # Sends emails synchronously in the given block
360 def self.with_synched_deliveries(&block)
360 def self.with_synched_deliveries(&block)
361 saved_method = ActionMailer::Base.delivery_method
361 saved_method = ActionMailer::Base.delivery_method
362 if m = saved_method.to_s.match(%r{^async_(.+)$})
362 if m = saved_method.to_s.match(%r{^async_(.+)$})
363 synched_method = m[1]
363 synched_method = m[1]
364 ActionMailer::Base.delivery_method = synched_method.to_sym
364 ActionMailer::Base.delivery_method = synched_method.to_sym
365 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
365 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
366 end
366 end
367 yield
367 yield
368 ensure
368 ensure
369 ActionMailer::Base.delivery_method = saved_method
369 ActionMailer::Base.delivery_method = saved_method
370 end
370 end
371
371
372 def mail(headers={}, &block)
372 def mail(headers={}, &block)
373 headers.merge! 'X-Mailer' => 'Redmine',
373 headers.merge! 'X-Mailer' => 'Redmine',
374 'X-Redmine-Host' => Setting.host_name,
374 'X-Redmine-Host' => Setting.host_name,
375 'X-Redmine-Site' => Setting.app_title,
375 'X-Redmine-Site' => Setting.app_title,
376 'X-Auto-Response-Suppress' => 'OOF',
376 'X-Auto-Response-Suppress' => 'OOF',
377 'Auto-Submitted' => 'auto-generated',
377 'Auto-Submitted' => 'auto-generated',
378 'From' => Setting.mail_from,
378 'From' => Setting.mail_from,
379 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
379 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
380
380
381 # Removes the author from the recipients and cc
381 # Removes the author from the recipients and cc
382 # if the author does not want to receive notifications
382 # if the author does not want to receive notifications
383 # about what the author do
383 # about what the author do
384 if @author && @author.logged? && @author.pref.no_self_notified
384 if @author && @author.logged? && @author.pref.no_self_notified
385 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
385 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
386 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
386 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
387 end
387 end
388
388
389 if @author && @author.logged?
389 if @author && @author.logged?
390 redmine_headers 'Sender' => @author.login
390 redmine_headers 'Sender' => @author.login
391 end
391 end
392
392
393 # Blind carbon copy recipients
393 # Blind carbon copy recipients
394 if Setting.bcc_recipients?
394 if Setting.bcc_recipients?
395 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
395 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
396 headers[:to] = nil
396 headers[:to] = nil
397 headers[:cc] = nil
397 headers[:cc] = nil
398 end
398 end
399
399
400 if @message_id_object
400 if @message_id_object
401 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
401 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
402 end
402 end
403 if @references_objects
403 if @references_objects
404 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
404 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
405 end
405 end
406
406
407 m = if block_given?
407 m = if block_given?
408 super headers, &block
408 super headers, &block
409 else
409 else
410 super headers do |format|
410 super headers do |format|
411 format.text
411 format.text
412 format.html unless Setting.plain_text_mail?
412 format.html unless Setting.plain_text_mail?
413 end
413 end
414 end
414 end
415 set_language_if_valid @initial_language
415 set_language_if_valid @initial_language
416
416
417 m
417 m
418 end
418 end
419
419
420 def initialize(*args)
420 def initialize(*args)
421 @initial_language = current_language
421 @initial_language = current_language
422 set_language_if_valid Setting.default_language
422 set_language_if_valid Setting.default_language
423 super
423 super
424 end
424 end
425
425
426 def self.deliver_mail(mail)
426 def self.deliver_mail(mail)
427 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
427 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
428 begin
428 begin
429 # Log errors when raise_delivery_errors is set to false, Rails does not
429 # Log errors when raise_delivery_errors is set to false, Rails does not
430 mail.raise_delivery_errors = true
430 mail.raise_delivery_errors = true
431 super
431 super
432 rescue Exception => e
432 rescue Exception => e
433 if ActionMailer::Base.raise_delivery_errors
433 if ActionMailer::Base.raise_delivery_errors
434 raise e
434 raise e
435 else
435 else
436 Rails.logger.error "Email delivery error: #{e.message}"
436 Rails.logger.error "Email delivery error: #{e.message}"
437 end
437 end
438 end
438 end
439 end
439 end
440
440
441 def self.method_missing(method, *args, &block)
441 def self.method_missing(method, *args, &block)
442 if m = method.to_s.match(%r{^deliver_(.+)$})
442 if m = method.to_s.match(%r{^deliver_(.+)$})
443 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
443 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
444 send(m[1], *args).deliver
444 send(m[1], *args).deliver
445 else
445 else
446 super
446 super
447 end
447 end
448 end
448 end
449
449
450 private
450 private
451
451
452 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
452 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
453 def redmine_headers(h)
453 def redmine_headers(h)
454 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
454 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
455 end
455 end
456
456
457 def self.token_for(object, rand=true)
457 def self.token_for(object, rand=true)
458 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
458 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
459 hash = [
459 hash = [
460 "redmine",
460 "redmine",
461 "#{object.class.name.demodulize.underscore}-#{object.id}",
461 "#{object.class.name.demodulize.underscore}-#{object.id}",
462 timestamp.strftime("%Y%m%d%H%M%S")
462 timestamp.strftime("%Y%m%d%H%M%S")
463 ]
463 ]
464 if rand
464 if rand
465 hash << Redmine::Utils.random_hex(8)
465 hash << Redmine::Utils.random_hex(8)
466 end
466 end
467 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
467 host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '')
468 host = "#{::Socket.gethostname}.redmine" if host.empty?
468 host = "#{::Socket.gethostname}.redmine" if host.empty?
469 "#{hash.join('.')}@#{host}"
469 "#{hash.join('.')}@#{host}"
470 end
470 end
471
471
472 # Returns a Message-Id for the given object
472 # Returns a Message-Id for the given object
473 def self.message_id_for(object)
473 def self.message_id_for(object)
474 token_for(object, true)
474 token_for(object, true)
475 end
475 end
476
476
477 # Returns a uniq token for a given object referenced by all notifications
477 # Returns a uniq token for a given object referenced by all notifications
478 # related to this object
478 # related to this object
479 def self.references_for(object)
479 def self.references_for(object)
480 token_for(object, false)
480 token_for(object, false)
481 end
481 end
482
482
483 def message_id(object)
483 def message_id(object)
484 @message_id_object = object
484 @message_id_object = object
485 end
485 end
486
486
487 def references(object)
487 def references(object)
488 @references_objects ||= []
488 @references_objects ||= []
489 @references_objects << object
489 @references_objects << object
490 end
490 end
491
491
492 def mylogger
492 def mylogger
493 Rails.logger
493 Rails.logger
494 end
494 end
495 end
495 end
@@ -1,768 +1,774
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class MailerTest < ActiveSupport::TestCase
20 class MailerTest < ActiveSupport::TestCase
21 include Redmine::I18n
21 include Redmine::I18n
22 include ActionDispatch::Assertions::SelectorAssertions
22 include ActionDispatch::Assertions::SelectorAssertions
23 fixtures :projects, :enabled_modules, :issues, :users, :members,
23 fixtures :projects, :enabled_modules, :issues, :users, :members,
24 :member_roles, :roles, :documents, :attachments, :news,
24 :member_roles, :roles, :documents, :attachments, :news,
25 :tokens, :journals, :journal_details, :changesets,
25 :tokens, :journals, :journal_details, :changesets,
26 :trackers, :projects_trackers,
26 :trackers, :projects_trackers,
27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
29 :versions,
29 :versions,
30 :comments
30 :comments
31
31
32 def setup
32 def setup
33 ActionMailer::Base.deliveries.clear
33 ActionMailer::Base.deliveries.clear
34 Setting.host_name = 'mydomain.foo'
34 Setting.host_name = 'mydomain.foo'
35 Setting.protocol = 'http'
35 Setting.protocol = 'http'
36 Setting.plain_text_mail = '0'
36 Setting.plain_text_mail = '0'
37 end
37 end
38
38
39 def test_generated_links_in_emails
39 def test_generated_links_in_emails
40 Setting.default_language = 'en'
40 Setting.default_language = 'en'
41 Setting.host_name = 'mydomain.foo'
41 Setting.host_name = 'mydomain.foo'
42 Setting.protocol = 'https'
42 Setting.protocol = 'https'
43
43
44 journal = Journal.find(3)
44 journal = Journal.find(3)
45 assert Mailer.deliver_issue_edit(journal)
45 assert Mailer.deliver_issue_edit(journal)
46
46
47 mail = last_email
47 mail = last_email
48 assert_not_nil mail
48 assert_not_nil mail
49
49
50 assert_select_email do
50 assert_select_email do
51 # link to the main ticket
51 # link to the main ticket
52 assert_select 'a[href=?]',
52 assert_select 'a[href=?]',
53 'https://mydomain.foo/issues/2#change-3',
53 'https://mydomain.foo/issues/2#change-3',
54 :text => 'Feature request #2: Add ingredients categories'
54 :text => 'Feature request #2: Add ingredients categories'
55 # link to a referenced ticket
55 # link to a referenced ticket
56 assert_select 'a[href=?][title=?]',
56 assert_select 'a[href=?][title=?]',
57 'https://mydomain.foo/issues/1',
57 'https://mydomain.foo/issues/1',
58 "#{ESCAPED_UCANT} print recipes (New)",
58 "#{ESCAPED_UCANT} print recipes (New)",
59 :text => '#1'
59 :text => '#1'
60 # link to a changeset
60 # link to a changeset
61 assert_select 'a[href=?][title=?]',
61 assert_select 'a[href=?][title=?]',
62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
63 'This commit fixes #1, #2 and references #1 &amp; #3',
63 'This commit fixes #1, #2 and references #1 &amp; #3',
64 :text => 'r2'
64 :text => 'r2'
65 # link to a description diff
65 # link to a description diff
66 assert_select 'a[href=?][title=?]',
66 assert_select 'a[href=?][title=?]',
67 'https://mydomain.foo/journals/diff/3?detail_id=4',
67 'https://mydomain.foo/journals/diff/3?detail_id=4',
68 'View differences',
68 'View differences',
69 :text => 'diff'
69 :text => 'diff'
70 # link to an attachment
70 # link to an attachment
71 assert_select 'a[href=?]',
71 assert_select 'a[href=?]',
72 'https://mydomain.foo/attachments/download/4/source.rb',
72 'https://mydomain.foo/attachments/download/4/source.rb',
73 :text => 'source.rb'
73 :text => 'source.rb'
74 end
74 end
75 end
75 end
76
76
77 def test_generated_links_with_prefix
77 def test_generated_links_with_prefix
78 Setting.default_language = 'en'
78 Setting.default_language = 'en'
79 relative_url_root = Redmine::Utils.relative_url_root
79 relative_url_root = Redmine::Utils.relative_url_root
80 Setting.host_name = 'mydomain.foo/rdm'
80 Setting.host_name = 'mydomain.foo/rdm'
81 Setting.protocol = 'http'
81 Setting.protocol = 'http'
82
82
83 journal = Journal.find(3)
83 journal = Journal.find(3)
84 assert Mailer.deliver_issue_edit(journal)
84 assert Mailer.deliver_issue_edit(journal)
85
85
86 mail = last_email
86 mail = last_email
87 assert_not_nil mail
87 assert_not_nil mail
88
88
89 assert_select_email do
89 assert_select_email do
90 # link to the main ticket
90 # link to the main ticket
91 assert_select 'a[href=?]',
91 assert_select 'a[href=?]',
92 'http://mydomain.foo/rdm/issues/2#change-3',
92 'http://mydomain.foo/rdm/issues/2#change-3',
93 :text => 'Feature request #2: Add ingredients categories'
93 :text => 'Feature request #2: Add ingredients categories'
94 # link to a referenced ticket
94 # link to a referenced ticket
95 assert_select 'a[href=?][title=?]',
95 assert_select 'a[href=?][title=?]',
96 'http://mydomain.foo/rdm/issues/1',
96 'http://mydomain.foo/rdm/issues/1',
97 "#{ESCAPED_UCANT} print recipes (New)",
97 "#{ESCAPED_UCANT} print recipes (New)",
98 :text => '#1'
98 :text => '#1'
99 # link to a changeset
99 # link to a changeset
100 assert_select 'a[href=?][title=?]',
100 assert_select 'a[href=?][title=?]',
101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
102 'This commit fixes #1, #2 and references #1 &amp; #3',
102 'This commit fixes #1, #2 and references #1 &amp; #3',
103 :text => 'r2'
103 :text => 'r2'
104 # link to a description diff
104 # link to a description diff
105 assert_select 'a[href=?][title=?]',
105 assert_select 'a[href=?][title=?]',
106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
107 'View differences',
107 'View differences',
108 :text => 'diff'
108 :text => 'diff'
109 # link to an attachment
109 # link to an attachment
110 assert_select 'a[href=?]',
110 assert_select 'a[href=?]',
111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
112 :text => 'source.rb'
112 :text => 'source.rb'
113 end
113 end
114 end
114 end
115
115
116 def test_issue_edit_should_generate_url_with_hostname_for_relations
116 def test_issue_edit_should_generate_url_with_hostname_for_relations
117 journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now)
117 journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now)
118 journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
118 journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
119 Mailer.deliver_issue_edit(journal)
119 Mailer.deliver_issue_edit(journal)
120 assert_not_nil last_email
120 assert_not_nil last_email
121 assert_select_email do
121 assert_select_email do
122 assert_select 'a[href=?]', 'http://mydomain.foo/issues/2', :text => 'Feature request #2'
122 assert_select 'a[href=?]', 'http://mydomain.foo/issues/2', :text => 'Feature request #2'
123 end
123 end
124 end
124 end
125
125
126 def test_generated_links_with_prefix_and_no_relative_url_root
126 def test_generated_links_with_prefix_and_no_relative_url_root
127 Setting.default_language = 'en'
127 Setting.default_language = 'en'
128 relative_url_root = Redmine::Utils.relative_url_root
128 relative_url_root = Redmine::Utils.relative_url_root
129 Setting.host_name = 'mydomain.foo/rdm'
129 Setting.host_name = 'mydomain.foo/rdm'
130 Setting.protocol = 'http'
130 Setting.protocol = 'http'
131 Redmine::Utils.relative_url_root = nil
131 Redmine::Utils.relative_url_root = nil
132
132
133 journal = Journal.find(3)
133 journal = Journal.find(3)
134 assert Mailer.deliver_issue_edit(journal)
134 assert Mailer.deliver_issue_edit(journal)
135
135
136 mail = last_email
136 mail = last_email
137 assert_not_nil mail
137 assert_not_nil mail
138
138
139 assert_select_email do
139 assert_select_email do
140 # link to the main ticket
140 # link to the main ticket
141 assert_select 'a[href=?]',
141 assert_select 'a[href=?]',
142 'http://mydomain.foo/rdm/issues/2#change-3',
142 'http://mydomain.foo/rdm/issues/2#change-3',
143 :text => 'Feature request #2: Add ingredients categories'
143 :text => 'Feature request #2: Add ingredients categories'
144 # link to a referenced ticket
144 # link to a referenced ticket
145 assert_select 'a[href=?][title=?]',
145 assert_select 'a[href=?][title=?]',
146 'http://mydomain.foo/rdm/issues/1',
146 'http://mydomain.foo/rdm/issues/1',
147 "#{ESCAPED_UCANT} print recipes (New)",
147 "#{ESCAPED_UCANT} print recipes (New)",
148 :text => '#1'
148 :text => '#1'
149 # link to a changeset
149 # link to a changeset
150 assert_select 'a[href=?][title=?]',
150 assert_select 'a[href=?][title=?]',
151 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
151 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
152 'This commit fixes #1, #2 and references #1 &amp; #3',
152 'This commit fixes #1, #2 and references #1 &amp; #3',
153 :text => 'r2'
153 :text => 'r2'
154 # link to a description diff
154 # link to a description diff
155 assert_select 'a[href=?][title=?]',
155 assert_select 'a[href=?][title=?]',
156 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
156 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
157 'View differences',
157 'View differences',
158 :text => 'diff'
158 :text => 'diff'
159 # link to an attachment
159 # link to an attachment
160 assert_select 'a[href=?]',
160 assert_select 'a[href=?]',
161 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
161 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
162 :text => 'source.rb'
162 :text => 'source.rb'
163 end
163 end
164 ensure
164 ensure
165 # restore it
165 # restore it
166 Redmine::Utils.relative_url_root = relative_url_root
166 Redmine::Utils.relative_url_root = relative_url_root
167 end
167 end
168
168
169 def test_email_headers
169 def test_email_headers
170 issue = Issue.find(1)
170 issue = Issue.find(1)
171 Mailer.deliver_issue_add(issue)
171 Mailer.deliver_issue_add(issue)
172 mail = last_email
172 mail = last_email
173 assert_not_nil mail
173 assert_not_nil mail
174 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
174 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
175 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
175 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
176 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
176 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
177 end
177 end
178
178
179 def test_email_headers_should_include_sender
179 def test_email_headers_should_include_sender
180 issue = Issue.find(1)
180 issue = Issue.find(1)
181 Mailer.deliver_issue_add(issue)
181 Mailer.deliver_issue_add(issue)
182 mail = last_email
182 mail = last_email
183 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
183 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
184 end
184 end
185
185
186 def test_plain_text_mail
186 def test_plain_text_mail
187 Setting.plain_text_mail = 1
187 Setting.plain_text_mail = 1
188 journal = Journal.find(2)
188 journal = Journal.find(2)
189 Mailer.deliver_issue_edit(journal)
189 Mailer.deliver_issue_edit(journal)
190 mail = last_email
190 mail = last_email
191 assert_equal "text/plain; charset=UTF-8", mail.content_type
191 assert_equal "text/plain; charset=UTF-8", mail.content_type
192 assert_equal 0, mail.parts.size
192 assert_equal 0, mail.parts.size
193 assert !mail.encoded.include?('href')
193 assert !mail.encoded.include?('href')
194 end
194 end
195
195
196 def test_html_mail
196 def test_html_mail
197 Setting.plain_text_mail = 0
197 Setting.plain_text_mail = 0
198 journal = Journal.find(2)
198 journal = Journal.find(2)
199 Mailer.deliver_issue_edit(journal)
199 Mailer.deliver_issue_edit(journal)
200 mail = last_email
200 mail = last_email
201 assert_equal 2, mail.parts.size
201 assert_equal 2, mail.parts.size
202 assert mail.encoded.include?('href')
202 assert mail.encoded.include?('href')
203 end
203 end
204
204
205 def test_from_header
205 def test_from_header
206 with_settings :mail_from => 'redmine@example.net' do
206 with_settings :mail_from => 'redmine@example.net' do
207 Mailer.test_email(User.find(1)).deliver
207 Mailer.test_email(User.find(1)).deliver
208 end
208 end
209 mail = last_email
209 mail = last_email
210 assert_equal 'redmine@example.net', mail.from_addrs.first
210 assert_equal 'redmine@example.net', mail.from_addrs.first
211 end
211 end
212
212
213 def test_from_header_with_phrase
213 def test_from_header_with_phrase
214 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
214 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
215 Mailer.test_email(User.find(1)).deliver
215 Mailer.test_email(User.find(1)).deliver
216 end
216 end
217 mail = last_email
217 mail = last_email
218 assert_equal 'redmine@example.net', mail.from_addrs.first
218 assert_equal 'redmine@example.net', mail.from_addrs.first
219 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
219 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
220 end
220 end
221
221
222 def test_should_not_send_email_without_recipient
222 def test_should_not_send_email_without_recipient
223 news = News.first
223 news = News.first
224 user = news.author
224 user = news.author
225 # Remove members except news author
225 # Remove members except news author
226 news.project.memberships.each {|m| m.destroy unless m.user == user}
226 news.project.memberships.each {|m| m.destroy unless m.user == user}
227
227
228 user.pref.no_self_notified = false
228 user.pref.no_self_notified = false
229 user.pref.save
229 user.pref.save
230 User.current = user
230 User.current = user
231 Mailer.news_added(news.reload).deliver
231 Mailer.news_added(news.reload).deliver
232 assert_equal 1, last_email.bcc.size
232 assert_equal 1, last_email.bcc.size
233
233
234 # nobody to notify
234 # nobody to notify
235 user.pref.no_self_notified = true
235 user.pref.no_self_notified = true
236 user.pref.save
236 user.pref.save
237 User.current = user
237 User.current = user
238 ActionMailer::Base.deliveries.clear
238 ActionMailer::Base.deliveries.clear
239 Mailer.news_added(news.reload).deliver
239 Mailer.news_added(news.reload).deliver
240 assert ActionMailer::Base.deliveries.empty?
240 assert ActionMailer::Base.deliveries.empty?
241 end
241 end
242
242
243 def test_issue_add_message_id
243 def test_issue_add_message_id
244 issue = Issue.find(2)
244 issue = Issue.find(2)
245 Mailer.deliver_issue_add(issue)
245 Mailer.deliver_issue_add(issue)
246 mail = last_email
246 mail = last_email
247 assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
247 assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
248 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
248 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
249 end
249 end
250
250
251 def test_issue_edit_message_id
251 def test_issue_edit_message_id
252 journal = Journal.find(3)
252 journal = Journal.find(3)
253 journal.issue = Issue.find(2)
253 journal.issue = Issue.find(2)
254
254
255 Mailer.deliver_issue_edit(journal)
255 Mailer.deliver_issue_edit(journal)
256 mail = last_email
256 mail = last_email
257 assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
257 assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
258 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
258 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
259 assert_select_email do
259 assert_select_email do
260 # link to the update
260 # link to the update
261 assert_select "a[href=?]",
261 assert_select "a[href=?]",
262 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
262 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
263 end
263 end
264 end
264 end
265
265
266 def test_message_posted_message_id
266 def test_message_posted_message_id
267 message = Message.find(1)
267 message = Message.find(1)
268 Mailer.message_posted(message).deliver
268 Mailer.message_posted(message).deliver
269 mail = last_email
269 mail = last_email
270 assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
270 assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
271 assert_include "redmine.message-1.20070512151532@example.net", mail.references
271 assert_include "redmine.message-1.20070512151532@example.net", mail.references
272 assert_select_email do
272 assert_select_email do
273 # link to the message
273 # link to the message
274 assert_select "a[href=?]",
274 assert_select "a[href=?]",
275 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
275 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
276 :text => message.subject
276 :text => message.subject
277 end
277 end
278 end
278 end
279
279
280 def test_reply_posted_message_id
280 def test_reply_posted_message_id
281 message = Message.find(3)
281 message = Message.find(3)
282 Mailer.message_posted(message).deliver
282 Mailer.message_posted(message).deliver
283 mail = last_email
283 mail = last_email
284 assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
284 assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
285 assert_include "redmine.message-1.20070512151532@example.net", mail.references
285 assert_include "redmine.message-1.20070512151532@example.net", mail.references
286 assert_select_email do
286 assert_select_email do
287 # link to the reply
287 # link to the reply
288 assert_select "a[href=?]",
288 assert_select "a[href=?]",
289 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
289 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
290 :text => message.subject
290 :text => message.subject
291 end
291 end
292 end
292 end
293
293
294 test "#issue_add should notify project members" do
294 test "#issue_add should notify project members" do
295 issue = Issue.find(1)
295 issue = Issue.find(1)
296 assert Mailer.deliver_issue_add(issue)
296 assert Mailer.deliver_issue_add(issue)
297 assert last_email.bcc.include?('dlopper@somenet.foo')
297 assert last_email.bcc.include?('dlopper@somenet.foo')
298 end
298 end
299
299
300 test "#issue_add should not notify project members that are not allow to view the issue" do
300 test "#issue_add should not notify project members that are not allow to view the issue" do
301 issue = Issue.find(1)
301 issue = Issue.find(1)
302 Role.find(2).remove_permission!(:view_issues)
302 Role.find(2).remove_permission!(:view_issues)
303 assert Mailer.deliver_issue_add(issue)
303 assert Mailer.deliver_issue_add(issue)
304 assert !last_email.bcc.include?('dlopper@somenet.foo')
304 assert !last_email.bcc.include?('dlopper@somenet.foo')
305 end
305 end
306
306
307 test "#issue_add should notify issue watchers" do
307 test "#issue_add should notify issue watchers" do
308 issue = Issue.find(1)
308 issue = Issue.find(1)
309 user = User.find(9)
309 user = User.find(9)
310 # minimal email notification options
310 # minimal email notification options
311 user.pref.no_self_notified = '1'
311 user.pref.no_self_notified = '1'
312 user.pref.save
312 user.pref.save
313 user.mail_notification = false
313 user.mail_notification = false
314 user.save
314 user.save
315
315
316 Watcher.create!(:watchable => issue, :user => user)
316 Watcher.create!(:watchable => issue, :user => user)
317 assert Mailer.deliver_issue_add(issue)
317 assert Mailer.deliver_issue_add(issue)
318 assert last_email.bcc.include?(user.mail)
318 assert last_email.bcc.include?(user.mail)
319 end
319 end
320
320
321 test "#issue_add should not notify watchers not allowed to view the issue" do
321 test "#issue_add should not notify watchers not allowed to view the issue" do
322 issue = Issue.find(1)
322 issue = Issue.find(1)
323 user = User.find(9)
323 user = User.find(9)
324 Watcher.create!(:watchable => issue, :user => user)
324 Watcher.create!(:watchable => issue, :user => user)
325 Role.non_member.remove_permission!(:view_issues)
325 Role.non_member.remove_permission!(:view_issues)
326 assert Mailer.deliver_issue_add(issue)
326 assert Mailer.deliver_issue_add(issue)
327 assert !last_email.bcc.include?(user.mail)
327 assert !last_email.bcc.include?(user.mail)
328 end
328 end
329
329
330 def test_issue_add_should_include_enabled_fields
330 def test_issue_add_should_include_enabled_fields
331 Setting.default_language = 'en'
331 Setting.default_language = 'en'
332 issue = Issue.find(2)
332 issue = Issue.find(2)
333 assert Mailer.deliver_issue_add(issue)
333 assert Mailer.deliver_issue_add(issue)
334 assert_mail_body_match '* Target version: 1.0', last_email
334 assert_mail_body_match '* Target version: 1.0', last_email
335 assert_select_email do
335 assert_select_email do
336 assert_select 'li', :text => 'Target version: 1.0'
336 assert_select 'li', :text => 'Target version: 1.0'
337 end
337 end
338 end
338 end
339
339
340 def test_issue_add_should_not_include_disabled_fields
340 def test_issue_add_should_not_include_disabled_fields
341 Setting.default_language = 'en'
341 Setting.default_language = 'en'
342 issue = Issue.find(2)
342 issue = Issue.find(2)
343 tracker = issue.tracker
343 tracker = issue.tracker
344 tracker.core_fields -= ['fixed_version_id']
344 tracker.core_fields -= ['fixed_version_id']
345 tracker.save!
345 tracker.save!
346 assert Mailer.deliver_issue_add(issue)
346 assert Mailer.deliver_issue_add(issue)
347 assert_mail_body_no_match 'Target version', last_email
347 assert_mail_body_no_match 'Target version', last_email
348 assert_select_email do
348 assert_select_email do
349 assert_select 'li', :text => /Target version/, :count => 0
349 assert_select 'li', :text => /Target version/, :count => 0
350 end
350 end
351 end
351 end
352
352
353 # test mailer methods for each language
353 # test mailer methods for each language
354 def test_issue_add
354 def test_issue_add
355 issue = Issue.find(1)
355 issue = Issue.find(1)
356 valid_languages.each do |lang|
356 valid_languages.each do |lang|
357 Setting.default_language = lang.to_s
357 Setting.default_language = lang.to_s
358 assert Mailer.deliver_issue_add(issue)
358 assert Mailer.deliver_issue_add(issue)
359 end
359 end
360 end
360 end
361
361
362 def test_issue_edit
362 def test_issue_edit
363 journal = Journal.find(1)
363 journal = Journal.find(1)
364 valid_languages.each do |lang|
364 valid_languages.each do |lang|
365 Setting.default_language = lang.to_s
365 Setting.default_language = lang.to_s
366 assert Mailer.deliver_issue_edit(journal)
366 assert Mailer.deliver_issue_edit(journal)
367 end
367 end
368 end
368 end
369
369
370 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
370 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
371 journal = Journal.find(1)
371 journal = Journal.find(1)
372 journal.private_notes = true
372 journal.private_notes = true
373 journal.save!
373 journal.save!
374
374
375 Role.find(2).add_permission! :view_private_notes
375 Role.find(2).add_permission! :view_private_notes
376 Mailer.deliver_issue_edit(journal)
376 Mailer.deliver_issue_edit(journal)
377 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
377 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
378
378
379 Role.find(2).remove_permission! :view_private_notes
379 Role.find(2).remove_permission! :view_private_notes
380 Mailer.deliver_issue_edit(journal)
380 Mailer.deliver_issue_edit(journal)
381 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
381 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
382 end
382 end
383
383
384 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
384 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
385 Issue.find(1).set_watcher(User.find_by_login('someone'))
385 Issue.find(1).set_watcher(User.find_by_login('someone'))
386 journal = Journal.find(1)
386 journal = Journal.find(1)
387 journal.private_notes = true
387 journal.private_notes = true
388 journal.save!
388 journal.save!
389
389
390 Role.non_member.add_permission! :view_private_notes
390 Role.non_member.add_permission! :view_private_notes
391 Mailer.deliver_issue_edit(journal)
391 Mailer.deliver_issue_edit(journal)
392 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
392 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
393
393
394 Role.non_member.remove_permission! :view_private_notes
394 Role.non_member.remove_permission! :view_private_notes
395 Mailer.deliver_issue_edit(journal)
395 Mailer.deliver_issue_edit(journal)
396 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
396 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
397 end
397 end
398
398
399 def test_issue_edit_should_mark_private_notes
399 def test_issue_edit_should_mark_private_notes
400 journal = Journal.find(2)
400 journal = Journal.find(2)
401 journal.private_notes = true
401 journal.private_notes = true
402 journal.save!
402 journal.save!
403
403
404 with_settings :default_language => 'en' do
404 with_settings :default_language => 'en' do
405 Mailer.deliver_issue_edit(journal)
405 Mailer.deliver_issue_edit(journal)
406 end
406 end
407 assert_mail_body_match '(Private notes)', last_email
407 assert_mail_body_match '(Private notes)', last_email
408 end
408 end
409
409
410 def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
410 def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
411 issue = Issue.generate!
411 issue = Issue.generate!
412 private_issue = Issue.generate!(:is_private => true)
412 private_issue = Issue.generate!(:is_private => true)
413 IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
413 IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
414 issue.reload
414 issue.reload
415 assert_equal 1, issue.journals.size
415 assert_equal 1, issue.journals.size
416 journal = issue.journals.first
416 journal = issue.journals.first
417 ActionMailer::Base.deliveries.clear
417 ActionMailer::Base.deliveries.clear
418
418
419 Mailer.deliver_issue_edit(journal)
419 Mailer.deliver_issue_edit(journal)
420 last_email.bcc.each do |email|
420 last_email.bcc.each do |email|
421 user = User.find_by_mail(email)
421 user = User.find_by_mail(email)
422 assert private_issue.visible?(user), "Issue was not visible to #{user}"
422 assert private_issue.visible?(user), "Issue was not visible to #{user}"
423 end
423 end
424 end
424 end
425
425
426 def test_document_added
426 def test_document_added
427 document = Document.find(1)
427 document = Document.find(1)
428 valid_languages.each do |lang|
428 valid_languages.each do |lang|
429 Setting.default_language = lang.to_s
429 Setting.default_language = lang.to_s
430 assert Mailer.document_added(document).deliver
430 assert Mailer.document_added(document).deliver
431 end
431 end
432 end
432 end
433
433
434 def test_attachments_added
434 def test_attachments_added
435 attachements = [ Attachment.find_by_container_type('Document') ]
435 attachements = [ Attachment.find_by_container_type('Document') ]
436 valid_languages.each do |lang|
436 valid_languages.each do |lang|
437 Setting.default_language = lang.to_s
437 Setting.default_language = lang.to_s
438 assert Mailer.attachments_added(attachements).deliver
438 assert Mailer.attachments_added(attachements).deliver
439 end
439 end
440 end
440 end
441
441
442 def test_version_file_added
442 def test_version_file_added
443 attachements = [ Attachment.find_by_container_type('Version') ]
443 attachements = [ Attachment.find_by_container_type('Version') ]
444 assert Mailer.attachments_added(attachements).deliver
444 assert Mailer.attachments_added(attachements).deliver
445 assert_not_nil last_email.bcc
445 assert_not_nil last_email.bcc
446 assert last_email.bcc.any?
446 assert last_email.bcc.any?
447 assert_select_email do
447 assert_select_email do
448 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
448 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
449 end
449 end
450 end
450 end
451
451
452 def test_project_file_added
452 def test_project_file_added
453 attachements = [ Attachment.find_by_container_type('Project') ]
453 attachements = [ Attachment.find_by_container_type('Project') ]
454 assert Mailer.attachments_added(attachements).deliver
454 assert Mailer.attachments_added(attachements).deliver
455 assert_not_nil last_email.bcc
455 assert_not_nil last_email.bcc
456 assert last_email.bcc.any?
456 assert last_email.bcc.any?
457 assert_select_email do
457 assert_select_email do
458 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
458 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
459 end
459 end
460 end
460 end
461
461
462 def test_news_added
462 def test_news_added
463 news = News.first
463 news = News.first
464 valid_languages.each do |lang|
464 valid_languages.each do |lang|
465 Setting.default_language = lang.to_s
465 Setting.default_language = lang.to_s
466 assert Mailer.news_added(news).deliver
466 assert Mailer.news_added(news).deliver
467 end
467 end
468 end
468 end
469
469
470 def test_news_added_should_notify_project_news_watchers
470 def test_news_added_should_notify_project_news_watchers
471 user1 = User.generate!
471 user1 = User.generate!
472 user2 = User.generate!
472 user2 = User.generate!
473 news = News.find(1)
473 news = News.find(1)
474 news.project.enabled_module('news').add_watcher(user1)
474 news.project.enabled_module('news').add_watcher(user1)
475
475
476 Mailer.news_added(news).deliver
476 Mailer.news_added(news).deliver
477 assert_include user1.mail, last_email.bcc
477 assert_include user1.mail, last_email.bcc
478 assert_not_include user2.mail, last_email.bcc
478 assert_not_include user2.mail, last_email.bcc
479 end
479 end
480
480
481 def test_news_comment_added
481 def test_news_comment_added
482 comment = Comment.find(2)
482 comment = Comment.find(2)
483 valid_languages.each do |lang|
483 valid_languages.each do |lang|
484 Setting.default_language = lang.to_s
484 Setting.default_language = lang.to_s
485 assert Mailer.news_comment_added(comment).deliver
485 assert Mailer.news_comment_added(comment).deliver
486 end
486 end
487 end
487 end
488
488
489 def test_message_posted
489 def test_message_posted
490 message = Message.first
490 message = Message.first
491 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
491 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
492 recipients = recipients.compact.uniq
492 recipients = recipients.compact.uniq
493 valid_languages.each do |lang|
493 valid_languages.each do |lang|
494 Setting.default_language = lang.to_s
494 Setting.default_language = lang.to_s
495 assert Mailer.message_posted(message).deliver
495 assert Mailer.message_posted(message).deliver
496 end
496 end
497 end
497 end
498
498
499 def test_wiki_content_added
499 def test_wiki_content_added
500 content = WikiContent.find(1)
500 content = WikiContent.find(1)
501 valid_languages.each do |lang|
501 valid_languages.each do |lang|
502 Setting.default_language = lang.to_s
502 Setting.default_language = lang.to_s
503 assert_difference 'ActionMailer::Base.deliveries.size' do
503 assert_difference 'ActionMailer::Base.deliveries.size' do
504 assert Mailer.wiki_content_added(content).deliver
504 assert Mailer.wiki_content_added(content).deliver
505 assert_select_email do
505 assert_select_email do
506 assert_select 'a[href=?]',
506 assert_select 'a[href=?]',
507 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
507 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
508 :text => 'CookBook documentation'
508 :text => 'CookBook documentation'
509 end
509 end
510 end
510 end
511 end
511 end
512 end
512 end
513
513
514 def test_wiki_content_updated
514 def test_wiki_content_updated
515 content = WikiContent.find(1)
515 content = WikiContent.find(1)
516 valid_languages.each do |lang|
516 valid_languages.each do |lang|
517 Setting.default_language = lang.to_s
517 Setting.default_language = lang.to_s
518 assert_difference 'ActionMailer::Base.deliveries.size' do
518 assert_difference 'ActionMailer::Base.deliveries.size' do
519 assert Mailer.wiki_content_updated(content).deliver
519 assert Mailer.wiki_content_updated(content).deliver
520 assert_select_email do
520 assert_select_email do
521 assert_select 'a[href=?]',
521 assert_select 'a[href=?]',
522 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
522 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
523 :text => 'CookBook documentation'
523 :text => 'CookBook documentation'
524 end
524 end
525 end
525 end
526 end
526 end
527 end
527 end
528
528
529 def test_account_information
529 def test_account_information
530 user = User.find(2)
530 user = User.find(2)
531 valid_languages.each do |lang|
531 valid_languages.each do |lang|
532 user.update_attribute :language, lang.to_s
532 user.update_attribute :language, lang.to_s
533 user.reload
533 user.reload
534 assert Mailer.account_information(user, 'pAsswORd').deliver
534 assert Mailer.account_information(user, 'pAsswORd').deliver
535 end
535 end
536 end
536 end
537
537
538 def test_lost_password
538 def test_lost_password
539 token = Token.find(2)
539 token = Token.find(2)
540 valid_languages.each do |lang|
540 valid_languages.each do |lang|
541 token.user.update_attribute :language, lang.to_s
541 token.user.update_attribute :language, lang.to_s
542 token.reload
542 token.reload
543 assert Mailer.lost_password(token).deliver
543 assert Mailer.lost_password(token).deliver
544 end
544 end
545 end
545 end
546
546
547 def test_register
547 def test_register
548 token = Token.find(1)
548 token = Token.find(1)
549 Setting.host_name = 'redmine.foo'
549 Setting.host_name = 'redmine.foo'
550 Setting.protocol = 'https'
550 Setting.protocol = 'https'
551
551
552 valid_languages.each do |lang|
552 valid_languages.each do |lang|
553 token.user.update_attribute :language, lang.to_s
553 token.user.update_attribute :language, lang.to_s
554 token.reload
554 token.reload
555 ActionMailer::Base.deliveries.clear
555 ActionMailer::Base.deliveries.clear
556 assert Mailer.register(token).deliver
556 assert Mailer.register(token).deliver
557 mail = last_email
557 mail = last_email
558 assert_select_email do
558 assert_select_email do
559 assert_select "a[href=?]",
559 assert_select "a[href=?]",
560 "https://redmine.foo/account/activate?token=#{token.value}",
560 "https://redmine.foo/account/activate?token=#{token.value}",
561 :text => "https://redmine.foo/account/activate?token=#{token.value}"
561 :text => "https://redmine.foo/account/activate?token=#{token.value}"
562 end
562 end
563 end
563 end
564 end
564 end
565
565
566 def test_test
566 def test_test
567 user = User.find(1)
567 user = User.find(1)
568 valid_languages.each do |lang|
568 valid_languages.each do |lang|
569 user.update_attribute :language, lang.to_s
569 user.update_attribute :language, lang.to_s
570 assert Mailer.test_email(user).deliver
570 assert Mailer.test_email(user).deliver
571 end
571 end
572 end
572 end
573
573
574 def test_reminders
574 def test_reminders
575 Mailer.reminders(:days => 42)
575 Mailer.reminders(:days => 42)
576 assert_equal 1, ActionMailer::Base.deliveries.size
576 assert_equal 1, ActionMailer::Base.deliveries.size
577 mail = last_email
577 mail = last_email
578 assert mail.bcc.include?('dlopper@somenet.foo')
578 assert mail.bcc.include?('dlopper@somenet.foo')
579 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
579 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
580 assert_equal '1 issue(s) due in the next 42 days', mail.subject
580 assert_equal '1 issue(s) due in the next 42 days', mail.subject
581 end
581 end
582
582
583 def test_reminders_should_not_include_closed_issues
583 def test_reminders_should_not_include_closed_issues
584 with_settings :default_language => 'en' do
584 with_settings :default_language => 'en' do
585 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
585 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
586 :subject => 'Closed issue', :assigned_to_id => 3,
586 :subject => 'Closed issue', :assigned_to_id => 3,
587 :due_date => 5.days.from_now,
587 :due_date => 5.days.from_now,
588 :author_id => 2)
588 :author_id => 2)
589 ActionMailer::Base.deliveries.clear
589 ActionMailer::Base.deliveries.clear
590
590
591 Mailer.reminders(:days => 42)
591 Mailer.reminders(:days => 42)
592 assert_equal 1, ActionMailer::Base.deliveries.size
592 assert_equal 1, ActionMailer::Base.deliveries.size
593 mail = last_email
593 mail = last_email
594 assert mail.bcc.include?('dlopper@somenet.foo')
594 assert mail.bcc.include?('dlopper@somenet.foo')
595 assert_mail_body_no_match 'Closed issue', mail
595 assert_mail_body_no_match 'Closed issue', mail
596 end
596 end
597 end
597 end
598
598
599 def test_reminders_for_users
599 def test_reminders_for_users
600 Mailer.reminders(:days => 42, :users => ['5'])
600 Mailer.reminders(:days => 42, :users => ['5'])
601 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
601 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
602 Mailer.reminders(:days => 42, :users => ['3'])
602 Mailer.reminders(:days => 42, :users => ['3'])
603 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
603 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
604 mail = last_email
604 mail = last_email
605 assert mail.bcc.include?('dlopper@somenet.foo')
605 assert mail.bcc.include?('dlopper@somenet.foo')
606 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
606 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
607 end
607 end
608
608
609 def test_reminder_should_include_issues_assigned_to_groups
609 def test_reminder_should_include_issues_assigned_to_groups
610 with_settings :default_language => 'en' do
610 with_settings :default_language => 'en' do
611 group = Group.generate!
611 group = Group.generate!
612 group.users << User.find(2)
612 group.users << User.find(2)
613 group.users << User.find(3)
613 group.users << User.find(3)
614
614
615 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
615 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
616 :subject => 'Assigned to group', :assigned_to => group,
616 :subject => 'Assigned to group', :assigned_to => group,
617 :due_date => 5.days.from_now,
617 :due_date => 5.days.from_now,
618 :author_id => 2)
618 :author_id => 2)
619 ActionMailer::Base.deliveries.clear
619 ActionMailer::Base.deliveries.clear
620
620
621 Mailer.reminders(:days => 7)
621 Mailer.reminders(:days => 7)
622 assert_equal 2, ActionMailer::Base.deliveries.size
622 assert_equal 2, ActionMailer::Base.deliveries.size
623 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
623 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
624 ActionMailer::Base.deliveries.each do |mail|
624 ActionMailer::Base.deliveries.each do |mail|
625 assert_mail_body_match 'Assigned to group', mail
625 assert_mail_body_match 'Assigned to group', mail
626 end
626 end
627 end
627 end
628 end
628 end
629
629
630 def test_mailer_should_not_change_locale
630 def test_mailer_should_not_change_locale
631 Setting.default_language = 'en'
631 Setting.default_language = 'en'
632 # Set current language to italian
632 # Set current language to italian
633 set_language_if_valid 'it'
633 set_language_if_valid 'it'
634 # Send an email to a french user
634 # Send an email to a french user
635 user = User.find(1)
635 user = User.find(1)
636 user.language = 'fr'
636 user.language = 'fr'
637 Mailer.account_activated(user).deliver
637 Mailer.account_activated(user).deliver
638 mail = last_email
638 mail = last_email
639 assert_mail_body_match 'Votre compte', mail
639 assert_mail_body_match 'Votre compte', mail
640
640
641 assert_equal :it, current_language
641 assert_equal :it, current_language
642 end
642 end
643
643
644 def test_with_deliveries_off
644 def test_with_deliveries_off
645 Mailer.with_deliveries false do
645 Mailer.with_deliveries false do
646 Mailer.test_email(User.find(1)).deliver
646 Mailer.test_email(User.find(1)).deliver
647 end
647 end
648 assert ActionMailer::Base.deliveries.empty?
648 assert ActionMailer::Base.deliveries.empty?
649 # should restore perform_deliveries
649 # should restore perform_deliveries
650 assert ActionMailer::Base.perform_deliveries
650 assert ActionMailer::Base.perform_deliveries
651 end
651 end
652
652
653 def test_token_for_should_strip_trailing_gt_from_address_with_full_name
654 with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
655 assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
656 end
657 end
658
653 def test_layout_should_include_the_emails_header
659 def test_layout_should_include_the_emails_header
654 with_settings :emails_header => "*Header content*" do
660 with_settings :emails_header => "*Header content*" do
655 with_settings :plain_text_mail => 0 do
661 with_settings :plain_text_mail => 0 do
656 assert Mailer.test_email(User.find(1)).deliver
662 assert Mailer.test_email(User.find(1)).deliver
657 assert_select_email do
663 assert_select_email do
658 assert_select ".header" do
664 assert_select ".header" do
659 assert_select "strong", :text => "Header content"
665 assert_select "strong", :text => "Header content"
660 end
666 end
661 end
667 end
662 end
668 end
663 with_settings :plain_text_mail => 1 do
669 with_settings :plain_text_mail => 1 do
664 assert Mailer.test_email(User.find(1)).deliver
670 assert Mailer.test_email(User.find(1)).deliver
665 mail = last_email
671 mail = last_email
666 assert_not_nil mail
672 assert_not_nil mail
667 assert_include "*Header content*", mail.body.decoded
673 assert_include "*Header content*", mail.body.decoded
668 end
674 end
669 end
675 end
670 end
676 end
671
677
672 def test_layout_should_not_include_empty_emails_header
678 def test_layout_should_not_include_empty_emails_header
673 with_settings :emails_header => "", :plain_text_mail => 0 do
679 with_settings :emails_header => "", :plain_text_mail => 0 do
674 assert Mailer.test_email(User.find(1)).deliver
680 assert Mailer.test_email(User.find(1)).deliver
675 assert_select_email do
681 assert_select_email do
676 assert_select ".header", false
682 assert_select ".header", false
677 end
683 end
678 end
684 end
679 end
685 end
680
686
681 def test_layout_should_include_the_emails_footer
687 def test_layout_should_include_the_emails_footer
682 with_settings :emails_footer => "*Footer content*" do
688 with_settings :emails_footer => "*Footer content*" do
683 with_settings :plain_text_mail => 0 do
689 with_settings :plain_text_mail => 0 do
684 assert Mailer.test_email(User.find(1)).deliver
690 assert Mailer.test_email(User.find(1)).deliver
685 assert_select_email do
691 assert_select_email do
686 assert_select ".footer" do
692 assert_select ".footer" do
687 assert_select "strong", :text => "Footer content"
693 assert_select "strong", :text => "Footer content"
688 end
694 end
689 end
695 end
690 end
696 end
691 with_settings :plain_text_mail => 1 do
697 with_settings :plain_text_mail => 1 do
692 assert Mailer.test_email(User.find(1)).deliver
698 assert Mailer.test_email(User.find(1)).deliver
693 mail = last_email
699 mail = last_email
694 assert_not_nil mail
700 assert_not_nil mail
695 assert_include "\n-- \n", mail.body.decoded
701 assert_include "\n-- \n", mail.body.decoded
696 assert_include "*Footer content*", mail.body.decoded
702 assert_include "*Footer content*", mail.body.decoded
697 end
703 end
698 end
704 end
699 end
705 end
700
706
701 def test_layout_should_not_include_empty_emails_footer
707 def test_layout_should_not_include_empty_emails_footer
702 with_settings :emails_footer => "" do
708 with_settings :emails_footer => "" do
703 with_settings :plain_text_mail => 0 do
709 with_settings :plain_text_mail => 0 do
704 assert Mailer.test_email(User.find(1)).deliver
710 assert Mailer.test_email(User.find(1)).deliver
705 assert_select_email do
711 assert_select_email do
706 assert_select ".footer", false
712 assert_select ".footer", false
707 end
713 end
708 end
714 end
709 with_settings :plain_text_mail => 1 do
715 with_settings :plain_text_mail => 1 do
710 assert Mailer.test_email(User.find(1)).deliver
716 assert Mailer.test_email(User.find(1)).deliver
711 mail = last_email
717 mail = last_email
712 assert_not_nil mail
718 assert_not_nil mail
713 assert_not_include "\n-- \n", mail.body.decoded
719 assert_not_include "\n-- \n", mail.body.decoded
714 end
720 end
715 end
721 end
716 end
722 end
717
723
718 def test_should_escape_html_templates_only
724 def test_should_escape_html_templates_only
719 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
725 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
720 mail = last_email
726 mail = last_email
721 assert_equal 2, mail.parts.size
727 assert_equal 2, mail.parts.size
722 assert_include '<tag>', text_part.body.encoded
728 assert_include '<tag>', text_part.body.encoded
723 assert_include '&lt;tag&gt;', html_part.body.encoded
729 assert_include '&lt;tag&gt;', html_part.body.encoded
724 end
730 end
725
731
726 def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
732 def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
727 mail = Mailer.test_email(User.find(1))
733 mail = Mailer.test_email(User.find(1))
728 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
734 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
729
735
730 ActionMailer::Base.raise_delivery_errors = true
736 ActionMailer::Base.raise_delivery_errors = true
731 assert_raise Exception, "delivery error" do
737 assert_raise Exception, "delivery error" do
732 mail.deliver
738 mail.deliver
733 end
739 end
734 ensure
740 ensure
735 ActionMailer::Base.raise_delivery_errors = false
741 ActionMailer::Base.raise_delivery_errors = false
736 end
742 end
737
743
738 def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
744 def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
739 mail = Mailer.test_email(User.find(1))
745 mail = Mailer.test_email(User.find(1))
740 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
746 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
741
747
742 Rails.logger.expects(:error).with("Email delivery error: delivery error")
748 Rails.logger.expects(:error).with("Email delivery error: delivery error")
743 ActionMailer::Base.raise_delivery_errors = false
749 ActionMailer::Base.raise_delivery_errors = false
744 assert_nothing_raised do
750 assert_nothing_raised do
745 mail.deliver
751 mail.deliver
746 end
752 end
747 end
753 end
748
754
749 def test_mail_should_return_a_mail_message
755 def test_mail_should_return_a_mail_message
750 assert_kind_of ::Mail::Message, Mailer.test_email(User.find(1))
756 assert_kind_of ::Mail::Message, Mailer.test_email(User.find(1))
751 end
757 end
752
758
753 private
759 private
754
760
755 def last_email
761 def last_email
756 mail = ActionMailer::Base.deliveries.last
762 mail = ActionMailer::Base.deliveries.last
757 assert_not_nil mail
763 assert_not_nil mail
758 mail
764 mail
759 end
765 end
760
766
761 def text_part
767 def text_part
762 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
768 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
763 end
769 end
764
770
765 def html_part
771 def html_part
766 last_email.parts.detect {|part| part.content_type.include?('text/html')}
772 last_email.parts.detect {|part| part.content_type.include?('text/html')}
767 end
773 end
768 end
774 end
General Comments 0
You need to be logged in to leave comments. Login now