commit 738101ac291235cc1415f4db5c9c023b6521e34f Author: Isis Lovecruft isis@torproject.org Date: Wed Mar 19 18:03:15 2014 +0000
Always expect a Deferred from ReCaptcha.checkSolution().
Currently, we `checkSolution()` will return a RecaptchaResponse if the HTTP POST argument fields are blank. Otherwise, it returns a deferred. This is bad. It means we have to do all kinds of checks to see if we can add callbacks on the returned object. It should always return a deferred which callbacks with a RecaptchaResponse, and so we should test for that and fail if that is not what is happening. --- lib/bridgedb/test/test_HTTPServer.py | 6 +++--- lib/bridgedb/test/test_txrecaptcha.py | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/lib/bridgedb/test/test_HTTPServer.py b/lib/bridgedb/test/test_HTTPServer.py index 507b1f0..9b5a6e4 100644 --- a/lib/bridgedb/test/test_HTTPServer.py +++ b/lib/bridgedb/test/test_HTTPServer.py @@ -309,9 +309,9 @@ class ReCaptchaProtectedResourceTests(unittest.TestCase): self.request.addArg('captcha_challenge_field', '') self.request.addArg('captcha_response_field', '')
- self.assertIsInstance( - self.captchaResource.checkSolution(self.request), - HTTPServer.txrecaptcha.RecaptchaResponse) + self.assertIs(False, + self.successResultOf( + self.captchaResource.checkSolution(self.request)))
def test_getRemoteIP_useRandomIP(self): """Check that removing our remoteip setting produces a random IP.""" diff --git a/lib/bridgedb/test/test_txrecaptcha.py b/lib/bridgedb/test/test_txrecaptcha.py index c7dcbb1..81484dc 100644 --- a/lib/bridgedb/test/test_txrecaptcha.py +++ b/lib/bridgedb/test/test_txrecaptcha.py @@ -181,12 +181,21 @@ class SubmitTests(unittest.TestCase): self.ip = "1.2.3.4"
def test_submit_emptyResponseField(self): - """An empty 'recaptcha_response_field' should immediately return a - RecaptchaResponse whose error_code is 'incorrect-captcha-sol'.""" - response = txrecaptcha.submit(self.challenge, '', self.key, self.ip) - self.assertIsInstance(response, txrecaptcha.RecaptchaResponse) - self.assertIs(response.is_valid, False) - self.assertEqual(response.error_code, 'incorrect-captcha-sol') + """An empty 'recaptcha_response_field' should return a deferred which + callbacks with a RecaptchaResponse whose error_code is + 'incorrect-captcha-sol'. + """ + def checkResponse(response): + """Check that the response is a + :class:`txcaptcha.RecaptchaResponse`. + """ + self.assertIsInstance(response, txrecaptcha.RecaptchaResponse) + self.assertIs(response.is_valid, False) + self.assertEqual(response.error_code, 'incorrect-captcha-sol') + + d = txrecaptcha.submit(self.challenge, '', self.key, self.ip) + d.addCallback(checkResponse) + return d
def test_submit_returnsDeferred(self): """:func:`txrecaptcha.submit` should return a deferred."""
tor-commits@lists.torproject.org