PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 14ms app.codeStats 0ms

/tests/regressiontests/servers/tests.py

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