Skip to content

Captcha #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ gem "semantic_menu", git: "git://github.com/michaek/semantic_menu.git"

gem 'will_paginate' # Pagination
gem 'redcarpet', '~> 3.0' # Markdown
gem 'simple_captcha', :git => 'git://github.com/ArturG/simple-captcha.git'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
17 changes: 13 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
GIT
remote: git://github.com/ArturG/simple-captcha.git
revision: 707e56a96c7f44af7f4da77099ab1c53d2860044
specs:
simple_captcha (0.1.6)

GIT
remote: git://github.com/michaek/semantic_menu.git
revision: 5298f586467cc9a91e85befcca673b8406c8846f
Expand Down Expand Up @@ -44,7 +50,9 @@ GEM
multi_json (~> 1.0)
addressable (2.3.5)
arel (3.0.3)
bcrypt-ruby (3.1.2)
bcrypt (3.1.7)
bcrypt-ruby (3.1.5)
bcrypt (>= 3.1.3)
bson (1.9.2)
bson_ext (1.9.2)
bson (~> 1.9.2)
Expand Down Expand Up @@ -126,7 +134,7 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
metaclass (0.0.3)
metaclass (0.0.4)
metadown (1.0.1)
redcarpet
method_source (0.8.2)
Expand Down Expand Up @@ -190,7 +198,7 @@ GEM
rspec-core (2.14.7)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.5)
rspec-mocks (2.14.6)
rspec-rails (2.14.1)
actionpack (>= 3.0)
activemodel (>= 3.0)
Expand All @@ -215,7 +223,7 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.8)
sqlite3 (1.3.9)
term-ansicolor (1.3.0)
tins (~> 1.0)
thor (0.18.1)
Expand Down Expand Up @@ -269,6 +277,7 @@ DEPENDENCIES
rspec-rails
sass-rails
semantic_menu!
simple_captcha!
simple_form!
simplecov
sqlite3
Expand Down
34 changes: 34 additions & 0 deletions app/assets/stylesheets/questions.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,37 @@
float:right;
padding-top:4px;
}

.simple_captcha {
border: 1px solid #ccc; padding: 5px !important;
}

.simple_captcha, .simple_captcha div {
margin-left: 150px;
display: table;
}

.simple_captcha .simple_captcha_field, .simple_captcha .simple_captcha_image {
border: 1px solid #ccc;
margin: 0px 0px 2px 0px !important;
padding: 0px !important;
}

.simple_captcha .simple_captcha_image img {
margin: 0px !important;
padding: 0px !important;
width: 110px !important;
}

.simple_captcha .simple_captcha_label {
font-size: 12px;
}

.simple_captcha .simple_captcha_field input {
width: 150px !important;
font-size: 16px;
border: none;
background-color: #efefef;
}


16 changes: 10 additions & 6 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ def create
@answer = Answer.create params[:answer]
@answer.question = @question
@answer.user = current_user

Notification.new_answer(@question).deliver

create!(:notice => "Answer Posted!"){ question_url(params[:question_id]) }

captcha = params[:answer][:captcha]
captcha_key = params[:answer][:captcha_key]

if simple_captcha_valid?(captcha, captcha_key) && !captcha_key.nil? && !captcha_key.nil?
Notification.new_answer(@question).deliver
create!(:notice => "Answer Posted!"){ question_url(params[:question_id]) }
else
redirect_to @question, notice: "Incorrect captcha, are you robot?"
end
end

def update
Expand All @@ -19,7 +25,5 @@ def update
format.html { redirect_to question_url(resource.question) }
end
end


end

2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers

protect_from_forgery
helper_method :title

Expand Down
1 change: 0 additions & 1 deletion app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ class Answer

belongs_to :question
belongs_to :user

end
3 changes: 2 additions & 1 deletion app/views/answers/_form_content.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
.inputs
= f.input :description, :as => :text, :label => "Answer",
:hint => "You can format your input with <a href=\"http://daringfireball.net/projects/markdown/\">Markdown</a>".html_safe

.inputs
= f.show_simple_captcha
.actions
= f.button :submit, :value => "Post Answer", :class => "primary btn"

5 changes: 5 additions & 0 deletions app/views/simple_captcha/_simple_captcha.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.simple_captcha
.simple_captcha_image
= simple_captcha_options[:image]
.simple_captcha_field
= simple_captcha_options[:field]
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
Bundler.require(:default, :assets, Rails.env)
SimpleCaptcha.store = 'mongomapper'
end

module HacketyHackCom
Expand Down
7 changes: 7 additions & 0 deletions config/locales/simple_captcha.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
simple_captcha:
placeholder: "Enter the value"
label: "Enter the code in the box:"
message:
default: "Secret Code did not match with the Image"
user: "The secret Image and code were different"
6 changes: 4 additions & 2 deletions features/answers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Feature: CRUD actions for answers
Scenario: Create an answer
Given a question exists
And I answer that question
Then I should be notified that my answer was submitted
Then I should see captcha
And I should be notified that my answer was submitted
And an email should be sent to the author
And I should be able to see my answer
And my answer should show on my profile page
Expand All @@ -14,5 +15,6 @@ Feature: CRUD actions for answers
Given a question exists
And I answer that question
When I edit that answer
Then I should see the updated answer
Then I should see captcha
And I should see the updated answer

5 changes: 5 additions & 0 deletions features/step_definitions/answer_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@
page.should have_content('Edit: Did you try magic?')
end

Then(/^I should see captcha$/) do
page.should have_css('.simple_captcha')
page.should have_css('.simple_captcha_image')
page.should have_css('.simple_captcha_field')
end
28 changes: 28 additions & 0 deletions spec/controllers/answers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe AnswersController do
let(:newbie){Fabricate(:user)}
let(:tricky){Fabricate(:question)}

describe "#create" do

before(:each) do
sign_in newbie
@valid_captcha_answer = { :description => "Foo", :captcha => "Bar", :captcha_key => "FooBar" }
@invalid_captcha_answer = { :description => "Foo"}
end

context "post answer" do
it "responds with success if captcha is correct" do
sign_in newbie
post :create, :question_id => tricky, :answer => @valid_captcha_answer
flash[:notice].should == "Answer Posted!"
end

it "responds with failure if captcha is incorrect" do
post :create, :question_id => tricky, :answer => @invalid_captcha_answer
flash[:notice].should == "Incorrect captcha, are you robot?"
end
end
end
end
2 changes: 1 addition & 1 deletion spec/fabricators/answer_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Fabricator(:answer) do
description "MyString"
user ""
question_id question
end
2 changes: 1 addition & 1 deletion spec/views/questions/index.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

it "renders an autodiscovery link in for the head content" do
render :template => "questions/index.html.haml", :locals => {:collection => [question]}
render :template => "questions/index", :handlers => [:haml], :locals => {:collection => [question]}

view.content_for(:head).should include(questions_url(format: :atom))
end
Expand Down