def test_save(self): with media_root(): storage = RandomFilenameFileSystemStorage( randomfilename_length=DEFAULT_LENGTH) name1 = storage.save('foo/bar.txt', ContentFile('Hello world!')) storage.delete(name1) self.assertFilename(name1, 'foo/bar.txt') name2 = storage.save('foo/bar.txt', ContentFile('Hello world!')) storage.delete(name2) self.assertFilename(name2, 'foo/bar.txt') self.assertNotEqual(name1, name2)
def test_save(self): with media_root(): storage = RandomFilenameFileSystemStorage( randomfilename_length=DEFAULT_LENGTH ) name1 = storage.save('foo/bar.txt', ContentFile('Hello world!')) storage.delete(name1) self.assertFilename(name1, 'foo/bar.txt') name2 = storage.save('foo/bar.txt', ContentFile('Hello world!')) storage.delete(name2) self.assertFilename(name2, 'foo/bar.txt') self.assertNotEqual(name1, name2)
def test_save_exception(self): with media_root(): storage = RandomFilenameFileSystemStorage( randomfilename_length=DEFAULT_LENGTH) name = storage.save('foo/bar.txt', ContentFile('Hello world!')) self.assertRaises(IOError, storage.save, name + posixpath.sep, ContentFile('Hello world!'))
def test_save_permissions(self): with media_root(): with patch(settings, FILE_UPLOAD_PERMISSIONS=stat.S_IRUSR): storage = SafeFileSystemStorage() content = 'Hello world!' name = storage.save('hello.txt', ContentFile('Hello world!')) self.assertTrue(os.access(storage.path(name), os.R_OK)) self.assertFalse(os.access(storage.path(name), os.W_OK)) self.assertFalse(os.access(storage.path(name), os.X_OK))
def test_save_exception(self): with media_root(): storage = RandomFilenameFileSystemStorage( randomfilename_length=DEFAULT_LENGTH ) name = storage.save('foo/bar.txt', ContentFile('Hello world!')) self.assertRaises(IOError, storage.save, name + posixpath.sep, ContentFile('Hello world!'))
def test_save_no_uniquify(self): with media_root(): storage = SafeFileSystemStorage(uniquify_names=False) # Save one copy content = 'Hello world!' name = storage.save('hello.txt', ContentFile(content)) self.assertEqual(name, 'hello.txt') self.assertEqual(open(storage.path(name)).read(), content) # Save another, which should throw an exception content = 'Hello.' self.assertRaises(OSError, storage._save, 'hello.txt', ContentFile(content))
def test_save_tempfile(self): with media_root(): storage = SafeFileSystemStorage() content = 'Hello world!' f = TemporaryUploadedFile(name='filename', content_type='text/plain', size=len(content), charset='utf-8') f.write(content) f.seek(0) name = storage.save('hello.txt', f) self.assertEqual(name, 'hello.txt') self.assertEqual(open(storage.path(name)).read(), content)
def test_save(self): with media_root(): storage = SafeFileSystemStorage() # Save one copy content = 'Hello world!' name = storage.save('hello.txt', ContentFile(content)) self.assertEqual(name, 'hello.txt') self.assertEqual(open(storage.path(name)).read(), content) # Save another, which should be renamed content = 'Hello.' name = storage._save('hello.txt', ContentFile(content)) self.assertTrue(name in ('hello_.txt', 'hello_1.txt'), "%r is not 'hello_.txt' or 'hello_1.txt'" % name) self.assertEqual(open(storage.path(name)).read(), content)