Commit d22c261e52998c42af3feb91cf9028eb9cb7685e
Committed by
Antonio Terceiro
1 parent
7fac6d07
Exists in
master
and in
29 other branches
ActionItem791: validate contact before deliver mail
Showing
3 changed files
with
14 additions
and
0 deletions
Show diff stats
app/models/contact.rb
@@ -16,6 +16,7 @@ class Contact < ActiveRecord::Base #WithoutTable | @@ -16,6 +16,7 @@ class Contact < ActiveRecord::Base #WithoutTable | ||
16 | validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) | 16 | validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) |
17 | 17 | ||
18 | def deliver | 18 | def deliver |
19 | + return false unless self.valid? | ||
19 | Contact::Sender.deliver_mail(self) | 20 | Contact::Sender.deliver_mail(self) |
20 | end | 21 | end |
21 | 22 |
test/functional/contact_controller_test.rb
@@ -74,4 +74,10 @@ class ContactControllerTest < Test::Unit::TestCase | @@ -74,4 +74,10 @@ class ContactControllerTest < Test::Unit::TestCase | ||
74 | assert_tag :tag => 'input', :attributes => {:name => 'contact[receive_a_copy]'} | 74 | assert_tag :tag => 'input', :attributes => {:name => 'contact[receive_a_copy]'} |
75 | end | 75 | end |
76 | 76 | ||
77 | + should 'not deliver contact if mandatory field is blank' do | ||
78 | + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'} | ||
79 | + assert_response :success | ||
80 | + assert_template 'new' | ||
81 | + end | ||
82 | + | ||
77 | end | 83 | end |
test/unit/contact_test.rb
@@ -37,4 +37,11 @@ class ContactTest < ActiveSupport::TestCase | @@ -37,4 +37,11 @@ class ContactTest < ActiveSupport::TestCase | ||
37 | assert c.deliver | 37 | assert c.deliver |
38 | end | 38 | end |
39 | 39 | ||
40 | + should 'not deliver message if contact is invalid' do | ||
41 | + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :environment => Environment.default) | ||
42 | + c = Contact.new(:name => 'john', :subject => 'hi', :message => 'hi, all', :dest => ent) | ||
43 | + assert !c.valid? | ||
44 | + assert !c.deliver | ||
45 | + end | ||
46 | + | ||
40 | end | 47 | end |