Unverified Commit 62e5f009 authored by Damian Johnson's avatar Damian Johnson Committed by Philipp Winter
Browse files

Fix captcha tests

Nothing too particularly interesting...

  Traceback (most recent call last):
    File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_captcha.py", line 63, in test_get
      if os.environ.has_key(envkey):
  builtins.AttributeError: '_Environ' object has no attribute 'has_key'

  Traceback (most recent call last):
    File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_captcha.py", line 191, in test_get
      image, challenge = c.get()
    File "/home/atagar/Desktop/tor/bridgedb/bridgedb/captcha.py", line 391, in get
      self.image = imageFile.read()
    File "/usr/lib/python3.5/codecs.py", line 321, in decode
      (result, consumed) = self._buffer_decode(data, self.errors, final)
  builtins.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

  Traceback (most recent call last):
    File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_captcha.py", line 163, in test_createChallenge_hmacValid
      challenge = c.createChallenge('ShouldHaveAValidHMAC')
    File "/home/atagar/Desktop/tor/bridgedb/bridgedb/captcha.py", line 369, in createChallenge
      encBlob = self.publicKey.encrypt(blob)
    File "/usr/local/lib/python3.5/dist-packages/Crypto/Cipher/PKCS1_OAEP.py", line 121, in encrypt
      db = lHash + ps + b'\x01' + _copy_bytes(None, None, message)
  builtins.TypeError: can't concat bytes to str

Test results changed as follows...

  before: FAILED (skips=114, failures=21, errors=52, successes=797)
  after:  FAILED (skips=115, failures=29, errors=34, successes=806)
parent 6e0fe683
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -366,7 +366,7 @@ class GimpCaptcha(Captcha):
        """
        timestamp = str(int(time.time())).zfill(12)
        blob = timestamp + answer
        encBlob = self.publicKey.encrypt(blob)
        encBlob = self.publicKey.encrypt(blob.encode('utf-8'))
        hmac = crypto.getHMAC(self.hmacKey, encBlob)
        challenge = urlsafe_b64encode(hmac + encBlob)
        return challenge
@@ -387,7 +387,7 @@ class GimpCaptcha(Captcha):
        try:
            imageFilename = random.choice(os.listdir(self.cacheDir))
            imagePath = os.path.join(self.cacheDir, imageFilename)
            with open(imagePath) as imageFile:
            with open(imagePath, 'rb') as imageFile:
                self.image = imageFile.read()
        except IndexError:
            raise GimpCaptchaError("CAPTCHA cache dir appears empty: %r"
+2 −2
Original line number Diff line number Diff line
@@ -368,7 +368,7 @@ class CaptchaFetchResource(CaptchaResource):
            logging.error("Unhandled error while retrieving Gimp captcha!")
            logging.error(impossible)

        return (capt.image, capt.challenge)
        return (capt.image, capt.challenge.decode('utf-8') if isinstance(capt.challenge, bytes) else capt.challenge)

    def getPreferredTransports(self, supportedTransports):
        """Choose which transport a client should request, based on their list
@@ -476,7 +476,7 @@ class CaptchaFetchResource(CaptchaResource):
        }

        try:
            data["data"][0]["image"] = base64.b64encode(image)
            data["data"][0]["image"] = base64.b64encode(image).decode('utf-8')
        except Exception as impossible:
            logging.error("Could not construct or encode captcha!")
            logging.error(impossible)
+3 −3
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ class ReCaptchaTests(unittest.TestCase):
        # Force urllib.request to do anything less idiotic than the defaults:
        envkey = 'HTTPS_PROXY'
        oldkey = None
        if os.environ.has_key(envkey):
        if envkey in os.environ:
            oldkey = os.environ[envkey]
        os.environ[envkey] = '127.0.0.1:9150'
        # This stupid thing searches the environment for ``<protocol>_PROXY``
@@ -179,7 +179,7 @@ class GimpCaptchaTests(unittest.TestCase):
        self.assertEqual(hmac, correctHMAC)

        decrypted = self.sekrit.decrypt(orig)
        timestamp = int(decrypted[:12].lstrip('0'))
        timestamp = int(decrypted[:12].lstrip(b'0'))
        # The timestamp should be within 30 seconds of right now.
        self.assertApproximates(timestamp, int(time.time()), 30)
        self.assertEqual('ThisAnswerShouldDecryptToThis', decrypted[12:])
@@ -238,7 +238,7 @@ class GimpCaptchaTests(unittest.TestCase):
        c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey,
                                self.cacheDir)
        image, challenge = c.get()
        challengeBadB64 = challenge.rstrip('==') + "\x42\x42\x42"
        challengeBadB64 = challenge.decode('utf-8').rstrip('==') + "\x42\x42\x42"
        self.assertEquals(
            c.check(challenge, c.answer, c.secretKey, c.hmacKey),
            True)