@@ -276,7 +276,7 @@ def unicontent(r):
276276def 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"{} != {}\n Got 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