def test_base64_unsafe(self): """Ensure that we raise an HTTPNotFound if the ciphertext is not a base64 safe string""" base64_unsafe_message = "$@#his3##d*f" with self.assertRaises(HTTPNotFound) as exc: captcha.decrypt(base64_unsafe_message, config) self.assertEqual(str(exc.exception), '$@#his3##d*f is garbage')
def test_invalid_token(self): """Ensure that we raise a HTTPGone if the token is no longer valid""" invalid_token = "!!!!" with self.assertRaises(HTTPGone) as exc: captcha.decrypt(invalid_token, config) self.assertEqual(str(exc.exception), 'captcha token is no longer valid')
def test_jpeg_generator_called_correctly(self, jpeg_generator): """ Make sure jpeg_generator() is called correctly. We don't have an easy way to make sure the captcha looks right (if we did, it wouldn't be a good captcha and also we shouldn't publish it in unit tests), so let's just make sure jpeg_generator() is called correctly. """ request = mock.MagicMock() request.registry.settings = { 'captcha.secret': 'gFqE6rcBXVLssjLjffsQsAa-nlm5Bg06MTKrVT9hsMA=', 'captcha.ttl': 600, 'captcha.image_width': 1920, 'captcha.image_height': 1080, 'captcha.font_path': '/usr/share/fonts/liberation/LiberationMono-Bold.ttf', 'captcha.font_size': 24, 'captch.font_color': '#012345', 'captcha.padding': 6 } # We need to put a captcha onto the request. cipherkey, url = captcha.generate_captcha(None, request) request.matchdict = {'cipherkey': cipherkey} image = captcha.captcha_image(request) self.assertEqual(image, jpeg_generator.return_value) plainkey = captcha.decrypt(cipherkey, request.registry.settings) jpeg_generator.assert_called_once_with(plainkey, request.registry.settings)
def test_decrypt_from_encrypt(self): """Ensure that decrypt can decrypt what encrypt generated.""" plaintext = "don't let eve see this!" bobs_message = captcha.encrypt(plaintext, config) result = captcha.decrypt(bobs_message, config) self.assertEqual(result, plaintext)
def test_text_type_cipherkey(self): """Ensure that decrypt can decrypt what encrypt generated, when it is a six.text_type.""" plaintext = "don't let eve see this!" bobs_message = captcha.encrypt(plaintext, config).decode('utf-8') result = captcha.decrypt(bobs_message, config) self.assertEqual(result, plaintext)
def test_captcha_can_be_solved(self): """Assert that the generated catpcha can be solved.""" request = mock.MagicMock() request.registry.settings = { 'captcha.secret': 'gFqE6rcBXVLssjLjffsQsAa-nlm5Bg06MTKrVT9hsMA=', 'captcha.ttl': 600 } request.session = {} cipherkey, url = captcha.generate_captcha(None, request) self.assertEqual(request.session['captcha'], cipherkey) request.route_url.assert_called_once_with('captcha_image', cipherkey=cipherkey) self.assertEqual(url, request.route_url.return_value) # Let's cheat and find out what the correct value for this cipherkey is and make sure it is # accepted by validate(). plainkey = captcha.decrypt(cipherkey, request.registry.settings) value = captcha.math_generator(plainkey, request.registry.settings)[1] self.assertTrue(captcha.validate(request, cipherkey, value))