diff --git a/Gemfile b/Gemfile index f3e292881..afa51a69e 100644 --- a/Gemfile +++ b/Gemfile @@ -26,7 +26,7 @@ gem 'acts-as-taggable-on' gem "responders" gem 'foundation-rails' gem 'acts_as_votable' -gem "recaptcha", require: "recaptcha/rails" +gem 'simple_captcha2', require: 'simple_captcha' gem 'ckeditor' gem 'cancancan' gem 'social-share-button' diff --git a/Gemfile.lock b/Gemfile.lock index f3c45c93d..9b4dc71a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -205,7 +205,6 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (10.4.2) - recaptcha (0.4.0) responders (2.1.0) railties (>= 4.2.0, < 5) rest-client (1.8.0) @@ -236,6 +235,8 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (~> 1.1) + simple_captcha2 (0.3.4) + rails (>= 4.1) simplecov (0.10.0) docile (~> 1.1.0) json (~> 1.8) @@ -317,10 +318,10 @@ DEPENDENCIES poltergeist quiet_assets rails (= 4.2.3) - recaptcha responders rspec-rails (~> 3.0) sass-rails (~> 5.0) + simple_captcha2 social-share-button spring turbolinks diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ad69fd511..083165303 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,8 @@ require "application_responder" class ApplicationController < ActionController::Base - check_authorization unless: :devise_controller? - + include SimpleCaptcha::ControllerHelpers self.responder = ApplicationResponder respond_to :html @@ -38,9 +37,4 @@ class ApplicationController < ActionController::Base end end - def verify_captcha?(resource) - return true unless recaptcha_keys? - verify_recaptcha(model: resource) - end - end diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index d72965432..84f7d11a4 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -1,5 +1,4 @@ class DebatesController < ApplicationController - include RecaptchaHelper before_action :authenticate_user!, except: [:index, :show] load_and_authorize_resource @@ -28,7 +27,7 @@ class DebatesController < ApplicationController def create @debate = Debate.new(debate_params) @debate.author = current_user - if verify_captcha?(@debate) and @debate.save + if @debate.save_with_captcha redirect_to @debate, notice: t('flash.actions.create.notice', resource_name: 'Debate') else render :new @@ -52,7 +51,7 @@ class DebatesController < ApplicationController end def debate_params - params.require(:debate).permit(:title, :description, :tag_list, :terms_of_service) + params.require(:debate).permit(:title, :description, :tag_list, :terms_of_service, :captcha, :captcha_key) end def set_voted_values(debates_ids) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 4991cdf70..c979d6315 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -1,12 +1,10 @@ class RegistrationsController < Devise::RegistrationsController - include RecaptchaHelper def create - if verify_captcha?(resource) + build_resource(sign_up_params) + if resource.valid_with_captcha? super else - build_resource(sign_up_params) - flash.now[:alert] = t('recaptcha.errors.verification_failed') render :new end end @@ -15,7 +13,7 @@ class RegistrationsController < Devise::RegistrationsController private def sign_up_params - params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :use_nickname, :nickname) + params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :use_nickname, :nickname, :captcha, :captcha_key) end end diff --git a/app/helpers/recaptcha_helper.rb b/app/helpers/recaptcha_helper.rb deleted file mode 100644 index fb4cceb20..000000000 --- a/app/helpers/recaptcha_helper.rb +++ /dev/null @@ -1,12 +0,0 @@ -module RecaptchaHelper - - def recaptchable?(resource) - resource.new_record? - end - - def recaptcha_keys? - Recaptcha.configuration.public_key.present? && - Recaptcha.configuration.private_key.present? - end - -end \ No newline at end of file diff --git a/app/models/debate.rb b/app/models/debate.rb index 28181101a..26fd2dbf0 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,6 +1,6 @@ require 'numeric' class Debate < ActiveRecord::Base - + apply_simple_captcha TITLE_LENGTH = Debate.columns.find{|c| c.name == 'title'}.limit acts_as_votable diff --git a/app/models/user.rb b/app/models/user.rb index 4eb832546..6857d981c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + apply_simple_captcha devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index c38d2c476..56af80136 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -37,10 +37,10 @@
This is
' + fill_in 'debate_captcha', with: correct_captcha_text check 'debate_terms_of_service' click_button 'Create Debate' @@ -86,6 +88,7 @@ feature 'Debates' do fill_in 'debate_title', with: 'A test' fill_in 'debate_description', with: 'A test' fill_in 'debate_tag_list', with: 'user_id=1, &a=3, ' + fill_in 'debate_captcha', with: SimpleCaptcha::SimpleCaptchaData.first.value check 'debate_terms_of_service' click_button 'Create Debate' diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb index 8ea48eabc..7c0b3209a 100644 --- a/spec/features/tags_spec.rb +++ b/spec/features/tags_spec.rb @@ -65,6 +65,7 @@ feature 'Tags' do visit new_debate_path fill_in 'debate_title', with: 'Title' fill_in 'debate_description', with: 'Description' + fill_in 'debate_captcha', with: correct_captcha_text check 'debate_terms_of_service' fill_in 'debate_tag_list', with: "Impuestos, Economía, Hacienda" diff --git a/spec/features/users_spec.rb b/spec/features/users_spec.rb index c15869bae..be67c3e27 100644 --- a/spec/features/users_spec.rb +++ b/spec/features/users_spec.rb @@ -11,6 +11,7 @@ feature 'Users' do fill_in 'user_email', with: 'manuela@madrid.es' fill_in 'user_password', with: 'judgementday' fill_in 'user_password_confirmation', with: 'judgementday' + fill_in 'user_captcha', with: correct_captcha_text click_button 'Sign up' diff --git a/spec/helpers/recaptcha_helper_spec.rb b/spec/helpers/recaptcha_helper_spec.rb deleted file mode 100644 index 7d44e2f08..000000000 --- a/spec/helpers/recaptcha_helper_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'rails_helper' - -describe RecaptchaHelper do - - describe '#recaptchable?' do - - it 'should be true if new record' do - debate = build(:debate) - expect(helper.recaptchable?(debate)).to be true - end - - it 'should be false if existing record' do - debate = create(:debate) - expect(helper.recaptchable?(debate)).to be false - end - - end - - describe "#recaptcha_keys?" do - - it "should be true if Recaptcha keys are configured" do - allow(Recaptcha.configuration).to receive(:public_key).and_return("akjasf") - allow(Recaptcha.configuration).to receive(:private_key).and_return("akjasf4532") - - expect(helper.recaptcha_keys?).to be true - end - - it "should be false if Recaptcha keys are not configured" do - allow(Recaptcha.configuration).to receive(:public_key).and_return(nil) - allow(Recaptcha.configuration).to receive(:private_key).and_return(nil) - - expect(helper.recaptcha_keys?).to be false - end - - end -end diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index d07f60261..b625138b0 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -9,6 +9,7 @@ module CommonActions fill_in 'user_email', with: 'manuela@madrid.es' fill_in 'user_password', with: 'judgementday' fill_in 'user_password_confirmation', with: 'judgementday' + fill_in 'user_captcha', with: correct_captcha_text click_button 'Sign up' end @@ -51,4 +52,8 @@ module CommonActions expect(page).to have_content 'It will be done next week.' end + def correct_captcha_text + SimpleCaptcha::SimpleCaptchaData.first.value + end + end \ No newline at end of file