Commit 8849f0f95ffae506fb0547c93648ca9af53c9d6c

Authored by Joenio Costa
Committed by Daniela Feitosa
1 parent 00641c73

Always ask captcha question on comments

(ActionItem2027)
app/controllers/public/content_viewer_controller.rb
@@ -76,8 +76,13 @@ class ContentViewerController < ApplicationController @@ -76,8 +76,13 @@ class ContentViewerController < ApplicationController
76 76
77 @form_div = params[:form] 77 @form_div = params[:form]
78 78
79 - if request.post? && params[:comment] && params[self.icaptcha_field].blank? && params[:confirm] == 'true' && @page.accept_comments?  
80 - add_comment 79 + if params[:comment] && params[self.icaptcha_field].blank? && params[:confirm] == 'true'
  80 + @comment = Comment.new(params[:comment])
  81 + if request.post? && @page.accept_comments?
  82 + add_comment
  83 + end
  84 + else
  85 + @comment = Comment.new
81 end 86 end
82 87
83 if request.post? && params[:remove_comment] 88 if request.post? && params[:remove_comment]
@@ -114,7 +119,6 @@ class ContentViewerController < ApplicationController @@ -114,7 +119,6 @@ class ContentViewerController < ApplicationController
114 protected 119 protected
115 120
116 def add_comment 121 def add_comment
117 - @comment = Comment.new(params[:comment])  
118 @comment.author = user if logged_in? 122 @comment.author = user if logged_in?
119 @comment.article = @page 123 @comment.article = @page
120 if @comment.save 124 if @comment.save
app/models/comment.rb
1 class Comment < ActiveRecord::Base 1 class Comment < ActiveRecord::Base
2 2
  3 + has_captcha
  4 +
3 track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"], :custom_target => :action_tracker_target 5 track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"], :custom_target => :action_tracker_target
4 6
5 validates_presence_of :title, :body 7 validates_presence_of :title, :body
app/views/content_viewer/_comment_form.rhtml
@@ -38,6 +38,10 @@ @@ -38,6 +38,10 @@
38 38
39 <%= required labelled_form_field(_('Title'), text_field(:comment, :title)) %> 39 <%= required labelled_form_field(_('Title'), text_field(:comment, :title)) %>
40 <%= required labelled_form_field(_('Enter your comment'), text_area(:comment, :body, :rows => 5)) %> 40 <%= required labelled_form_field(_('Enter your comment'), text_area(:comment, :body, :rows => 5)) %>
  41 +
  42 + <%= required labelled_form_field(_("What is the result of '%s = ?'") % @comment.captcha.task, text_field(:comment, :captcha_solution)) %>
  43 + <%= hidden_field(:comment, :captcha_secret) %>
  44 +
41 <% button_bar do %> 45 <% button_bar do %>
42 <%= submit_button('add', _('Post comment'), :onclick => "this.form.confirm.value = 'true'; this.disabled = true; this.form.submit(); return true;") %> 46 <%= submit_button('add', _('Post comment'), :onclick => "this.form.confirm.value = 'true'; this.disabled = true; this.form.submit(); return true;") %>
43 <%= button_to_function :cancel, _('Cancel'), "f=jQuery(this).parents('.post_comment_box'); f.removeClass('opened'); f.addClass('closed'); return false" %> 47 <%= button_to_function :cancel, _('Cancel'), "f=jQuery(this).parents('.post_comment_box'); f.removeClass('opened'); f.addClass('closed'); return false" %>
features/comment.feature
@@ -81,3 +81,16 @@ Feature: comment @@ -81,3 +81,16 @@ Feature: comment
81 Then I should see "Enter your comment" within "div#page-comment-form div.post_comment_box.opened" 81 Then I should see "Enter your comment" within "div#page-comment-form div.post_comment_box.opened"
82 And I should be exactly on /booking/article-with-comment 82 And I should be exactly on /booking/article-with-comment
83 And I should be moved to anchor "comment_form" 83 And I should be moved to anchor "comment_form"
  84 +
  85 + Scenario: ask captcha question
  86 + Given I am on /booking/article-with-comment
  87 + When I follow "Post a comment" within ".post-comment-button"
  88 + Then I should see "What is the result of "
  89 +
  90 + @selenium
  91 + Scenario: keep comments field filled while trying to do a comment
  92 + Given I am on /booking/article-with-comment
  93 + And I fill in "Name" with "Joey Ramone"
  94 + When I press "Post comment"
  95 + Then the "Name" field should contain "Joey Ramone"
  96 + And I should see "errors prohibited"
