From 96540d701c6cd27b2895787a50ce69931b8091be Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Dec 2020 16:09:30 +0100 Subject: [PATCH] fixing lineterminator in csv --- ereuse_devicehub/resources/documents/documents.py | 8 ++++---- tests/test_documents.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index fb5572bd..a7db64c6 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -117,7 +117,7 @@ class DevicesDocumentView(DeviceView): def generate_post_csv(self, query): """Get device query and put information in csv format.""" data = StringIO() - cw = csv.writer(data, delimiter=';', quotechar='"') + cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') first = True for device in query: d = DeviceRow(device) @@ -126,8 +126,8 @@ class DevicesDocumentView(DeviceView): first = False cw.writerow(d.values()) bfile = data.getvalue().encode('utf-8') - insert_hash(bfile) output = make_response(bfile) + insert_hash(bfile) output.headers['Content-Disposition'] = 'attachment; filename=export.csv' output.headers['Content-type'] = 'text/csv' return output @@ -197,10 +197,10 @@ class CheckView(View): def get(self): qry = dict(request.values) - hash3 = qry['hash'] + hash3 = qry.get('hash') result = False - if ReportHash.query.filter_by(hash3=hash3).count(): + if hash3 and ReportHash.query.filter_by(hash3=hash3).count(): result = True return jsonify(result) diff --git a/tests/test_documents.py b/tests/test_documents.py index 895aaf30..eecee5ad 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -135,7 +135,7 @@ def test_export_basic_snapshot(user: UserClient): @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) -def test_check_insert_hash(app: Devicehub, user: UserClient): +def test_check_insert_hash(app: Devicehub, user: UserClient, client: Client): """Test export device information in a csv file.""" snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot) csv_str, _ = user.get(res=documents.DocumentDef.t, @@ -144,10 +144,17 @@ def test_check_insert_hash(app: Devicehub, user: UserClient): query=[('filter', {'type': ['Computer']})]) hash3 = hashlib.sha3_256(csv_str.encode('utf-8')).hexdigest() assert ReportHash.query.filter_by(hash3=hash3).count() == 1 - result, status = user.get(res=documents.DocumentDef.t, item='check/', query=[('hash', hash3)]) + result, status = client.get(res=documents.DocumentDef.t, item='check/', query=[('hash', hash3)]) assert status.status_code == 200 assert result == True + ff = open('/tmp/test.csv', 'w') + ff.write(csv_str) + ff.close() + + a= open('/tmp/test.csv').read() + assert hash3 == hashlib.sha3_256(a.encode('utf-8')).hexdigest() + @pytest.mark.mvp def test_export_extended(app: Devicehub, user: UserClient):