[tor-commits] [bridgedb/develop] Fix txcaptcha tests

phw at torproject.org phw at torproject.org
Wed Feb 19 18:27:17 UTC 2020


commit e2af8e34664c1ed91ccbe2b5b93e4b2b99f9270c
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Jan 13 18:15:20 2020 -0800

    Fix txcaptcha tests
    
    The _encodeIfNecessary() helper didn't make sense. It claimed to encode unicode
    into unicode whereas what it really did was simply normalizing inputs to bytes.
    
    I tested urlencode() with a mixture of unicode/bytes and it normalizes its
    inputs so this helper doesn't do anything. This fixes...
    
            Traceback (most recent call last):
            File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_txrecaptcha.py", line 274, in test_encodeIfNecessary
            self.assertIsInstance(origString, unicode)
            builtins.NameError: name 'unicode' is not defined
    
    Test results changed as follows...
    
      before: FAILED (skips=109, failures=18, errors=386, successes=468)
      after:  FAILED (skips=109, failures=18, errors=383, successes=470)
---
 bridgedb/test/test_txrecaptcha.py | 11 +----------
 bridgedb/txrecaptcha.py           | 21 ++++++++-------------
 2 files changed, 9 insertions(+), 23 deletions(-)

diff --git a/bridgedb/test/test_txrecaptcha.py b/bridgedb/test/test_txrecaptcha.py
index fd7365f..db37506 100644
--- a/bridgedb/test/test_txrecaptcha.py
+++ b/bridgedb/test/test_txrecaptcha.py
@@ -142,7 +142,7 @@ class BodyProducerTests(unittest.TestCase):
 
     def setUp(self):
         """Setup the tests."""
-        self.content = 'Line 1\r\nLine 2\r\n'
+        self.content = b'Line 1\r\nLine 2\r\n'
         self.producer = txrecaptcha._BodyProducer(self.content)
 
     def test_interface(self):
@@ -265,12 +265,3 @@ class MiscTests(unittest.TestCase):
         result = txrecaptcha._ebRequest(fail)
         self.assertIsInstance(result, txrecaptcha.RecaptchaResponse)
         self.assertRegexpMatches(result.error_code, msg)
-
-    def test_encodeIfNecessary(self):
-        """:func:`txrecapcha._encodeIfNecessary` should convert unicode objects
-        into strings.
-        """
-        origString = 'abc'
-        self.assertIsInstance(origString, unicode)
-        newString = txrecaptcha._encodeIfNecessary(origString)
-        self.assertIsInstance(newString, str)
diff --git a/bridgedb/txrecaptcha.py b/bridgedb/txrecaptcha.py
index 6308af8..7292360 100644
--- a/bridgedb/txrecaptcha.py
+++ b/bridgedb/txrecaptcha.py
@@ -46,8 +46,8 @@ from zope.interface import implementer
 from bridgedb.crypto import SSLVerifyingContextFactory
 
 #: This was taken from :data:`recaptcha.client.captcha.API_SSL_SERVER`.
-API_SSL_SERVER = API_SERVER = "https://www.google.com/recaptcha/api"
-API_SSL_VERIFY_URL = "%s/verify" % API_SSL_SERVER
+API_SSL_SERVER = API_SERVER = b"https://www.google.com/recaptcha/api"
+API_SSL_VERIFY_URL = b"%s/verify" % API_SSL_SERVER
 
 #: (:class:`OpenSSL.crypto.X509`) Only trust certificate for the reCAPTCHA
 #: :data:`API_SSL_SERVER` which were signed by the Google Internet Authority CA.
@@ -253,12 +253,6 @@ def _ebRequest(fail):
     error = fail.getErrorMessage() or "possible problem in _ebRequest()"
     return RecaptchaResponse(is_valid=False, error_code=error)
 
-def _encodeIfNecessary(string):
-    """Encode unicode objects in utf-8 if necessary."""
-    if isinstance(string, unicode):
-        return string.encode('utf-8')
-    return string
-
 def submit(recaptcha_challenge_field, recaptcha_response_field,
            private_key, remoteip, agent=_agent):
     """Submits a reCaptcha request for verification. This function is a patched
@@ -292,13 +286,14 @@ def submit(recaptcha_challenge_field, recaptcha_response_field,
         return d
 
     params = urllib.parse.urlencode({
-        'privatekey': _encodeIfNecessary(private_key),
-        'remoteip':   _encodeIfNecessary(remoteip),
-        'challenge':  _encodeIfNecessary(recaptcha_challenge_field),
-        'response':   _encodeIfNecessary(recaptcha_response_field)})
+        'privatekey': private_key,
+        'remoteip':   remoteip,
+        'challenge':  recaptcha_challenge_field,
+        'response':   recaptcha_response_field,
+    }).encode('utf-8')
     body = _BodyProducer(params)
     headers = Headers({"Content-type": ["application/x-www-form-urlencoded"],
                        "User-agent": ["reCAPTCHA Python"]})
-    d = agent.request('POST', API_SSL_VERIFY_URL, headers, body)
+    d = agent.request(b'POST', API_SSL_VERIFY_URL, headers, body)
     d.addCallbacks(_cbRequest, _ebRequest)
     return d





More information about the tor-commits mailing list