Commit fa6ea4bd0e7abd2b618e8ab1430222f34948b8a5

Authored by Victor Costa
2 parents f092d7d1 9fef020f

Merge branch 'email_template' into production

Conflicts:
	app/views/admin_panel/index.html.erb
app/controllers/admin/environment_email_templates_controller.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class EnvironmentEmailTemplatesController < EmailTemplatesController
  2 +
  3 + protect 'manage_email_templates', :environment
  4 +
  5 + protected
  6 +
  7 + def owner
  8 + environment
  9 + end
  10 +
  11 + before_filter :only => :index do
  12 + @back_to = url_for(:controller => :admin_panel)
  13 + end
  14 +
  15 +end
... ...
app/controllers/email_templates_controller.rb 0 → 100644
... ... @@ -0,0 +1,62 @@
  1 +class EmailTemplatesController < ApplicationController
  2 +
  3 + def index
  4 + @email_templates = owner.email_templates
  5 + end
  6 +
  7 + def show
  8 + @email_template = owner.email_templates.find(params[:id])
  9 +
  10 + respond_to do |format|
  11 + format.html # show.html.erb
  12 + format.json { render json: @email_template }
  13 + end
  14 + end
  15 +
  16 + def show_parsed
  17 + @email_template = owner.email_templates.find(params[:id])
  18 + template_params = {:profile => owner, :environment => environment}
  19 + render json: {:parsed_body => @email_template.parsed_body(template_params), :parsed_subject => @email_template.parsed_subject(template_params)}
  20 + end
  21 +
  22 + def new
  23 + @email_template = owner.email_templates.build(:owner => owner)
  24 + end
  25 +
  26 + def edit
  27 + @email_template = owner.email_templates.find(params[:id])
  28 + end
  29 +
  30 + def create
  31 + @email_template = owner.email_templates.build(params[:email_template])
  32 + @email_template.owner = owner
  33 +
  34 + if @email_template.save
  35 + session[:notice] = _('Email template was successfully created.')
  36 + redirect_to url_for(:action => :index)
  37 + else
  38 + render action: "new"
  39 + end
  40 + end
  41 +
  42 + def update
  43 + @email_template = owner.email_templates.find(params[:id])
  44 +
  45 + if @email_template.update_attributes(params[:email_template])
  46 + session[:notice] = _('Email template was successfully updated.')
  47 + redirect_to url_for(:action => :index)
  48 + else
  49 + render action: "edit"
  50 + end
  51 + end
  52 +
  53 + def destroy
  54 + @email_template = owner.email_templates.find(params[:id])
  55 + @email_template.destroy
  56 +
  57 + respond_to do |format|
  58 + format.html { redirect_to url_for(:action => :index)}
  59 + format.json { head :no_content }
  60 + end
  61 + end
  62 +end
... ...
app/controllers/my_profile/email_templates_controller.rb
... ... @@ -1,63 +0,0 @@
1   -class EmailTemplatesController < MyProfileController
2   -
3   - protect 'manage_email_templates', :profile
4   -
5   - def index
6   - @email_templates = profile.email_templates
7   - end
8   -
9   - def show
10   - @email_template = profile.email_templates.find(params[:id])
11   -
12   - respond_to do |format|
13   - format.html # show.html.erb
14   - format.json { render json: @email_template }
15   - end
16   - end
17   -
18   - def show_parsed
19   - @email_template = profile.email_templates.find(params[:id])
20   - template_params = {:profile => profile, :environment => environment}
21   - render json: {:parsed_body => @email_template.parsed_body(template_params), :parsed_subject => @email_template.parsed_subject(template_params)}
22   - end
23   -
24   - def new
25   - @email_template = profile.email_templates.build
26   - end
27   -
28   - def edit
29   - @email_template = profile.email_templates.find(params[:id])
30   - end
31   -
32   - def create
33   - @email_template = profile.email_templates.build(params[:email_template], :owner => profile)
34   -
35   - if @email_template.save
36   - session[:notice] = _('Email template was successfully created.')
37   - redirect_to url_for(:action => :index)
38   - else
39   - render action: "new"
40   - end
41   - end
42   -
43   - def update
44   - @email_template = profile.email_templates.find(params[:id])
45   -
46   - if @email_template.update_attributes(params[:email_template])
47   - session[:notice] = _('Email template was successfully updated.')
48   - redirect_to url_for(:action => :index)
49   - else
50   - render action: "edit"
51   - end
52   - end
53   -
54   - def destroy
55   - @email_template = profile.email_templates.find(params[:id])
56   - @email_template.destroy
57   -
58   - respond_to do |format|
59   - format.html { redirect_to url_for(:action => :index)}
60   - format.json { head :no_content }
61   - end
62   - end
63   -end
app/controllers/my_profile/profile_email_templates_controller.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class ProfileEmailTemplatesController < EmailTemplatesController
  2 +
  3 + needs_profile
  4 + protect 'manage_email_templates', :profile
  5 +
  6 + protected
  7 +
  8 + def owner
  9 + profile
  10 + end
  11 +
  12 + before_filter :only => :index do
  13 + @back_to = url_for(:controller => :profile_editor)
  14 + end
  15 +
  16 +end
