2015-10-26 13:55:46 +00:00
|
|
|
require 'spec_helper'
|
2016-10-14 09:33:34 +00:00
|
|
|
require 'securerandom'
|
2015-10-26 13:55:46 +00:00
|
|
|
|
|
|
|
describe RuCaptcha do
|
2016-10-14 09:33:34 +00:00
|
|
|
class CustomSession
|
|
|
|
attr_accessor :id
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
self.id = SecureRandom.hex
|
|
|
|
end
|
|
|
|
end
|
2015-10-26 13:55:46 +00:00
|
|
|
class Simple < ActionController::Base
|
|
|
|
def session
|
2016-10-14 09:33:34 +00:00
|
|
|
@session ||= CustomSession.new
|
2015-10-26 13:55:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
|
|
|
@params ||= {}
|
|
|
|
end
|
2016-10-14 09:33:34 +00:00
|
|
|
|
|
|
|
def custom_session
|
|
|
|
Rails.cache.read(self.rucaptcha_sesion_key_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_custom_session
|
|
|
|
Rails.cache.delete(self.rucaptcha_sesion_key_key)
|
|
|
|
end
|
2015-10-26 13:55:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:simple) { Simple.new }
|
|
|
|
|
2016-10-14 09:33:34 +00:00
|
|
|
describe '.rucaptcha_sesion_key_key' do
|
|
|
|
it 'should work' do
|
|
|
|
expect(simple.rucaptcha_sesion_key_key).to eq ['rucaptcha-session', simple.session.id].join(':')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-26 13:55:46 +00:00
|
|
|
describe '.generate_rucaptcha' do
|
|
|
|
it 'should work' do
|
2015-10-30 03:32:02 +00:00
|
|
|
expect(RuCaptcha::Captcha).to receive(:random_chars).and_return('abcd')
|
2015-10-26 13:55:46 +00:00
|
|
|
expect(simple.generate_rucaptcha).not_to be_nil
|
2016-10-14 09:33:34 +00:00
|
|
|
expect(simple.custom_session[:code]).to eq('abcd')
|
2015-10-26 13:55:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.verify_rucaptcha?' do
|
2016-04-25 02:09:35 +00:00
|
|
|
context 'Nil of param' do
|
|
|
|
it 'should work when params[:_rucaptcha] is nil' do
|
|
|
|
simple.params[:_rucaptcha] = nil
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should work when session[:_rucaptcha] is nil' do
|
2016-10-14 09:33:34 +00:00
|
|
|
simple.clean_custom_session
|
2016-04-25 02:09:35 +00:00
|
|
|
simple.params[:_rucaptcha] = 'Abcd'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-26 13:55:46 +00:00
|
|
|
context 'Correct chars in params' do
|
|
|
|
it 'should work' do
|
2016-10-14 09:33:34 +00:00
|
|
|
Rails.cache.write(simple.rucaptcha_sesion_key_key, {
|
|
|
|
time: Time.now.to_i,
|
|
|
|
code: 'abcd'
|
|
|
|
})
|
2015-10-26 13:55:46 +00:00
|
|
|
simple.params[:_rucaptcha] = 'Abcd'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(true)
|
2016-10-14 09:33:34 +00:00
|
|
|
expect(simple.custom_session).to eq nil
|
|
|
|
|
|
|
|
Rails.cache.write(simple.rucaptcha_sesion_key_key, {
|
|
|
|
time: Time.now.to_i,
|
|
|
|
code: 'abcd'
|
|
|
|
})
|
2015-10-26 13:55:46 +00:00
|
|
|
simple.params[:_rucaptcha] = 'AbcD'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Incorrect chars' do
|
2016-05-23 07:11:04 +00:00
|
|
|
it 'should work' do
|
2016-10-14 09:33:34 +00:00
|
|
|
Rails.cache.write(simple.rucaptcha_sesion_key_key, {
|
|
|
|
time: Time.now.to_i - 60,
|
|
|
|
code: 'abcd'
|
|
|
|
})
|
2015-10-26 13:55:46 +00:00
|
|
|
simple.params[:_rucaptcha] = 'd123'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(false)
|
2016-10-14 09:33:34 +00:00
|
|
|
expect(simple.custom_session).to eq nil
|
2015-10-26 13:55:46 +00:00
|
|
|
end
|
|
|
|
end
|
2015-11-02 08:59:42 +00:00
|
|
|
|
|
|
|
describe 'Expires Session key' do
|
2016-05-23 07:11:04 +00:00
|
|
|
it 'should work' do
|
2016-10-14 09:33:34 +00:00
|
|
|
Rails.cache.write(simple.rucaptcha_sesion_key_key, {
|
|
|
|
time: Time.now.to_i - 121,
|
|
|
|
code: 'abcd'
|
|
|
|
})
|
2015-11-02 08:59:42 +00:00
|
|
|
simple.params[:_rucaptcha] = 'abcd'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2015-10-26 13:55:46 +00:00
|
|
|
end
|
|
|
|
end
|