Commit 37e4ba3e6fe873d63a8fafe5ec528bd8111b4176

Authored by Daniela Feitosa
Committed by Antonio Terceiro
1 parent 30e81255

Send e-mails through environment

* Environment admin can send e-mail to all environment users
  * Community admin can send e-mail to all community members
  * Added migrate to create mailing
  * Added job to send mailing

(ActionItem1659)
app/controllers/admin/users_controller.rb
... ... @@ -19,4 +19,18 @@ class UsersController < AdminController
19 19 end
20 20 end
21 21  
  22 + def send_mail
  23 + @mailing = environment.mailings.build(params[:mailing])
  24 + if request.post?
  25 + @mailing.locale = locale
  26 + @mailing.person = user
  27 + if @mailing.save
  28 + session[:notice] = _('The e-mails are being sent')
  29 + redirect_to :action => 'index'
  30 + else
  31 + session[:notice] = _('Could not create the e-mail')
  32 + end
  33 + end
  34 + end
  35 +
22 36 end
... ...
app/controllers/my_profile/profile_members_controller.rb
... ... @@ -92,4 +92,18 @@ class ProfileMembersController < MyProfileController
92 92 render :layout => false
93 93 end
94 94  
  95 + def send_mail
  96 + @mailing = profile.mailings.build(params[:mailing])
  97 + if request.post?
  98 + @mailing.locale = locale
  99 + @mailing.person = user
  100 + if @mailing.save
  101 + session[:notice] = _('The e-mails are being sent')
  102 + redirect_to :action => 'index'
  103 + else
  104 + session[:notice] = _('Could not create the e-mail')
  105 + end
  106 + end
  107 + end
  108 +
95 109 end
... ...
app/models/environment.rb
... ... @@ -158,6 +158,8 @@ class Environment < ActiveRecord::Base
158 158 has_many :qualifiers
159 159 has_many :certifiers
160 160  
  161 + has_many :mailings, :class_name => 'EnvironmentMailing', :foreign_key => :source_id, :as => 'source'
  162 +
161 163 acts_as_accessible
162 164  
163 165 def superior_intances
... ...
app/models/environment_mailing.rb 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +class EnvironmentMailing < Mailing
  2 +
  3 + def recipient(offset=0)
  4 + Person.find(:first, :conditions => [ "environment_id = ?", source_id], :order => :id, :offset => offset)
  5 + end
  6 +
  7 + def each_recipient
  8 + offset = 0
  9 + while person = recipient(offset)
  10 + unless self.already_sent_mailing_to?(person)
  11 + yield person
  12 + end
  13 + offset = offset + 1
  14 + end
  15 + end
  16 +
  17 + def signature_message
  18 + _('Sent by Noosfero %s.') % source.name
  19 + end
  20 +
  21 + def url
  22 + source.top_url
  23 + end
  24 +end
... ...
app/models/mailing.rb 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +class Mailing < ActiveRecord::Base
  2 +
  3 + validates_presence_of :source_id, :subject, :body
  4 + belongs_to :source, :foreign_key => :source_id, :polymorphic => true
  5 + belongs_to :person
  6 +
  7 + has_many :mailing_sents
  8 +
  9 + xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation'
  10 +
  11 + after_create do |mailing|
  12 + Delayed::Job.enqueue MailingJob.new(mailing.id)
  13 + end
  14 +
  15 + def generate_from
  16 + "#{source.name} <#{source.contact_email}>"
  17 + end
  18 +
  19 + def generate_subject
  20 + '[%s] %s' % [source.name, subject]
  21 + end
  22 +
  23 + def signature_message
  24 + _('Sent by Noosfero.')
  25 + end
  26 +
  27 + def url
  28 + ''
  29 + end
  30 +
  31 + def already_sent_mailing_to?(recipient)
  32 + self.mailing_sents.find_by_person_id(recipient.id)
  33 + end
  34 +
  35 + def deliver
  36 + offset = 0
  37 + people = []
  38 + each_recipient do |recipient|
  39 + Mailing::Sender.deliver_mail(self, recipient.email)
  40 + self.mailing_sents.create(:person => recipient)
  41 + end
  42 + end
  43 +
  44 + class Sender < ActionMailer::Base
  45 + def mail(mailing, recipient)
  46 + content_type 'text/html'
  47 + recipients recipient
  48 + from mailing.generate_from
  49 + reply_to mailing.person.email
  50 + subject mailing.generate_subject
  51 + body :message => mailing.body,
  52 + :signature_message => mailing.signature_message,
  53 + :url => mailing.url
  54 + end
  55 + end
  56 +end
