Skip to content

Commit

Permalink
Refactoring verify_recaptcha_test
Browse files Browse the repository at this point in the history
  • Loading branch information
pivotal committed Jun 18, 2008
1 parent b716125 commit a34eab4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions test/verify_recaptcha_test.rb
Expand Up @@ -29,24 +29,22 @@ def test_should_raise_exception_without_private_key
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)
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"
Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response_with_body("true\n"))
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
response = response_with_body("false\nbad-news")
Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response)
expect_http_post(response_with_body("false\nbad-news"))

errors = mock
errors.expects(:add_to_base).with("Captcha response is incorrect, please try again.")
Expand All @@ -70,6 +68,10 @@ def initialize
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
Expand Down

0 comments on commit a34eab4

Please sign in to comment.