Commit 224e08e591d3c36e3b12cef4fa7d1c34c40e9d17

Authored by JoenioCosta
1 parent 02d5bfa0

ActionItem192: validate commants email format for unauthenticated user

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1672 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/comment.rb
@@ -9,6 +9,7 @@ class Comment < ActiveRecord::Base @@ -9,6 +9,7 @@ class Comment < ActiveRecord::Base
9 # unauthenticated authors: 9 # unauthenticated authors:
10 validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) 10 validates_presence_of :name, :if => (lambda { |record| !record.email.blank? })
11 validates_presence_of :email, :if => (lambda { |record| !record.name.blank? }) 11 validates_presence_of :email, :if => (lambda { |record| !record.name.blank? })
  12 + validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |record| !record.email.blank? })
12 13
13 # require either a recognized author or an external person 14 # require either a recognized author or an external person
14 validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? }) 15 validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? })
test/unit/comment_test.rb
@@ -41,7 +41,7 @@ class CommentTest < Test::Unit::TestCase @@ -41,7 +41,7 @@ class CommentTest < Test::Unit::TestCase
41 # if given name, require email 41 # if given name, require email
42 c1 = Comment.new 42 c1 = Comment.new
43 c1.name = 'My Name' 43 c1.name = 'My Name'
44 - assert_mandatory c1, :email 44 + assert_mandatory c1, :email, 'my@email.com'
45 45
46 # if given email, require name 46 # if given email, require name
47 c2 = Comment.new 47 c2 = Comment.new
@@ -137,5 +137,10 @@ class CommentTest < Test::Unit::TestCase @@ -137,5 +137,10 @@ class CommentTest < Test::Unit::TestCase
137 assert_equal comments, Comment.recent(2) 137 assert_equal comments, Comment.recent(2)
138 end 138 end
139 139
  140 + should 'not accept invalid email' do
  141 + c = Comment.new(:name => 'My Name', :email => 'my@invalid')
  142 + c.valid?
  143 + assert c.errors.invalid?(:email)
  144 + end
140 145
141 end 146 end