~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/regressiontests/file_storage/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
import os
 
3
import shutil
 
4
import sys
 
5
import tempfile
 
6
import time
 
7
import unittest
 
8
from cStringIO import StringIO
 
9
from django.conf import settings
 
10
from django.core.exceptions import SuspiciousOperation
 
11
from django.core.files.base import ContentFile
 
12
from django.core.files.images import get_image_dimensions
 
13
from django.core.files.storage import FileSystemStorage
 
14
from django.core.files.uploadedfile import UploadedFile
 
15
from unittest import TestCase
 
16
try:
 
17
    import threading
 
18
except ImportError:
 
19
    import dummy_threading as threading
 
20
 
 
21
try:
 
22
    # Checking for the existence of Image is enough for CPython, but
 
23
    # for PyPy, you need to check for the underlying modules
 
24
    from PIL import Image, _imaging
 
25
except ImportError:
 
26
    Image = None
 
27
 
 
28
class FileStorageTests(unittest.TestCase):
 
29
    storage_class = FileSystemStorage
 
30
    
 
31
    def setUp(self):
 
32
        self.temp_dir = tempfile.mktemp()
 
33
        os.makedirs(self.temp_dir)
 
34
        self.storage = self.storage_class(location=self.temp_dir)
 
35
    
 
36
    def tearDown(self):
 
37
        os.rmdir(self.temp_dir)
 
38
        
 
39
    def test_file_access_options(self):
 
40
        """
 
41
        Standard file access options are available, and work as expected.
 
42
        """
 
43
        self.failIf(self.storage.exists('storage_test'))
 
44
        f = self.storage.open('storage_test', 'w')
 
45
        f.write('storage contents')
 
46
        f.close()
 
47
        self.assert_(self.storage.exists('storage_test'))
 
48
 
 
49
        f = self.storage.open('storage_test', 'r')
 
50
        self.assertEqual(f.read(), 'storage contents')
 
51
        f.close()
 
52
        
 
53
        self.storage.delete('storage_test')
 
54
        self.failIf(self.storage.exists('storage_test'))
 
55
 
 
56
    def test_file_storage_prevents_directory_traversal(self):
 
57
        """
 
58
        File storage prevents directory traversal (files can only be accessed if
 
59
        they're below the storage location).
 
60
        """
 
61
        self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
 
62
        self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
 
63
 
 
64
class CustomStorage(FileSystemStorage):
 
65
    def get_available_name(self, name):
 
66
        """
 
67
        Append numbers to duplicate files rather than underscores, like Trac.
 
68
        """
 
69
        parts = name.split('.')
 
70
        basename, ext = parts[0], parts[1:]
 
71
        number = 2
 
72
        while self.exists(name):
 
73
            name = '.'.join([basename, str(number)] + ext)
 
74
            number += 1
 
75
 
 
76
        return name
 
77
 
 
78
class CustomStorageTests(FileStorageTests):
 
79
    storage_class = CustomStorage
 
80
    
 
81
    def test_custom_get_available_name(self):
 
82
        first = self.storage.save('custom_storage', ContentFile('custom contents'))
 
83
        self.assertEqual(first, 'custom_storage')
 
84
        second = self.storage.save('custom_storage', ContentFile('more contents'))
 
85
        self.assertEqual(second, 'custom_storage.2')
 
86
        self.storage.delete(first)
 
87
        self.storage.delete(second)
 
88
 
 
89
class UnicodeFileNameTests(unittest.TestCase):
 
90
    def test_unicode_file_names(self):
 
91
        """
 
92
        Regression test for #8156: files with unicode names I can't quite figure
 
93
        out the encoding situation between doctest and this file, but the actual
 
94
        repr doesn't matter; it just shouldn't return a unicode object.
 
95
        """
 
96
        uf = UploadedFile(name=u'¿Cómo?',content_type='text')
 
97
        self.assertEqual(type(uf.__repr__()), str)
 
98
 
 
99
# Tests for a race condition on file saving (#4948).
 
100
# This is written in such a way that it'll always pass on platforms
 
101
# without threading.
 
102
 
 
103
class SlowFile(ContentFile):
 
104
    def chunks(self):
 
105
        time.sleep(1)
 
106
        return super(ContentFile, self).chunks()
 
107
 
 
108
class FileSaveRaceConditionTest(TestCase):
 
109
    def setUp(self):
 
110
        self.storage_dir = tempfile.mkdtemp()
 
111
        self.storage = FileSystemStorage(self.storage_dir)
 
112
        self.thread = threading.Thread(target=self.save_file, args=['conflict'])
 
113
 
 
114
    def tearDown(self):
 
115
        shutil.rmtree(self.storage_dir)
 
