insert hash3 when document is download

This commit is contained in:
Cayo Puigdefabregas 2020-12-21 11:34:03 +01:00
parent db19f90009
commit db610d2853
2 changed files with 22 additions and 1 deletions

View file

@ -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.documents.device_row import DeviceRow
from ereuse_devicehub.resources.lot import LotView from ereuse_devicehub.resources.lot import LotView
from ereuse_devicehub.resources.lot.models import Lot 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()) cw.writerow(d.keys())
first = False first = False
cw.writerow(d.values()) 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-Disposition'] = 'attachment; filename=export.csv'
output.headers['Content-type'] = 'text/csv' output.headers['Content-type'] = 'text/csv'
return output return output

View file

@ -1,4 +1,5 @@
import csv import csv
import hashlib
from datetime import datetime from datetime import datetime
from io import StringIO from io import StringIO
from pathlib import Path 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.device import models as d
from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.tag.model import Tag from ereuse_devicehub.resources.tag.model import Tag
from ereuse_devicehub.resources.hash_reports import ReportHash
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from tests import conftest
from tests.conftest import file 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_user) > 0
assert len(csv_user2) == 0 assert len(csv_user2) == 0
@pytest.mark.mvp @pytest.mark.mvp
def test_export_basic_snapshot(user: UserClient): def test_export_basic_snapshot(user: UserClient):
"""Test export device information in a csv file.""" """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][18] == export_csv[1][18], 'Computer information are not equal'
assert fixture_csv[1][20:] == export_csv[1][20:], '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 @pytest.mark.mvp
def test_export_extended(app: Devicehub, user: UserClient): def test_export_extended(app: Devicehub, user: UserClient):
"""Test a export device with all information and a lot of components.""" """Test a export device with all information and a lot of components."""