diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
a
|
b
|
|
5 | 5 | >>> import tempfile |
6 | 6 | >>> from django.core.files.storage import FileSystemStorage |
7 | 7 | >>> from django.core.files.base import ContentFile |
| 8 | >>> import os.path |
8 | 9 | |
9 | 10 | # Set up a unique temporary directory |
10 | 11 | >>> import os |
… |
… |
|
43 | 44 | ... |
44 | 45 | SuspiciousOperation: Attempted access to '/etc/passwd' denied. |
45 | 46 | |
| 47 | # Case should be preserved by the storage backend |
| 48 | |
| 49 | # Set up a unique temporary directory with a mixed case name |
| 50 | >>> temp_dir2 = tempfile.mktemp() |
| 51 | >>> dn, bn = os.path.split(temp_dir2) |
| 52 | >>> bn = bn[0].swapcase() + bn[1:-1] + bn[-1].swapcase() |
| 53 | >>> temp_dir2 = os.path.join(dn, bn) |
| 54 | >>> os.makedirs(temp_dir2) |
| 55 | |
| 56 | >>> temp_storage2 = FileSystemStorage(location=temp_dir2) |
| 57 | |
| 58 | # Ask the storage backend to store a file with a mixed case filename |
| 59 | >>> mixed_case = 'CaSe_SeNsItIvE' |
| 60 | >>> file = temp_storage2.open(mixed_case, 'w') |
| 61 | >>> file.write('storage contents') |
| 62 | >>> file.close() |
| 63 | >>> os.path.join(temp_dir2, mixed_case) == temp_storage2.path(mixed_case) |
| 64 | True |
| 65 | >>> temp_storage2.delete(mixed_case) |
| 66 | |
46 | 67 | # Custom storage systems can be created to customize behavior |
47 | 68 | |
48 | 69 | >>> class CustomStorage(FileSystemStorage): |
… |
… |
|
70 | 91 | >>> custom_storage.delete(first) |
71 | 92 | >>> custom_storage.delete(second) |
72 | 93 | |
73 | | # Cleanup the temp dir |
| 94 | # Cleanup the temp dirs |
74 | 95 | >>> os.rmdir(temp_dir) |
| 96 | >>> os.rmdir(temp_dir2) |
75 | 97 | |
76 | 98 | |
77 | 99 | # Regression test for #8156: files with unicode names I can't quite figure out the |
diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py
a
|
b
|
|
1 | 1 | import os |
| 2 | import os.path |
2 | 3 | import errno |
3 | 4 | import shutil |
4 | 5 | import unittest |
… |
… |
|
229 | 230 | # CustomUploadError is the error that should have been raised |
230 | 231 | self.assertEqual(err.__class__, uploadhandler.CustomUploadError) |
231 | 232 | |
| 233 | def test_filename_case_preservation(self): |
| 234 | """ |
| 235 | The storage backend shouldn't mess with the case of the filenames |
| 236 | uploaded. |
| 237 | """ |
| 238 | # Synthetize the contents of a file upload with a mixed |
| 239 | # case filename so we don't have to carry such a file |
| 240 | # with the Django tests |
| 241 | OUR_BOUNDARY='oUrBoUnDaRyStRiNg' |
| 242 | post_data = [ |
| 243 | '--%s' % OUR_BOUNDARY, |
| 244 | 'Content-Disposition: form-data; name="file_field"; filename="MiXeD_cAsE.txt"', |
| 245 | 'Content-Type: application/octet-stream', |
| 246 | '', |
| 247 | 'file contents\n' |
| 248 | '', |
| 249 | '--%s--\r\n' % OUR_BOUNDARY, |
| 250 | ] |
| 251 | response = self.client.post('/file_uploads/filename_case/', '\r\n'.join(post_data), |
| 252 | 'multipart/form-data; boundary=%s' % OUR_BOUNDARY) |
| 253 | self.assertEqual(response.status_code, 200) |
| 254 | id = int(response.content) |
| 255 | obj = FileModel.objects.get(pk=id) |
| 256 | # The name of the file uploaded and the file stored in the server-side |
| 257 | # shouldn't differ |
| 258 | self.assertEqual(os.path.basename(obj.testfile.path), 'MiXeD_cAsE.txt') |
| 259 | |
232 | 260 | class DirectoryCreationTests(unittest.TestCase): |
233 | 261 | """ |
234 | 262 | Tests for error handling during directory creation |
diff --git a/tests/regressiontests/file_uploads/urls.py b/tests/regressiontests/file_uploads/urls.py
a
|
b
|
|
9 | 9 | (r'^quota/broken/$', views.file_upload_quota_broken), |
10 | 10 | (r'^getlist_count/$', views.file_upload_getlist_count), |
11 | 11 | (r'^upload_errors/$', views.file_upload_errors), |
| 12 | (r'^filename_case/$', views.file_upload_filename_case_view), |
12 | 13 | ) |
diff --git a/tests/regressiontests/file_uploads/views.py b/tests/regressiontests/file_uploads/views.py
a
|
b
|
|
88 | 88 | def file_upload_errors(request): |
89 | 89 | request.upload_handlers.insert(0, ErroringUploadHandler()) |
90 | 90 | return file_upload_echo(request) |
| 91 | |
| 92 | def file_upload_filename_case_view(request): |
| 93 | # Adding the file to the database should succeed |
| 94 | file = request.FILES['file_field'] |
| 95 | obj = FileModel() |
| 96 | obj.testfile.save(file.name, file) |
| 97 | |
| 98 | return HttpResponse('%d' % obj.pk) |
| 99 | |