diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 69f0b4ba..5cc43bfc 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -304,13 +304,13 @@ class UploadSnapshotForm(SnapshotMixin, FlaskForm): try: snapshot_json = schema.load(snapshot_json) + response = self.build(snapshot_json) except ValidationError as err: txt = "{}".format(err) self.errors(txt=txt) self.result[filename] = 'Error' continue - response = self.build(snapshot_json) db.session.add(response) devices.append(response.device.binding.device) diff --git a/ereuse_devicehub/resources/device/sync.py b/ereuse_devicehub/resources/device/sync.py index 926b3238..90403871 100644 --- a/ereuse_devicehub/resources/device/sync.py +++ b/ereuse_devicehub/resources/device/sync.py @@ -32,6 +32,13 @@ DEVICES_ALLOW_DUPLICITY = [ 'GraphicCard', ] +err_motherboard = "Error: We have detected that a there is a device" +err_motherboard += " in your inventory with this system UUID. " +err_motherboard += "We proceed to block this snapshot to prevent its" +err_motherboard += " information from being updated incorrectly." +err_motherboard += " The solution we offer you to inventory this device " +err_motherboard += "is to do it by creating a placeholder." + class Sync: """Synchronizes the device and components with the database.""" @@ -70,6 +77,17 @@ class Sync: 2. A list of Add / Remove (not yet added to session). """ db_device = self.execute_register(device) + motherboard = None + if components: + for c in components: + if c.type == "Motherboard": + motherboard = c + + if motherboard: + for c in db_device.components: + if c.type == "Motherboard" and motherboard.hid != c.hid: + raise ValidationError(err_motherboard) + db_components, actions = OrderedSet(), OrderedSet() if components is not None: # We have component info (see above) if not isinstance(db_device, Computer): diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 40c87687..3ac8d4e9 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -12,6 +12,7 @@ from flask_wtf.csrf import generate_csrf from ereuse_devicehub.client import UserClient, UserClientFlask from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub +from ereuse_devicehub.parser.models import SnapshotsLog from ereuse_devicehub.resources.action.models import Snapshot from ereuse_devicehub.resources.device.models import Device, Placeholder from ereuse_devicehub.resources.lot.models import Lot @@ -2578,3 +2579,37 @@ def test_snapshot_is_server_erase(user3: UserClientFlask): assert snapshot2.is_server_erase assert snapshot in snapshot.device.actions assert snapshot2 in snapshot.device.actions + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_system_uuid_motherboard(user3: UserClientFlask): + # we want to do an snapshot log when there are the same system-uuid for + # 2 computers with diferent motherboard + snapshot = create_device(user3, 'real-eee-1001pxd.snapshot.12.json') + device = snapshot.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' + for c in snapshot_json['components']: + if c['type'] == 'Motherboard': + c['serialNumber'] = 'ABee0123456720' + 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() + assert snapshot2 is None + for c in snapshot.device.components: + if c.type == 'Motherboard': + assert c.serial_number == 'eee0123456720' + + txt = "We have detected that a there is a device in your inventory" + assert txt in SnapshotsLog.query.all()[-1].description diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index f7a31cce..a4753943 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1365,3 +1365,18 @@ def test_placeholder_actions(user: UserClient): assert dev.binding.device.actions == [] assert len(dev.components[0].actions) == 3 assert len(dev.binding.device.components[0].actions) == 0 + + +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_system_uuid_motherboard(user: UserClient): + """This test the actions of a placeholder of one snapshot""" + s = yaml2json('real-eee-1001pxd.snapshot.12') + snap1, _ = user.post(s, res=Snapshot) + + for c in s['components']: + if c['type'] == 'Motherboard': + c['serialNumber'] = 'ABee0123456720' + + snap2, _ = user.post(s, res=Snapshot, status=422) + txt = "We have detected that a there is a device in your inventory" + assert txt in snap2['message'][0]