... ...
app/models/mailing_sent.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +class MailingSent < ActiveRecord::Base
  2 + belongs_to :mailing
  3 + belongs_to :person
  4 +end
... ...
app/models/organization.rb
... ... @@ -19,6 +19,8 @@ class Organization &lt; Profile
19 19  
20 20 has_many :validations, :class_name => 'CreateEnterprise', :foreign_key => :target_id
21 21  
  22 + has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source'
  23 +
22 24 def validation_methodology
23 25 self.validation_info ? self.validation_info.validation_methodology : nil
24 26 end
... ...
app/models/organization_mailing.rb 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +class OrganizationMailing < Mailing
  2 +
  3 + def generate_from
  4 + "#{person.name} <#{source.environment.contact_email}>"
  5 + end
  6 +
  7 + def recipient(offset=0)
  8 + environment_id = source.environment_id
  9 + Person.find(:first, :conditions => ['environment_id = ? and role_assignments.resource_type = ? and role_assignments.resource_id = ?', environment_id, 'Profile', source.id], :include => :role_assignments, :order => "profiles.id", :offset => offset)
  10 + end
  11 +
  12 + def each_recipient
  13 + offset = 0
  14 + while person = recipient(offset)
  15 + unless self.already_sent_mailing_to?(person)
  16 + yield person
  17 + end
  18 + offset = offset + 1
  19 + end
  20 + end
  21 +
  22 + def signature_message
  23 + _('Sent by community %s.') % source.name
  24 + end
  25 +
  26 + include ActionController::UrlWriter
  27 + def url
  28 + url_for(source.url)
  29 + end
  30 +end
... ...
app/models/person.rb
... ... @@ -14,6 +14,8 @@ class Person &lt; Profile
14 14  
15 15 has_many :requested_tasks, :class_name => 'Task', :foreign_key => :requestor_id, :dependent => :destroy
16 16  
  17 + has_many :mailings
  18 +
17 19 named_scope :more_popular,
18 20 :select => "#{Profile.qualified_column_names}, count(friend_id) as total",
19 21 :group => Profile.qualified_column_names,
... ...
app/models/profile.rb
... ... @@ -587,9 +587,9 @@ private :generate_url, :url_options
587 587 end
588 588  
589 589 include ActionView::Helpers::TextHelper
590   - def short_name
  590 + def short_name(chars = 15)
591 591 if self[:nickname].blank?
592   - truncate self.name, 15, '...'
  592 + truncate self.name, chars, '...'
593 593 else
594 594 self[:nickname]
595 595 end
... ...
app/views/mailing/sender/mail.rhtml 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 + <head>
  4 + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5 + </head>
  6 + <body>
  7 + <%= word_wrap(@message) %>
  8 + <p>
  9 + --<br/>
  10 + <%= @signature_message %><br/>
  11 + <%= @url %>
  12 + </p>
  13 + </body>
  14 +</html>
  15 +
  16 +
  17 +
... ...
app/views/profile_members/_index_buttons.rhtml 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<% button_bar do %>
  2 + <%= button :back, _('Back'), :controller => 'profile_editor' %>
  3 + <%= button :add, _('Add members'), :action => 'add_members' if profile.enterprise? %>
  4 + <% if profile.community? and user.has_permission?(:invite_members, profile) %>
  5 + <%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'select_address_book' %>
  6 + <% end %>
  7 + <%= button :send, _('Send e-mail to members'), :action => 'send_mail' %>
  8 +<% end %>
... ...
app/views/profile_members/index.rhtml
  1 +<h1><%= h profile.short_name(50) %></h1>
  2 +
  3 +<%= render :partial => 'index_buttons' %>
  4 +
