<commit_before>require 'test/unit'
require 'rails/version' # For getting the rails version constants
require 'active_support/vendor' # For loading I18n
require 'mocha'
require 'net/http'
require File.dirname(__FILE__) + '/../lib/recaptcha'

class RecaptchaVerifyTest < Test::Unit::TestCase
  def setup

    ENV['RECAPTCHA_PRIVATE_KEY'] = '<KEY>'
    @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"] = ENV['RECAPTCHA_PRIVATE_KEY']
    @expected_post_data["remoteip"]   = @controller.request.remote_ip
    @expected_post_data["challenge"]  = "challenge"
    @expected_post_data["response"]   = "response"
    
    @expected_uri = URI.parse("http://#{Recaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
  end

  def test_should_raise_exception_without_private_key
    assert_raise Recaptcha::RecaptchaError do
      ENV['RECAPTCHA_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.session[:recaptcha_error]
  end
  
  def test_returns_true_on_success
    @controller.session[:recaptcha_error] = "previous error that should be cleared"    
    expect_http_post(response_with_body("true\n"))

    assert @controller.verify_recaptcha
    assert_nil @controller.session[: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, "Captcha response is incorrect, please try again.")
    
    model = mock(:class => mock(:name => mock(:underscore => 'foo')))
    model.expects(:valid?)
    model.expects(:errors).returns(errors)

    assert !@controller.verify_recaptcha(:model => model)
    assert_equal "bad-news", @controller.session[:recaptcha_error]
  end

  def test_returns_true_on_success_with_optional_key
    @controller.session[:recaptcha_error] = "previous error that should be cleared"
    # reset private key
    @expected_post_data["privatekey"] =  '<KEY>'
    expect_http_post(response_with_body("true\n"))

    assert @controller.verify_recaptcha(:private_key => 'ADIFFERENTPRIVATEKEY<KEY>XX')
    assert_nil @controller.session[:recaptcha_error]
  end
  
  private

  class TestController
    include Recaptcha::Verify
    attr_accessor :request, :params, :session
    
    def initialize
      @session = {}
    end
  end
  
  def expect_http_post(response)
    Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
  end<commit_msg>Tests for timeout, ad8bbbb0<commit_after>require 'test/unit'
require 'rails/version' # For getting the rails version constants
require 'active_support/vendor' # For loading I18n
require 'mocha'
require 'net/http'
require File.dirname(__FILE__) + '/../lib/recaptcha'

class RecaptchaVerifyTest < Test::Unit::TestCase
  def setup

    ENV['RECAPTCHA_PRIVATE_KEY'] = '<KEY>'
    @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"] = ENV['RECAPTCHA_PRIVATE_KEY']
    @expected_post_data["remoteip"]   = @controller.request.remote_ip
    @expected_post_data["challenge"]  = "challenge"
    @expected_post_data["response"]   = "response"
    
    @expected_uri = URI.parse("http://#{Recaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
  end

  def test_should_raise_exception_without_private_key
    assert_raise Recaptcha::RecaptchaError do
      ENV['RECAPTCHA_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.session[:recaptcha_error]
  end
  
  def test_returns_true_on_success
    @controller.session[:recaptcha_error] = "previous error that should be cleared"    
    expect_http_post(response_with_body("true\n"))

    assert @controller.verify_recaptcha
    assert_nil @controller.session[: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, "Captcha response is incorrect, please try again.")
    model = mock(:valid? => false, :errors => errors)

    assert !@controller.verify_recaptcha(:model => model)
    assert_equal "bad-news", @controller.session[:recaptcha_error]
  end

  def test_returns_true_on_success_with_optional_key
    @controller.session[:recaptcha_error] = "previous error that should be cleared"
    # reset private key
    @expected_post_data["privatekey"] =  '<KEY>'
    expect_http_post(response_with_body("true\n"))

    assert @controller.verify_recaptcha(:private_key => '<KEY>')
    assert_nil @controller.session[:recaptcha_error]
  end

  def test_timeout
    expect_http_post(Timeout::Error, :exception => true)
    assert !@controller.verify_recaptcha()
    assert_equal "recaptcha-not-reachable", @controller.session[:recaptcha_error]
  end

  private

  class TestController
    include Recaptcha::Verify
    attr_accessor :request, :params, :session
    
    def initialize
      @session = {}
    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
{'fraction_non_alphanumeric': 0.11222949169602416, 'fraction_numerical': 0.0023485992283173965, 'mean_word_length': 4.716203259827421, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3841670', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1890, 'n_words': 322}