Browse files

adding fakeweb for better testing

  • Loading branch information...
1 parent 2bd9e21 commit 6a4df7fcdcd59fab41ddec22e5abe3ac590ba99a @matthutchinson committed Aug 3, 2011
Showing with 32 additions and 37 deletions.
  1. +2 −2 acts_as_textcaptcha.gemspec
  2. +7 −7 lib/acts_as_textcaptcha/textcaptcha.rb
  3. +2 −2 test/test_helper.rb
  4. +21 −26 test/textcaptcha_test.rb
View
4 acts_as_textcaptcha.gemspec
@@ -21,13 +21,13 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_dependency('bcrypt-ruby', '~> 2.1.2')
+ s.add_dependency('bcrypt-ruby', '~> 2.1.4')
s.add_development_dependency('rails')
s.add_development_dependency('activerecord')
s.add_development_dependency('bundler')
s.add_development_dependency('rcov')
s.add_development_dependency('rdoc')
s.add_development_dependency('sqlite3')
- s.add_development_dependency('mynyml-redgreen')
+ s.add_development_dependency('fakeweb')
end
View
14 lib/acts_as_textcaptcha/textcaptcha.rb
@@ -15,7 +15,7 @@
module ActsAsTextcaptcha
- # dont use Railtie if Rails < 3
+ # dont use Railtie if Rails < 3
unless Rails::VERSION::MAJOR < 3
class Railtie < ::Rails::Railtie
rake_tasks do
@@ -32,7 +32,7 @@ def acts_as_textcaptcha(options = nil)
cattr_accessor :textcaptcha_config
attr_accessor :spam_question, :spam_answers, :spam_answer
attr_protected :spam_question if respond_to?(:accessible_attributes) && accessible_attributes.nil?
-
+
validate :validate_textcaptcha
if options.is_a?(Hash)
@@ -50,10 +50,10 @@ def acts_as_textcaptcha(options = nil)
module InstanceMethods
-
+
# override this method to toggle spam checking, default is on (true)
def perform_textcaptcha?; true end
-
+
def textcaptcha
return if !perform_textcaptcha? || validate_spam_answer
@@ -66,7 +66,7 @@ def textcaptcha
end
if textcaptcha_config[:api_key]
begin
- resp = Net::HTTP.get(URI.parse('http://textcaptcha.com/api/'+textcaptcha_config[:api_key]))
+ resp = Net::HTTP.get(URI.parse("http://textcaptcha.com/api/#{textcaptcha_config[:api_key]}"))
return if resp.empty?
if defined?(ActiveSupport::XmlMini)
@@ -84,7 +84,7 @@ def textcaptcha
end
return
rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ECONNREFUSED,
- Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, URI::InvalidURIError => e
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, URI::InvalidURIError => e
log_textcaptcha("failed to load or parse textcaptcha with key '#{textcaptcha_config[:api_key]}'; #{e}")
end
end
@@ -98,7 +98,7 @@ def textcaptcha
end
end
end
-
+
private
def validate_spam_answer
(spam_answer && spam_answers) ? spam_answers.split('-').include?(encrypt_answer(md5_answer(spam_answer))) : false
View
4 test/test_helper.rb
@@ -3,8 +3,8 @@
ENV['RAILS_ENV'] = 'test'
require 'minitest/autorun'
-require 'minitest/spec'
-require 'redgreen'
+require 'fakeweb'
+#require 'minitest/spec'
require 'active_record'
require 'rails'
View
47 test/textcaptcha_test.rb
@@ -7,18 +7,16 @@ class Widget < ActiveRecord::Base
class Comment < ActiveRecord::Base
# inline options with api_key only
- acts_as_textcaptcha 'api_key' => '8u5ixtdnq9csc84cok0owswgo',
- 'bcrypt_salt' => '$2a$10$j0bmycH.SVfD1b5mpEGPpe'
+ acts_as_textcaptcha 'api_key' => '8u5ixtdnq9csc84cok0owswgo',
+ 'bcrypt_salt' => '$2a$10$j0bmycH.SVfD1b5mpEGPpe'
end
class Review < ActiveRecord::Base
# inline options with all possible options
- acts_as_textcaptcha({'api_key' => '8u5ixtdnq9csc84cok0owswgo',
+ acts_as_textcaptcha 'api_key' => '8u5ixtdnq9csc84cok0owswgo',
'bcrypt_salt' => '$2a$10$j0bmycH.SVfD1b5mpEGPpe',
'bcrypt_cost' => '3',
- 'questions' => [{'question' => '1+1', 'answers' => '2,two'},
- {'question' => 'The green hat is what color?', 'answers' => 'green'},
- {'question' => 'Which is bigger: 67, 14 or 6', 'answers' => '67,sixtyseven,sixty seven,sixty-seven'}]})
+ 'questions' => [{'question' => 'The green hat is what color?', 'answers' => 'green'}]
end
class Note < ActiveRecord::Base
@@ -27,7 +25,7 @@ class Note < ActiveRecord::Base
'bcrypt_salt' => '$2a$10$j0bmycH.SVfD1b5mpEGPpe'
attr_accessor :turn_off_captcha
-
+
def perform_textcaptcha?
!turn_off_captcha
end
@@ -43,19 +41,12 @@ class Contact
end
-
describe 'Textcaptcha' do
- before do
- @comment = Comment.new
- @review = Review.new
- @note = Note.new
- @contact = Contact.new
- end
-
describe 'validations' do
before(:each) do
+ @note = Note.new
@note.textcaptcha
end
@@ -99,6 +90,7 @@ class Contact
end
it "should validate a non ActiveRecord object" do
+ @contact = Contact.new
@contact.textcaptcha
@contact.spam_question.must_equal('one+1')
@@ -114,6 +106,7 @@ class Contact
describe 'encryption' do
it 'should encrypt spam_answers (joined by - seperator) MD5 digested and using BCrypt engine with salt' do
+ @note = Note.new
@note.spam_answers.must_be_nil
@note.textcaptcha
encrypted_answers = [2,' TwO '].collect { |answer| BCrypt::Engine.hash_secret(Digest::MD5.hexdigest(answer.to_s.strip.downcase), '$2a$10$j0bmycH.SVfD1b5mpEGPpe', 1) }.join('-')
@@ -125,6 +118,7 @@ class Contact
describe 'textcaptcha API service' do
it 'should generate spam question from textcaptcha service' do
+ @comment = Comment.new
@comment.textcaptcha
@comment.spam_question.wont_be_nil
@comment.spam_answers.wont_be_nil
@@ -136,29 +130,30 @@ class Contact
describe 'and textcaptcha unavailable' do
before(:each) do
- #Net::HTTP.stub(:get).and_raise(SocketError)
+ @review = Review.new
end
- it 'should fall back to random user defined question when set' do
- #@review.generate_spam_question
- #@review.spam_question.should_not be_nil
- #@review.possible_answers.should_not be_nil
- #@review.possible_answers.should be_an(Array)
+ after(:each) do
+ FakeWeb.clean_registry
+ end
+
+ it 'should fall back to a random user defined question when NET error and at least one fallback question is defined' do
+ FakeWeb.register_uri(:get, %r|http://textcaptcha\.com/api/|, :status => ['401', 'Not Found'])
- #@review.validate_textcaptcha.should be_false
- #@review.should_not be_valid
+ @review.textcaptcha
+ @review.spam_question.must_equal('The green hat is what color?')
+ @review.spam_answers.wont_be_nil
end
- it 'should not generate any spam question/answer if no user defined questions set' do
+ #it 'should not generate any spam question/answer if no user defined questions set' do
#@comment.generate_spam_question
#@comment.spam_question.should be_nil
#@comment.possible_answers.should be_nil
#@comment.validate_textcaptcha.should be_true
#@comment.should be_valid
- end
+ #end
end
end
end
-

0 comments on commit 6a4df7f

Please sign in to comment.