diff --git a/app/models/comment.rb b/app/models/comment.rb index 2e28215..b625d6c 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,6 +9,7 @@ class Comment < ActiveRecord::Base # unauthenticated authors: validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) validates_presence_of :email, :if => (lambda { |record| !record.name.blank? }) + validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |record| !record.email.blank? }) # require either a recognized author or an external person validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? }) diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index ff62846..afa9ba0 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -41,7 +41,7 @@ class CommentTest < Test::Unit::TestCase # if given name, require email c1 = Comment.new c1.name = 'My Name' - assert_mandatory c1, :email + assert_mandatory c1, :email, 'my@email.com' # if given email, require name c2 = Comment.new @@ -137,5 +137,10 @@ class CommentTest < Test::Unit::TestCase assert_equal comments, Comment.recent(2) end + should 'not accept invalid email' do + c = Comment.new(:name => 'My Name', :email => 'my@invalid') + c.valid? + assert c.errors.invalid?(:email) + end end -- libgit2 0.21.2