<commit_before>require 'test/unit'
require 'rubygems'
require 'mocha'
require 'net/http'
require File.dirname(__FILE__) + '/../lib/recaptcha'

class VerifyReCaptchaTest < 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://#{Ambethia::ReCaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
  end
  
  def test_should_raise_exception_without_private_key
    assert_raise Ambethia::ReCaptcha::ReCaptchaError do
      ENV['RECAPTCHA_PRIVATE_KEY'] = nil
      @controller.verify_recaptcha
    end
  end

  def test_should_return_false_when_key_is_invalid
    response = response_with_body("false\ninvalid-site-private-key")
    Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)

    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"    
    Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response_with_body("true\n"))

    assert @controller.verify_recaptcha
    assert_nil @controller.session[:recaptcha_error]
  end
  
  def test_errors_should_be_added_to_model
    response = response_with_body("false\nbad-news")
    Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
    
    errors = mock
    errors.expects(:add_to_base).with("Captcha response is incorrect, please try again.")
    
    model = mock
    model.expects(:valid?)
    model.expects(:errors).returns(errors)

    assert !@controller.verify_recaptcha(model)
    assert_equal "bad-news", @controller.session[:recaptcha_error]
  end
  
  private
  
  class TestController
    include Ambethia::ReCaptcha::Controller    
    attr_accessor :request, :params, :session
    
    def initialize
      @session = {}
    end
  end
  
  def response_with_body(body)
    stub(:body => body)
  end
end<commit_msg>Refactoring verify_recaptcha_test<commit_after>require 'test/unit'
require 'rubygems'
require 'mocha'
require 'net/http'
require File.dirname(__FILE__) + '/../lib/recaptcha'

class VerifyReCaptchaTest < 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://#{Ambethia::ReCaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
  end
  
  def test_should_raise_exception_without_private_key
    assert_raise Ambethia::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_to_base).with("Captcha response is incorrect, please try again.")
    
    model = mock
    model.expects(:valid?)
    model.expects(:errors).returns(errors)

    assert !@controller.verify_recaptcha(model)
    assert_equal "bad-news", @controller.session[:recaptcha_error]
  end
  
  private
  
  class TestController
    include Ambethia::ReCaptcha::Controller    
    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
  
  def response_with_body(body)
    stub(:body => body)
  end
end
{'fraction_non_alphanumeric': 0.11217232269863849, 'fraction_numerical': 0.001625685836212152, 'mean_word_length': 4.599544937428896, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '4913222', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1601, 'n_words': 244}