Пример #1
0
    def test_captcha_validate_fail(self):
        """Assert an error when the captcha fails validation."""
        request = mock.Mock()
        request.errors = Errors()
        request.errors.status = None
        request.registry.settings = validators.config
        request.user = None
        # We'll cheat since we know the captcha.secret and figure out the solution.
        plainkey, value = captcha.math_generator(None, validators.config)
        cipherkey = captcha.encrypt(plainkey, validators.config)
        request.session = {'captcha': cipherkey}
        # By adding a 0 onto the end of the value, we are wrong by 100!
        request.validated = {
            'captcha_key': cipherkey,
            'captcha_value': value + '0'
        }

        validators.validate_captcha(request)

        self.assertEqual(
            request.errors,
            [{
                'location': 'body',
                'name': 'captcha_value',
                'description': 'Incorrect response to the captcha.'
            }])
        self.assertEqual(request.errors.status, exceptions.HTTPBadRequest.code)
Пример #2
0
    def test_with_plainkey(self):
        """
        Assert correct return value when passing a valid plainkey to the function.
        """
        puzzle, value = captcha.math_generator('42 + 7 =', None)

        self.assertEqual(puzzle, '42 + 7 =')
        self.assertEqual(value, '49')
Пример #3
0
    def test_plainkey_None_nondeterministic(self):
        """
        Assert correct behavior when plainkey is passed as None without mocking random. For this
        test, we will assert that the math summation matches the expected value.
        """
        puzzle, value = captcha.math_generator(None, None)

        puzzle = puzzle.split()
        self.assertEqual(int(puzzle[0]) + int(puzzle[2]), int(value))
        self.assertEqual(puzzle[1], '+')
        self.assertEqual(puzzle[3], '=')
Пример #4
0
    def test_plainkey_None_deterministic(self):
        """
        Assert correct behavior when plainkey is passed as None using a mocked random so we get a
        deterministic response.
        """
        with mock.patch('bodhi.server.captcha.random.randint',
                        side_effect=[1, 2]):
            puzzle, value = captcha.math_generator(None, None)

        self.assertEqual(puzzle, '1 + 2 =')
        self.assertEqual(value, '3')
Пример #5
0
    def test_captcha_validate_success(self):
        """Assert an error when the captcha fails validation."""
        request = mock.Mock()
        request.errors = Errors()
        request.errors.status = None
        request.registry.settings = validators.config
        request.user = None
        # We'll cheat since we know the captcha.secret and figure out the solution.
        plainkey, value = captcha.math_generator(None, validators.config)
        cipherkey = captcha.encrypt(plainkey, validators.config)
        request.session = {'captcha': cipherkey}
        request.validated = {'captcha_key': cipherkey, 'captcha_value': value}

        validators.validate_captcha(request)

        self.assertEqual(request.errors, [])
        self.assertEqual(request.errors.status, None)
        self.assertTrue('captcha' not in request.session)
Пример #6
0
    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))