fix test reliable

This commit is contained in:
Cayo Puigdefabregas 2023-01-16 15:26:58 +01:00
parent 520f1726be
commit 6fc802e159
6 changed files with 76 additions and 39 deletions

View file

@ -2,7 +2,6 @@ import copy
import csv import csv
import datetime import datetime
import json import json
import uuid
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
import pandas as pd import pandas as pd
@ -204,7 +203,7 @@ class FilterForm(FlaskForm):
if filter_type: if filter_type:
self.devices = self.devices.filter(Device.type.in_(filter_type)) self.devices = self.devices.filter(Device.type.in_(filter_type))
return self.devices.filter(Device.active == True).order_by( return self.devices.filter(Device.active.is_(False)).order_by(
Device.updated.desc() Device.updated.desc()
) )
@ -1735,22 +1734,21 @@ class UserTrustsForm(FlaskForm):
try: try:
return self._unic return self._unic
except Exception: except Exception:
self._unic = ( self._devices = (
Device.query.filter_by( Device.query.filter_by(
hid=self.device.hid, owner=g.user, placeholder=None, active=True hid=self.device.hid, owner=g.user, placeholder=None, active=True
).count() )
< 2 .order_by(Device.updated.asc())
.all()
) )
self._unic = len(self._devices) < 2
return self._unic return self._unic
def show(self): def show(self):
if not self.snapshot or not self.device: if not self.snapshot or not self.device:
return False return False
if not self.snapshot.active:
return False
if not hasattr(self.device, 'system_uuid'): if not hasattr(self.device, 'system_uuid'):
return False return False
@ -1762,6 +1760,8 @@ class UserTrustsForm(FlaskForm):
return True return True
if not self.unic(): if not self.unic():
if self.device == self._devices[0]:
return False
# To do merge # To do merge
return True return True

View file

@ -39,7 +39,6 @@ def upgrade_data():
sql = f"update {get_inv()}.device set chid='{chid}' where id={dev_id};" sql = f"update {get_inv()}.device set chid='{chid}' where id={dev_id};"
con.execute(sql) con.execute(sql)
sql = f"update {get_inv()}.snapshot set active=true;"
con.execute(sql) con.execute(sql)
@ -56,19 +55,11 @@ def upgrade():
schema=f'{get_inv()}', schema=f'{get_inv()}',
) )
op.add_column(
'snapshot',
sa.Column('active', sa.Boolean(), default=True, nullable=True),
schema=f'{get_inv()}',
)
upgrade_data() upgrade_data()
op.alter_column('computer', 'user_trusts', nullable=False, schema=f'{get_inv()}') op.alter_column('computer', 'user_trusts', nullable=False, schema=f'{get_inv()}')
op.alter_column('snapshot', 'active', nullable=False, schema=f'{get_inv()}')
def downgrade(): def downgrade():
op.drop_column('computer', 'user_trusts', schema=f'{get_inv()}') op.drop_column('computer', 'user_trusts', schema=f'{get_inv()}')
op.drop_column('device', 'chid', schema=f'{get_inv()}') op.drop_column('device', 'chid', schema=f'{get_inv()}')
op.drop_column('snapshot', 'active', schema=f'{get_inv()}')

View file

@ -39,8 +39,6 @@ class SnapshotsLog(Thing):
def get_status(self): def get_status(self):
if self.snapshot: if self.snapshot:
if not self.snapshot.active:
return Severity(2)
return Severity(self.severity) return Severity(self.severity)
return '' return ''
@ -72,9 +70,6 @@ class SnapshotsLog(Thing):
if not self.snapshot: if not self.snapshot:
return '' return ''
if not self.snapshot.active:
return ''
if not self.snapshot.device: if not self.snapshot.device:
return '' return ''

View file

@ -48,17 +48,14 @@ from sqlalchemy.util import OrderedSet
from teal.db import ( from teal.db import (
CASCADE_OWN, CASCADE_OWN,
INHERIT_COND, INHERIT_COND,
IP,
POLYMORPHIC_ID, POLYMORPHIC_ID,
POLYMORPHIC_ON, POLYMORPHIC_ON,
URL, URL,
ResourceNotFound,
StrictVersionType, StrictVersionType,
check_lower, check_lower,
check_range, check_range,
) )
from teal.enums import Country, Currency, Subdivision from teal.enums import Currency
from teal.marshmallow import ValidationError
from teal.resource import url_for_resource from teal.resource import url_for_resource
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
@ -678,7 +675,6 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice):
sid = Column(CIText(), nullable=True) sid = Column(CIText(), nullable=True)
settings_version = Column(CIText(), nullable=True) settings_version = Column(CIText(), nullable=True)
is_server_erase = Column(Boolean(), nullable=True) is_server_erase = Column(Boolean(), nullable=True)
active = Column(Boolean(), default=True, nullable=False)
def get_last_lifetimes(self): def get_last_lifetimes(self):
"""We get the lifetime and serial_number of the first disk""" """We get the lifetime and serial_number of the first disk"""

View file

