rucaptcha/spec/controller_helpers_spec.rb

116 lines
3.2 KiB
Ruby
Raw Normal View History

2015-10-26 13:55:46 +00:00
require 'spec_helper'
require 'securerandom'
2015-10-26 13:55:46 +00:00
describe RuCaptcha do
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
@session ||= CustomSession.new
2015-10-26 13:55:46 +00:00
end
def params
@params ||= {}
end
def custom_session
2016-10-29 03:06:10 +00:00
RuCaptcha.cache.read(self.rucaptcha_sesion_key_key)
end
def clean_custom_session
2016-10-29 03:06:10 +00:00
RuCaptcha.cache.delete(self.rucaptcha_sesion_key_key)
end
2015-10-26 13:55:46 +00:00
end
let(:simple) { Simple.new }
describe '.rucaptcha_sesion_key_key' do
it 'should work' do
2019-12-20 07:36:24 +00:00
session_id_digest = Digest::SHA256.hexdigest(simple.session.id.inspect)
expect(simple.rucaptcha_sesion_key_key).to eq ['rucaptcha-session', session_id_digest].join(':')
end
end
2015-10-26 13:55:46 +00:00
describe '.generate_rucaptcha' do
it 'should work' do
allow(RuCaptcha).to receive(:create).and_return(['abcde', 'fake image data'])
expect(simple.generate_rucaptcha).to eq 'fake image data'
expect(simple.custom_session[:code]).to eq('abcde')
2015-10-26 13:55:46 +00:00
end
end
describe '.verify_rucaptcha?' do
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
simple.clean_custom_session
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-29 03:06:10 +00:00
RuCaptcha.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)
expect(simple.custom_session).to eq nil
2016-10-29 03:06:10 +00:00
RuCaptcha.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
it 'should keep session when given :keep_session' do
RuCaptcha.cache.write(simple.rucaptcha_sesion_key_key, {
time: Time.now.to_i,
code: 'abcd'
})
simple.params[:_rucaptcha] = 'abcd'
expect(simple.verify_rucaptcha?(nil, keep_session: true)).to eq(true)
expect(simple.custom_session).not_to eq nil
expect(simple.verify_rucaptcha?).to eq(true)
expect(simple.verify_rucaptcha?).to eq(false)
end
2015-10-26 13:55:46 +00:00
end
describe 'Incorrect chars' do
2016-05-23 07:11:04 +00:00
it 'should work' do
2016-10-29 03:06:10 +00:00
RuCaptcha.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)
expect(simple.custom_session).to eq nil
2015-10-26 13:55:46 +00:00
end
end
describe 'Expires Session key' do
2016-05-23 07:11:04 +00:00
it 'should work' do
2016-10-29 03:06:10 +00:00
RuCaptcha.cache.write(simple.rucaptcha_sesion_key_key, {
time: Time.now.to_i - 121,
code: 'abcd'
})
simple.params[:_rucaptcha] = 'abcd'
expect(simple.verify_rucaptcha?).to eq(false)
end
end
2015-10-26 13:55:46 +00:00
end
end