2020-07-07 15:17:41 +00:00
|
|
|
import csv
|
|
|
|
from datetime import datetime
|
|
|
|
from io import StringIO
|
|
|
|
from pathlib import Path
|
2018-11-21 13:26:56 +00:00
|
|
|
|
2020-08-17 09:18:52 +00:00
|
|
|
import pytest
|
|
|
|
import teal.marshmallow
|
|
|
|
from ereuse_utils.test import ANY
|
|
|
|
|
2018-11-21 13:26:56 +00:00
|
|
|
from ereuse_devicehub.client import Client, UserClient
|
2020-10-21 11:05:28 +00:00
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
2020-07-07 15:17:41 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Snapshot
|
|
|
|
from ereuse_devicehub.resources.documents import documents
|
2020-10-21 11:05:28 +00:00
|
|
|
from ereuse_devicehub.resources.device import models as d
|
2020-07-28 14:14:56 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2020-10-21 11:05:28 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
|
|
|
from ereuse_devicehub.db import db
|
2018-11-21 13:26:56 +00:00
|
|
|
from tests.conftest import file
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-11-21 13:26:56 +00:00
|
|
|
def test_erasure_certificate_public_one(user: UserClient, client: Client):
|
|
|
|
"""Public user can get certificate from one device as HTML or PDF."""
|
|
|
|
s = file('erase-sectors.snapshot')
|
2020-07-07 15:17:41 +00:00
|
|
|
snapshot, _ = user.post(s, res=Snapshot)
|
2018-11-21 13:26:56 +00:00
|
|
|
|
2020-08-05 16:19:27 +00:00
|
|
|
doc, response = user.get(res=documents.DocumentDef.t,
|
2018-11-21 13:26:56 +00:00
|
|
|
item='erasures/{}'.format(snapshot['device']['id']),
|
|
|
|
accept=ANY)
|
|
|
|
assert 'html' in response.content_type
|
|
|
|
assert '<html' in doc
|
|
|
|
assert '2018' in doc
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
doc, response = client.get(res=documents.DocumentDef.t,
|
2018-11-21 13:26:56 +00:00
|
|
|
item='erasures/{}'.format(snapshot['device']['id']),
|
|
|
|
query=[('format', 'PDF')],
|
|
|
|
accept='application/pdf')
|
|
|
|
assert 'application/pdf' == response.content_type
|
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
erasure = next(e for e in snapshot['actions'] if e['type'] == 'EraseSectors')
|
2018-11-21 13:26:56 +00:00
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
doc, response = client.get(res=documents.DocumentDef.t,
|
2018-11-21 13:26:56 +00:00
|
|
|
item='erasures/{}'.format(erasure['id']),
|
|
|
|
accept=ANY)
|
|
|
|
assert 'html' in response.content_type
|
|
|
|
assert '<html' in doc
|
|
|
|
assert '2018' in doc
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-11-21 13:26:56 +00:00
|
|
|
def test_erasure_certificate_private_query(user: UserClient):
|
|
|
|
"""Logged-in user can get certificates using queries as HTML and
|
|
|
|
PDF.
|
|
|
|
"""
|
|
|
|
s = file('erase-sectors.snapshot')
|
2020-07-07 15:17:41 +00:00
|
|
|
snapshot, response = user.post(s, res=Snapshot)
|
2018-11-21 13:26:56 +00:00
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
doc, response = user.get(res=documents.DocumentDef.t,
|
2018-11-21 13:26:56 +00:00
|
|
|
item='erasures/',
|
|
|
|
query=[('filter', {'id': [snapshot['device']['id']]})],
|
|
|
|
accept=ANY)
|
|
|
|
assert 'html' in response.content_type
|
|
|
|
assert '<html' in doc
|
|
|
|
assert '2018' in doc
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
doc, response = user.get(res=documents.DocumentDef.t,
|
2018-11-21 13:26:56 +00:00
|
|
|
item='erasures/',
|
|
|
|
query=[
|
|
|
|
('filter', {'id': [snapshot['device']['id']]}),
|
|
|
|
('format', 'PDF')
|
|
|
|
],
|
|
|
|
accept='application/pdf')
|
|
|
|
assert 'application/pdf' == response.content_type
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-11-21 13:26:56 +00:00
|
|
|
def test_erasure_certificate_wrong_id(client: Client):
|
2020-07-07 15:17:41 +00:00
|
|
|
client.get(res=documents.DocumentDef.t, item='erasures/this-is-not-an-id',
|
2018-11-21 13:26:56 +00:00
|
|
|
status=teal.marshmallow.ValidationError)
|
2020-07-07 15:17:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
def test_export_basic_snapshot(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']})])
|
|
|
|
f = StringIO(csv_str)
|
2020-10-21 20:33:58 +00:00
|
|
|
obj_csv = csv.reader(f, f, delimiter=';', quotechar='"')
|
2020-07-07 15:17:41 +00:00
|
|
|
export_csv = list(obj_csv)
|
|
|
|
|
|
|
|
# Open fixture csv and transform to list
|
|
|
|
with Path(__file__).parent.joinpath('files').joinpath('basic.csv').open() as csv_file:
|
2020-10-21 20:33:58 +00:00
|
|
|
obj_csv = csv.reader(csv_file, delimiter=';', quotechar='"')
|
2020-07-07 15:17:41 +00:00
|
|
|
fixture_csv = list(obj_csv)
|
|
|
|
|
2020-10-21 20:33:58 +00:00
|
|
|
assert isinstance(datetime.strptime(export_csv[1][17], '%c'), datetime), \
|
2020-07-07 15:17:41 +00:00
|
|
|
'Register in field is not a datetime'
|
|
|
|
|
|
|
|
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
|
2020-10-21 20:33:58 +00:00
|
|
|
assert fixture_csv[1][:17] == export_csv[1][:17], 'Computer information are not equal'
|
|
|
|
assert fixture_csv[1][20:] == export_csv[1][20:], 'Computer information are not equal'
|
2020-07-07 15:17:41 +00:00
|
|
|
|
2020-10-19 11:06:54 +00:00
|
|
|
@pytest.mark.mvp
|
2020-10-21 11:05:28 +00:00
|
|
|
def test_export_extended(app: Devicehub, user: UserClient):
|
2020-10-19 11:06:54 +00:00
|
|
|
"""Test a export device with all information and a lot of components."""
|
2020-10-21 20:07:56 +00:00
|
|
|
snapshot1, _ = user.post(file('real-eee-1001pxd.snapshot.12'), res=Snapshot, status=201)
|
|
|
|
snapshot2, _ = user.post(file('complete.export.snapshot'), res=Snapshot, status=201)
|
2020-10-21 11:05:28 +00:00
|
|
|
with app.app_context():
|
|
|
|
# Create a pc with a tag
|
|
|
|
tag = Tag(id='foo', owner_id=user.user['id'])
|
|
|
|
# pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id'])
|
2020-10-21 20:07:56 +00:00
|
|
|
pc = d.Device.query.filter_by(id=snapshot1['device']['id']).first()
|
2020-10-21 11:05:28 +00:00
|
|
|
pc.tags.add(tag)
|
|
|
|
db.session.add(pc)
|
|
|
|
db.session.commit()
|
2020-10-19 11:06:54 +00:00
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
item='devices/',
|
|
|
|
accept='text/csv',
|
|
|
|
query=[('filter', {'type': ['Computer']})])
|
2020-10-21 11:05:28 +00:00
|
|
|
|
2020-10-19 11:06:54 +00:00
|
|
|
f = StringIO(csv_str)
|
2020-10-21 20:07:56 +00:00
|
|
|
obj_csv = csv.reader(f, f, delimiter=';', quotechar='"')
|
2020-10-19 11:06:54 +00:00
|
|
|
export_csv = list(obj_csv)
|
|
|
|
|
|
|
|
# Open fixture csv and transform to list
|
|
|
|
with Path(__file__).parent.joinpath('files').joinpath(
|
|
|
|
'proposal_extended_csv_report.csv').open() as csv_file:
|
2020-10-21 20:07:56 +00:00
|
|
|
obj_csv = csv.reader(csv_file, delimiter=';', quotechar='"')
|
2020-10-19 11:06:54 +00:00
|
|
|
fixture_csv = list(obj_csv)
|
|
|
|
|
2020-10-21 20:33:58 +00:00
|
|
|
assert isinstance(datetime.strptime(export_csv[1][17], '%c'), datetime), \
|
|
|
|
'Register in field is not a datetime'
|
|
|
|
|
2020-10-21 20:07:56 +00:00
|
|
|
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
|
|
|
|
assert fixture_csv[1][:17] == export_csv[1][:17], 'Computer information are not equal'
|
|
|
|
assert fixture_csv[1][20:80] == export_csv[1][20:80], 'Computer information are not equal'
|
2020-10-21 20:33:58 +00:00
|
|
|
assert fixture_csv[1][81:] == export_csv[1][81:], 'Computer information are not equal'
|
2020-10-21 20:07:56 +00:00
|
|
|
assert fixture_csv[2][:17] == export_csv[2][:17], 'Computer information are not equal'
|
|
|
|
assert fixture_csv[2][20:80] == export_csv[2][20:80], 'Computer information are not equal'
|
|
|
|
assert fixture_csv[2][81:104] == export_csv[2][81:104], 'Computer information are not equal'
|
|
|
|
assert fixture_csv[2][105:128] == export_csv[2][105:128], 'Computer information are not equal'
|
|
|
|
assert fixture_csv[2][129:] == export_csv[2][129:], 'Computer information are not equal'
|
2020-10-19 11:06:54 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
def test_export_empty(user: UserClient):
|
|
|
|
"""Test to check works correctly exporting csv without any information,
|
|
|
|
export a placeholder device.
|
|
|
|
"""
|
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
accept='text/csv',
|
|
|
|
item='devices/')
|
|
|
|
f = StringIO(csv_str)
|
|
|
|
obj_csv = csv.reader(f, f)
|
|
|
|
export_csv = list(obj_csv)
|
|
|
|
|
|
|
|
assert len(export_csv) == 0, 'Csv is not empty'
|
|
|
|
|
|
|
|
|
2020-08-12 09:59:12 +00:00
|
|
|
@pytest.mark.xfail(reason='Feature not developed (Beta)')
|
2020-07-07 15:17:41 +00:00
|
|
|
def test_export_computer_monitor(user: UserClient):
|
|
|
|
"""Test a export device type computer monitor."""
|
|
|
|
snapshot, _ = user.post(file('computer-monitor.snapshot'), res=Snapshot)
|
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
item='devices/',
|
|
|
|
accept='text/csv',
|
|
|
|
query=[('filter', {'type': ['ComputerMonitor']})])
|
|
|
|
f = StringIO(csv_str)
|
|
|
|
obj_csv = csv.reader(f, f)
|
|
|
|
export_csv = list(obj_csv)
|
|
|
|
# Open fixture csv and transform to list
|
|
|
|
with Path(__file__).parent.joinpath('files').joinpath('computer-monitor.csv').open() \
|
|
|
|
as csv_file:
|
|
|
|
obj_csv = csv.reader(csv_file)
|
|
|
|
fixture_csv = list(obj_csv)
|
|
|
|
|
|
|
|
# Pop dates fields from csv lists to compare them
|
|
|
|
fixture_csv[1] = fixture_csv[1][:8]
|
|
|
|
export_csv[1] = export_csv[1][:8]
|
|
|
|
|
|
|
|
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
|
|
|
|
assert fixture_csv[1] == export_csv[1], 'Component information are not equal'
|
|
|
|
|
|
|
|
|
2020-08-12 09:59:12 +00:00
|
|
|
@pytest.mark.xfail(reason='Feature not developed (Beta)')
|
2020-07-07 15:17:41 +00:00
|
|
|
def test_export_keyboard(user: UserClient):
|
|
|
|
"""Test a export device type keyboard."""
|
|
|
|
snapshot, _ = user.post(file('keyboard.snapshot'), res=Snapshot)
|
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
item='devices/',
|
|
|
|
accept='text/csv',
|
|
|
|
query=[('filter', {'type': ['Keyboard']})])
|
|
|
|
f = StringIO(csv_str)
|
|
|
|
obj_csv = csv.reader(f, f)
|
|
|
|
export_csv = list(obj_csv)
|
|
|
|
# Open fixture csv and transform to list
|
|
|
|
with Path(__file__).parent.joinpath('files').joinpath('keyboard.csv').open() as csv_file:
|
|
|
|
obj_csv = csv.reader(csv_file)
|
|
|
|
fixture_csv = list(obj_csv)
|
|
|
|
|
|
|
|
# Pop dates fields from csv lists to compare them
|
|
|
|
fixture_csv[1] = fixture_csv[1][:8]
|
|
|
|
export_csv[1] = export_csv[1][:8]
|
|
|
|
|
|
|
|
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
|
|
|
|
assert fixture_csv[1] == export_csv[1], 'Component information are not equal'
|
|
|
|
|
|
|
|
|
2020-08-12 09:59:12 +00:00
|
|
|
@pytest.mark.xfail(reason='Feature not developed (Beta)')
|
2020-07-07 15:17:41 +00:00
|
|
|
def test_export_multiple_different_devices(user: UserClient):
|
|
|
|
"""Test function 'Export' of multiple different device types (like
|
|
|
|
computers, keyboards, monitors, etc..)
|
|
|
|
"""
|
|
|
|
# Open fixture csv and transform to list
|
|
|
|
with Path(__file__).parent.joinpath('files').joinpath('multiples_devices.csv').open() \
|
|
|
|
as csv_file:
|
|
|
|
fixture_csv = list(csv.reader(csv_file))
|
|
|
|
for row in fixture_csv:
|
|
|
|
del row[8] # We remove the 'Registered in' column
|
|
|
|
|
|
|
|
# Post all devices snapshots
|
|
|
|
snapshot_pc, _ = user.post(file('real-eee-1001pxd.snapshot.11'), res=Snapshot)
|
|
|
|
snapshot_empty, _ = user.post(file('basic.snapshot'), res=Snapshot)
|
|
|
|
snapshot_keyboard, _ = user.post(file('keyboard.snapshot'), res=Snapshot)
|
|
|
|
snapshot_monitor, _ = user.post(file('computer-monitor.snapshot'), res=Snapshot)
|
|
|
|
|
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
item='devices/',
|
|
|
|
query=[('filter', {'type': ['Computer', 'Keyboard', 'Monitor']})],
|
|
|
|
accept='text/csv')
|
|
|
|
f = StringIO(csv_str)
|
|
|
|
obj_csv = csv.reader(f, f)
|
|
|
|
export_csv = list(obj_csv)
|
|
|
|
|
|
|
|
for row in export_csv:
|
|
|
|
del row[8]
|
|
|
|
|
|
|
|
assert fixture_csv == export_csv
|
2020-07-28 14:14:56 +00:00
|
|
|
|
|
|
|
|
2020-07-27 11:57:31 +00:00
|
|
|
@pytest.mark.mvp
|
2020-08-05 10:24:36 +00:00
|
|
|
def test_report_devices_stock_control(user: UserClient, user2: UserClient):
|
2020-07-27 11:57:31 +00:00
|
|
|
"""Test export device information in a csv file."""
|
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)
|
2020-08-05 10:24:36 +00:00
|
|
|
snapshot2, _ = user2.post(file('basic.snapshot2'), res=Snapshot)
|
|
|
|
|
2020-07-27 11:57:31 +00:00
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
item='stock/',
|
|
|
|
accept='text/csv',
|
|
|
|
query=[('filter', {'type': ['Computer']})])
|
|
|
|
f = StringIO(csv_str)
|
|
|
|
obj_csv = csv.reader(f, f)
|
|
|
|
export_csv = list(obj_csv)
|
|
|
|
|
|
|
|
# Open fixture csv and transform to list
|
|
|
|
with Path(__file__).parent.joinpath('files').joinpath('basic-stock.csv').open() as csv_file:
|
|
|
|
obj_csv = csv.reader(csv_file)
|
|
|
|
fixture_csv = list(obj_csv)
|
|
|
|
|
2020-08-05 10:24:36 +00:00
|
|
|
assert user.user['id'] != user2.user['id']
|
|
|
|
assert len(export_csv) == 2
|
|
|
|
|
2020-07-27 11:57:31 +00:00
|
|
|
assert isinstance(datetime.strptime(export_csv[1][5], '%c'), datetime), \
|
|
|
|
'Register in field is not a datetime'
|
|
|
|
|
|
|
|
# Pop dates fields from csv lists to compare them
|
|
|
|
fixture_csv[1] = fixture_csv[1][:5] + fixture_csv[1][6:]
|
|
|
|
export_csv[1] = export_csv[1][:5] + export_csv[1][6:]
|
|
|
|
|
|
|
|
assert fixture_csv[0] == export_csv[0], 'Headers are not equal'
|
|
|
|
assert fixture_csv[1] == export_csv[1], 'Computer information are not equal'
|
2020-08-18 17:31:43 +00:00
|
|
|
assert fixture_csv == export_csv
|
|
|
|
|
|
|
|
|
2020-07-28 14:14:56 +00:00
|
|
|
@pytest.mark.mvp
|
2020-08-18 19:02:47 +00:00
|
|
|
def test_get_document_lots(user: UserClient, user2: UserClient):
|
2020-07-28 14:14:56 +00:00
|
|
|
"""Tests submitting and retreiving all lots."""
|
|
|
|
|
2020-08-18 19:02:47 +00:00
|
|
|
l, _ = user.post({'name': 'Lot1', 'description': 'comments,lot1,testcomment-lot1,'}, res=Lot)
|
|
|
|
l, _ = user.post({'name': 'Lot2', 'description': 'comments,lot2,testcomment-lot2,'}, res=Lot)
|
|
|
|
l, _ = user2.post({'name': 'Lot3-User2', 'description': 'comments,lot3,testcomment-lot3,'}, res=Lot)
|
2020-07-28 14:14:56 +00:00
|
|
|
|
|
|
|
csv_str, _ = user.get(res=documents.DocumentDef.t,
|
|
|
|
item='lots/',
|
|
|
|
accept='text/csv')
|
2020-08-18 19:02:47 +00:00
|
|
|
|
|
|
|
csv2_str, _ = user2.get(res=documents.DocumentDef.t,
|
|
|
|
item='lots/',
|
|
|
|
accept='text/csv')
|
|
|
|
|
2020-07-28 14:14:56 +00:00
|
|
|
f = StringIO(csv_str)
|
|
|
|
obj_csv = csv.reader(f, f)
|
|
|
|
export_csv = list(obj_csv)
|
2020-08-18 19:02:47 +00:00
|
|
|
|
|
|
|
f = StringIO(csv2_str)
|
|
|
|
obj2_csv = csv.reader(f, f)
|
|
|
|
export2_csv = list(obj2_csv)
|
|
|
|
|
|
|
|
assert len(export_csv) == 3
|
|
|
|
assert len(export2_csv) == 2
|
|
|
|
|
|
|
|
assert export_csv[0] == export2_csv[0] == ['Id', 'Name', 'Registered in', 'Description']
|
|
|
|
|
|
|
|
assert export_csv[1][1] == 'Lot1' or 'Lot2'
|
|
|
|
assert export_csv[1][3] == 'comments,lot1,testcomment-lot1,' or 'comments,lot2,testcomment-lot2,'
|
|
|
|
assert export2_csv[1][1] == 'Lot3-User2'
|
|
|
|
assert export2_csv[1][3] == 'comments,lot3,testcomment-lot3,'
|