1 5 <div id="members-list">
2 6 <%= render :partial => 'members_list' %>
3 7 </div>
4 8  
5   -<% button_bar do %>
6   - <%= button :back, _('Back'), :controller => 'profile_editor' %>
7   - <%= button :add, _('Add members'), :action => 'add_members' if profile.enterprise? %>
8   - <% if profile.community? and user.has_permission?(:invite_members, profile) %>
9   - <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'select_address_book' %>
10   - <% end %>
11   -<% end %>
  9 +<%= render :partial => 'index_buttons' %>
... ...
app/views/profile_members/send_mail.rhtml 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +<h1><%= h profile.short_name(50) %></h1>
  2 +
  3 +<h2><%= _('Send e-mail to members') %></h2>
  4 +
  5 +<%= error_messages_for :mailing %>
  6 +
  7 +<%= render :file => 'shared/tiny_mce' %>
  8 +
  9 +<% form_for :mailing, :url => {:action => 'send_mail'}, :html => {:id => 'mailing-form'} do |f| %>
  10 + <%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %>
  11 + <%= labelled_form_field(_('Body:'), f.text_area(:body)) %>
  12 + <%= submit_button(:send, _('Send')) %>
  13 + <%= button :cancel, _('Cancel e-mail'), :action => 'index' %>
  14 +<% end %>
... ...
app/views/users/index.rhtml
... ... @@ -2,9 +2,12 @@
2 2  
3 3 <ul>
4 4 <li>
5   - <%= _('Download users list') %>:
6   - <%= link_to '[CSV]', :format => 'csv' %>
7   - <%= link_to '[XML]', :format => 'xml' %>
  5 + <%= _('Download users list') %>:
  6 + <%= link_to '[CSV]', :format => 'csv' %>
  7 + <%= link_to '[XML]', :format => 'xml' %>
  8 + </li>
  9 + <li>
  10 + <%= link_to _('Send e-mail to users'), :action => 'send_mail' %>
8 11 </li>
9 12 </ul>
10 13  
... ...
app/views/users/send_mail.rhtml 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +<h1><%= _('Send e-mail to users') %></h1>
  2 +
  3 +<%= error_messages_for :mailing %>
  4 +
  5 +<%= render :file => 'shared/tiny_mce' %>
  6 +
  7 +<% form_for :mailing, :url => {:action => 'send_mail'} do |f| %>
  8 + <%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %>
  9 + <%= labelled_form_field(_('Body:'), f.text_area(:body)) %>
  10 + <%= submit_button(:send, _('Send')) %>
  11 + <%= button :cancel, _('Cancel e-mail'), :controller => 'users' %>
  12 +<% end %>
... ...
db/migrate/20100902202247_create_mailings.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class CreateMailings < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :mailings do |t|
  4 + t.string :type
  5 + t.string :subject
  6 + t.text :body
  7 + t.integer :source_id
  8 + t.string :source_type
  9 + t.references :person
  10 + t.string :locale
  11 + t.timestamps
  12 + end
  13 + end
  14 +
  15 + def self.down
  16 + drop_table :mailings
  17 + end
  18 +end
... ...
db/migrate/20100909103951_create_mailing_sents.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class CreateMailingSents < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :mailing_sents do |t|
  4 + t.references :mailing
  5 + t.references :person
  6 + t.timestamps
  7 + end
  8 + end
  9 +
  10 + def self.down
  11 + drop_table :mailing_sents
  12 + end
  13 +end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 20100901203836) do
  12 +ActiveRecord::Schema.define(:version => 20100909103951) do
13 13  
14 14 create_table "article_versions", :force => true do |t|
15 15 t.integer "article_id"
... ... @@ -261,6 +261,25 @@ ActiveRecord::Schema.define(:version =&gt; 20100901203836) do
261 261 t.boolean "is_from_solidarity_economy", :default => false
262 262 end
263 263  
  264 + create_table "mailing_sents", :force => true do |t|
  265 + t.integer "mailing_id"
  266 + t.integer "person_id"
  267 + t.datetime "created_at"
  268 + t.datetime "updated_at"
  269 + end
  270 +
  271 + create_table "mailings", :force => true do |t|
  272 + t.string "type"
  273 + t.string "subject"
  274 + t.text "body"
  275 + t.integer "source_id"
  276 + t.string "source_type"
  277 + t.integer "person_id"
  278 + t.string "locale"
  279 + t.datetime "created_at"
  280 + t.datetime "updated_at"
  281 + end
  282 +
