PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/servers/tests.py

https://code.google.com/p/mango-py/
Python | 69 lines | 55 code | 6 blank | 8 comment | 6 complexity | 4611b8a1c41ae7d98800071da107d776 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Tests for django.core.servers.
  3. """
  4. import os
  5. import django
  6. from django.test import TestCase
  7. from django.core.handlers.wsgi import WSGIHandler
  8. from django.core.servers.basehttp import AdminMediaHandler
  9. from django.conf import settings
  10. class AdminMediaHandlerTests(TestCase):
  11. def setUp(self):
  12. self.admin_media_file_path = os.path.abspath(
  13. os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
  14. )
  15. self.handler = AdminMediaHandler(WSGIHandler())
  16. def test_media_urls(self):
  17. """
  18. Tests that URLs that look like absolute file paths after the
  19. settings.ADMIN_MEDIA_PREFIX don't turn into absolute file paths.
  20. """
  21. # Cases that should work on all platforms.
  22. data = (
  23. ('%scss/base.css' % settings.ADMIN_MEDIA_PREFIX, ('css', 'base.css')),
  24. )
  25. # Cases that should raise an exception.
  26. bad_data = ()
  27. # Add platform-specific cases.
  28. if os.sep == '/':
  29. data += (
  30. # URL, tuple of relative path parts.
  31. ('%s\\css/base.css' % settings.ADMIN_MEDIA_PREFIX, ('\\css', 'base.css')),
  32. )
  33. bad_data += (
  34. '%s/css/base.css' % settings.ADMIN_MEDIA_PREFIX,
  35. '%s///css/base.css' % settings.ADMIN_MEDIA_PREFIX,
  36. '%s../css/base.css' % settings.ADMIN_MEDIA_PREFIX,
  37. )
  38. elif os.sep == '\\':
  39. bad_data += (
  40. '%sC:\css/base.css' % settings.ADMIN_MEDIA_PREFIX,
  41. '%s/\\css/base.css' % settings.ADMIN_MEDIA_PREFIX,
  42. '%s\\css/base.css' % settings.ADMIN_MEDIA_PREFIX,
  43. '%s\\\\css/base.css' % settings.ADMIN_MEDIA_PREFIX
  44. )
  45. for url, path_tuple in data:
  46. try:
  47. output = self.handler.file_path(url)
  48. except ValueError:
  49. self.fail("Got a ValueError exception, but wasn't expecting"
  50. " one. URL was: %s" % url)
  51. rel_path = os.path.join(*path_tuple)
  52. desired = os.path.normcase(
  53. os.path.join(self.admin_media_file_path, rel_path))
  54. self.assertEqual(output, desired,
  55. "Got: %s, Expected: %s, URL was: %s" % (output, desired, url))
  56. for url in bad_data:
  57. try:
  58. output = self.handler.file_path(url)
  59. except ValueError:
  60. continue
  61. self.fail('URL: %s should have caused a ValueError exception.'
  62. % url)