Skip to content

Commit

Permalink
Add tests for writing PNG/JPEG image files (with proper MIME-types) i…
Browse files Browse the repository at this point in the history
…n GridFS backend
  • Loading branch information
rclement committed Apr 11, 2018
1 parent a5e80a1 commit e1cc98d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
5 changes: 1 addition & 4 deletions flask_fs/backends/gridfs.py
Expand Up @@ -61,10 +61,7 @@ def write(self, filename, content):
if hasattr(content, 'content_type'):
kwargs['content_type'] = content.content_type

return self.fs.put(
self.as_binary(content),
**kwargs
)
return self.fs.put(self.as_binary(content), **kwargs)

def delete(self, filename):
regex = '^{0}'.format(re.escape(filename))
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Expand Up @@ -52,8 +52,12 @@ def jpgfile():


class Utils(object):
def filestorage(self, filename, content):
return FileStorage(self.file(content), filename)
def filestorage(self, filename, content, content_type=None):
return FileStorage(
self.file(content),
filename,
content_type=content_type
)

def file(self, content):
if isinstance(content, six.binary_type):
Expand Down
37 changes: 37 additions & 0 deletions tests/test_gridfs_backend.py
Expand Up @@ -10,13 +10,26 @@
from flask_fs.storage import Config

import pytest
import mimetypes
import six


TEST_DB = 'fstest'


class GridFsBackendTest(BackendTestCase):
hasher = 'md5'

@pytest.fixture
def pngimage(self, pngfile):
with open(pngfile, 'rb') as f:
yield f

@pytest.fixture
def jpgimage(self, jpgfile):
with open(jpgfile, 'rb') as f:
yield f

@pytest.fixture(autouse=True)
def setup(self):
self.client = MongoClient()
Expand Down Expand Up @@ -58,3 +71,27 @@ def test_delete_with_versions(self, faker):

self.backend.delete(filename)
assert not self.file_exists(filename)

def test_write_pngimage(self, pngimage, utils):
filename = 'test.png'
content = six.binary_type(pngimage.read())
content_type = mimetypes.guess_type(filename)[0]
f = utils.filestorage(filename, content, content_type)
self.backend.write(filename, f)

with self.backend.open(filename, 'rb') as f:
assert f.content_type == content_type

self.assert_bin_equal(filename, content)

def test_write_jpgimage(self, jpgimage, utils):
filename = 'test.jpg'
content = six.binary_type(jpgimage.read())
content_type = mimetypes.guess_type(filename)[0]
f = utils.filestorage(filename, content, content_type)
self.backend.write(filename, f)

with self.backend.open(filename, 'rb') as f:
assert f.content_type == content_type

self.assert_bin_equal(filename, content)

0 comments on commit e1cc98d

Please sign in to comment.