Add some test case

This commit is contained in:
Jason Lee 2015-10-26 21:55:46 +08:00
parent d3e6240d3a
commit 4ec52d887b
8 changed files with 174 additions and 2 deletions

7
Gemfile Normal file
View File

@ -0,0 +1,7 @@
source 'https://rubygems.org'
gemspec
# For test OCR
gem 'rtesseract'
gem 'mini_magick'
gem 'active_support'

44
Gemfile.lock Normal file
View File

@ -0,0 +1,44 @@
PATH
remote: .
specs:
rucaptcha (0.1.3)
posix-spawn (>= 0.3.0)
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
mini_magick (4.3.6)
mini_portile (0.6.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
posix-spawn (0.3.11)
rake (10.4.2)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
rtesseract (1.3.1)
nokogiri
PLATFORMS
ruby
DEPENDENCIES
mini_magick
rake
rspec (>= 3.3.0)
rtesseract
rucaptcha!
BUNDLED WITH
1.10.6

View File

@ -1,4 +1,6 @@
require 'active_support/core_ext'
require 'rails'
require 'action_controller'
require 'active_support/all'
require_relative 'rucaptcha/version'
require_relative 'rucaptcha/configuration'
require_relative 'rucaptcha/controller_helpers'

View File

@ -13,7 +13,7 @@ module RuCaptcha
def random_rucaptcha_chars
chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
chars.gsub!(/[0ol1]/i, (rand(9) + 1).to_s)
chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
chars
end

12
spec/configure_spec.rb Normal file
View File

@ -0,0 +1,12 @@
require 'spec_helper'
describe RuCaptcha do
describe 'normal' do
it 'should read right config with spec_helper set' do
expect(RuCaptcha.config.len).to eq(2)
expect(RuCaptcha.config.width).to eq(123)
expect(RuCaptcha.config.height).to eq(33)
expect(RuCaptcha.config.implode).to eq(0.111)
end
end
end

View File

@ -0,0 +1,59 @@
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
expect(simple).to receive(:random_rucaptcha_chars).and_return('abcd')
expect(simple.generate_rucaptcha).not_to be_nil
expect(simple.session[:_rucaptcha]).to eq('abcd')
end
end
describe '.random_rucaptcha_chars' do
it 'should len equal config.len' do
expect(simple.random_rucaptcha_chars.length).to eq(RuCaptcha.config.len)
end
it 'should return 0-9 and lower str' do
expect(simple.random_rucaptcha_chars).to match(/[a-z0-9]/)
end
it 'should not include [0ol1]' do
10000.times do
expect(simple.random_rucaptcha_chars).not_to match(/[0ol1]/i)
end
end
end
describe '.verify_rucaptcha?' do
context 'Correct chars in params' do
it 'should work' do
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
simple.session[:_rucaptcha] = 'abcd'
simple.params[:_rucaptcha] = 'd123'
expect(simple.verify_rucaptcha?).to eq(false)
end
end
end
end

34
spec/ocr_spec.rb Normal file
View File

@ -0,0 +1,34 @@
require 'spec_helper'
describe 'OCR' do
before do
@samples = []
10.times do
@samples << SecureRandom.hex(2)
end
@filenames = []
@samples.each do |chars|
fname = File.join(File.dirname(__FILE__), "..", "tmp", "#{chars}.png")
img = RuCaptcha::Captcha.create(chars)
File.open(fname, 'w+') do |f|
f.puts img
end
@filenames << fname
end
end
after do
`rm #{File.join(File.dirname(__FILE__), '../tmp/*.png')}`
end
it 'should not read by OCR lib' do
results = []
@samples.each do |chars|
str = RTesseract.new(File.join(File.dirname(__FILE__), "..", "tmp", "#{chars}.png")).to_s
results << "- Chars: #{chars}, OCR read #{str.strip}"
expect(chars).not_to eq(str)
end
puts %(------------------------\nOCR all results: \n#{results.join("\n")})
end
end

14
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,14 @@
require 'rubygems'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rucaptcha'
require 'rtesseract'
RuCaptcha.configure do
self.len = 2
self.width = 123
self.height = 33
self.implode = 0.111
end