2017-01-10 16:29:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-03-01 20:32:02 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import base64
|
2016-12-08 03:50:48 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from dbfilestorage.models import DBFile
|
|
|
|
|
2016-12-09 15:07:16 +00:00
|
|
|
from django.contrib.auth.models import User
|
2017-01-10 16:29:28 +00:00
|
|
|
from django.core.files.base import ContentFile
|
2016-12-08 03:50:48 +00:00
|
|
|
from django.core.files.storage import default_storage
|
2016-12-09 15:07:16 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-12-08 20:30:39 +00:00
|
|
|
from django.test import TestCase, Client
|
2016-12-08 03:50:48 +00:00
|
|
|
from django.test.utils import override_settings
|
|
|
|
|
2016-12-09 15:07:16 +00:00
|
|
|
|
2018-03-01 20:32:02 +00:00
|
|
|
|
2016-12-08 03:50:48 +00:00
|
|
|
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
|
2016-12-08 20:43:34 +00:00
|
|
|
DEFAULT_FILE_STORAGE = "dbfilestorage.storage.DBFileStorage"
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
|
2016-12-08 20:30:39 +00:00
|
|
|
@override_settings(DEFAULT_FILE_STORAGE=DEFAULT_FILE_STORAGE)
|
2016-12-08 03:50:48 +00:00
|
|
|
class DBFileTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.filename = "kris.jpg"
|
|
|
|
self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename)
|
2017-01-23 19:08:51 +00:00
|
|
|
|
2017-08-11 15:30:15 +00:00
|
|
|
self._upload(self.filepath)
|
|
|
|
|
|
|
|
def _upload(self, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = self.filepath
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
with open(self.filepath, 'rb') as f:
|
2017-08-11 15:30:15 +00:00
|
|
|
return default_storage.save(name, f)
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def test_upload(self):
|
|
|
|
""" Test that the file storage uploads and puts in DB Properly """
|
2017-01-27 20:52:02 +00:00
|
|
|
self.assertTrue(DBFile.objects.filter(name=self.filepath).exists())
|
2016-12-09 15:07:16 +00:00
|
|
|
|
2017-01-10 16:29:28 +00:00
|
|
|
def test_content_file(self):
|
|
|
|
""" Test that this code works with ContentFile as well """
|
2018-03-01 20:32:02 +00:00
|
|
|
content_file = ContentFile(b"\\u018aBSt\\xf8rage")
|
2017-01-10 16:29:28 +00:00
|
|
|
default_storage.save("unicode", content_file)
|
2017-01-27 20:52:02 +00:00
|
|
|
unicode_file = DBFile.objects.get(name="unicode")
|
2018-03-01 20:32:02 +00:00
|
|
|
self.assertEqual(str(unicode_file),
|
2017-01-27 20:52:02 +00:00
|
|
|
"unicode <application/octet-stream>")
|
2017-01-10 16:29:28 +00:00
|
|
|
|
2016-12-09 15:07:16 +00:00
|
|
|
def test_no_duplicate_upload(self):
|
|
|
|
""" Test that it won't make a new file if it already exists """
|
|
|
|
# uploads once in setup already
|
|
|
|
name2 = self._upload()
|
2017-01-25 19:59:43 +00:00
|
|
|
self.assertEqual(self.filepath, name2)
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def test_equality(self):
|
|
|
|
""" Test that the DB entry matches what is expected from the file """
|
|
|
|
with open(self.filepath, 'rb') as f:
|
2017-01-27 20:52:02 +00:00
|
|
|
dbf = DBFile.objects.get(name=self.filepath)
|
2018-03-01 20:32:02 +00:00
|
|
|
self.assertEqual(base64.b64decode(dbf.b64), f.read())
|
2016-12-08 03:50:48 +00:00
|
|
|
self.assertEqual(dbf.content_type, 'image/jpeg')
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
""" Test that the storage mechanism can upload """
|
2017-01-27 20:52:02 +00:00
|
|
|
dbf = default_storage.open(self.filepath)
|
2016-12-08 03:50:48 +00:00
|
|
|
with open(self.filepath, 'rb') as f:
|
|
|
|
self.assertEqual(dbf.read(), f.read())
|
|
|
|
|
|
|
|
def test_exists(self):
|
2018-03-01 20:32:02 +00:00
|
|
|
""" Test that the storage mechanism can check existence """
|
2017-01-27 20:52:02 +00:00
|
|
|
self.assertTrue(default_storage.exists(self.filepath))
|
2016-12-08 20:30:39 +00:00
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
""" Test Deletion """
|
2017-01-27 20:52:02 +00:00
|
|
|
self.assertTrue(DBFile.objects.filter(name=self.filepath).exists())
|
|
|
|
default_storage.delete(self.filepath)
|
|
|
|
self.assertFalse(DBFile.objects.filter(name=self.filepath).exists())
|
2017-01-10 16:29:28 +00:00
|
|
|
# Also test that calling delete on something that doesn't exist,
|
|
|
|
# errors silently
|
|
|
|
self.assertFalse(DBFile.objects.filter(name="Nothing").exists())
|
|
|
|
default_storage.delete("Nothing")
|
2016-12-08 20:30:39 +00:00
|
|
|
|
|
|
|
def test_size(self):
|
|
|
|
""" Ensure we can get the proper size """
|
2017-01-27 20:52:02 +00:00
|
|
|
size = default_storage.size(self.filepath)
|
2016-12-08 20:30:39 +00:00
|
|
|
self.assertGreater(size, 0)
|
|
|
|
|
2017-05-10 14:46:12 +00:00
|
|
|
def test_raw_save(self):
|
2018-03-01 20:32:02 +00:00
|
|
|
CONTENT_DATA_1 = "Here's some stuff! ƊBStørage - ONE"
|
|
|
|
CONTENT_DATA_2 = "Here's some stuff! ƊBStørage - TWO"
|
2017-05-10 14:46:12 +00:00
|
|
|
FILE_NAME = "saveable.txt"
|
|
|
|
self.assertFalse(DBFile.objects.filter(name=FILE_NAME).exists())
|
|
|
|
|
|
|
|
# -- Save to a _new_ file
|
|
|
|
content_file = ContentFile(CONTENT_DATA_1.encode("utf-8"))
|
|
|
|
default_storage.save(FILE_NAME, content_file)
|
|
|
|
self.assertTrue(DBFile.objects.filter(name=FILE_NAME).exists())
|
|
|
|
|
|
|
|
with default_storage.open(FILE_NAME, "rb") as f:
|
|
|
|
read_back = f.read().decode("utf-8")
|
|
|
|
self.assertEqual(read_back, CONTENT_DATA_1)
|
|
|
|
|
|
|
|
# -- Save to an _existing_ file
|
|
|
|
content_file = ContentFile(CONTENT_DATA_2.encode("utf-8"))
|
|
|
|
default_storage.save(FILE_NAME, content_file)
|
|
|
|
self.assertTrue(DBFile.objects.filter(name=FILE_NAME).exists())
|
|
|
|
with default_storage.open(FILE_NAME, "rb") as f:
|
|
|
|
read_back = f.read().decode("utf-8")
|
|
|
|
self.assertEqual(read_back, CONTENT_DATA_2)
|
|
|
|
|
|
|
|
# -- Clean up after ourselves
|
|
|
|
default_storage.delete(FILE_NAME)
|
|
|
|
self.assertFalse(DBFile.objects.filter(name=FILE_NAME).exists())
|
|
|
|
|
2016-12-08 20:30:39 +00:00
|
|
|
def test_url(self):
|
2017-01-27 20:52:02 +00:00
|
|
|
""" Test that the url returned is the filename """
|
|
|
|
self.assertIn(self.filename, default_storage.url(self.filename))
|
2016-12-08 20:30:39 +00:00
|
|
|
|
|
|
|
def test_view(self):
|
|
|
|
client = Client()
|
2017-01-27 20:52:02 +00:00
|
|
|
url = default_storage.url(self.filepath)
|
|
|
|
resp = client.get(url)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2017-08-11 15:30:15 +00:00
|
|
|
|
2017-01-20 19:38:41 +00:00
|
|
|
def test_view_fails(self):
|
|
|
|
client = Client()
|
|
|
|
url = default_storage.url("failure")
|
2016-12-08 20:30:39 +00:00
|
|
|
resp = client.get(url)
|
2017-01-20 19:38:41 +00:00
|
|
|
self.assertEqual(resp.status_code, 404)
|
|
|
|
|
2016-12-09 15:07:16 +00:00
|
|
|
def test_admin(self):
|
|
|
|
my_admin = User.objects.create_superuser(
|
|
|
|
username='tester',
|
|
|
|
email='test@test.com',
|
|
|
|
password='top_secret')
|
|
|
|
client = Client()
|
|
|
|
client.login(username=my_admin.username, password='top_secret')
|
|
|
|
url = reverse("admin:dbfilestorage_dbfile_changelist")
|
|
|
|
resp = client.get(url)
|
2017-01-27 20:52:02 +00:00
|
|
|
self.assertContains(resp, self.filepath)
|
2017-01-23 20:56:51 +00:00
|
|
|
|
|
|
|
def test_mtime(self):
|
|
|
|
""" Ensure we can get the modified time """
|
2017-01-25 19:59:43 +00:00
|
|
|
mtime = default_storage.modified_time(self.filepath)
|
2017-01-23 20:56:51 +00:00
|
|
|
self.assertIsNotNone(mtime)
|
2017-08-11 15:30:15 +00:00
|
|
|
|
|
|
|
def test_listdir(self):
|
|
|
|
""" Make sure listdir works, and only returns things under 'dirname' """
|
|
|
|
names = [
|
2018-03-01 20:32:02 +00:00
|
|
|
'dirname/kris.jpg',
|
|
|
|
'dirname/kris2.jpg',
|
|
|
|
'dirname/kris3.jpg']
|
2017-08-11 15:30:15 +00:00
|
|
|
|
|
|
|
for name in names:
|
|
|
|
self._upload(name=name)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
default_storage.listdir("dirname"),
|
|
|
|
([], names)
|
|
|
|
)
|
|
|
|
|