264 283 create_table "product_categorizations", :force => true do |t|
265 284 t.integer "category_id"
266 285 t.integer "product_id"
... ...
features/send_email_to_environment_members.feature 0 → 100644
... ... @@ -0,0 +1,53 @@
  1 +Feature: send emails to environment members users
  2 + As an administrator
  3 + I want to send email to all users
  4 +
  5 + Scenario: Cant access if not logged in
  6 + Given I am not logged in
  7 + When I go to /admin/users/send_mail
  8 + Then I should see "Access denied"
  9 +
  10 + Scenario: Cant access as normal user
  11 + Given the following user
  12 + | login |
  13 + | ultraje |
  14 + And I am logged in as "ultraje"
  15 + When I go to /admin/users/send_mail
  16 + Then I should see "Access denied"
  17 +
  18 + Scenario: Send e-mail to members
  19 + Given I am logged in as admin
  20 + When I follow "Administration"
  21 + And I follow "Manage users"
  22 + And I follow "Send e-mail to users"
  23 + And I fill in "Subject" with "Hello, user!"
  24 + And I fill in "body" with "We have some news"
  25 + When I press "Send"
  26 + Then I should be on /admin/users
  27 +
  28 + Scenario: Not send e-mail to members if subject is blank
  29 + Given I am logged in as admin
  30 + When I follow "Administration"
  31 + And I follow "Manage users"
  32 + And I follow "Send e-mail to users"
  33 + And I fill in "body" with "We have some news"
  34 + When I press "Send"
  35 + Then I should be on /admin/users/send_mail
  36 +
  37 + Scenario: Not send e-mail to members if body is blank
  38 + Given I am logged in as admin
  39 + When I follow "Administration"
  40 + And I follow "Manage users"
  41 + And I follow "Send e-mail to users"
  42 + And I fill in "Subject" with "Hello, user!"
  43 + When I press "Send"
  44 + Then I should be on /admin/users/send_mail
  45 +
  46 + Scenario: Cancel creation of mailing
  47 + Given I am logged in as admin
  48 + When I follow "Administration"
  49 + And I follow "Manage users"
  50 + And I follow "Send e-mail to users"
  51 + Then I should be on /admin/users/send_mail
  52 + When I follow "Cancel e-mail"
  53 + Then I should be on /admin/users
... ...
features/send_email_to_organization_members.feature 0 → 100644
... ... @@ -0,0 +1,57 @@
  1 +Feature: send emails to organization members
  2 + As a organization administrator
  3 + I want to send email to all members
  4 +
  5 + Background:
  6 + Given the following users
  7 + | login | name |
  8 + | joaosilva | Joao Silva |
  9 + And the following communities
  10 + | identifier | name |
  11 + | sample-community | Sample Community |
  12 + And "Joao Silva" is admin of "Sample Community"
  13 +
  14 + Scenario: Cant access if not logged in
  15 + Given I am not logged in
  16 + When I go to /myprofile/sample-community/profile_members/send_mail
  17 + Then I should be on login page
  18 +
  19 + Scenario: Cant access as normal user
  20 + Given the following user
  21 + | login |
  22 + | josesilva |
  23 + And I am logged in as "josesilva"
  24 + When I go to /myprofile/sample-community/profile_members/send_mail
  25 + Then I should see "Access denied"
  26 +
  27 + Scenario: Send e-mail to members
  28 + Given I am logged in as "joaosilva"
  29 + And I go to Sample Community's members management
  30 + And I follow "Send e-mail to members"
  31 + And I fill in "Subject" with "Hello, member!"
  32 + And I fill in "body" with "We have some news"
  33 + When I press "Send"
  34 + Then I should be on Sample Community's members management
  35 +
  36 + Scenario: Not send e-mail to members if subject is blank
  37 + Given I am logged in as "joaosilva"
  38 + And I go to Sample Community's members management
  39 + And I follow "Send e-mail to members"
  40 + And I fill in "body" with "We have some news"
  41 + When I press "Send"
  42 + Then I should be on /myprofile/sample-community/profile_members/send_mail
  43 +
  44 + Scenario: Not send e-mail to members if body is blank
  45 + Given I am logged in as "joaosilva"
  46 + And I go to Sample Community's members management
  47 + And I follow "Send e-mail to members"
  48 + And I fill in "Subject" with "Hello, user!"
  49 + When I press "Send"
  50 + Then I should be on /myprofile/sample-community/profile_members/send_mail
  51 +
  52 + Scenario: Cancel creation of mailing
  53 + Given I am logged in as "joaosilva"
  54 + And I go to Sample Community's members management
  55 + And I follow "Send e-mail to members"
  56 + When I follow "Cancel e-mail"
  57 + Then I should be on Sample Community's members management
