Skip to content

Commit ed52375

Browse files
committed
add md5 test
1 parent 006e7bc commit ed52375

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

flask_pymongo/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
__all__ = ("PyMongo", "ASCENDING", "DESCENDING", "BSONObjectIdConverter", "BSONProvider")
2828

2929
import hashlib
30+
import warnings
3031
from mimetypes import guess_type
3132
from typing import Any
3233

@@ -191,7 +192,9 @@ def get_upload(filename):
191192
try:
192193
etag = fileobj.sha1
193194
except AttributeError:
194-
etag = fileobj.md5
195+
with warnings.catch_warnings():
196+
warnings.simplefilter("ignore")
197+
etag = fileobj.md5
195198
if etag is None:
196199
pos = fileobj.tell()
197200
raw_data = fileobj.read()

tests/test_gridfs.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

3-
from hashlib import sha1
3+
import warnings
4+
from hashlib import md5, sha1
45
from io import BytesIO
56

67
import pytest
@@ -95,6 +96,26 @@ def test_it_sets_supports_conditional_gets(self):
9596
resp = self.mongo.send_file("myfile.txt")
9697
assert resp.status_code == 304
9798

99+
def test_it_sets_supports_conditional_gets_md5(self):
100+
# a basic conditional GET
101+
md5_hash = md5(self.myfile.getvalue()).hexdigest()
102+
environ_args = {
103+
"method": "GET",
104+
"headers": {
105+
"If-None-Match": md5_hash,
106+
},
107+
}
108+
storage = storage = GridFS(self.mongo.db)
109+
with storage.new_file(filename="myfile.txt") as grid_file:
110+
grid_file.write(self.myfile.getvalue())
111+
with warnings.catch_warnings():
112+
warnings.simplefilter("ignore")
113+
grid_file.set("md5", md5_hash)
114+
115+
with self.app.test_request_context(**environ_args):
116+
resp = self.mongo.send_file("myfile.txt")
117+
assert resp.status_code == 304
118+
98119
def test_it_sets_cache_headers(self):
99120
resp = self.mongo.send_file("myfile.txt", cache_for=60)
100121
assert resp.cache_control.max_age == 60

0 commit comments

Comments
 (0)