116
 
 
117
    def save_file(self, name):
 
118
        name = self.storage.save(name, SlowFile("Data"))
 
119
 
 
120
    def test_race_condition(self):
 
121
        self.thread.start()
 
122
        name = self.save_file('conflict')
 
123
        self.thread.join()
 
124
        self.assert_(self.storage.exists('conflict'))
 
125
        self.assert_(self.storage.exists('conflict_'))
 
126
        self.storage.delete('conflict')
 
127
        self.storage.delete('conflict_')
 
128
 
 
129
class FileStoragePermissions(TestCase):
 
130
    def setUp(self):
 
131
        self.old_perms = settings.FILE_UPLOAD_PERMISSIONS
 
132
        settings.FILE_UPLOAD_PERMISSIONS = 0666
 
133
        self.storage_dir = tempfile.mkdtemp()
 
134
        self.storage = FileSystemStorage(self.storage_dir)
 
135
 
 
136
    def tearDown(self):
 
137
        settings.FILE_UPLOAD_PERMISSIONS = self.old_perms
 
138
        shutil.rmtree(self.storage_dir)
 
139
 
 
140
    def test_file_upload_permissions(self):
 
141
        name = self.storage.save("the_file", ContentFile("data"))
 
142
        actual_mode = os.stat(self.storage.path(name))[0] & 0777
 
143
        self.assertEqual(actual_mode, 0666)
 
144
 
 
145
 
 
146
class FileStoragePathParsing(TestCase):
 
147
    def setUp(self):
 
148
        self.storage_dir = tempfile.mkdtemp()
 
149
        self.storage = FileSystemStorage(self.storage_dir)
 
150
 
 
151
    def tearDown(self):
 
152
        shutil.rmtree(self.storage_dir)
 
153
 
 
154
    def test_directory_with_dot(self):
 
155
        """Regression test for #9610.
 
156
 
 
157
        If the directory name contains a dot and the file name doesn't, make
 
158
        sure we still mangle the file name instead of the directory name.
 
159
        """
 
160
 
 
161
        self.storage.save('dotted.path/test', ContentFile("1"))
 
162
        self.storage.save('dotted.path/test', ContentFile("2"))
 
163
 
 
164
        self.failIf(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
 
165
        self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
 
166
        self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_')))
 
167
 
 
168
    def test_first_character_dot(self):
 
169
        """
 
170
        File names with a dot as their first character don't have an extension,
 
171
        and the underscore should get added to the end.
 
172
        """
 
173
        self.storage.save('dotted.path/.test', ContentFile("1"))
 
174
        self.storage.save('dotted.path/.test', ContentFile("2"))
 
175
 
 
176
        self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
 
177
        # Before 2.6, a leading dot was treated as an extension, and so
 
178
        # underscore gets added to beginning instead of end.
 
179
        if sys.version_info < (2, 6):
 
180
            self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/_.test')))
 
181
        else:
 
182
            self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_')))
 
183
 
 
184
if Image is not None:
 
185
    class DimensionClosingBug(TestCase):
 
186
        """
 
187
        Test that get_image_dimensions() properly closes files (#8817)
 
188
        """
 
189
        def test_not_closing_of_files(self):
 
190
            """
 
191
            Open files passed into get_image_dimensions() should stay opened.
 
192
            """
 
193
            empty_io = StringIO()
 
194
            try:
 
195
                get_image_dimensions(empty_io)
 
196
            finally:
 
197
                self.assert_(not empty_io.closed)
 
198
 
 
199
        def test_closing_of_filenames(self):
 
200
            """
 
201
            get_image_dimensions() called with a filename should closed the file.
 
202
            """
 
203
            # We need to inject a modified open() builtin into the images module
 
204
            # that checks if the file was closed properly if the function is
 
205
            # called with a filename instead of an file object.
 
206
            # get_image_dimensions will call our catching_open instead of the
 
207
            # regular builtin one.
 
208
 
 
209
            class FileWrapper(object):
 
210
                _closed = []
 
211
                def __init__(self, f):
 
212
                    self.f = f
 
213
                def __getattr__(self, name):
 
214
                    return getattr(self.f, name)
 
215
                def close(self):
 
216
                    self._closed.append(True)
 
217
                    self.f.close()
 
218
 
 
219
            def catching_open(*args):
 
220
                return FileWrapper(open(*args))
 
221
 
 
222
            from django.core.files import images
 
223
            images.open = catching_open
 
224
            try:
 
225
                get_image_dimensions(os.path.join(os.path.dirname(__file__), "test1.png"))
 
226
            finally:
 
227
                del images.open
 
228
            self.assert_(FileWrapper._closed)