@ -275,8 +275,7 @@ class Device(Thing):
for ac in actions_one: for ac in actions_one:
ac.real_created = ac.created ac.real_created = ac.created
if ac.type != 'Snapshot' or ac.active: actions.append(ac)
actions.append(ac)
return sorted(actions, key=lambda x: x.real_created) return sorted(actions, key=lambda x: x.real_created)
@ -996,15 +995,8 @@ class Device(Thing):
if not snapshot1: if not snapshot1:
return return
self.reset_components(snapshot1)
return return
def reset_components(self, snapshot):
for c in snapshot.components:
if c.parent is None:
c.parent = snapshot.device
def __lt__(self, other): def __lt__(self, other):
return self.id < other.id return self.id < other.id

View file

@ -2677,8 +2677,10 @@ def test_system_uuid_motherboard(user3: UserClientFlask):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_unreliable_device(user3: UserClientFlask): def test_unreliable_device(user3: UserClientFlask):
# Create device
snapshot = create_device(user3, 'real-eee-1001pxd.snapshot.12.json') snapshot = create_device(user3, 'real-eee-1001pxd.snapshot.12.json')
# Update device
uri = '/inventory/upload-snapshot/' uri = '/inventory/upload-snapshot/'
file_name = 'real-eee-1001pxd.snapshot.12' file_name = 'real-eee-1001pxd.snapshot.12'
snapshot_json = conftest.yaml2json(file_name) snapshot_json = conftest.yaml2json(file_name)
@ -2694,7 +2696,13 @@ def test_unreliable_device(user3: UserClientFlask):
user3.post(uri, data=data, content_type="multipart/form-data") user3.post(uri, data=data, content_type="multipart/form-data")
snapshot2 = Snapshot.query.filter_by(uuid=snapshot_json['uuid']).first() snapshot2 = Snapshot.query.filter_by(uuid=snapshot_json['uuid']).first()
assert snapshot2.device == snapshot.device assert snapshot2.device == snapshot.device
assert Snapshot.query.count() == 2
snapshots = Snapshot.query.all()
assert snapshots[0].device == snapshots[1].device
assert len(snapshots[0].device.components)
assert snapshot2 in snapshots
# Change update for new device
uuid2 = snapshot2.uuid uuid2 = snapshot2.uuid
uri = f"/inventory/snapshots/{uuid2}/" uri = f"/inventory/snapshots/{uuid2}/"
user3.get(uri) user3.get(uri)
@ -2706,8 +2714,63 @@ def test_unreliable_device(user3: UserClientFlask):
assert Device.query.filter_by(hid=snapshot.device.hid).count() == 2 assert Device.query.filter_by(hid=snapshot.device.hid).count() == 2
user3.post(uri, data=data) user3.post(uri, data=data)
assert Device.query.filter_by(hid=snapshot.device.hid).count() == 4 assert Device.query.filter_by(hid=snapshot.device.hid).count() == 4
assert Snapshot.query.count() == 3 assert Snapshot.query.count() == 2
snapshots = Snapshot.query.all()
assert snapshot2 not in snapshots
assert snapshots[0].device != snapshots[1].device
assert len(snapshots[0].device.components) == 4
assert len(snapshots[1].device.components) == 9
assert len(snapshots[0].device.actions) == 11
assert len(snapshots[1].device.actions) == 10
import pdb
pdb.set_trace() @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_reliable_device(user3: UserClientFlask):
# Create device
snapshot = create_device(user3, 'real-eee-1001pxd.snapshot.12.json')
# Update device
uri = '/inventory/upload-snapshot/'
file_name = 'real-eee-1001pxd.snapshot.12'
snapshot_json = conftest.yaml2json(file_name)
snapshot_json['uuid'] = 'c058e8d2-fb92-47cb-a4b7-522b75561136'
b_snapshot = bytes(json.dumps(snapshot_json), 'utf-8')
file_snap = (BytesIO(b_snapshot), file_name)
user3.get(uri)
data = {
'snapshot': file_snap,
'csrf_token': generate_csrf(),
}
user3.post(uri, data=data, content_type="multipart/form-data")
snapshot2 = Snapshot.query.filter_by(uuid=snapshot_json['uuid']).first()
# Change update for new device
uuid2 = snapshot2.uuid
uri = f"/inventory/snapshots/{uuid2}/"
user3.get(uri)
data = {
'snapshot_type': "new_device",
'csrf_token': generate_csrf(),
}
user3.post(uri, data=data)
# Change update for update
snapshot3 = Snapshot.query.all()[-1]
uuid3 = snapshot3.uuid
uri = f"/inventory/snapshots/{uuid3}/"
user3.get(uri)
data = {
'snapshot_type': "update",
'csrf_token': generate_csrf(),
}
assert Device.query.filter_by(hid=snapshot.device.hid).count() == 4
user3.post(uri, data=data)
assert Device.query.filter_by(hid=snapshot.device.hid).count() == 2
assert Snapshot.query.count() == 1
assert Snapshot.query.first() == snapshot
assert len(snapshot.device.components) == 4
assert len(snapshot.device.actions) == 4