features/comment_reply.feature
@@ -64,9 +64,11 @@ Feature: comment @@ -64,9 +64,11 @@ Feature: comment
64 64
65 @selenium 65 @selenium
66 Scenario: reply a comment 66 Scenario: reply a comment
67 - Given I am logged in as "booking" 67 + Given skip comments captcha
68 And I go to /booking/another-article 68 And I go to /booking/another-article
69 And I follow "Reply" within ".comment-balloon" 69 And I follow "Reply" within ".comment-balloon"
  70 + And I fill in "Name" within "comment-balloon" with "Joey"
  71 + And I fill in "e-mail" within "comment-balloon" with "joey@ramones.com"
70 And I fill in "Title" within "comment-balloon" with "Hey ho, let's go!" 72 And I fill in "Title" within "comment-balloon" with "Hey ho, let's go!"
71 And I fill in "Enter your comment" within "comment-balloon" with "Hey ho, let's go!" 73 And I fill in "Enter your comment" within "comment-balloon" with "Hey ho, let's go!"
72 When I press "Post comment" within ".comment-balloon" 74 When I press "Post comment" within ".comment-balloon"
features/step_definitions/noosfero_steps.rb
@@ -359,6 +359,7 @@ Given /^the articles of &quot;(.+)&quot; are moderated$/ do |organization| @@ -359,6 +359,7 @@ Given /^the articles of &quot;(.+)&quot; are moderated$/ do |organization|
359 end 359 end
360 360
361 Given /^the following comments?$/ do |table| 361 Given /^the following comments?$/ do |table|
  362 + Comment.skip_captcha!
362 table.hashes.each do |item| 363 table.hashes.each do |item|
363 data = item.dup 364 data = item.dup
364 article = Article.find_by_name(data.delete("article")) 365 article = Article.find_by_name(data.delete("article"))
@@ -416,4 +417,6 @@ Given /^the search index is empty$/ do @@ -416,4 +417,6 @@ Given /^the search index is empty$/ do
416 ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => '*:*')) 417 ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => '*:*'))
417 end 418 end
418 419
419 - 420 +Given /^skip comments captcha$/ do
  421 + Comment.any_instance.stubs(:skip_captcha?).returns(true)
  422 +end
test/functional/content_viewer_controller_test.rb
@@ -15,6 +15,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -15,6 +15,7 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
15 15
16 @profile = create_user('testinguser').person 16 @profile = create_user('testinguser').person
17 @environment = @profile.environment 17 @environment = @profile.environment
  18 + Comment.skip_captcha!
18 end 19 end
19 attr_reader :profile, :environment 20 attr_reader :profile, :environment
20 21
test/functional/search_controller_test.rb
@@ -18,6 +18,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -18,6 +18,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
18 domain.save! 18 domain.save!
19 19
20 @product_category = fast_create(ProductCategory) 20 @product_category = fast_create(ProductCategory)
  21 + Comment.skip_captcha!
21 end 22 end
22 23
23 def create_article_with_optional_category(name, profile, category = nil) 24 def create_article_with_optional_category(name, profile, category = nil)
test/unit/article_test.rb
@@ -7,6 +7,7 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -7,6 +7,7 @@ class ArticleTest &lt; Test::Unit::TestCase
7 def setup 7 def setup
8 Test::Unit::TestCase::setup 8 Test::Unit::TestCase::setup
9 @profile = create_user('testing').person 9 @profile = create_user('testing').person
  10 + Comment.skip_captcha!
