| 1 | from django import forms |
| 2 | from django.contrib.auth.models import User |
| 3 | from django.http import HttpResponse |
| 4 | from django.template import engines |
| 5 | |
| 6 | from captcha.fields import CaptchaField |
| 7 | |
| 8 | |
| 9 | TEST_TEMPLATE = r""" |
| 10 | <!doctype html> |
| 11 | <html> |
| 12 | <head> |
| 13 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 14 | <title>captcha test</title> |
| 15 | </head> |
| 16 | <body> |
| 17 | {% if passed %} |
| 18 | <p style="color:green">Form validated</p> |
| 19 | {% endif %} |
| 20 | {% if form.errors %} |
| 21 | {{form.errors}} |
| 22 | {% endif %} |
| 23 | |
| 24 | <form action="{% url 'captcha-test' %}" method="post"> |
| 25 | {{form.as_p}} |
| 26 | <p><input type="submit" value="Continue →"></p> |
| 27 | </form> |
| 28 | </body> |
| 29 | </html> |
| 30 | """ |
| 31 | |
| 32 | |
| 33 | def _get_template(template_string): |
| 34 | return engines["django"].from_string(template_string) |
| 35 | |
| 36 | |
| 37 | def _test(request, form_class): |
| 38 | passed = False |
| 39 | if request.POST: |
| 40 | form = form_class(request.POST) |
| 41 | if form.is_valid(): |
| 42 | passed = True |
| 43 | else: |
| 44 | form = form_class() |
| 45 | |
| 46 | t = _get_template(TEST_TEMPLATE) |
| 47 | |
| 48 | return HttpResponse( |
| 49 | t.render(context=dict(passed=passed, form=form), request=request) |
| 50 | ) |
| 51 | |
| 52 | |
| 53 | def test(request): |
| 54 | class CaptchaTestForm(forms.Form): |
| 55 | subject = forms.CharField(max_length=100) |
| 56 | sender = forms.EmailField() |
| 57 | captcha = CaptchaField(help_text="asdasd") |
| 58 | |
| 59 | return _test(request, CaptchaTestForm) |
| 60 | |
| 61 | |
| 62 | def test_model_form(request): |
| 63 | class CaptchaTestModelForm(forms.ModelForm): |
| 64 | subject = forms.CharField(max_length=100) |
| 65 | sender = forms.EmailField() |
| 66 | captcha = CaptchaField(help_text="asdasd") |
| 67 | |
| 68 | class Meta: |
| 69 | model = User |
| 70 | fields = ("subject", "sender", "captcha") |
| 71 | |
| 72 | return _test(request, CaptchaTestModelForm) |
| 73 | |
| 74 | |
| 75 | def test_custom_generator(request): |
| 76 | class CaptchaTestModelForm(forms.ModelForm): |
| 77 | subject = forms.CharField(max_length=100) |
| 78 | sender = forms.EmailField() |
| 79 | captcha = CaptchaField(generator=lambda: ("111111", "111111")) |
| 80 | |
| 81 | class Meta: |
| 82 | model = User |
| 83 | fields = ("subject", "sender", "captcha") |
| 84 | |
| 85 | return _test(request, CaptchaTestModelForm) |
| 86 | |
| 87 | |
| 88 | def test_custom_error_message(request): |
| 89 | class CaptchaTestErrorMessageForm(forms.Form): |
| 90 | captcha = CaptchaField( |
| 91 | help_text="asdasd", error_messages=dict(invalid="TEST CUSTOM ERROR MESSAGE") |
| 92 | ) |
| 93 | |
| 94 | return _test(request, CaptchaTestErrorMessageForm) |
| 95 | |
| 96 | |
| 97 | def test_non_required(request): |
| 98 | class CaptchaTestForm(forms.Form): |
| 99 | sender = forms.EmailField() |
| 100 | subject = forms.CharField(max_length=100) |
| 101 | captcha = CaptchaField(help_text="asdasd", required=False) |
| 102 | |
| 103 | return _test(request, CaptchaTestForm) |
| 104 | |
| 105 | |
| 106 | def test_id_prefix(request): |
| 107 | class CaptchaTestForm(forms.Form): |
| 108 | sender = forms.EmailField() |
| 109 | subject = forms.CharField(max_length=100) |
| 110 | captcha1 = CaptchaField(id_prefix="form1") |
| 111 | captcha2 = CaptchaField(id_prefix="form2") |
| 112 | |
| 113 | return _test(request, CaptchaTestForm) |