Skip to content

Commit 4426e33

Browse files
committed
Add assertNoFormPostErrors for use instead of testing the status code
of form post responses to make it easier to see what goes wrong when the form in a test suddently doesn't validate - Legacy-Id: 12744
1 parent 698965e commit 4426e33

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

ietf/utils/test_utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def unicontent(r):
276276
def reload_db_objects(*objects):
277277
"""Rerequest the given arguments from the database so they're refreshed, to be used like
278278
279-
foo, bar = reload_objects(foo, bar)"""
279+
foo, bar = reload_db_objects(foo, bar)"""
280280

281281
t = tuple(o.__class__.objects.get(pk=o.pk) for o in objects)
282282
if len(objects) == 1:
@@ -307,5 +307,27 @@ def assertValidHTMLResponse(self, resp):
307307
self.assertTrue(resp['Content-Type'].startswith('text/html'))
308308
self.assertValidHTML(resp.content)
309309

310+
def assertNoFormPostErrors(self, response, error_css_selector=".has-error"):
311+
"""Try to fish out form errors, if none found at least check the
312+
status code to be a redirect.
313+
314+
Assumptions:
315+
- a POST is followed by a 302 redirect
316+
- form errors can be found with a simple CSS selector
317+
318+
"""
319+
320+
if response.status_code == 200:
321+
from pyquery import PyQuery
322+
from lxml import html
323+
self.maxDiff = None
324+
325+
errors = [html.tostring(n).decode() for n in PyQuery(response.content)(error_css_selector)]
326+
if errors:
327+
explanation = u"{} != {}\nGot form back with errors:\n----\n".format(response.status_code, 302) + u"----\n".join(errors)
328+
self.assertEqual(response.status_code, 302, explanation)
329+
330+
self.assertEqual(response.status_code, 302)
331+
310332
def __str__(self):
311333
return "%s (%s.%s)" % (self._testMethodName, strclass(self.__class__),self._testMethodName)

0 commit comments

Comments
 (0)