def test_validation_with_broken_value(self):
     result = self.client.post(reverse('rest_captcha')).json()
     data = dict(captcha_key=result['captcha_key'], captcha_value='ABCD')
     serial = RestCaptchaSerializer(data=data)
     assert serial.is_valid() is False
     self.assertDictEqual(serial.errors,
                          {'non_field_errors': ['Invalid captcha value']})
 def test_validation_with_undefined_key(self):
     data = dict(captcha_key=str(uuid4()), captcha_value='ABCD')
     serial = RestCaptchaSerializer(data=data)
     assert serial.is_valid() is False
     assert 'non_field_errors' in serial.errors
     self.assertEqual('Invalid or expired captcha key',
                      str(serial.errors['non_field_errors'][0]))
 def test_validation_with_undefined_key(self):
     data = dict(captcha_key=str(uuid4()), captcha_value='ABCD')
     serial = RestCaptchaSerializer(data=data)
     assert serial.is_valid() is False
     self.assertDictEqual(
         serial.errors,
         {'non_field_errors': ['Invalid or expared captcha key']})
    def test_validation_valid2(self):
        result = self.client.post(reverse('rest_captcha')).json()
        key = utils.get_cache_key(result['captcha_key'])
        cache.set(key, 'GOOD')

        data = dict(captcha_key=result['captcha_key'], captcha_value='GoOD')
        serial = RestCaptchaSerializer(data=data)
        assert serial.is_valid() is True
    def test_master_captcha(self):
        api_settings.MASTER_CAPTCHA = {'master_key': 'GOOD'}
        data = dict(captcha_key='master_key', captcha_value='BAD')
        serial = RestCaptchaSerializer(data=data)
        assert serial.is_valid() is False

        data = dict(captcha_key='master_key', captcha_value='GOOD')
        serial = RestCaptchaSerializer(data=data)
        assert serial.is_valid() is True
    def test_validation_second_try(self):
        result = self.client.post(reverse('rest_captcha')).json()
        key = utils.get_cache_key(result['captcha_key'])
        cache.set(key, 'GOOD')

        data = dict(captcha_key=result['captcha_key'], captcha_value='BAD')
        serial = RestCaptchaSerializer(data=data)
        assert serial.is_valid() is False

        # second try with GOOD VAL
        data = dict(captcha_key=result['captcha_key'], captcha_value='GOOD')
        serial = RestCaptchaSerializer(data=data)
        assert serial.is_valid() is False
 def test_validation_with_broken_keys(self):
     data = dict(captcha_key=str(uuid4()), captcha_value_123='ABCD')
     serial = RestCaptchaSerializer(data=data)
     assert serial.is_valid() is False
     self.assertDictEqual(serial.errors,
                          {'captcha_value': ['This field is required.']})
    def test_validation_empty_value(self):
        result = self.client.post(reverse('rest_captcha')).json()

        data = dict(captcha_key=result['captcha_key'], captcha_value='')
        serial = RestCaptchaSerializer(data=data)
        assert serial.is_valid() is False