[web/test] return the hash of uploaded files in FileUploadTC instead of their contents
json must be unicode, which doesn't go well with arbitrary file
contents. Compute the file hash instead, as we know the hexdigest is
(ascii) text.
--- a/cubicweb/web/test/data/views.py Mon Feb 08 15:29:06 2016 +0100
+++ b/cubicweb/web/test/data/views.py Mon Feb 08 15:37:52 2016 +0100
@@ -16,34 +16,36 @@
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
+import hashlib
+
from cubicweb.predicates import has_related_entities
from cubicweb.web.views.ajaxcontroller import ajaxfunc
from cubicweb.web.views.ibreadcrumbs import IBreadCrumbsAdapter
-def _recursive_replace_stream_by_content(tree):
+def _recursive_replace_stream_by_md5(tree):
""" Search for streams (i.e. object that have a 'read' method) in a tree
- (which branches are lists or tuples), and substitute them by their content,
+ (whose branches are lists or tuples), and substitute them by their md5 hash,
leaving other leafs identical. A copy of the tree with only lists as
branches is returned.
"""
if not isinstance(tree, (list, tuple)):
if hasattr(tree, 'read'):
- return tree.read()
+ return hashlib.md5(tree.read()).hexdigest()
return tree
else:
- return [_recursive_replace_stream_by_content(value)
+ return [_recursive_replace_stream_by_md5(value)
for value in tree]
@ajaxfunc(output_type='json')
def fileupload(self):
""" Return a json copy of the web request form in which uploaded files
- are read and the received streams are replaced by their content.
+ are read and the received streams are replaced by their md5 hash.
"""
try:
result_dict = {}
for key, value in self._cw.form.items():
- result_dict[key] = _recursive_replace_stream_by_content(value)
+ result_dict[key] = _recursive_replace_stream_by_md5(value)
return result_dict
except Exception as ex:
import traceback as tb
--- a/cubicweb/web/test/unittest_web.py Mon Feb 08 15:29:06 2016 +0100
+++ b/cubicweb/web/test/unittest_web.py Mon Feb 08 15:37:52 2016 +0100
@@ -19,6 +19,7 @@
from json import loads
from os.path import join
import tempfile
+import hashlib
try:
import requests
@@ -75,13 +76,17 @@
with self._fobject(fname) as f:
return f.read()
+ def _fhash(self, fname):
+ content = self._fcontent(fname)
+ return hashlib.md5(content).hexdigest()
+
def test_single_file_upload(self):
files = {'file': ('schema.py', self._fobject('schema.py'))}
webreq = requests.post(self._post_url, files=files)
# check backward compat : a single uploaded file leads to a single
# 2-uple in the request form
expect = {'fname': u'fileupload',
- 'file': ['schema.py', self._fcontent('schema.py')]}
+ 'file': ['schema.py', self._fhash('schema.py')]}
self.assertEqual(webreq.status_code, 200)
self.assertDictEqual(expect, loads(webreq.text))
@@ -90,8 +95,8 @@
('files', ('views.py', self._fobject('views.py')))]
webreq = requests.post(self._post_url, files=files,)
expect = {'fname': u'fileupload',
- 'files': [['schema.py', self._fcontent('schema.py')],
- ['views.py', self._fcontent('views.py')]],}
+ 'files': [['schema.py', self._fhash('schema.py')],
+ ['views.py', self._fhash('views.py')]],}
self.assertEqual(webreq.status_code, 200)
self.assertDictEqual(expect, loads(webreq.text))