1. Mikhail Korobov
  2. django-webtest


Mikhail Korobov  committed 8ffdac4

assertFormError API is also supported

  • Participants
  • Parent commits 95cb56a
  • Branches default

Comments (0)

Files changed (5)

File README.rst

View file
 You also get the ``response.template`` and ``response.context`` goodness that
 is usually only available if you use django's native test client. These
 attributes contain a list of templates that were used to render the response
-and the context used to render these templates.
+and the context used to render these templates. This also means that django's
+native ``self.assertFormError``,  ``self.assertTemplateUsed`` and
+``self.assertTemplateNotUsed`` API is also supported for WebTest responses.
 Unlike django's native test client CSRF checks are not suppressed
 by default so missing CSRF tokens will cause test fails (and that's good).

File django_webtest_tests/templates/form.html

View file
+<!DOCTYPE html>
+    <form action='' method='post'>{% csrf_token %}
+        {{ form.as_p }}
+        <input type='submit'>
+    </form>

File django_webtest_tests/testapp_tests/tests.py

View file
 from webtest import AppError
 from django_webtest import WebTest
 from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+from testapp_tests.views import PasswordForm
 class GetRequestTest(WebTest):
     def test_get_request(self):
         self.assertEqual(response.status_int, 403)
+class FormSubmitTest(WebTest):
+    def test_form_submit(self):
+        page = self.app.get(reverse('check_password'))
+        page.form['password'] = 'bar'
+        page_with_errors = page.form.submit()
+        self.assertFormError(page_with_errors, 'form', 'password', 'Incorrect password.')
+        page_with_errors.form['password'] = 'foo'
+        page_with_errors.form.submit().follow() # check for 302 response
 class TemplateContextTest(WebTest):
     def test_rendered_templates(self):
         response = self.app.get('/template/index.html')
     def test_auth_is_enabled(self):
         from django.conf import settings
         remote_user_middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
         assert remote_user_middleware in settings.MIDDLEWARE_CLASSES
         assert 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS

File django_webtest_tests/testapp_tests/views.py

View file
+from django import forms
+from django.http import HttpResponseRedirect
+from django.views.generic.simple import direct_to_template
+class PasswordForm(forms.Form):
+    password = forms.CharField(widget=forms.PasswordInput())
+    def clean_password(self):
+        if self.cleaned_data['password'] != 'foo':
+            raise forms.ValidationError('Incorrect password.')
+        return self.cleaned_data['password']
+def check_password(request):
+    form = PasswordForm(request.POST or None)
+    if form.is_valid():
+        return HttpResponseRedirect('/')
+    return direct_to_template(request, 'form.html', {'form': form})

File django_webtest_tests/urls.py

View file
 urlpatterns = patterns('',
     url(r'^$', simple_method_test, name='simple-method-test'),
     url(r'^template/(.*)$', simple_template_render, name='simple-template-test'),
-    # Uncomment the next line to enable the admin:
-    # (r'^admin/', include(admin.site.urls)),
+    url(r'^check-password/$', 'testapp_tests.views.check_password', name='check_password')