parent
6a2d7a5289
commit
e6161b15af
@ -0,0 +1,98 @@ |
||||
class UserMailer < ActionMailer::Base |
||||
helper :application # textilizable |
||||
|
||||
default :from => "from@example.com" |
||||
|
||||
# Subject can be set in your I18n file at config/locales/en.yml |
||||
# with the following lookup: |
||||
# |
||||
# en.user_mailer.test_mail.subject |
||||
# |
||||
def test_mail(user) |
||||
@greeting = "Hi" |
||||
|
||||
to = "#{user.name} <#{user.mail}>" |
||||
|
||||
mail :to => to, :subject => 'Test' |
||||
end |
||||
|
||||
def issue_added(user, issue) |
||||
@issue = issue |
||||
@user = user |
||||
|
||||
headers["X-OpenProject-Project"] = issue.project.identifier |
||||
headers["X-OpenProject-Issue-Id"] = issue.id |
||||
headers["X-OpenProject-Issue-Author"] = issue.author.login |
||||
headers["X-OpenProject-Type"] = 'Issue' |
||||
|
||||
assigned_to_header issue.assigned_to |
||||
|
||||
#message_id issue |
||||
|
||||
to = user.mail |
||||
|
||||
locale = user.language.presence || I18n.default_locale # || Setting.default_language |
||||
|
||||
I18n.with_locale(locale) do |
||||
subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" |
||||
|
||||
mail :to => to, :subject => subject |
||||
end |
||||
end |
||||
|
||||
def issue_updated(user, journal) |
||||
@user = user |
||||
@journal = journal |
||||
@issue = journal.journaled.reload |
||||
|
||||
headers["X-OpenProject-Project"] = @issue.project.identifier |
||||
headers["X-OpenProject-Issue-Id"] = @issue.id |
||||
headers["X-OpenProject-Issue-Author"] = @issue.author.login |
||||
headers["X-OpenProject-Type"] = 'Issue' |
||||
|
||||
assigned_to_header @issue.assigned_to |
||||
|
||||
to = user.mail |
||||
|
||||
#message_id journal |
||||
#references issue |
||||
#@author = journal.user |
||||
|
||||
locale = user.language.presence || I18n.default_locale |
||||
|
||||
I18n.with_locale(locale) do |
||||
subject = "[#{@issue.project.name} - #{@issue.tracker.name} ##{@issue.id}] " |
||||
subject << "(#{@issue.status.name}) " if journal.details['status_id'] |
||||
subject << @issue.subject |
||||
|
||||
mail :to => to, :subject => subject |
||||
end |
||||
end |
||||
|
||||
def lost_password(token) |
||||
@token = token |
||||
@url = url_for(:controller => :account, |
||||
:action => :lost_password, |
||||
:token => @token.value) |
||||
|
||||
headers["X-OpenProject-Type"] = 'Account' |
||||
|
||||
user = token.user |
||||
|
||||
to = user.mail |
||||
|
||||
locale = user.language.presence || I18n.default_locale |
||||
|
||||
I18n.with_locale(locale) do |
||||
subject = t(:mail_subject_lost_password, :value => Setting.app_title) |
||||
|
||||
mail :to => to, :subject => subject |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def assigned_to_header(user) |
||||
headers["X-OpenProject-Issue-Assignee"] = user.login if user |
||||
end |
||||
end |
@ -1,3 +0,0 @@ |
||||
<%= l(:text_issue_added, :id => "##{@issue.id}", :author => h(@issue.author)) %> |
||||
<hr /> |
||||
<%= render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
@ -1,4 +0,0 @@ |
||||
<%= l(:text_issue_added, :id => "##{@issue.id}", :author => @issue.author) %> |
||||
|
||||
---------------------------------------- |
||||
<%= render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
@ -1,11 +0,0 @@ |
||||
<%= l(:text_issue_updated, :id => "##{@issue.id}", :author => h(@journal.user)) %> |
||||
|
||||
<ul> |
||||
<% for detail in @journal.details %> |
||||
<li><%= @journal.render_detail(detail, true) %></li> |
||||
<% end %> |
||||
</ul> |
||||
|
||||
<%= textilizable(@journal, :notes, :only_path => false) %> |
||||
<hr /> |
||||
<%= render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
@ -1,9 +0,0 @@ |
||||
<%= l(:text_issue_updated, :id => "##{@issue.id}", :author => @journal.user) %> |
||||
|
||||
<% for detail in @journal.details -%> |
||||
<%= @journal.render_detail(detail, true) %> |
||||
<% end -%> |
||||
|
||||
<%= @journal.notes if @journal.notes? %> |
||||
---------------------------------------- |
||||
<%= render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
@ -1,4 +0,0 @@ |
||||
<p><%= l(:mail_body_lost_password) %><br /> |
||||
<%= auto_link(@url) %></p> |
||||
|
||||
<p><%= l(:field_login) %>: <b><%=h @token.user.login %></b></p> |
@ -1,4 +0,0 @@ |
||||
<%= l(:mail_body_lost_password) %> |
||||
<%= @url %> |
||||
|
||||
<%= l(:field_login) %>: <%= @token.user.login %> |
@ -0,0 +1,21 @@ |
||||
<%= t(:text_issue_added, :id => "##{@issue.id}", :author => @issue.author) %> |
||||
<hr /> |
||||
<% #render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
||||
|
||||
<h1><%= link_to("#{@issue.tracker.name} ##{@issue.id}: #{@issue.subject}", issue_url(@issue)) %></h1> |
||||
|
||||
<ul> |
||||
<li><%= t(:field_author) %>: <%= @issue.author %></li> |
||||
<li><%= t(:field_status) %>: <%= @issue.status %></li> |
||||
<li><%= t(:field_priority) %>: <%= @issue.priority %></li> |
||||
<li><%= t(:field_assigned_to) %>: <%= @issue.assigned_to %></li> |
||||
<li><%= t(:field_category) %>: <%= @issue.category %></li> |
||||
<li><%= t(:field_fixed_version) %>: <%= @issue.fixed_version %></li> |
||||
|
||||
<% @issue.custom_field_values.each do |value| %> |
||||
<li><%= value.custom_field.name %>: <%= show_value(value) %></li> |
||||
<% end %> |
||||
</ul> |
||||
|
||||
<%= textilizable(@issue.description) %> |
||||
|
@ -0,0 +1,20 @@ |
||||
<%= t(:text_issue_added, :id => "##{@issue.id}", :author => @issue.author) %> |
||||
|
||||
---------------------------------------- |
||||
<% #render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
||||
|
||||
<%= "#{@issue.tracker.name} ##{@issue.id}: #{@issue.subject}" %> |
||||
<%= issue_url(@issue) %> |
||||
|
||||
<%= t(:field_author) %>: <%= @issue.author %> |
||||
<%= t(:field_status) %>: <%= @issue.status %> |
||||
<%= t(:field_priority) %>: <%= @issue.priority %> |
||||
<%= t(:field_assigned_to) %>: <%= @issue.assigned_to %> |
||||
<%= t(:field_category) %>: <%= @issue.category %> |
||||
<%= t(:field_fixed_version) %>: <%= @issue.fixed_version %> |
||||
|
||||
<% @issue.custom_field_values.each do |value| %> |
||||
<%= value.custom_field.name %>: <%= show_value(value) %> |
||||
<% end %> |
||||
|
||||
<%= @issue.description %> |
@ -0,0 +1,29 @@ |
||||
<%= t(:text_issue_updated, :id => "##{@issue.id}", :author => @journal.user) %> |
||||
|
||||
<ul> |
||||
<% @journal.details.each do |detail| %> |
||||
<li><%= @journal.render_detail(detail, true) %></li> |
||||
<% end %> |
||||
</ul> |
||||
|
||||
<%= textilizable(@journal.notes, :only_path => false) %> |
||||
<hr /> |
||||
<%# render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
||||
|
||||
<h1><%= link_to("#{@issue.tracker.name} ##{@issue.id}: #{@issue.subject}", issue_url(@issue)) %></h1> |
||||
|
||||
<ul> |
||||
<li><%= t(:field_author) %>: <%= @issue.author %></li> |
||||
<li><%= t(:field_status) %>: <%= @issue.status %></li> |
||||
<li><%= t(:field_priority) %>: <%= @issue.priority %></li> |
||||
<li><%= t(:field_assigned_to) %>: <%= @issue.assigned_to %></li> |
||||
<li><%= t(:field_category) %>: <%= @issue.category %></li> |
||||
<li><%= t(:field_fixed_version) %>: <%= @issue.fixed_version %></li> |
||||
|
||||
<% @issue.custom_field_values.each do |value| %> |
||||
<li><%= value.custom_field.name %>: <%= show_value(value) %></li> |
||||
<% end %> |
||||
</ul> |
||||
|
||||
<%= textilizable(@issue.description) %> |
||||
|
@ -0,0 +1,25 @@ |
||||
<%= t(:text_issue_updated, :id => "##{@issue.id}", :author => @journal.user) %> |
||||
|
||||
<% @journal.details.each do |detail| %> |
||||
<%= @journal.render_detail(detail, true) %> |
||||
<% end %> |
||||
|
||||
<%= @journal.notes if @journal.notes? %> |
||||
---------------------------------------- |
||||
<% # render :partial => "issue_text", :locals => { :issue => @issue, :issue_url => @issue_url } %> |
||||
|
||||
<%= "#{@issue.tracker.name} ##{@issue.id}: #{@issue.subject}" %> |
||||
<%= issue_url(@issue) %> |
||||
|
||||
<%= t(:field_author) %>: <%= @issue.author %> |
||||
<%= t(:field_status) %>: <%= @issue.status %> |
||||
<%= t(:field_priority) %>: <%= @issue.priority %> |
||||
<%= t(:field_assigned_to) %>: <%= @issue.assigned_to %> |
||||
<%= t(:field_category) %>: <%= @issue.category %> |
||||
<%= t(:field_fixed_version) %>: <%= @issue.fixed_version %> |
||||
|
||||
<% @issue.custom_field_values.each do |value| %> |
||||
<%= value.custom_field.name %>: <%= show_value(value) %> |
||||
<% end %> |
||||
|
||||
<%= @issue.description %> |
@ -0,0 +1,4 @@ |
||||
<p><%= t(:mail_body_lost_password) %><br /> |
||||
<%= link_to(@url, @url) %></p> |
||||
|
||||
<p><%= t(:field_login) %>: <b><%= @token.user.login %></b></p> |
@ -0,0 +1,4 @@ |
||||
<%= t(:mail_body_lost_password) %> |
||||
<%= @url %> |
||||
|
||||
<%= t(:field_login) %>: <%= @token.user.login %> |
@ -0,0 +1,152 @@ |
||||
require File.expand_path('../../test_helper', __FILE__) |
||||
|
||||
class UserMailerTest < ActionMailer::TestCase |
||||
#include Redmine::I18n |
||||
|
||||
setup do |
||||
User.delete_all |
||||
Issue.delete_all |
||||
Project.delete_all |
||||
Tracker.delete_all |
||||
ActionMailer::Base.deliveries.clear |
||||
end |
||||
|
||||
test 'test email sends a simple greeting to the given user' do |
||||
user = FactoryGirl.create(:user, :mail => 'foo@bar.de', :language => :de) |
||||
|
||||
mail = UserMailer.test_mail(user) |
||||
mail.deliver |
||||
|
||||
assert_equal 1, ActionMailer::Base.deliveries.size |
||||
|
||||
assert_equal "Test", mail.subject |
||||
assert_equal ['foo@bar.de'], mail.to |
||||
assert_equal ['from@example.com'], mail.from |
||||
assert_match "Hi", mail.body.encoded |
||||
end |
||||
|
||||
test "issue added" do |
||||
user = FactoryGirl.create(:user) |
||||
issue = FactoryGirl.create(:issue) |
||||
|
||||
# creating an issue actually sends an email, wow |
||||
ActionMailer::Base.deliveries.clear |
||||
|
||||
mail = UserMailer.issue_added(user, issue) |
||||
mail.deliver |
||||
|
||||
assert_equal 1, ActionMailer::Base.deliveries.size |
||||
|
||||
# todo |
||||
# assert_equal "Test", mail.subject |
||||
# assert_equal ['foo@bar.de'], mail.to |
||||
# assert_equal ['from@example.com'], mail.from |
||||
# assert_match "Hi", mail.body.encoded |
||||
end |
||||
|
||||
test 'test_email_headers' do |
||||
user = FactoryGirl.create(:user) |
||||
issue = FactoryGirl.create(:issue) |
||||
|
||||
mail = UserMailer.issue_added(user, issue) |
||||
mail.deliver |
||||
|
||||
assert_not_nil mail |
||||
assert_equal 'bulk', mail.header_string('Precedence') |
||||
assert_equal 'auto-generated', mail.header_string('Auto-Submitted') |
||||
end |
||||
|
||||
test 'test_issue_add_message_id' do |
||||
user = FactoryGirl.create(:user) |
||||
issue = FactoryGirl.create(:issue) |
||||
|
||||
mail = UserMailer.issue_added(user, issue) |
||||
mail.deliver |
||||
|
||||
assert_not_nil mail |
||||
assert_equal UserMailer.message_id_for(issue), mail.message_id |
||||
assert_nil mail.references |
||||
end |
||||
|
||||
test 'test_issue_add' do |
||||
user = FactoryGirl.create(:user) |
||||
issue = FactoryGirl.create(:issue) |
||||
|
||||
assert UserMailer.issue_added(user, issue).deliver |
||||
end |
||||
|
||||
test 'test_issue_edit' do |
||||
user = FactoryGirl.create(:user) |
||||
journal = Journal.find(1) |
||||
assert UserMailer.issue_updated(user, journal).deliver |
||||
end |
||||
|
||||
def test_lost_password |
||||
token = FactoryGirl.create(:token) |
||||
assert UserMailer.lost_password(token).deliver |
||||
end |
||||
|
||||
|
||||
|
||||
context("#issue_add") do |
||||
setup do |
||||
ActionMailer::Base.deliveries.clear |
||||
Setting.bcc_recipients = '1' |
||||
@user = FactoryGirl.create(:user, :mail => 'foo@bar.de') |
||||
@issue = FactoryGirl.create(:issue) |
||||
end |
||||
|
||||
should "send one email per recipient" do |
||||
assert UserMailer.issue_added(@user, @issue).deliver |
||||
assert_equal 1, ActionMailer::Base.deliveries.size |
||||
assert_equal ['foo@bar.de'], last_email.to |
||||
end |
||||
|
||||
should "change mail language depending on recipient language" do |
||||
Setting.stubs(:available_languages).returns(['en', 'de']) |
||||
set_language_if_valid 'en' |
||||
|
||||
@user.language = 'de' |
||||
|
||||
assert UserMailer.issue_added(@user, @issue).deliver |
||||
assert_equal 1, ActionMailer::Base.deliveries.size |
||||
|
||||
mail = last_email |
||||
assert_equal ['foo@bar.de'], mail.to |
||||
assert mail.body.include?('erstellt') |
||||
assert !mail.body.include?('reported') |
||||
assert_equal :en, current_language |
||||
end |
||||
|
||||
should "falls back to default language if user has no language" do |
||||
# 1. user's language |
||||
# 2. Setting.default_language |
||||
# 3. :en |
||||
|
||||
Setting.stubs(:available_languages).returns(['en', 'de', 'fr']) |
||||
set_language_if_valid 'fr' |
||||
|
||||
Setting.default_language = 'de' |
||||
|
||||
@user.language = '' # (auto) |
||||
|
||||
assert UserMailer.issue_added(@user, @issue).deliver |
||||
assert_equal 1, ActionMailer::Base.deliveries.size |
||||
|
||||
mail = last_email |
||||
assert_equal ['foo@bar.de'], mail.to |
||||
assert !mail.body.include?('reported') |
||||
assert mail.body.include?('erstellt') |
||||
assert_equal :fr, current_language |
||||
end |
||||
end |
||||
|
||||
def last_email |
||||
mail = ActionMailer::Base.deliveries.last |
||||
assert_not_nil mail |
||||
mail |
||||
end |
||||
|
||||
|
||||
|
||||
end |
Loading…
Reference in new issue