Merge pull request #388 from eReuse/bugfix/3962-system-uuid

2 computers with diferents motherboard with the same system-uuid
This commit is contained in:
cayop 2022-10-24 18:27:46 +02:00 committed by GitHub
commit 95ac4e794b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 1 deletions

View file

@ -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)

View file

@ -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):

View file

@ -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

View file

@ -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]