... ...
lib/mailing_job.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +class MailingJob < Struct.new(:mailing_id)
  2 + def perform
  3 + mailing = Mailing.find(mailing_id)
  4 + Noosfero.with_locale(mailing.locale) do
  5 + mailing.deliver
  6 + end
  7 + end
  8 +end
... ...
public/stylesheets/application.css
... ... @@ -3640,6 +3640,11 @@ h1#agenda-title {
3640 3640 text-align: left;
3641 3641 }
3642 3642  
  3643 +.controller-profile_members #mailing-form .formfield textarea,
  3644 +.controller-profile_members #mailing-form .type-text input {
  3645 + width: 100%;
  3646 +}
  3647 +
3643 3648 /* ==> public/stylesheets/controller_search.css <== */
3644 3649 /* @import url(pagination.css); ALREADY INCLUDED ABOVE */
3645 3650  
... ...
test/functional/profile_members_controller_test.rb
... ... @@ -280,4 +280,31 @@ class ProfileMembersControllerTest &lt; Test::Unit::TestCase
280 280 assert p_roles, [r]
281 281 end
282 282  
  283 + should 'add locale on mailing' do
  284 + community = fast_create(Community)
  285 + admin_user = create_user_with_permission('profile_admin_user', 'manage_memberships', community)
  286 + login_as('profile_admin_user')
  287 + @controller.stubs(:locale).returns('pt')
  288 + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'}
  289 + assert_equal 'pt', assigns(:mailing).locale
  290 + end
  291 +
  292 + should 'save mailing' do
  293 + community = fast_create(Community)
  294 + admin_user = create_user_with_permission('profile_admin_user', 'manage_memberships', community)
  295 + login_as('profile_admin_user')
  296 + @controller.stubs(:locale).returns('pt')
  297 + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'}
  298 + assert_equal ['Hello', 'We have some news'], [assigns(:mailing).subject, assigns(:mailing).body]
  299 + assert_redirected_to :action => 'index'
  300 + end
  301 +
  302 + should 'add the user logged on mailing' do
  303 + community = fast_create(Community)
  304 + admin_user = create_user_with_permission('profile_admin_user', 'manage_memberships', community)
  305 + login_as('profile_admin_user')
  306 + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'}
  307 + assert_equal Profile['profile_admin_user'], assigns(:mailing).person
  308 + end
  309 +
