From 8849f0f95ffae506fb0547c93648ca9af53c9d6c Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Tue, 9 Aug 2011 15:12:42 -0300 Subject: [PATCH] Always ask captcha question on comments --- app/controllers/public/content_viewer_controller.rb | 10 +++++++--- app/models/comment.rb | 2 ++ app/views/content_viewer/_comment_form.rhtml | 4 ++++ features/comment.feature | 13 +++++++++++++ features/comment_reply.feature | 4 +++- features/step_definitions/noosfero_steps.rb | 5 ++++- test/functional/content_viewer_controller_test.rb | 1 + test/functional/search_controller_test.rb | 1 + test/unit/article_test.rb | 1 + test/unit/category_finder_test.rb | 3 ++- test/unit/category_test.rb | 1 + test/unit/comment_notifier_test.rb | 1 + test/unit/comment_test.rb | 7 +++++++ test/unit/community_test.rb | 1 + test/unit/forum_helper_test.rb | 1 + 15 files changed, 49 insertions(+), 6 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 121a1fa..fa17d3f 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -76,8 +76,13 @@ class ContentViewerController < ApplicationController @form_div = params[:form] - if request.post? && params[:comment] && params[self.icaptcha_field].blank? && params[:confirm] == 'true' && @page.accept_comments? - add_comment + if params[:comment] && params[self.icaptcha_field].blank? && params[:confirm] == 'true' + @comment = Comment.new(params[:comment]) + if request.post? && @page.accept_comments? + add_comment + end + else + @comment = Comment.new end if request.post? && params[:remove_comment] @@ -114,7 +119,6 @@ class ContentViewerController < ApplicationController protected def add_comment - @comment = Comment.new(params[:comment]) @comment.author = user if logged_in? @comment.article = @page if @comment.save diff --git a/app/models/comment.rb b/app/models/comment.rb index e0385a2..024fdc3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,7 @@ class Comment < ActiveRecord::Base + has_captcha + track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"], :custom_target => :action_tracker_target validates_presence_of :title, :body diff --git a/app/views/content_viewer/_comment_form.rhtml b/app/views/content_viewer/_comment_form.rhtml index 6cd9286..d5191a1 100644 --- a/app/views/content_viewer/_comment_form.rhtml +++ b/app/views/content_viewer/_comment_form.rhtml @@ -38,6 +38,10 @@ <%= required labelled_form_field(_('Title'), text_field(:comment, :title)) %> <%= required labelled_form_field(_('Enter your comment'), text_area(:comment, :body, :rows => 5)) %> + + <%= required labelled_form_field(_("What is the result of '%s = ?'") % @comment.captcha.task, text_field(:comment, :captcha_solution)) %> + <%= hidden_field(:comment, :captcha_secret) %> + <% button_bar do %> <%= submit_button('add', _('Post comment'), :onclick => "this.form.confirm.value = 'true'; this.disabled = true; this.form.submit(); return true;") %> <%= button_to_function :cancel, _('Cancel'), "f=jQuery(this).parents('.post_comment_box'); f.removeClass('opened'); f.addClass('closed'); return false" %> diff --git a/features/comment.feature b/features/comment.feature index 4c24e8b..3a416cd 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -81,3 +81,16 @@ Feature: comment Then I should see "Enter your comment" within "div#page-comment-form div.post_comment_box.opened" And I should be exactly on /booking/article-with-comment And I should be moved to anchor "comment_form" + + Scenario: ask captcha question + Given I am on /booking/article-with-comment + When I follow "Post a comment" within ".post-comment-button" + Then I should see "What is the result of " + + @selenium + Scenario: keep comments field filled while trying to do a comment + Given I am on /booking/article-with-comment + And I fill in "Name" with "Joey Ramone" + When I press "Post comment" + Then the "Name" field should contain "Joey Ramone" + And I should see "errors prohibited" diff --git a/features/comment_reply.feature b/features/comment_reply.feature index 1b59396..6192956 100644 --- a/features/comment_reply.feature +++ b/features/comment_reply.feature @@ -64,9 +64,11 @@ Feature: comment @selenium Scenario: reply a comment - Given I am logged in as "booking" + Given skip comments captcha And I go to /booking/another-article And I follow "Reply" within ".comment-balloon" + And I fill in "Name" within "comment-balloon" with "Joey" + And I fill in "e-mail" within "comment-balloon" with "joey@ramones.com" And I fill in "Title" within "comment-balloon" with "Hey ho, let's go!" And I fill in "Enter your comment" within "comment-balloon" with "Hey ho, let's go!" When I press "Post comment" within ".comment-balloon" diff --git a/features/step_definitions/noosfero_steps.rb b/features/step_definitions/noosfero_steps.rb index a7a98fa..9eedcd1 100644 --- a/features/step_definitions/noosfero_steps.rb +++ b/features/step_definitions/noosfero_steps.rb @@ -359,6 +359,7 @@ Given /^the articles of "(.+)" are moderated$/ do |organization| end Given /^the following comments?$/ do |table| + Comment.skip_captcha! table.hashes.each do |item| data = item.dup article = Article.find_by_name(data.delete("article")) @@ -416,4 +417,6 @@ Given /^the search index is empty$/ do ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => '*:*')) end - +Given /^skip comments captcha$/ do + Comment.any_instance.stubs(:skip_captcha?).returns(true) +end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index cd58673..fe04760 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -15,6 +15,7 @@ class ContentViewerControllerTest < Test::Unit::TestCase @profile = create_user('testinguser').person @environment = @profile.environment + Comment.skip_captcha! end attr_reader :profile, :environment diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 9b46e1c..a3e29a9 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -18,6 +18,7 @@ class SearchControllerTest < Test::Unit::TestCase domain.save! @product_category = fast_create(ProductCategory) + Comment.skip_captcha! end def create_article_with_optional_category(name, profile, category = nil) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index b46f105..6eba630 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -7,6 +7,7 @@ class ArticleTest < Test::Unit::TestCase def setup Test::Unit::TestCase::setup @profile = create_user('testing').person + Comment.skip_captcha! end attr_reader :profile diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index 1cb2323..a397b53 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -8,7 +8,8 @@ class CategoryFinderTest < ActiveSupport::TestCase @finder = CategoryFinder.new(@category) @product_category = fast_create(ProductCategory, :name => 'Products') - Profile.rebuild_solr_index + Profile.rebuild_index + Comment.skip_captcha! end should 'search for articles in a specific category' do diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index cacf305..5d50271 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -5,6 +5,7 @@ class CategoryTest < Test::Unit::TestCase def setup @env = fast_create(Environment) + Comment.skip_captcha! end def test_mandatory_field_name diff --git a/test/unit/comment_notifier_test.rb b/test/unit/comment_notifier_test.rb index 3b9c38e..5a55489 100644 --- a/test/unit/comment_notifier_test.rb +++ b/test/unit/comment_notifier_test.rb @@ -10,6 +10,7 @@ class CommentNotifierTest < Test::Unit::TestCase ActionMailer::Base.deliveries = [] @profile = create_user('user_comment_test').person @article = fast_create(Article, :name => 'Article test', :profile_id => @profile.id, :notify_comments => true) + Comment.skip_captcha! end should 'deliver mail after make aarticle commment' do diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 96e95f2..ceedc28 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -327,4 +327,11 @@ class CommentTest < Test::Unit::TestCase assert_nil Comment.new(:email => 'my@email.com').author_url end + should 'have the captcha_solution be solved' do + c = Comment.new + assert !c.valid? && c.errors.invalid?(:captcha_solution) + c.skip_captcha! + assert !c.valid? && !c.errors.invalid?(:captcha_solution) + end + end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index 9588ca6..37c45f4 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -4,6 +4,7 @@ class CommunityTest < Test::Unit::TestCase def setup @person = fast_create(Person) + Comment.skip_captcha! end attr_reader :person diff --git a/test/unit/forum_helper_test.rb b/test/unit/forum_helper_test.rb index cbeccbf..e14c1b7 100644 --- a/test/unit/forum_helper_test.rb +++ b/test/unit/forum_helper_test.rb @@ -12,6 +12,7 @@ class ForumHelperTest < Test::Unit::TestCase @environment = Environment.default @profile = create_user('forum_helper_test').person @forum = fast_create(Forum, :profile_id => profile.id, :name => 'Forum test') + Comment.skip_captcha! end attr :profile -- libgit2 0.21.2