Skip to content

Commit 8a1a9b0

Browse files
committed
fix tests
1 parent 51d2a0d commit 8a1a9b0

File tree

3 files changed

+11
-26
lines changed

3 files changed

+11
-26
lines changed

flask_pymongo/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from functools import partial
3030
from mimetypes import guess_type
31+
import hashlib
3132

3233
from flask import abort, current_app, request
3334
from gridfs import GridFS, NoFile
@@ -163,14 +164,20 @@ def get_upload(filename):
163164
# mostly copied from flask/helpers.py, with
164165
# modifications for GridFS
165166
data = wrap_file(request.environ, fileobj, buffer_size=1024 * 255)
167+
content_type, _ = guess_type(filename)
166168
response = current_app.response_class(
167169
data,
168-
mimetype=fileobj.content_type,
170+
mimetype=content_type,
169171
direct_passthrough=True,
170172
)
171173
response.content_length = fileobj.length
172174
response.last_modified = fileobj.upload_date
173-
response.set_etag(fileobj.md5)
175+
# Compute the sha1 sum of the file for the etag.
176+
pos = fileobj.tell()
177+
raw_data = fileobj.read()
178+
fileobj.seek(pos)
179+
digest = hashlib.sha1(raw_data).hexdigest()
180+
response.set_etag(digest)
174181
response.cache_control.max_age = cache_for
175182
response.cache_control.public = True
176183
response.make_conditional(request)

flask_pymongo/tests/test_gridfs.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from hashlib import md5
1+
from hashlib import sha1
22
from io import BytesIO
33

44
from bson.objectid import ObjectId
@@ -30,15 +30,6 @@ def test_it_saves_files(self):
3030
gridfs = GridFS(self.mongo.db)
3131
assert gridfs.exists({"filename": "my-file"})
3232

33-
def test_it_guesses_type_from_filename(self):
34-
fileobj = BytesIO(b"these are the bytes")
35-
36-
self.mongo.save_file("my-file.txt", fileobj)
37-
38-
gridfs = GridFS(self.mongo.db)
39-
gridfile = gridfs.find_one({"filename": "my-file.txt"})
40-
assert gridfile.content_type == "text/plain"
41-
4233
def test_it_saves_files_with_props(self):
4334
fileobj = BytesIO(b"these are the bytes")
4435

@@ -82,7 +73,7 @@ def test_it_sets_supports_conditional_gets(self):
8273
environ_args = {
8374
"method": "GET",
8475
"headers": {
85-
"If-None-Match": md5(self.myfile.getvalue()).hexdigest(),
76+
"If-None-Match": sha1(self.myfile.getvalue()).hexdigest(),
8677
},
8778
}
8879

flask_pymongo/tests/test_wrappers.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,3 @@ def test_find_one_or_404(self):
1818
# now it should not raise
1919
thing = self.mongo.db.things.find_one_or_404({"_id": "thing"})
2020
assert thing["val"] == "foo", "got wrong thing"
21-
22-
# also test with dotted-named collections
23-
self.mongo.db.things.morethings.delete_many({})
24-
try:
25-
self.mongo.db.things.morethings.find_one_or_404({"_id": "thing"})
26-
except HTTPException as notfound:
27-
assert notfound.code == 404, "raised wrong exception"
28-
29-
self.mongo.db.things.morethings.insert_one({"_id": "thing", "val": "foo"})
30-
31-
# now it should not raise
32-
thing = self.mongo.db.things.morethings.find_one_or_404({"_id": "thing"})
33-
assert thing["val"] == "foo", "got wrong thing"

0 commit comments

Comments
 (0)