10 end 11 end
11 attr_reader :profile 12 attr_reader :profile
12 13
test/unit/category_finder_test.rb
@@ -8,7 +8,8 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase @@ -8,7 +8,8 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
8 @finder = CategoryFinder.new(@category) 8 @finder = CategoryFinder.new(@category)
9 @product_category = fast_create(ProductCategory, :name => 'Products') 9 @product_category = fast_create(ProductCategory, :name => 'Products')
10 10
11 - Profile.rebuild_solr_index 11 + Profile.rebuild_index
  12 + Comment.skip_captcha!
12 end 13 end
13 14
14 should 'search for articles in a specific category' do 15 should 'search for articles in a specific category' do
test/unit/category_test.rb
@@ -5,6 +5,7 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -5,6 +5,7 @@ class CategoryTest &lt; Test::Unit::TestCase
5 5
6 def setup 6 def setup
7 @env = fast_create(Environment) 7 @env = fast_create(Environment)
  8 + Comment.skip_captcha!
8 end 9 end
9 10
10 def test_mandatory_field_name 11 def test_mandatory_field_name
test/unit/comment_notifier_test.rb
@@ -10,6 +10,7 @@ class CommentNotifierTest &lt; Test::Unit::TestCase @@ -10,6 +10,7 @@ class CommentNotifierTest &lt; Test::Unit::TestCase
10 ActionMailer::Base.deliveries = [] 10 ActionMailer::Base.deliveries = []
11 @profile = create_user('user_comment_test').person 11 @profile = create_user('user_comment_test').person
12 @article = fast_create(Article, :name => 'Article test', :profile_id => @profile.id, :notify_comments => true) 12 @article = fast_create(Article, :name => 'Article test', :profile_id => @profile.id, :notify_comments => true)
  13 + Comment.skip_captcha!
13 end 14 end
14 15
15 should 'deliver mail after make aarticle commment' do 16 should 'deliver mail after make aarticle commment' do
test/unit/comment_test.rb
@@ -327,4 +327,11 @@ class CommentTest &lt; Test::Unit::TestCase @@ -327,4 +327,11 @@ class CommentTest &lt; Test::Unit::TestCase
327 assert_nil Comment.new(:email => 'my@email.com').author_url 327 assert_nil Comment.new(:email => 'my@email.com').author_url
328 end 328 end
329 329
  330 + should 'have the captcha_solution be solved' do
  331 + c = Comment.new
  332 + assert !c.valid? && c.errors.invalid?(:captcha_solution)
  333 + c.skip_captcha!
  334 + assert !c.valid? && !c.errors.invalid?(:captcha_solution)
  335 + end
  336 +
330 end 337 end
test/unit/community_test.rb
@@ -4,6 +4,7 @@ class CommunityTest &lt; Test::Unit::TestCase @@ -4,6 +4,7 @@ class CommunityTest &lt; Test::Unit::TestCase
4 4
5 def setup 5 def setup
6 @person = fast_create(Person) 6 @person = fast_create(Person)
  7 + Comment.skip_captcha!
7 end 8 end
8 9
9 attr_reader :person 10 attr_reader :person
test/unit/forum_helper_test.rb
@@ -12,6 +12,7 @@ class ForumHelperTest &lt; Test::Unit::TestCase @@ -12,6 +12,7 @@ class ForumHelperTest &lt; Test::Unit::TestCase
12 @environment = Environment.default 12 @environment = Environment.default
13 @profile = create_user('forum_helper_test').person 13 @profile = create_user('forum_helper_test').person
14 @forum = fast_create(Forum, :profile_id => profile.id, :name => 'Forum test') 14 @forum = fast_create(Forum, :profile_id => profile.id, :name => 'Forum test')
  15 + Comment.skip_captcha!
15 end 16 end
16 17
17 attr :profile 18 attr :profile