283 310 end
... ...
test/unit/environment_mailing_test.rb 0 → 100644
... ... @@ -0,0 +1,97 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class EnvironmentMailingTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + ActionMailer::Base.delivery_method = :test
  7 + ActionMailer::Base.perform_deliveries = true
  8 + ActionMailer::Base.deliveries = []
  9 + @environment = fast_create(Environment, :name => 'Network')
  10 + create_user('user_one', :environment_id => @environment.id)
  11 + create_user('user_two', :environment_id => @environment.id)
  12 + end
  13 + attr_reader :environment
  14 +
  15 +
  16 + should 'require source_id' do
  17 + mailing = EnvironmentMailing.new
  18 + mailing.valid?
  19 + assert mailing.errors.invalid?(:source_id)
  20 +
  21 + mailing.source_id = environment.id
  22 + mailing.valid?
  23 + assert !mailing.errors.invalid?(:source_id)
  24 + end
  25 +
  26 + should 'return environment name' do
  27 + mailing = EnvironmentMailing.new(:source => environment)
  28 + assert_equal environment.name, mailing.source.name
  29 + end
  30 +
  31 + should 'return environment with source_id' do
  32 + mailing = EnvironmentMailing.new(:source => environment)
  33 + assert_equal environment, mailing.source
  34 + end
  35 +
  36 + should 'return signature message' do
  37 + mailing = EnvironmentMailing.new(:source => environment)
  38 + assert_equal 'Sent by Noosfero Network.', mailing.signature_message
  39 + end
  40 +
  41 + should 'return url for environment on url' do
  42 + mailing = EnvironmentMailing.new(:source => environment)
  43 + environment.domains << Domain.create(:name => 'noosfero.net', :is_default => true)
  44 + assert_equal 'http://noosfero.net', mailing.url
  45 + end
  46 +
  47 + should 'display name and email on generate_from' do
  48 + person = Person['user_one']
  49 + mailing = EnvironmentMailing.new(:source => environment, :person => person)
  50 + assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from
  51 + end
  52 +
  53 + should 'deliver mailing to each recipient after create' do
  54 + person = Person['user_one']
  55 + mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
  56 + process_delayed_job_queue
  57 + assert_equal 2, ActionMailer::Base.deliveries.count
  58 + end
  59 +
  60 + should 'create mailing sent to each recipient after delivering mailing' do
  61 + person = Person['user_one']
  62 + mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
  63 + assert_difference MailingSent, :count, 2 do
  64 + process_delayed_job_queue
  65 + end
  66 + end
  67 +
  68 + should 'change locale according to the mailing locale' do
  69 + person = Person['user_one']
  70 + mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person)
  71 + Noosfero.expects(:with_locale).with('pt')
  72 + process_delayed_job_queue
  73 + end
  74 +
  75 + should 'return recipient' do
  76 + mailing = EnvironmentMailing.new(:source => environment)
  77 + assert_equal Person['user_one'], mailing.recipient
  78 + end
  79 +
  80 + should 'return true if already sent mailing to a recipient' do
  81 + person = Person['user_one']
  82 + mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
  83 + process_delayed_job_queue
  84 +
  85 + assert mailing.already_sent_mailing_to?(person)
  86 + end
  87 +
  88 + should 'return false if did not sent mailing to a recipient' do
  89 + recipient = fast_create(Person)
  90 + person = Person['user_one']
  91 + mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
  92 + process_delayed_job_queue
  93 +
  94 + assert !mailing.already_sent_mailing_to?(recipient)
  95 + end
  96 +
  97 +end
... ...
test/unit/mailing_job_test.rb 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class MailingJobTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @environment = fast_create(Environment)
  7 + @person_1 = create_user('user_one', :environment_id => @environment.id).person
  8 + create_user('user_two', :environment_id => @environment.id)
  9 + end
  10 + attr_reader :environment
  11 +
  12 + should 'create delayed job' do
  13 + assert_difference Delayed::Job, :count, 1 do
  14 + mailing = EnvironmentMailing.create(:source_id => environment.id, :subject => 'Hello', :body => 'We have some news', :person => @person_1)
  15 + end
  16 + end
  17 +
  18 + should 'change locale according to the locale informed' do
  19 + mailing = EnvironmentMailing.create(:source_id => environment.id, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => @person_1)
  20 + Noosfero.expects(:with_locale).with('pt')
  21 + process_delayed_job_queue
  22 + end
  23 +end
... ...
test/unit/mailing_sent_test.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class MailingSentTest < ActiveSupport::TestCase
  4 +
  5 + should 'return mailing and person' do
  6 + person = fast_create(Person)
  7 + mailing = Mailing.create(:source => Environment.default, :subject => 'Hello', :body => 'We have some news')
  8 + sent = MailingSent.create(:mailing => mailing, :person => person)
  9 +
  10 + mailing_sent = MailingSent.find(sent.id)
  11 + assert_equal [mailing, person], [mailing_sent.mailing, mailing_sent.person]
  12 + end
  13 +end
