-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2752] Add math captcha to contactform
- Loading branch information
Showing
5 changed files
with
164 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django import forms | ||
from django.test import TestCase | ||
from django.utils.translation import gettext as _ | ||
|
||
from ..forms import MathCaptchaField | ||
|
||
|
||
class MockForm(forms.Form): | ||
captcha = MathCaptchaField(range_=(4, 4), operators=["+"]) | ||
captcha_2 = MathCaptchaField(range_=(4, 4), operators=["-"]) | ||
|
||
|
||
class MathCaptchaFieldUnitTest(TestCase): | ||
def test_captcha_invalid(self): | ||
test_cases = [ | ||
{ | ||
"captcha": "", | ||
"message": _("Dit veld is vereist."), | ||
"reason": "field required", | ||
}, | ||
{ | ||
"captcha": " ", | ||
"message": _("Voer een geheel getal in."), | ||
"reason": "wrong input type", | ||
}, | ||
{ | ||
"captcha": 42, | ||
"message": _("Voer een geheel getal in."), | ||
"reason": "wrong input type", | ||
}, | ||
{ | ||
"captcha": "42", # captcha only computes 2 numbers between 1 and 10 | ||
"message": _("Fout antwoord, probeer het opnieuw."), | ||
"reason": "wrong answer", | ||
}, | ||
] | ||
for test_case in test_cases: | ||
with self.subTest(reason=test_case["reason"]): | ||
form = MockForm( | ||
data={ | ||
"captcha": test_case["captcha"], | ||
"captcha_2": test_case["captcha"], | ||
}, | ||
) | ||
self.assertFalse(form.is_valid()) | ||
self.assertEqual(form.errors["captcha"], [test_case["message"]]) | ||
|
||
def test_captcha_valid(self): | ||
form = MockForm( | ||
data={"captcha": "8", "captcha_2": "0"}, | ||
) | ||
self.assertTrue(form.is_valid()) |