|
| 1 | +from hashlib import md5 |
| 2 | +from io import BytesIO |
| 3 | + |
| 4 | +from gridfs import GridFS |
| 5 | +from werkzeug.exceptions import NotFound |
| 6 | +import pytest |
| 7 | + |
| 8 | +from flask_pymongo.tests.util import FlaskPyMongoTest |
| 9 | + |
| 10 | + |
| 11 | +class GridFSCleanupMixin(object): |
| 12 | + |
| 13 | + def tearDown(self): |
| 14 | + gridfs = GridFS(self.mongo.db) |
| 15 | + files = list(gridfs.find()) |
| 16 | + for gridfile in files: |
| 17 | + gridfs.delete(gridfile._id) |
| 18 | + |
| 19 | + super(GridFSCleanupMixin, self).tearDown() |
| 20 | + |
| 21 | + |
| 22 | +class TestSaveFile(GridFSCleanupMixin, FlaskPyMongoTest): |
| 23 | + |
| 24 | + def test_it_saves_files(self): |
| 25 | + fileobj = BytesIO(b"these are the bytes") |
| 26 | + |
| 27 | + self.mongo.save_file("my-file", fileobj) |
| 28 | + |
| 29 | + gridfs = GridFS(self.mongo.db) |
| 30 | + assert gridfs.exists({"filename": "my-file"}) |
| 31 | + |
| 32 | + def test_it_guesses_type_from_filename(self): |
| 33 | + fileobj = BytesIO(b"these are the bytes") |
| 34 | + |
| 35 | + self.mongo.save_file("my-file.txt", fileobj) |
| 36 | + |
| 37 | + gridfs = GridFS(self.mongo.db) |
| 38 | + gridfile = gridfs.find_one({"filename": "my-file.txt"}) |
| 39 | + assert gridfile.content_type == "text/plain" |
| 40 | + |
| 41 | + |
| 42 | +class TestSendFile(GridFSCleanupMixin, FlaskPyMongoTest): |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + super(TestSendFile, self).setUp() |
| 46 | + |
| 47 | + # make it bigger than 1 gridfs chunk |
| 48 | + self.myfile = BytesIO(b"a" * 500 * 1024) |
| 49 | + self.mongo.save_file("myfile.txt", self.myfile) |
| 50 | + |
| 51 | + def test_it_404s_for_missing_files(self): |
| 52 | + with pytest.raises(NotFound): |
| 53 | + self.mongo.send_file("no-such-file.txt") |
| 54 | + |
| 55 | + def test_it_sets_content_type(self): |
| 56 | + resp = self.mongo.send_file("myfile.txt") |
| 57 | + assert resp.content_type.startswith("text/plain") |
| 58 | + |
| 59 | + def test_it_sets_content_length(self): |
| 60 | + resp = self.mongo.send_file("myfile.txt") |
| 61 | + assert resp.content_length == len(self.myfile.getvalue()) |
| 62 | + |
| 63 | + def test_it_sets_supports_conditional_gets(self): |
| 64 | + # a basic conditional GET |
| 65 | + environ_args = { |
| 66 | + "method": "GET", |
| 67 | + "headers": { |
| 68 | + "If-None-Match": md5(self.myfile.getvalue()).hexdigest(), |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + with self.app.test_request_context(**environ_args): |
| 73 | + resp = self.mongo.send_file("myfile.txt") |
| 74 | + assert resp.status_code == 304 |
| 75 | + |
| 76 | + def test_it_sets_cache_headers(self): |
| 77 | + resp = self.mongo.send_file("myfile.txt", cache_for=60) |
| 78 | + assert resp.cache_control.max_age == 60 |
| 79 | + assert resp.cache_control.public is True |
| 80 | + |
| 81 | + def test_it_streams_results(self): |
| 82 | + resp = self.mongo.send_file("myfile.txt") |
| 83 | + assert resp.is_streamed |
0 commit comments