... ...
test/unit/mailing_test.rb 0 → 100644
... ... @@ -0,0 +1,102 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class MailingTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + ActionMailer::Base.deliveries = []
  7 + @environment = fast_create(Environment, :name => 'Network')
  8 + create_user('user_one', :environment_id => @environment.id)
  9 + create_user('user_two', :environment_id => @environment.id)
  10 + end
  11 + attr_reader :environment
  12 +
  13 + should 'source be able to polymorphic relationship' do
  14 + m = Mailing.new
  15 + assert_nothing_raised do
  16 + m.source = Environment.new
  17 + end
  18 + assert_nothing_raised do
  19 + m.source = Profile.new
  20 + end
  21 + end
  22 +
  23 + should 'require source_id' do
  24 + mailing = Mailing.new
  25 + mailing.valid?
  26 + assert mailing.errors.invalid?(:source_id)
  27 +
  28 + mailing.source_id = Environment.default.id
  29 + mailing.valid?
  30 + assert !mailing.errors.invalid?(:source_id)
  31 + end
  32 +
  33 + should 'require subject' do
  34 + mailing = Mailing.new
  35 + mailing.valid?
  36 + assert mailing.errors.invalid?(:subject)
  37 +
  38 + mailing.subject = 'Hello :)'
  39 + mailing.valid?
  40 + assert !mailing.errors.invalid?(:subject)
  41 + end
  42 +
  43 + should 'require body' do
  44 + mailing = Mailing.new
  45 + mailing.valid?
  46 + assert mailing.errors.invalid?(:body)
  47 +
  48 + mailing.body = 'We have some news!'
  49 + mailing.valid?
  50 + assert !mailing.errors.invalid?(:body)
  51 + end
  52 +
  53 + should 'return source' do
  54 + mailing = Mailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news')
  55 + assert_equal environment, Mailing.find(mailing.id).source
  56 + end
  57 +
  58 + should 'return source name' do
  59 + mailing = Mailing.new(:source => environment)
  60 + assert_equal environment.name, mailing.source.name
  61 + end
  62 +
  63 + should 'return source with source_id' do
  64 + mailing = Mailing.new(:source => environment)
  65 + assert_equal environment, mailing.source
  66 + end
  67 +
  68 + should 'return person with person_id' do
  69 + person = Person['user_one']
  70 + mailing = Mailing.new(:source => environment, :person => person)
  71 + assert_equal person, mailing.person
  72 + end
  73 +
  74 + should 'display name and email on generate_from' do
  75 + person = Person['user_one']
  76 + mailing = Mailing.new(:source => environment, :person => person)
  77 + assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from
  78 + end
  79 +
  80 + should 'generate subject' do
  81 + mailing = Mailing.new(:source => environment, :subject => 'Hello :)')
  82 + assert_equal "[#{environment.name}] #{mailing.subject}", mailing.generate_subject
  83 + end
  84 +
  85 + should 'return signature message' do
  86 + mailing = Mailing.new(:source => environment)
  87 + assert_equal 'Sent by Noosfero.', mailing.signature_message
  88 + end
  89 +
  90 + should 'return blank string on url' do
  91 + mailing = Mailing.new(:source => environment)
  92 + environment.domains << Domain.create(:name => 'noosfero.net', :is_default => true)
  93 + assert_equal '', mailing.url
  94 + end
  95 +
  96 + should 'deliver mailing to each recipient after create' do
  97 + person = Person['user_one']
  98 + mailing = Mailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
  99 + process_delayed_job_queue
  100 + assert_equal [], ActionMailer::Base.deliveries
  101 + end
  102 +end
