Skip to content

Commit

Permalink
Added test case for django-recaptcha functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
agseaton committed Apr 5, 2023
1 parent a02d792 commit eab80b7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
55 changes: 55 additions & 0 deletions testapp/forms/articleCaptcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.forms import fields, forms

from formset.widgets import SlugInput
from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Checkbox


class ArticleCaptchaForm(forms.Form):
"""
Using a ``SlugField``
---------------------
In Django's Admin, a ``SlugField`` can be configured to be prepopulated using the content of
another text input field. To emulate a similar behaviour, **django-formset** provides a special
widget named ``SlugInput``.
.. code-block:: python
from django.forms import fields, forms
from formset.widgets import SlugInput
class ArticleForm(...):
...
title = fields.CharField(
label="Title",
max_length=100,
)
slug = fields.SlugField(
label="Slug",
widget=SlugInput('title'),
)
...
This widget takes a single attribute with the name of another field of the same form.
That field's input value then is used to prepopulate the slug field, where the generated value
is produced by concatenating the values of the source fields, and then by transforming that
result into a valid slug (e.g. substituting dashes for spaces, lowercasing letters and rewriting
unicode characters to ASCII letters).
Prefilled slug fields aren't modified after a value has been saved as this usually is undesired
behaviour.
"""

captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox)

title = fields.CharField(
label="Title",
max_length=100,
)

slug = fields.SlugField(
label="Slug",
widget=SlugInput('title'),
)
1 change: 1 addition & 0 deletions testapp/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ certifi==2022.5.18.1
charset-normalizer==2.0.12
coverage==6.4.1
docutils==0.18.1
django-recaptcha==3.0.0
flake8==4.0.1
greenlet==2.0.1
idna==3.3
Expand Down
3 changes: 3 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'django.contrib.staticfiles',
'django.contrib.messages',
'formset',
'captcha',
'testapp',
]

Expand Down Expand Up @@ -115,3 +116,5 @@
}

FORMSET_IGNORE_MARKED_FOR_REMOVAL = False

SILENCED_SYSTEM_CHECKS = ['captcha.recaptcha_test_key_error']
4 changes: 4 additions & 0 deletions testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from testapp.forms.address import AddressForm
from testapp.forms.advertisement import AdvertisementForm, AdvertisementModelForm
from testapp.forms.article import ArticleForm
from testapp.forms.articleCaptcha import ArticleCaptchaForm
from testapp.forms.complete import CompleteForm
from testapp.forms.contact import (
SimpleContactCollection, ContactCollection, ContactCollectionList, IntermediateContactCollectionList,
Expand Down Expand Up @@ -420,6 +421,9 @@ class CompleteForm(FormMixin, forms.Form):
path('article', DemoFormView.as_view(
form_class=ArticleForm,
), kwargs={'group': 'form', 'index': 5}, name='article'),
path('article-captcha', DemoFormView.as_view(
form_class=ArticleCaptchaForm,
), kwargs={'group': 'form', 'index': 5}, name='article-captcha'),
path('opinion', DemoFormView.as_view(
form_class=OpinionForm,
), kwargs={'group': 'form', 'index': 6}, name='opinion'),
Expand Down

0 comments on commit eab80b7

Please sign in to comment.