diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index e65bee81..822adbf6 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -23,6 +23,7 @@ from ereuse_devicehub.resources.documents.device_row import DeviceRow, StockRow from ereuse_devicehub.resources.documents.device_row import DeviceRow from ereuse_devicehub.resources.lot import LotView from ereuse_devicehub.resources.lot.models import Lot +from ereuse_devicehub.resources.hash_reports import insert_hash @@ -125,7 +126,9 @@ class DevicesDocumentView(DeviceView): cw.writerow(d.keys()) first = False cw.writerow(d.values()) - output = make_response(data.getvalue().encode('utf-8')) + bfile = data.getvalue().encode('utf-8') + insert_hash(bfile) + output = make_response(bfile) output.headers['Content-Disposition'] = 'attachment; filename=export.csv' output.headers['Content-type'] = 'text/csv' return output diff --git a/tests/test_documents.py b/tests/test_documents.py index 281ca65a..162ed5a4 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -1,4 +1,5 @@ import csv +import hashlib from datetime import datetime from io import StringIO from pathlib import Path @@ -15,7 +16,9 @@ from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.device import models as d from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.tag.model import Tag +from ereuse_devicehub.resources.hash_reports import ReportHash from ereuse_devicehub.db import db +from tests import conftest from tests.conftest import file @@ -103,6 +106,7 @@ def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Clie assert len(csv_user) > 0 assert len(csv_user2) == 0 + @pytest.mark.mvp def test_export_basic_snapshot(user: UserClient): """Test export device information in a csv file.""" @@ -128,6 +132,20 @@ def test_export_basic_snapshot(user: UserClient): assert fixture_csv[1][18] == export_csv[1][18], 'Computer information are not equal' assert fixture_csv[1][20:] == export_csv[1][20:], 'Computer information are not equal' + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_check_insert_hash(app: Devicehub, user: UserClient): + """Test export device information in a csv file.""" + snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot) + csv_str, _ = user.get(res=documents.DocumentDef.t, + item='devices/', + accept='text/csv', + query=[('filter', {'type': ['Computer']})]) + hash3 = hashlib.sha3_256(csv_str.encode('utf-8')).hexdigest() + assert ReportHash.query.filter_by(hash3=hash3).count() == 1 + + @pytest.mark.mvp def test_export_extended(app: Devicehub, user: UserClient): """Test a export device with all information and a lot of components."""