diff --git a/app/models/contact.rb b/app/models/contact.rb index 437ab67..851ce37 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -16,6 +16,7 @@ class Contact < ActiveRecord::Base #WithoutTable validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) def deliver + return false unless self.valid? Contact::Sender.deliver_mail(self) end diff --git a/test/functional/contact_controller_test.rb b/test/functional/contact_controller_test.rb index c7e2f91..494d0a8 100644 --- a/test/functional/contact_controller_test.rb +++ b/test/functional/contact_controller_test.rb @@ -74,4 +74,10 @@ class ContactControllerTest < Test::Unit::TestCase assert_tag :tag => 'input', :attributes => {:name => 'contact[receive_a_copy]'} end + should 'not deliver contact if mandatory field is blank' do + post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'} + assert_response :success + assert_template 'new' + end + end diff --git a/test/unit/contact_test.rb b/test/unit/contact_test.rb index a2da596..d70267b 100644 --- a/test/unit/contact_test.rb +++ b/test/unit/contact_test.rb @@ -37,4 +37,11 @@ class ContactTest < ActiveSupport::TestCase assert c.deliver end + should 'not deliver message if contact is invalid' do + ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :environment => Environment.default) + c = Contact.new(:name => 'john', :subject => 'hi', :message => 'hi, all', :dest => ent) + assert !c.valid? + assert !c.deliver + end + end -- libgit2 0.21.2