... ...
app/helpers/email_template_helper.rb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +module EmailTemplateHelper
  2 +
  3 + def mail_with_template(params={})
  4 + if params[:email_template].present?
  5 + params[:body] = params[:email_template].parsed_body(params[:template_params])
  6 + params[:subject] = params[:email_template].parsed_subject(params[:template_params])
  7 + params[:content_type] = "text/html"
  8 + end
  9 + mail(params.except(:email_template))
  10 + end
  11 +
  12 +end
... ...
app/helpers/task_helper.rb
... ... @@ -5,7 +5,7 @@ module TaskHelper
5 5  
6 6 content_tag(
7 7 :div,
8   - labelled_form_field(description, select_tag("tasks[#{task.id}][task][email_template_id]", options_from_collection_for_select(email_templates, :id, :name), :include_blank => include_blank, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed', :profile => profile.identifier))),
  8 + labelled_form_field(description, select_tag("tasks[#{task.id}][task][email_template_id]", options_from_collection_for_select(email_templates, :id, :name), :include_blank => include_blank, 'data-url' => url_for(:controller => 'profile_email_templates', :action => 'show_parsed', :profile => profile.identifier))),
9 9 :class => 'template-selection'
10 10 )
11 11 end
... ...
app/mailers/task_mailer.rb
1 1 class TaskMailer < ActionMailer::Base
2 2  
  3 + include EmailTemplateHelper
  4 +
3 5 def target_notification(task, message)
4 6 @message = extract_message(message)
5 7 @target = task.target.name
... ... @@ -33,15 +35,13 @@ class TaskMailer &lt; ActionMailer::Base
33 35 @requestor = task.requestor.name
34 36 @environment = task.requestor.environment.name
35 37 @url = url_for(:host => task.requestor.environment.default_hostname, :controller => 'home')
36   - @email_template = task.email_template
37   - template_params = {:environment => task.requestor.environment, :task => task}
38 38  
39   - mail(
  39 + mail_with_template(
40 40 to: task.requestor.notification_emails,
41 41 from: self.class.generate_from(task),
42   - subject: @email_template.present? ? @email_template.parsed_subject(template_params) : '[%s] %s' % [task.requestor.environment.name, task.target_notification_description],
43   - body: @email_template.present? ? @email_template.parsed_body(template_params) : nil,
44   - content_type: @email_template.present? ? "text/html" : nil
  42 + subject: '[%s] %s' % [task.requestor.environment.name, task.target_notification_description],
  43 + email_template: task.email_template,
  44 + template_params: {:environment => task.requestor.environment, :task => task, :message => @message, :url => @url, :requestor => task.requestor}
45 45 )
46 46 end
47 47  
... ...
app/mailers/user_mailer.rb
1 1 class UserMailer < ActionMailer::Base
  2 +
  3 + include EmailTemplateHelper
  4 +
2 5 def activation_email_notify(user)
3 6 user_email = "#{user.login}@#{user.email_domain}"
4 7 @name = user.name
... ... @@ -22,10 +25,12 @@ class UserMailer &lt; ActionMailer::Base
22 25 @redirection = (true if user.return_to)
23 26 @join = (user.community_to_join if user.community_to_join)
24 27  
25   - mail(
  28 + mail_with_template(
26 29 from: "#{user.environment.name} <#{user.environment.contact_email}>",
27 30 to: user.email,
28 31 subject: _("[%s] Activate your account") % [user.environment.name],
  32 + template_params: {:environment => user.environment, :activation_code => @activation_code, :redirection => @redirection, :join => @join},
  33 + email_template: user.environment.email_templates.find_by_template_type(:user_activation),
29 34 )
30 35 end
31 36  
... ...
app/models/change_password.rb
... ... @@ -28,6 +28,13 @@ class ChangePassword &lt; Task
28 28 validates_presence_of :password_confirmation, :on => :update, :if => lambda { |change| change.status != Task::Status::CANCELLED }
29 29 validates_confirmation_of :password, :if => lambda { |change| change.status != Task::Status::CANCELLED }
30 30  
  31 + before_save :set_email_template
  32 +
  33 + def set_email_template
  34 + template = environment.email_templates.find_by_template_type(:user_change_password)
  35 + data[:email_template_id] = template.id unless template.nil?
  36 + end
  37 +
31 38 def environment
32 39 requestor.environment unless requestor.nil?
33 40 end
... ...
app/models/email_template.rb
... ... @@ -6,6 +6,10 @@ class EmailTemplate &lt; ActiveRecord::Base
6 6  
7 7 validates_presence_of :name
8 8  
  9 + validates :name, uniqueness: { scope: [:owner_type, :owner_id] }
  10 +
  11 + validates :template_type, uniqueness: { scope: [:owner_type, :owner_id] }, if: :unique_by_type?
  12 +
9 13 def parsed_body(params)
10 14 @parsed_body ||= parse(body, params)
11 15 end
... ... @@ -14,12 +18,26 @@ class EmailTemplate &lt; ActiveRecord::Base
14 18 @parsed_subject ||= parse(subject, params)
15 19 end
16 20  
  21 + def self.available_types
  22 + {
  23 + :task_rejection => {:description => _('Task Rejection'), :owner_type => Profile},
  24 + :task_acceptance => {:description => _('Task Acceptance'), :owner_type => Profile},
  25 + :organization_members => {:description => _('Organization Members'), :owner_type => Profile},
  26 + :user_activation => {:description => _('User Activation'), :unique => true, :owner_type => Environment},
  27 + :user_change_password => {:description => _('Change User Password'), :unique => true, :owner_type => Environment}
  28 + }
  29 + end
  30 +
17 31 def available_types
18   - HashWithIndifferentAccess.new ({
19   - :task_rejection => {:description => _('Task Rejection')},
20   - :task_acceptance => {:description => _('Task Acceptance')},
21   - :organization_members => {:description => _('Organization Members')}
22   - })
  32 + HashWithIndifferentAccess.new EmailTemplate.available_types.select {|k, v| owner.kind_of?(v[:owner_type])}
  33 + end
  34 +
  35 + def type_description
  36 + available_types.fetch(template_type, {})[:description]
  37 + end
  38 +
  39 + def unique_by_type?
  40 + available_types.fetch(template_type, {})[:unique]
23 41 end
24 42  
25 43 protected
... ...
app/models/environment.rb
... ... @@ -21,6 +21,7 @@ class Environment &lt; ActiveRecord::Base
21 21  
22 22 has_many :tasks, :dependent => :destroy, :as => 'target'
23 23 has_many :search_terms, :as => :context
  24 + has_many :email_templates, :foreign_key => :owner_id
24 25  
25 26 IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/
26 27  
... ... @@ -50,6 +51,7 @@ class Environment &lt; ActiveRecord::Base
50 51 'manage_environment_licenses' => N_('Manage environment licenses'),
51 52 'manage_environment_trusted_sites' => N_('Manage environment trusted sites'),
52 53 'edit_appearance' => N_('Edit appearance'),
  54 + 'manage_email_templates' => N_('Manage Email Templates'),
53 55 }
54 56  
55 57 module Roles
... ...
app/models/task.rb
... ... @@ -305,7 +305,8 @@ class Task &lt; ActiveRecord::Base
305 305 def to_liquid
306 306 HashWithIndifferentAccess.new({
307 307 :requestor => requestor,
308   - :reject_explanation => reject_explanation
  308 + :reject_explanation => reject_explanation,
  309 + :code => code
309 310 })
310 311 end
311 312  
... ...
app/views/admin_panel/index.html.erb
... ... @@ -12,6 +12,7 @@
12 12 <tr><td><%= link_to _('Licenses'), :controller =>'licenses' %></td></tr>
13 13 <tr><td><%= link_to _('Trusted sites'), :controller =>'trusted_sites' %></td></tr>
14 14 <tr><td><%= link_to _('Blocks'), :controller => 'features', :action => 'manage_blocks' %></td></tr>
  15 + <tr><td><%= link_to _('Email templates'), :controller =>'environment_email_templates' %></td></tr>
15 16 </table>
16 17  
17 18 <h2><%= _('Profiles') %></h2>
... ...
app/views/email_templates/_form.html.erb
1   -<%= form_for(@email_template, :url => {:controller => :email_templates, :action => @email_template.persisted? ? :update : :create, :id => @email_template.id}) do |f| %>
  1 +<%= form_for(@email_template, :url => {:action => @email_template.persisted? ? :update : :create, :id => @email_template.id}) do |f| %>
2 2  
3 3 <%= error_messages_for :email_template if @email_template.errors.any? %>
4 4  
... ... @@ -25,7 +25,7 @@
25 25  
26 26 <div class="actions">
27 27 <%= submit_button(:save, _('Save')) %>
28   - <%= button(:back, _('Back'), :controller => :email_templates) %>
  28 + <%= button(:back, _('Back'), :action => :index) %>
29 29 </div>
30 30  
31 31 <% end %>
... ...
app/views/email_templates/index.html.erb
... ... @@ -11,10 +11,10 @@
11 11 <% @email_templates.each do |email_template| %>
12 12 <tr>
13 13 <td><%= email_template.name %></td>
14   - <td><%= email_template.available_types[email_template.template_type][:description] if email_template.template_type.present? %></td>
  14 + <td><%= email_template.type_description %></td>
15 15 <td>
16   - <%= button_without_text(:edit, _('Edit'), {:controller => :email_templates, :action => :edit, :id => email_template.id}) %>
17   - <%= button_without_text(:remove, _('Remove'), {:controller => :email_templates, :action => :destroy, :id => email_template.id}, method: :delete, data: { confirm: 'Are you sure?' }) %>
  16 + <%= button_without_text(:edit, _('Edit'), {:action => :edit, :id => email_template.id}) %>
  17 + <%= button_without_text(:remove, _('Remove'), {:action => :destroy, :id => email_template.id}, method: :delete, data: { confirm: 'Are you sure?' }) %>
18 18 </td>
19 19 </tr>
20 20 <% end %>
... ... @@ -22,6 +22,6 @@
22 22  
23 23 <br />
24 24  
25   - <%= button(:new, _('New template'), :controller => :email_templates, :action => :new) %>
26   - <%= button(:back, _('Back to control panel'), :controller => :profile_editor) %>
  25 + <%= button(:new, _('New template'), :action => :new) %>
  26 + <%= button(:back, _('Back'), @back_to) %>
27 27 </div>
... ...
app/views/email_templates/show.html.erb
1 1 <p id="notice"><%= notice %></p>
2 2  
3 3  
4   -<%= link_to 'Edit', url_for(:controller => :email_templates, :action => :edit, :id => @email_template.id) %> |
5   -<%= link_to 'Back', url_for(:controller => :email_templates) %>
  4 +<%= link_to 'Edit', url_for(:action => :edit, :id => @email_template.id) %> |
  5 +<%= link_to 'Back', url_for(:action => :index) %>
... ...
app/views/profile/send_mail.html.erb
... ... @@ -6,7 +6,7 @@
6 6  
7 7 <% if @email_templates.present? %>
8 8 <div class="template-selection">
9   - <%= labelled_form_field(_('Select a template:'), select_tag(:template, options_from_collection_for_select(@email_templates, :id, :name), :include_blank => true, 'data-url' => url_for(:controller => 'email_templates', :action => 'show_parsed'))) %>
  9 + <%= labelled_form_field(_('Select a template:'), select_tag(:template, options_from_collection_for_select(@email_templates, :id, :name), :include_blank => true, 'data-url' => url_for(:controller => 'profile_email_templates', :action => 'show_parsed'))) %>
10 10 </div>
11 11 <% end %>
12 12  
... ...
app/views/profile_editor/index.html.erb
... ... @@ -72,7 +72,7 @@
72 72  
73 73 <%= control_panel_button(_('Edit welcome page'), 'welcome-page', :action => 'welcome_page') if has_welcome_page %>
74 74  
75   - <%= control_panel_button(_('Email Templates'), 'email-templates', :controller => :email_templates) if profile.organization? %>
  75 + <%= control_panel_button(_('Email Templates'), 'email-templates', :controller => :profile_email_templates) if profile.organization? %>
76 76  
77 77 <% @plugins.dispatch(:control_panel_buttons).each do |button| %>
78 78 <%= control_panel_button(button[:title], button[:icon], button[:url], button[:html_options]) %>
... ...
test/fixtures/roles.yml
... ... @@ -38,6 +38,7 @@ four:
38 38 - manage_environment_organizations
39 39 - manage_environment_templates
40 40 - manage_environment_licenses
  41 + - manage_email_templates
41 42 profile_admin:
42 43 id: 5
43 44 environment_id: 1
... ... @@ -60,6 +61,7 @@ profile_admin:
60 61 - manage_friends
61 62 - validate_enterprise
62 63 - publish_content
  64 + - manage_email_templates
63 65 profile_member:
64 66 id: 6
65 67 environment_id: 1
... ... @@ -100,3 +102,4 @@ environment_administrator:
100 102 - destroy_profile
101 103 - manage_environment_templates
102 104 - manage_environment_licenses
  105 + - manage_email_templates
... ...
test/functional/email_templates_controller_test.rb
... ... @@ -1,68 +0,0 @@
1   -require 'test_helper'
2   -
3   -class EmailTemplatesControllerTest < ActionController::TestCase
4   -
5   - setup do
6   - @profile = fast_create(Community)
7   - @email_template = EmailTemplate.create!(:name => 'template', :owner => @profile)
8   - @person = create_user_with_permission('templatemanager', 'manage_email_templates', @profile)
9   - login_as(@person.user.login)
10   - end
11   -
12   - attr_accessor :profile, :person
13   -
14   - test "should get index" do
15   - get :index, :profile => profile.identifier
16   - assert_response :success
17   - assert_not_nil assigns(:email_templates)
18   - end
19   -
20   - test "should get new" do
21   - get :new, :profile => profile.identifier
22   - assert_response :success
23   - end
24   -
25   - test "should create email_template" do
26   - assert_difference('EmailTemplate.count') do
27   - post :create, email_template: { :name => 'test' }, :profile => profile.identifier
28   - end
29   -
30   - assert_redirected_to url_for(:action => :index)
31   - end
32   -
33   - test "should show email_template" do
34   - get :show, id: @email_template, :profile => profile.identifier
35   - assert_response :success
36   - end
37   -
38   - test "should get edit" do
39   - get :edit, id: @email_template, :profile => profile.identifier
40   - assert_response :success
41   - end
42   -
43   - test "should update email_template" do
44   - put :update, id: @email_template, email_template: { }, :profile => profile.identifier
45   - assert_redirected_to url_for(:action => :index)
46   - end
47   -
48   - test "should destroy email_template" do
49   - assert_difference('EmailTemplate.count', -1) do
50   - delete :destroy, id: @email_template, :profile => profile.identifier
51   - end
52   -
53   - assert_redirected_to url_for(:action => :index)
54   - end
55   -
56   - test "should get parsed template" do
57   - environment = Environment.default
58   - @email_template.subject = '{{profile.name}} - {{profile.identifier}}'
59   - @email_template.body = '{{profile.name}} - {{profile.identifier}} - {{environment.name}}'
60   - @email_template.save!
61   - get :show_parsed, id: @email_template, :profile => profile.identifier
62   - assert_response :success
63   - json_response = ActiveSupport::JSON.decode(@response.body)
64   - assert_equal "#{profile.name} - #{profile.identifier}", json_response['parsed_subject']
65   - assert_equal "#{profile.name} - #{profile.identifier} - #{environment.name}", json_response['parsed_body']
66   - end
67   -
68   -end
test/functional/environment_email_templates_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,65 @@
  1 +require 'test_helper'
  2 +
  3 +class EnvironmentEmailTemplatesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @email_template = EmailTemplate.create!(:name => 'template', :owner => Environment.default)
  7 + person = create_user_with_permission('template_manager', 'manage_email_templates', Environment.default)
  8 + login_as(person.user.login)
  9 + end
  10 +
  11 + test "should get index" do
  12 + get :index
  13 + assert_response :success
  14 + assert_not_nil assigns(:email_templates)
  15 + end
  16 +
  17 + test "should get new" do
  18 + get :new
  19 + assert_response :success
  20 + end
  21 +
  22 + test "should create email_template" do
  23 + assert_difference('EmailTemplate.count') do
  24 + post :create, email_template: { :name => 'test' }
  25 + end
  26 +
  27 + assert_redirected_to url_for(:action => :index)
  28 + end
  29 +
  30 + test "should show email_template" do
  31 + get :show, id: @email_template
  32 + assert_response :success
  33 + end
  34 +
  35 + test "should get edit" do
  36 + get :edit, id: @email_template
  37 + assert_response :success
  38 + end
  39 +
  40 + test "should update email_template" do
  41 + put :update, id: @email_template, email_template: { }
  42 + assert_redirected_to url_for(:action => :index)
  43 + end
  44 +
  45 + test "should destroy email_template" do
  46 + assert_difference('EmailTemplate.count', -1) do
  47 + delete :destroy, id: @email_template
  48 + end
  49 +
  50 + assert_redirected_to url_for(:action => :index)
  51 + end
  52 +
  53 + test "should get parsed template" do
  54 + environment = Environment.default
  55 + @email_template.subject = '{{environment.name}}'
  56 + @email_template.body = '{{environment.name}}'
  57 + @email_template.save!
  58 + get :show_parsed, id: @email_template
  59 + assert_response :success
  60 + json_response = ActiveSupport::JSON.decode(@response.body)
  61 + assert_equal "#{environment.name}", json_response['parsed_subject']
  62 + assert_equal "#{environment.name}", json_response['parsed_body']
  63 + end
  64 +
  65 +end
... ...
test/functional/profile_editor_controller_test.rb
... ... @@ -629,7 +629,7 @@ class ProfileEditorControllerTest &lt; ActionController::TestCase
629 629 should 'display email template link for organizations in control panel' do
630 630 profile = fast_create(Organization)
631 631 get :index, :profile => profile.identifier
632   - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/email_templates" }
  632 + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_email_templates" }
633 633 end
634 634  
635 635 should 'not display email template link in control panel for person' do
... ...
test/functional/profile_email_templates_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,68 @@
  1 +require 'test_helper'
  2 +
  3 +class ProfileEmailTemplatesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @profile = fast_create(Community)
  7 + @email_template = EmailTemplate.create!(:name => 'template', :owner => @profile)
  8 + @person = create_user_with_permission('templatemanager', 'manage_email_templates', @profile)
  9 + login_as(@person.user.login)
  10 + end
  11 +
  12 + attr_accessor :profile, :person
  13 +
  14 + test "should get index" do
  15 + get :index, :profile => profile.identifier
  16 + assert_response :success
  17 + assert_not_nil assigns(:email_templates)
  18 + end
  19 +
  20 + test "should get new" do
  21 + get :new, :profile => profile.identifier
  22 + assert_response :success
  23 + end
  24 +
  25 + test "should create email_template" do
  26 + assert_difference('EmailTemplate.count') do
  27 + post :create, email_template: { :name => 'test' }, :profile => profile.identifier
  28 + end
  29 +
  30 + assert_redirected_to url_for(:action => :index)
  31 + end
  32 +
  33 + test "should show email_template" do
  34 + get :show, id: @email_template, :profile => profile.identifier
  35 + assert_response :success
  36 + end
  37 +
  38 + test "should get edit" do
  39 + get :edit, id: @email_template, :profile => profile.identifier
  40 + assert_response :success
  41 + end
  42 +
  43 + test "should update email_template" do
  44 + put :update, id: @email_template, email_template: { }, :profile => profile.identifier
  45 + assert_redirected_to url_for(:action => :index)
  46 + end
  47 +
  48 + test "should destroy email_template" do
  49 + assert_difference('EmailTemplate.count', -1) do
  50 + delete :destroy, id: @email_template, :profile => profile.identifier
  51 + end
  52 +
  53 + assert_redirected_to url_for(:action => :index)
  54 + end
  55 +
  56 + test "should get parsed template" do
  57 + environment = Environment.default
  58 + @email_template.subject = '{{profile.name}} - {{profile.identifier}}'
  59 + @email_template.body = '{{profile.name}} - {{profile.identifier}} - {{environment.name}}'
  60 + @email_template.save!
  61 + get :show_parsed, id: @email_template, :profile => profile.identifier
  62 + assert_response :success
  63 + json_response = ActiveSupport::JSON.decode(@response.body)
  64 + assert_equal "#{profile.name} - #{profile.identifier}", json_response['parsed_subject']
  65 + assert_equal "#{profile.name} - #{profile.identifier} - #{environment.name}", json_response['parsed_body']
  66 + end
  67 +
  68 +end
... ...
test/unit/change_password_test.rb
... ... @@ -71,4 +71,10 @@ class ChangePasswordTest &lt; ActiveSupport::TestCase
71 71 assert_match(/#{task.requestor.name} wants to change its password/, email.subject)
72 72 end
73 73  
  74 + should 'set email template when it exists' do
  75 + template = EmailTemplate.create!(:template_type => :user_change_password, :name => 'template1', :owner => Environment.default)
  76 + task = ChangePassword.create!(:requestor => person)
  77 + assert_equal template.id, task.email_template_id
  78 + end
  79 +
74 80 end
... ...
test/unit/email_template_helper_test.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class EmailTemplateHelperTest < ActionView::TestCase
  4 +
  5 + should 'replace body and subject with parsed values from template' do
  6 + template = mock
  7 + template.expects(:parsed_body).returns('parsed body')
  8 + template.expects(:parsed_subject).returns('parsed subject')
  9 + params = {:subject => 'subject', :body => 'body', :email_template => template}
  10 + expects(:mail).with({:subject => 'parsed subject', :body => 'parsed body', :content_type => 'text/html'})
  11 + mail_with_template(params)
  12 + end
  13 +
  14 + should 'do not change params if there is no email template' do
  15 + params = {:subject => 'subject', :body => 'body'}
  16 + expects(:mail).with(params)
  17 + mail_with_template(params)
  18 + end
  19 +
  20 +end
... ...
test/unit/email_template_test.rb
... ... @@ -19,4 +19,35 @@ class EmailTemplateTest &lt; ActiveSupport::TestCase
19 19 assert_equal 'Hi John', template.parsed_subject({:person => 'John'})
20 20 end
21 21  
  22 + should 'not create template with the same name of other' do
  23 + template1 = EmailTemplate.new(:template_type => :type1, :name => 'template', :owner => Environment.default)
  24 + template2 = EmailTemplate.new(:template_type => :type1, :name => 'template', :owner => Environment.default)
  25 + assert template1.save
  26 + assert !template2.save
  27 + end
  28 +
  29 + should 'not create duplicated template when template type is unique' do
  30 + template1 = EmailTemplate.new(:template_type => :user_activation, :name => 'template1', :owner => Environment.default)
  31 + template2 = EmailTemplate.new(:template_type => :user_activation, :name => 'template2', :owner => Environment.default)
  32 + assert template1.save
  33 + assert !template2.save
  34 + end
  35 +
  36 + should 'create duplicated template when template type is not unique' do
  37 + template1 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template1', :owner => Environment.default)
  38 + template2 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template2', :owner => Environment.default)
  39 + assert template1.save
  40 + assert template2.save
  41 + end
  42 +
  43 + should 'return available types when the owner is an environment' do
  44 + template = EmailTemplate.new(:owner => Environment.default)
  45 + assert_equal [:user_activation, :user_change_password], template.available_types.symbolize_keys.keys
  46 + end
  47 +
  48 + should 'return available types when the owner is a profile' do
  49 + template = EmailTemplate.new(:owner => Profile.new)
  50 + assert_equal [:task_rejection, :task_acceptance, :organization_members], template.available_types.symbolize_keys.keys
  51 + end
  52 +
22 53 end
... ...
test/unit/user_mailer_test.rb
... ... @@ -26,6 +26,24 @@ fast_create(Person))
26 26 assert_match /profile\/some-user\/friends\/suggest/, email.body.to_s
27 27 end
28 28  
  29 + should 'deliver activation code email' do
  30 + assert_difference 'ActionMailer::Base.deliveries.size' do
  31 + u = create_user('some-user')
  32 + UserMailer.activation_code(u).deliver
  33 + end
  34 + end
  35 +
  36 + should 'deliver activation code email with template' do
  37 + EmailTemplate.create!(:template_type => :user_activation, :name => 'template1', :subject => 'activation template subject', :body => 'activation template body', :owner => Environment.default)
  38 + assert_difference 'ActionMailer::Base.deliveries.size' do
  39 + u = create_user('some-user')
  40 + UserMailer.activation_code(u).deliver
  41 + end
  42 + mail = ActionMailer::Base.deliveries.last
  43 + assert_equal 'activation template subject', mail.subject.to_s
  44 + assert_equal 'activation template body', mail.body.to_s
  45 + end
  46 +
29 47 private
30 48  
31 49 def read_fixture(action)
... ...