From b06e3c400334cc8f19ebb44aecae8e162c84bf82 Mon Sep 17 00:00:00 2001 From: JoenioCosta Date: Thu, 27 Mar 2008 21:55:07 +0000 Subject: [PATCH] ActionItem146: implementing basic anti-spam for comments --- app/controllers/public/content_viewer_controller.rb | 4 +++- app/views/content_viewer/_comment_form.rhtml | 1 + test/functional/content_viewer_controller_test.rb | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 4b3c526..777dec7 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -2,6 +2,8 @@ class ContentViewerController < PublicController needs_profile + inverse_captcha :field => 'e_mail' + def view_page path = params[:page].join('/') @@ -32,7 +34,7 @@ class ContentViewerController < PublicController return end - if request.post? && params[:comment] + if request.post? && params[:comment] && params[self.icaptcha_field].blank? add_comment end diff --git a/app/views/content_viewer/_comment_form.rhtml b/app/views/content_viewer/_comment_form.rhtml index 3ec52b0..77b1254 100644 --- a/app/views/content_viewer/_comment_form.rhtml +++ b/app/views/content_viewer/_comment_form.rhtml @@ -19,6 +19,7 @@ <%= labelled_form_field(_('Name'), text_field(:comment, :name)) %> <%= labelled_form_field(_('e-mail'), text_field(:comment, :email)) %> + <%= icaptcha_field() %>

<%= _('If you are a registered user, you can login and be automatically recognized.') %> diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 355fd3c..3456fbe 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -90,7 +90,7 @@ class ContentViewerControllerTest < Test::Unit::TestCase # for example, RSS feeds profile = create_user('someone').person page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') -page.save! + page.save! feed = RssFeed.new(:name => 'testfeed') feed.profile = profile @@ -153,5 +153,24 @@ page.save! end + should 'not be able to post comment while inverse captcha field filled' do + profile = create_user('popstar').person + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') + page.save! + profile.home_page = page; profile.save! + + assert_no_difference Comment, :count do + post :view_page, :profile => profile.identifier, :page => [ 'myarticle' ], @controller.icaptcha_field => 'filled', :comment => { :title => 'crap!', :body => 'I think that this article is crap', :name => 'Anonymous coward', :email => 'coward@anonymous.com' } + end + end + + should 'render inverse captcha field' do + profile = create_user('popstar').person + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') + page.save! + profile.home_page = page; profile.save! + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] + assert_tag :tag => 'input', :attributes => { :type => 'text', :name => @controller.icaptcha_field } + end end -- libgit2 0.21.2