aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gems/recaptcha-0.3.1/test
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2011-02-23 14:06:21 +0000
committerLouise Crow <louise.crow@gmail.com>2011-02-23 14:06:21 +0000
commit5dbf3a24e23114077052ceff36be49a3aaf46182 (patch)
treeef9d5369f89f87516b31ef4dcf80781445052ab5 /vendor/gems/recaptcha-0.3.1/test
parent4cd0de91754f07afd0afb58d137b3adcf5a07611 (diff)
Replacing old recaptcha plugin with more recent gem.
Diffstat (limited to 'vendor/gems/recaptcha-0.3.1/test')
-rw-r--r--vendor/gems/recaptcha-0.3.1/test/recaptcha_test.rb39
-rw-r--r--vendor/gems/recaptcha-0.3.1/test/verify_recaptcha_test.rb95
2 files changed, 134 insertions, 0 deletions
diff --git a/vendor/gems/recaptcha-0.3.1/test/recaptcha_test.rb b/vendor/gems/recaptcha-0.3.1/test/recaptcha_test.rb
new file mode 100644
index 000000000..82845a9cc
--- /dev/null
+++ b/vendor/gems/recaptcha-0.3.1/test/recaptcha_test.rb
@@ -0,0 +1,39 @@
+require 'test/unit'
+require 'cgi'
+require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha'
+
+class RecaptchaClientHelperTest < Test::Unit::TestCase
+ include Recaptcha
+ include Recaptcha::ClientHelper
+ include Recaptcha::Verify
+
+ attr_accessor :session
+
+ def setup
+ @session = {}
+ Recaptcha.configure do |config|
+ config.public_key = '0000000000000000000000000000000000000000'
+ config.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
+ end
+ end
+
+ def test_recaptcha_tags
+ # Might as well match something...
+ assert_match /http:\/\/www.google.com\/recaptcha\/api\/challenge/, recaptcha_tags
+ end
+
+ def test_recaptcha_tags_with_ssl
+ assert_match /https:\/\/www.google.com\/recaptcha\/api\/challenge/, recaptcha_tags(:ssl => true)
+ end
+
+ def test_recaptcha_tags_without_noscript
+ assert_no_match /noscript/, recaptcha_tags(:noscript => false)
+ end
+
+ def test_should_raise_exception_without_public_key
+ assert_raise RecaptchaError do
+ Recaptcha.configuration.public_key = nil
+ recaptcha_tags
+ end
+ end
+end
diff --git a/vendor/gems/recaptcha-0.3.1/test/verify_recaptcha_test.rb b/vendor/gems/recaptcha-0.3.1/test/verify_recaptcha_test.rb
new file mode 100644
index 000000000..0e9afc8bb
--- /dev/null
+++ b/vendor/gems/recaptcha-0.3.1/test/verify_recaptcha_test.rb
@@ -0,0 +1,95 @@
+require 'test/unit'
+require 'active_support/core_ext/string'
+require 'rubygems'
+require 'mocha'
+require 'net/http'
+require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha'
+
+class RecaptchaVerifyTest < Test::Unit::TestCase
+ def setup
+ Recaptcha.configuration.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
+ @controller = TestController.new
+ @controller.request = stub(:remote_ip => "1.1.1.1")
+ @controller.params = {:recaptcha_challenge_field => "challenge", :recaptcha_response_field => "response"}
+
+ @expected_post_data = {}
+ @expected_post_data["privatekey"] = Recaptcha.configuration.private_key
+ @expected_post_data["remoteip"] = @controller.request.remote_ip
+ @expected_post_data["challenge"] = "challenge"
+ @expected_post_data["response"] = "response"
+
+ @expected_uri = URI.parse(Recaptcha.configuration.verify_url)
+ end
+
+ def test_should_raise_exception_without_private_key
+ assert_raise Recaptcha::RecaptchaError do
+ Recaptcha.configuration.private_key = nil
+ @controller.verify_recaptcha
+ end
+ end
+
+ def test_should_return_false_when_key_is_invalid
+ expect_http_post(response_with_body("false\ninvalid-site-private-key"))
+
+ assert !@controller.verify_recaptcha
+ assert_equal "invalid-site-private-key", @controller.flash[:recaptcha_error]
+ end
+
+ def test_returns_true_on_success
+ @controller.flash[:recaptcha_error] = "previous error that should be cleared"
+ expect_http_post(response_with_body("true\n"))
+
+ assert @controller.verify_recaptcha
+ assert_nil @controller.flash[:recaptcha_error]
+ end
+
+ def test_errors_should_be_added_to_model
+ expect_http_post(response_with_body("false\nbad-news"))
+
+ errors = mock
+ errors.expects(:add).with(:base, "Word verification response is incorrect, please try again.")
+ model = mock(:valid? => false, :errors => errors)
+
+ assert !@controller.verify_recaptcha(:model => model)
+ assert_equal "bad-news", @controller.flash[:recaptcha_error]
+ end
+
+ def test_returns_true_on_success_with_optional_key
+ @controller.flash[:recaptcha_error] = "previous error that should be cleared"
+ # reset private key
+ @expected_post_data["privatekey"] = 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX'
+ expect_http_post(response_with_body("true\n"))
+
+ assert @controller.verify_recaptcha(:private_key => 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX')
+ assert_nil @controller.flash[:recaptcha_error]
+ end
+
+ def test_timeout
+ expect_http_post(Timeout::Error, :exception => true)
+ assert !@controller.verify_recaptcha()
+ assert_equal "recaptcha-not-reachable", @controller.flash[:recaptcha_error]
+ end
+
+ private
+
+ class TestController
+ include Recaptcha::Verify
+ attr_accessor :request, :params, :flash
+
+ def initialize
+ @flash = {}
+ end
+ end
+
+ def expect_http_post(response, options = {})
+ unless options[:exception]
+ Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
+ else
+ Net::HTTP.expects(:post_form).raises response
+ end
+ end
+
+ def response_with_body(body)
+ stub(:body => body)
+ end
+end