adding live into snapshot and adding test

This commit is contained in:
Cayo Puigdefabregas 2020-11-24 13:44:49 +01:00
parent 8999b6e9a5
commit 87704db271
4 changed files with 45 additions and 26 deletions

View File

@ -59,7 +59,7 @@ def upgrade():
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('serial_number', sa.Unicode(), nullable=True, sa.Column('serial_number', sa.Unicode(), nullable=True,
comment='The serial number of the Hard Disk in lower case.'), comment='The serial number of the Hard Disk in lower case.'),
sa.Column('time', sa.Interval(), nullable=True), sa.Column('time', sa.SmallInteger(), nullable=True),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}' schema=f'{get_inv()}'

View File

@ -1297,7 +1297,7 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice):
""" """
serial_number = Column(Unicode(), check_lower('serial_number')) serial_number = Column(Unicode(), check_lower('serial_number'))
serial_number.comment = """The serial number of the Hard Disk in lower case.""" serial_number.comment = """The serial number of the Hard Disk in lower case."""
time = Column(Interval, nullable=True) time = Column(SmallInteger, nullable=False)
class Organize(JoinedTableMixin, ActionWithMultipleDevices): class Organize(JoinedTableMixin, ActionWithMultipleDevices):

View File

@ -16,7 +16,7 @@ from teal.resource import View
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest, from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
InitTransfer) InitTransfer, Live)
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
@ -160,6 +160,7 @@ class ActionView(View):
# Check if HID is null and add Severity:Warning to Snapshot # Check if HID is null and add Severity:Warning to Snapshot
if snapshot.device.hid is None: if snapshot.device.hid is None:
snapshot.severity = Severity.Warning snapshot.severity = Severity.Warning
self.live(snapshot)
db.session.add(snapshot) db.session.add(snapshot)
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(snapshot) # transform it back ret = self.schema.jsonify(snapshot) # transform it back
@ -167,6 +168,22 @@ class ActionView(View):
db.session.commit() db.session.commit()
return ret return ret
def live(self, snapshot):
test_hdd= [a for a in snapshot.actions if a.type == "TestDataStorage"]
if not (test_hdd and snapshot.device.allocated):
return
test_hdd = test_hdd[0]
time = test_hdd.power_cycle_count
if time:
data_live = {'time': time,
'serial_number': test_hdd.device.serial_number,
'device': snapshot.device
}
live = Live(**data_live)
snapshot.actions.add(live)
def transfer_ownership(self): def transfer_ownership(self):
"""Perform a InitTransfer action to change author_id of device""" """Perform a InitTransfer action to change author_id of device"""
pass pass

View File

@ -10,8 +10,9 @@ from flask import current_app as app, g
from sqlalchemy.util import OrderedSet from sqlalchemy.util import OrderedSet
from teal.enums import Currency, Subdivision from teal.enums import Currency, Subdivision
from ereuse_devicehub.client import UserClient
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.client import UserClient
from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.resources import enums from ereuse_devicehub.resources import enums
from ereuse_devicehub.resources.action import models from ereuse_devicehub.resources.action import models
from ereuse_devicehub.resources.device import states from ereuse_devicehub.resources.device import states
@ -245,29 +246,30 @@ def test_generic_action(action_model_state: Tuple[models.Action, states.Trading]
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live(): def test_live(user: UserClient, app: Devicehub):
"""Tests inserting a Live into the database and GETting it.""" """Tests inserting a Live into the database and GETting it."""
db_live = models.Live(ip=ipaddress.ip_address('79.147.10.10'), acer = file('acer.happy.battery.snapshot')
subdivision_confidence=84, snapshot, _ = user.post(acer, res=models.Snapshot)
subdivision=Subdivision['ES-CA'], device_id = snapshot['device']['id']
city='barcelona', db_device = Device.query.filter_by(id=1).one()
city_confidence=20, post_request = {"Transaction": "ccc", "name": "John", "end_users": 1,
isp='acme', "devices": [device_id], "description": "aaa",
device=Desktop(serial_number='sn1', model='ml1', manufacturer='mr1', "start_time": "2020-11-01T02:00:00+00:00",
chassis=ComputerChassis.Docking), "end_time": "2020-12-01T02:00:00+00:00"
organization='acme1', }
organization_type='acme1bis')
db.session.add(db_live) user.post(res=models.Allocate, data=post_request)
db.session.commit() acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
client = UserClient(app, 'foo@foo.com', 'foo', response_wrapper=app.response_class) hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0]
client.login() hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0]
live, _ = client.get(res=models.Action, item=str(db_live.id)) hdd_action['powerCycleCount'] += 1000
assert live['ip'] == '79.147.10.10' snapshot, _ = user.post(acer, res=models.Snapshot)
assert live['subdivision'] == 'ES-CA' db_device = Device.query.filter_by(id=1).one()
assert live['country'] == 'ES' action_live = [a for a in db_device.actions if a.type == 'Live']
device, _ = client.get(res=Device, item=live['device']['id']) assert len(action_live) == 1
assert device['usage'] == states.Usage.InUse.name assert action_live[0].time == 6293
assert action_live[0].serial_number == 'wd-wx11a80w7430'
@pytest.mark.mvp @pytest.mark.mvp