2015-10-26 13:55:46 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe RuCaptcha do
|
|
|
|
class Simple < ActionController::Base
|
|
|
|
def session
|
|
|
|
@session ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
|
|
|
@params ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:simple) { Simple.new }
|
|
|
|
|
|
|
|
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
|
|
|
|
expect(simple.session[:_rucaptcha]).to eq('abcd')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.verify_rucaptcha?' do
|
|
|
|
context 'Correct chars in params' do
|
|
|
|
it 'should work' do
|
2015-11-02 08:59:42 +00:00
|
|
|
simple.session[:_rucaptcha_at] = Time.now.to_i
|
2015-10-26 13:55:46 +00:00
|
|
|
simple.session[:_rucaptcha] = 'abcd'
|
|
|
|
simple.params[:_rucaptcha] = 'Abcd'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(true)
|
|
|
|
simple.params[:_rucaptcha] = 'AbcD'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Incorrect chars' do
|
|
|
|
it "should work" do
|
2015-11-02 08:59:42 +00:00
|
|
|
simple.session[:_rucaptcha_at] = Time.now.to_i - 60
|
2015-10-26 13:55:46 +00:00
|
|
|
simple.session[:_rucaptcha] = 'abcd'
|
|
|
|
simple.params[:_rucaptcha] = 'd123'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2015-11-02 08:59:42 +00:00
|
|
|
|
|
|
|
describe 'Expires Session key' do
|
|
|
|
it "should work" do
|
|
|
|
simple.session[:_rucaptcha_at] = Time.now.to_i - 121
|
|
|
|
simple.session[:_rucaptcha] = 'abcd'
|
|
|
|
simple.params[:_rucaptcha] = 'abcd'
|
|
|
|
expect(simple.verify_rucaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2015-10-26 13:55:46 +00:00
|
|
|
end
|
|
|
|
end
|