... ...
test/unit/organization_mailing_test.rb 0 → 100644
... ... @@ -0,0 +1,115 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class OrganizationMailingTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + ActionMailer::Base.delivery_method = :test
  7 + ActionMailer::Base.perform_deliveries = true
  8 + ActionMailer::Base.deliveries = []
  9 + @community = fast_create(Community)
  10 + @person = create_user('john').person
  11 + member_1 = create_user('user_one').person
  12 + member_2 = create_user('user_two').person
  13 + @community.add_member(member_1)
  14 + @community.add_member(member_2)
  15 + end
  16 + attr_reader :community, :person
  17 +
  18 + should 'require source_id' do
  19 + mailing = OrganizationMailing.new
  20 + mailing.valid?
  21 + assert mailing.errors.invalid?(:source_id)
  22 +
  23 + mailing.source_id = community.id
  24 + mailing.valid?
  25 + assert !mailing.errors.invalid?(:source_id)
  26 + end
  27 +
  28 + should 'return community name' do
  29 + mailing = OrganizationMailing.new(:source => community)
  30 + assert_equal community.name, mailing.source.name
  31 + end
  32 +
  33 + should 'return community with source_id' do
  34 + mailing = OrganizationMailing.new(:source => community)
  35 + assert_equal community, mailing.source
  36 + end
  37 +
  38 + should 'return person with person_id' do
  39 + mailing = OrganizationMailing.new(:source => community, :person => person)
  40 + assert_equal person, mailing.person
  41 + end
  42 +
  43 + should 'display name and email on generate_from' do
  44 + mailing = OrganizationMailing.new(:source => community, :person => person)
  45 + assert_equal "#{person.name} <#{community.environment.contact_email}>", mailing.generate_from
  46 + end
  47 +
  48 + should 'generate subject' do
  49 + mailing = OrganizationMailing.new(:source => community, :subject => 'Hello :)')
  50 + assert_equal "[#{community.name}] #{mailing.subject}", mailing.generate_subject
  51 + end
  52 +
  53 + should 'return signature message' do
  54 + mailing = OrganizationMailing.new(:source => community)
  55 + assert_equal "Sent by community #{community.name}.", mailing.signature_message
  56 + end
  57 +
  58 + should 'return url for organization on url' do
  59 + mailing = OrganizationMailing.new(:source => community)
  60 + assert_equal "#{community.environment.top_url}/#{community.name.to_slug}", mailing.url
  61 + end
  62 +
  63 + should 'deliver mailing to each member after create' do
  64 + mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
  65 + process_delayed_job_queue
  66 + assert_equal 2, ActionMailer::Base.deliveries.count
  67 + end
  68 +
  69 + should 'create mailing sent to each recipient after delivering mailing' do
  70 + mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
  71 + assert_difference MailingSent, :count, 2 do
  72 + process_delayed_job_queue
  73 + end
  74 + end
  75 +
  76 + should 'change locale according to the mailing locale' do
  77 + mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person)
  78 + Noosfero.expects(:with_locale).with('pt')
  79 + process_delayed_job_queue
  80 + end
  81 +
  82 + should 'have community by source_id' do
  83 + mailing = community.mailings.build(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
  84 + mailing.save!
  85 +
  86 + assert_equal community, Mailing.find(mailing.id).source
  87 + end
  88 +
  89 + should 'return recipient' do
  90 + mailing = OrganizationMailing.new(:source => community)
  91 + assert_equal Person['user_one'], mailing.recipient
  92 + end
  93 +
  94 + should 'return true if already sent mailing to a recipient' do
  95 + member = Person['user_one']
  96 + mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
  97 + process_delayed_job_queue
  98 + assert mailing.already_sent_mailing_to?(member)
  99 + end
  100 +
  101 + should 'return false if did not sent mailing to a recipient' do
  102 + recipient = fast_create(Person)
  103 + mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
  104 + process_delayed_job_queue
  105 +
  106 + assert !mailing.already_sent_mailing_to?(recipient)
  107 + end
  108 +
  109 + protected
  110 +
  111 + def url_for(url)
  112 + url
  113 + end
  114 +
  115 +end
... ...
test/unit/profile_test.rb
... ... @@ -823,11 +823,16 @@ class ProfileTest &lt; Test::Unit::TestCase
823 823 assert_equal 'code', p.nickname
824 824 end
825 825  
826   - should 'return truncated name in short_name if nickname is blank' do
  826 + should 'return truncated name in short_name with 15 chars by default if nickname is blank' do
827 827 p = Profile.new(:name => 'a123456789abcdefghij')
828 828 assert_equal 'a123456789ab...', p.short_name
829 829 end
830 830  
  831 + should 'return truncated name in short_name with chars size if nickname is blank' do
  832 + p = Profile.new(:name => 'a123456789abcdefghij')
  833 + assert_equal 'a123456...', p.short_name(10)
  834 + end
  835 +
831 836 should 'provide custom header' do
832 837 assert_equal 'my custom header', Profile.new(:custom_header => 'my custom header').custom_header
833 838 end
... ...