From 4ec52d887b5351aeb36db58d555ca79ed9460bee Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 26 Oct 2015 21:55:46 +0800 Subject: [PATCH] Add some test case --- Gemfile | 7 ++++ Gemfile.lock | 44 +++++++++++++++++++++ lib/rucaptcha.rb | 4 +- lib/rucaptcha/controller_helpers.rb | 2 +- spec/configure_spec.rb | 12 ++++++ spec/controller_helpers_spec.rb | 59 +++++++++++++++++++++++++++++ spec/ocr_spec.rb | 34 +++++++++++++++++ spec/spec_helper.rb | 14 +++++++ 8 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 spec/configure_spec.rb create mode 100644 spec/controller_helpers_spec.rb create mode 100644 spec/ocr_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a95dad9 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec +# For test OCR +gem 'rtesseract' +gem 'mini_magick' +gem 'active_support' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..64132bb --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index b65ce13..a75661a 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -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' diff --git a/lib/rucaptcha/controller_helpers.rb b/lib/rucaptcha/controller_helpers.rb index 4314833..e46cd8d 100644 --- a/lib/rucaptcha/controller_helpers.rb +++ b/lib/rucaptcha/controller_helpers.rb @@ -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 diff --git a/spec/configure_spec.rb b/spec/configure_spec.rb new file mode 100644 index 0000000..3063bea --- /dev/null +++ b/spec/configure_spec.rb @@ -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 diff --git a/spec/controller_helpers_spec.rb b/spec/controller_helpers_spec.rb new file mode 100644 index 0000000..1a3ecc1 --- /dev/null +++ b/spec/controller_helpers_spec.rb @@ -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 diff --git a/spec/ocr_spec.rb b/spec/ocr_spec.rb new file mode 100644 index 0000000..69a556f --- /dev/null +++ b/spec/ocr_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..80d706c --- /dev/null +++ b/spec/spec_helper.rb @@ -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