Skip to content
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

Enhancement/Recaptcha #648

Open
wants to merge 3 commits into
base: develop
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/commands/recaptcha/check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Recaptcha
class Check
RECAPTCHA_URI = 'https://www.google.com/recaptcha/api/siteverify'.freeze

def self.call(token, secret = ENV['RECAPTCHA_SECRET_KEY'])
new(token, secret).perform
end

def initialize(token, secret)
@token = token
@secret = secret
end

def perform
request = Net::HTTP::Post.new(uri)
request.set_form_data(secret: @secret, response: @token)

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: 3) do |req|
req.request(request)
end

JSON.parse(response.body).dig('success')
rescue Net::ReadTimeout, Net::OpenTimeout
false
end

def uri
@uri ||= URI(RECAPTCHA_URI)
end
end
end
4 changes: 1 addition & 3 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative '../helpers/recaptcha_helper'

class ContactsController < ApplicationController
def new
@contact = Contact.new(restroom_id: params['restroom_id'], restroom_name: params['restroom_name'])
Expand All @@ -15,7 +13,7 @@ def create

# Verify recaptcha code
recaptcha_response = params['g-recaptcha-response']
unless RecaptchaHelper.valid_token? recaptcha_response
unless Recaptcha::Check.call(recaptcha_response)
flash.now[:error] = I18n.t('helpers.reCAPTCHA.failed')
render :new
return
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/restrooms_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative '../helpers/recaptcha_helper'

class RestroomsController < ApplicationController
respond_to :html, :json

Expand Down Expand Up @@ -33,7 +31,7 @@ def create

# Verify recaptcha code
recaptcha_response = params['g-recaptcha-response']
unless RecaptchaHelper.valid_token? recaptcha_response
unless Recaptcha::Check.call(recaptcha_response)
flash.now[:error] = I18n.t('helpers.reCAPTCHA.failed')
render 'new'
return
Expand Down
25 changes: 0 additions & 25 deletions app/helpers/recaptcha_helper.rb

This file was deleted.

51 changes: 51 additions & 0 deletions spec/commands/recaptcha/check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

RSpec.describe Recaptcha::Check, type: :command do
subject { described_class.call(token, secret) }

let(:token) { 'token' }
let(:secret) { 'secret' }
let(:response_body) { { success: true } }
let(:response_status) { 200 }
let(:full_response) do
{
status: response_status,
body: response_body.to_json,
headers: { 'Content-Type' => 'application/json' }
}
end

before { stub_request(:post, described_class::RECAPTCHA_URI).to_return(full_response) }

context 'with valid params' do
it { is_expected.to be_truthy }

it 'requests with secret and token' do
subject

assert_requested(:post, described_class::RECAPTCHA_URI, times: 1) do |req|
req.body == 'secret=secret&response=token'
end
end
end

context 'with invalid params' do
let(:response_body) { { success: false } }

it 'responds with success: false in body' do
expect(subject).to be_falsy
end
end

context 'with a non responding server' do
before { stub_request(:any, described_class::RECAPTCHA_URI).to_timeout }

it 'does not raise error' do
expect {
subject
}.to_not raise_error
end

it { is_expected.to be_falsy }
end
end