@@ 12,7 12,7 @@ def clean_func(self):
return antispam
try:
antispam = int(antispam)
- except ValueError:
+ except (ValueError, TypeError):
raise PermissionDenied
if antispam < FORM_GUARD_MIN_VALUE:
raise PermissionDenied
@@ 2,5 2,73 @@
from __future__ import unicode_literals
from django.test import TestCase
+from django.forms.fields import CharField
+from django.test.utils import override_settings
+from django.core.exceptions import PermissionDenied
-# Create your tests here.
+from .forms import FormGuardForm
+from .settings import FORM_GUARD_MIN_VALUE
+from .templatetags.form_guard_tags import form_guard_js
+
+
+class TestFormGuard(TestCase):
+ def test_form(self):
+ form = FormGuardForm()
+ self.assertTrue(hasattr(form, 'FORM_GUARD_FIELD_NAME'))
+ field_name = form.FORM_GUARD_FIELD_NAME
+ self.assertEqual(len(form.fields), 1)
+ self.assertIn(field_name, form.fields.keys())
+ form_guard_field = form.fields[field_name]
+ self.assertTrue(isinstance(form_guard_field, CharField))
+ self.assertEqual(form_guard_field.initial, 'default')
+ self.assertTrue(form_guard_field.widget.is_hidden)
+ self.assertEqual(form_guard_field.widget.attrs['id'],
+ 'id_{0}'.format(field_name))
+ clean_func_name = 'clean_{0}'.format(field_name)
+ self.assertTrue(hasattr(form, clean_func_name))
+
+ @override_settings(DEBUG=False)
+ def test_form_clean_debug_off(self):
+ # field not included
+ data = {}
+ form = FormGuardForm(data)
+ field_name = form.FORM_GUARD_FIELD_NAME
+ clean_func_name = 'clean_{0}'.format(field_name)
+ self.assertFalse(form.is_valid())
+ self.assertRaises(
+ PermissionDenied, lambda: getattr(form, clean_func_name)())
+ # field is string
+ data = {field_name: 'default'}
+ form = FormGuardForm(data)
+ self.assertRaises(PermissionDenied, lambda: form.is_valid())
+ # field value less than min allowed
+ data = {field_name: FORM_GUARD_MIN_VALUE - 1}
+ form = FormGuardForm(data)
+ self.assertRaises(PermissionDenied, lambda: form.is_valid())
+ # field value more or equal than min allowed
+ data = {field_name: FORM_GUARD_MIN_VALUE}
+ form = FormGuardForm(data)
+ self.assertTrue(form.is_valid())
+ self.assertEqual(getattr(form, clean_func_name)(),
+ FORM_GUARD_MIN_VALUE)
+
+ @override_settings(DEBUG=True)
+ def test_form_clean_debug_on(self):
+ field_name = FormGuardForm.FORM_GUARD_FIELD_NAME
+ clean_func_name = 'clean_{0}'.format(field_name)
+ # field value less than min allowed but debug is on
+ data = {field_name: FORM_GUARD_MIN_VALUE - 1}
+ form = FormGuardForm(data)
+ self.assertTrue(form.is_valid())
+ self.assertEqual(int(getattr(form, clean_func_name)()),
+ FORM_GUARD_MIN_VALUE - 1)
+
+ def test_template_tag(self):
+ # argument is antispam field id (string)
+ ret = form_guard_js({}, 'field_id')
+ self.assertEqual(ret, {'FORM_GUARD_FIELD_ID': 'field_id'})
+ # argument is a form instance
+ form = FormGuardForm()
+ ret = form_guard_js({}, form)
+ fid = form.fields[form.FORM_GUARD_FIELD_NAME].widget.attrs.get('id')
+ self.assertEqual(ret, {'FORM_GUARD_FIELD_ID': fid})