PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/entelib/baseapp/tests/test_utils.py

http://entelib.googlecode.com/
Python | 47 lines | 41 code | 3 blank | 3 comment | 1 complexity | 1b2c965ee744f7a960261e65d51bcb4d MD5 | raw file
Possible License(s): GPL-3.0
  1. # -*- coding: utf-8 -*-
  2. from django.http import HttpResponseRedirect, HttpResponseNotFound, HttpResponsePermanentRedirect
  3. import re
  4. def accessed(response):
  5. """
  6. Use self.assert_(accessed(response)) to check if page wasn't redirected to login,
  7. because lack of perms or sth.
  8. Returns:
  9. True - page rendered properly
  10. False - request redirected to login page
  11. """
  12. login_infix = '/entelib/login/'
  13. if isinstance(response, HttpResponseNotFound): # not found, so also not accessed
  14. return False
  15. if isinstance(response, HttpResponsePermanentRedirect):
  16. if 'Location' not in response:
  17. return False
  18. return login_infix not in response['Location']
  19. if isinstance(response, HttpResponseRedirect):
  20. return login_infix not in response['Location']
  21. if not hasattr(response, 'redirect_chain'):
  22. return True
  23. rc = response.redirect_chain
  24. if not rc:
  25. return True
  26. return login_infix not in rc[-1][0]
  27. def choice(collection):
  28. ''' Simplified pseudo-choice ensuring repeatable results. '''
  29. lst = list(collection)
  30. length = len(lst)
  31. seed = 117
  32. index = (seed % length) ** 3 % length
  33. return lst[index]
  34. def form_errors_happened(html):
  35. ''' Checks if html contains form errors. '''
  36. return re.compile('.*<form.*error.*</form>.*', re.S).match(html)
  37. def no_form_errors_happened(html):
  38. return not form_errors_happened(html)