2020-12-29 16:37:13 +00:00
|
|
|
import os
|
2018-08-03 16:15:08 +00:00
|
|
|
import ipaddress
|
2020-12-29 16:37:13 +00:00
|
|
|
import json
|
|
|
|
import shutil
|
2020-11-23 14:55:24 +00:00
|
|
|
import copy
|
|
|
|
import pytest
|
|
|
|
|
2020-11-26 17:44:08 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-12-09 11:05:22 +00:00
|
|
|
from dateutil.tz import tzutc
|
2018-10-15 09:21:21 +00:00
|
|
|
from decimal import Decimal
|
2019-07-07 19:36:09 +00:00
|
|
|
from typing import Tuple, Type
|
2018-06-10 16:47:49 +00:00
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
from flask import current_app as app, g
|
2018-07-14 14:41:22 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2018-08-26 17:04:42 +00:00
|
|
|
from teal.enums import Currency, Subdivision
|
2018-07-14 14:41:22 +00:00
|
|
|
|
2018-04-30 17:58:19 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2020-12-29 16:37:13 +00:00
|
|
|
from ereuse_devicehub.client import UserClient, Client
|
2020-11-24 12:44:49 +00:00
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
2018-11-17 17:24:34 +00:00
|
|
|
from ereuse_devicehub.resources import enums
|
2019-05-11 14:27:22 +00:00
|
|
|
from ereuse_devicehub.resources.action import models
|
2018-10-13 12:53:46 +00:00
|
|
|
from ereuse_devicehub.resources.device import states
|
2018-07-14 14:41:22 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Desktop, Device, GraphicCard, HardDrive, \
|
2018-06-16 10:41:12 +00:00
|
|
|
RamModule, SolidStateDrive
|
2018-11-08 16:37:14 +00:00
|
|
|
from ereuse_devicehub.resources.enums import ComputerChassis, Severity, TestDataStorageLength
|
2018-08-03 16:15:08 +00:00
|
|
|
from tests import conftest
|
2018-07-22 20:42:49 +00:00
|
|
|
from tests.conftest import create_user, file
|
2018-04-30 17:58:19 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2018-04-30 17:58:19 +00:00
|
|
|
def test_author():
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Checks the default created author.
|
2018-04-30 17:58:19 +00:00
|
|
|
|
|
|
|
Note that the author can be accessed after inserting the row.
|
|
|
|
"""
|
|
|
|
user = create_user()
|
|
|
|
g.user = user
|
2019-05-11 14:27:22 +00:00
|
|
|
e = models.ActionWithOneDevice(device=Device())
|
2018-04-30 17:58:19 +00:00
|
|
|
db.session.add(e)
|
|
|
|
assert e.author is None
|
|
|
|
assert e.author_id is None
|
|
|
|
db.session.commit()
|
|
|
|
assert e.author == user
|
2018-06-10 16:47:49 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-06-10 16:47:49 +00:00
|
|
|
def test_erase_basic():
|
2018-07-22 20:42:49 +00:00
|
|
|
erasure = models.EraseBasic(
|
2018-06-10 16:47:49 +00:00
|
|
|
device=HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar'),
|
2018-11-17 17:24:34 +00:00
|
|
|
steps=[
|
|
|
|
models.StepZero(**conftest.T),
|
|
|
|
models.StepRandom(**conftest.T)
|
|
|
|
],
|
2018-08-26 17:04:42 +00:00
|
|
|
**conftest.T
|
2018-06-10 16:47:49 +00:00
|
|
|
)
|
|
|
|
db.session.add(erasure)
|
|
|
|
db.session.commit()
|
2018-07-22 20:42:49 +00:00
|
|
|
db_erasure = models.EraseBasic.query.one()
|
2018-06-10 16:47:49 +00:00
|
|
|
assert erasure == db_erasure
|
2019-05-11 14:27:22 +00:00
|
|
|
assert next(iter(db_erasure.device.actions)) == erasure
|
2018-11-17 17:24:34 +00:00
|
|
|
assert not erasure.standards, 'EraseBasic themselves do not have standards'
|
2018-06-10 16:47:49 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-06-10 16:47:49 +00:00
|
|
|
def test_validate_device_data_storage():
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Checks the validation for data-storage-only actions works."""
|
2018-06-10 16:47:49 +00:00
|
|
|
# We can't set a GraphicCard
|
|
|
|
with pytest.raises(TypeError,
|
|
|
|
message='EraseBasic.device must be a DataStorage '
|
|
|
|
'but you passed <GraphicCard None model=\'foo-bar\' S/N=\'foo\'>'):
|
2018-07-22 20:42:49 +00:00
|
|
|
models.EraseBasic(
|
2018-06-10 16:47:49 +00:00
|
|
|
device=GraphicCard(serial_number='foo', manufacturer='bar', model='foo-bar'),
|
|
|
|
clean_with_zeros=True,
|
2018-08-26 17:04:42 +00:00
|
|
|
**conftest.T
|
2018-06-10 16:47:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-11-17 17:24:34 +00:00
|
|
|
def test_erase_sectors_steps_erasure_standards_hmg_is5():
|
2018-07-22 20:42:49 +00:00
|
|
|
erasure = models.EraseSectors(
|
2018-06-10 16:47:49 +00:00
|
|
|
device=SolidStateDrive(serial_number='foo', manufacturer='bar', model='foo-bar'),
|
|
|
|
steps=[
|
2018-08-26 17:04:42 +00:00
|
|
|
models.StepZero(**conftest.T),
|
|
|
|
models.StepRandom(**conftest.T),
|
2018-11-17 17:24:34 +00:00
|
|
|
models.StepRandom(**conftest.T)
|
2018-08-26 17:04:42 +00:00
|
|
|
],
|
|
|
|
**conftest.T
|
2018-06-10 16:47:49 +00:00
|
|
|
)
|
|
|
|
db.session.add(erasure)
|
|
|
|
db.session.commit()
|
2018-07-22 20:42:49 +00:00
|
|
|
db_erasure = models.EraseSectors.query.one()
|
2018-06-10 16:47:49 +00:00
|
|
|
# Steps are in order
|
|
|
|
assert db_erasure.steps[0].num == 0
|
|
|
|
assert db_erasure.steps[1].num == 1
|
|
|
|
assert db_erasure.steps[2].num == 2
|
2018-11-17 17:24:34 +00:00
|
|
|
assert {enums.ErasureStandards.HMG_IS5} == erasure.standards
|
2018-06-10 16:47:49 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-11-09 10:22:13 +00:00
|
|
|
def test_test_data_storage_working():
|
|
|
|
"""Tests TestDataStorage with the resulting properties in Device."""
|
|
|
|
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
|
2018-07-22 20:42:49 +00:00
|
|
|
test = models.TestDataStorage(
|
2018-11-09 10:22:13 +00:00
|
|
|
device=hdd,
|
|
|
|
severity=Severity.Error,
|
2018-06-10 16:47:49 +00:00
|
|
|
elapsed=timedelta(minutes=25),
|
2018-10-08 08:37:32 +00:00
|
|
|
length=TestDataStorageLength.Short,
|
2018-11-09 10:22:13 +00:00
|
|
|
status=':-(',
|
2018-06-10 16:47:49 +00:00
|
|
|
lifetime=timedelta(days=120)
|
|
|
|
)
|
|
|
|
db.session.add(test)
|
2018-11-09 10:22:13 +00:00
|
|
|
db.session.flush()
|
|
|
|
assert hdd.working == [test]
|
|
|
|
assert not hdd.problems
|
|
|
|
# Add new test overriding the first test in the problems
|
|
|
|
# / working condition
|
|
|
|
test2 = models.TestDataStorage(
|
|
|
|
device=hdd,
|
|
|
|
severity=Severity.Warning,
|
|
|
|
elapsed=timedelta(minutes=25),
|
|
|
|
length=TestDataStorageLength.Short,
|
|
|
|
status=':-(',
|
|
|
|
lifetime=timedelta(days=120)
|
|
|
|
)
|
|
|
|
db.session.add(test2)
|
|
|
|
db.session.flush()
|
|
|
|
assert hdd.working == [test2]
|
|
|
|
assert hdd.problems == []
|
2018-06-10 16:47:49 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-06-10 16:47:49 +00:00
|
|
|
def test_install():
|
|
|
|
hdd = HardDrive(serial_number='sn')
|
2018-07-22 20:42:49 +00:00
|
|
|
install = models.Install(name='LinuxMint 18.04 es',
|
|
|
|
elapsed=timedelta(seconds=25),
|
|
|
|
device=hdd)
|
2018-06-10 16:47:49 +00:00
|
|
|
db.session.add(install)
|
|
|
|
db.session.commit()
|
2018-06-16 10:41:12 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2019-05-11 14:27:22 +00:00
|
|
|
def test_update_components_action_one():
|
2018-10-18 08:09:10 +00:00
|
|
|
computer = Desktop(serial_number='sn1',
|
|
|
|
model='ml1',
|
|
|
|
manufacturer='mr1',
|
|
|
|
chassis=ComputerChassis.Tower)
|
2018-06-16 10:41:12 +00:00
|
|
|
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
|
|
|
|
computer.components.add(hdd)
|
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# Add action
|
2019-04-30 00:02:23 +00:00
|
|
|
test = models.StressTest(elapsed=timedelta(minutes=1))
|
2019-05-11 14:27:22 +00:00
|
|
|
computer.actions_one.add(test)
|
2018-06-16 10:41:12 +00:00
|
|
|
assert test.device == computer
|
2019-05-11 14:27:22 +00:00
|
|
|
assert next(iter(test.components)) == hdd, 'Action has to have new components'
|
2018-06-16 10:41:12 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# Remove action
|
|
|
|
computer.actions_one.clear()
|
2018-06-16 10:41:12 +00:00
|
|
|
assert not test.device
|
2019-05-11 14:27:22 +00:00
|
|
|
assert not test.components, 'Action has to loose the components'
|
2018-06-16 10:41:12 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# If we add a component to a device AFTER assigning the action
|
|
|
|
# to the device, the action doesn't get the new component
|
|
|
|
computer.actions_one.add(test)
|
2018-06-16 10:41:12 +00:00
|
|
|
ram = RamModule()
|
|
|
|
computer.components.add(ram)
|
|
|
|
assert len(test.components) == 1
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2019-05-11 14:27:22 +00:00
|
|
|
def test_update_components_action_multiple():
|
2018-10-18 08:09:10 +00:00
|
|
|
computer = Desktop(serial_number='sn1',
|
|
|
|
model='ml1',
|
|
|
|
manufacturer='mr1',
|
|
|
|
chassis=ComputerChassis.Tower)
|
2018-06-16 10:41:12 +00:00
|
|
|
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
|
|
|
|
computer.components.add(hdd)
|
|
|
|
|
2019-07-07 19:36:09 +00:00
|
|
|
ready = models.Ready()
|
2018-06-16 10:41:12 +00:00
|
|
|
assert not ready.devices
|
|
|
|
assert not ready.components
|
|
|
|
|
|
|
|
# Add
|
2019-05-11 14:27:22 +00:00
|
|
|
computer.actions_multiple.add(ready)
|
2018-06-16 10:41:12 +00:00
|
|
|
assert ready.devices == OrderedSet([computer])
|
|
|
|
assert next(iter(ready.components)) == hdd
|
|
|
|
|
|
|
|
# Remove
|
2019-05-11 14:27:22 +00:00
|
|
|
computer.actions_multiple.remove(ready)
|
2018-06-16 10:41:12 +00:00
|
|
|
assert not ready.devices
|
|
|
|
assert not ready.components
|
|
|
|
|
|
|
|
# init / replace collection
|
|
|
|
ready.devices = OrderedSet([computer])
|
|
|
|
assert ready.devices
|
|
|
|
assert ready.components
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-06-16 10:41:12 +00:00
|
|
|
def test_update_parent():
|
2018-10-18 08:09:10 +00:00
|
|
|
computer = Desktop(serial_number='sn1',
|
|
|
|
model='ml1',
|
|
|
|
manufacturer='mr1',
|
|
|
|
chassis=ComputerChassis.Tower)
|
2018-06-16 10:41:12 +00:00
|
|
|
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
|
|
|
|
computer.components.add(hdd)
|
|
|
|
|
|
|
|
# Add
|
2018-07-22 20:42:49 +00:00
|
|
|
benchmark = models.BenchmarkDataStorage()
|
2018-06-16 10:41:12 +00:00
|
|
|
benchmark.device = hdd
|
|
|
|
assert benchmark.parent == computer
|
|
|
|
assert not benchmark.components
|
|
|
|
|
|
|
|
# Remove
|
|
|
|
benchmark.device = None
|
|
|
|
assert not benchmark.parent
|
2018-07-22 20:42:49 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2019-06-29 14:26:14 +00:00
|
|
|
@pytest.mark.parametrize('action_model_state',
|
|
|
|
(pytest.param(ams, id=ams[0].__class__.__name__)
|
|
|
|
for ams in [
|
|
|
|
(models.ToRepair, states.Physical.ToBeRepaired),
|
|
|
|
(models.Repair, states.Physical.Repaired),
|
|
|
|
(models.ToPrepare, states.Physical.Preparing),
|
2019-07-07 19:36:09 +00:00
|
|
|
(models.Ready, states.Physical.Ready),
|
2019-06-29 14:26:14 +00:00
|
|
|
(models.Prepare, states.Physical.Prepared)
|
|
|
|
]))
|
2019-05-11 14:27:22 +00:00
|
|
|
def test_generic_action(action_model_state: Tuple[models.Action, states.Trading],
|
|
|
|
user: UserClient):
|
|
|
|
"""Tests POSTing all generic actions."""
|
|
|
|
action_model, state = action_model_state
|
2018-07-22 20:42:49 +00:00
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
|
2019-05-11 14:27:22 +00:00
|
|
|
action = {'type': action_model.t, 'devices': [snapshot['device']['id']]}
|
|
|
|
action, _ = user.post(action, res=models.Action)
|
|
|
|
assert action['devices'][0]['id'] == snapshot['device']['id']
|
2018-07-22 20:42:49 +00:00
|
|
|
device, _ = user.get(res=Device, item=snapshot['device']['id'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert device['actions'][-1]['id'] == action['id']
|
2018-10-13 12:53:46 +00:00
|
|
|
assert device['physical'] == state.name
|
2020-10-16 13:08:54 +00:00
|
|
|
# Check if the update of device is changed
|
|
|
|
assert snapshot['device']['updated'] != device['updated']
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2020-11-24 12:44:49 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live(user: UserClient, client: Client, app: Devicehub):
|
2018-08-03 16:15:08 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it."""
|
2020-11-24 12:44:49 +00:00
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
2020-11-27 18:14:29 +00:00
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
2020-11-24 12:44:49 +00:00
|
|
|
"devices": [device_id], "description": "aaa",
|
2020-12-01 21:03:15 +00:00
|
|
|
"finalUserCode": "abcdefjhi",
|
2020-11-27 18:14:29 +00:00
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
2020-11-24 12:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0]
|
|
|
|
hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0]
|
2020-12-04 15:58:53 +00:00
|
|
|
hdd_action['lifetime'] += 1000
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
snapshot, _ = client.post(acer, res=models.Live)
|
2021-01-08 09:46:34 +00:00
|
|
|
db_device = Device.query.filter_by(id=device_id).one()
|
2020-11-24 12:44:49 +00:00
|
|
|
action_live = [a for a in db_device.actions if a.type == 'Live']
|
|
|
|
assert len(action_live) == 1
|
2020-12-04 15:58:53 +00:00
|
|
|
assert action_live[0].usage_time_hdd == timedelta(hours=hdd_action['lifetime'])
|
|
|
|
assert action_live[0].usage_time_allocate == timedelta(hours=1000)
|
2020-12-01 21:03:15 +00:00
|
|
|
assert action_live[0].final_user_code == post_request['finalUserCode']
|
2020-11-24 12:44:49 +00:00
|
|
|
assert action_live[0].serial_number == 'wd-wx11a80w7430'
|
2020-12-28 14:31:57 +00:00
|
|
|
assert action_live[0].licence_version == '1.0'
|
2020-12-04 15:58:53 +00:00
|
|
|
assert str(action_live[0].snapshot_uuid) == acer['uuid']
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-04 15:58:53 +00:00
|
|
|
|
2021-01-04 14:53:58 +00:00
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_live_example(user: UserClient, client: Client, app: Devicehub):
|
|
|
|
"""Tests inserting a Live into the database and GETting it."""
|
|
|
|
acer = file('snapshotLive')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
|
|
|
|
acer = file('live')
|
|
|
|
live, _ = client.post(acer, res=models.Live)
|
2021-01-08 09:46:34 +00:00
|
|
|
db_device = Device.query.filter_by(id=device_id).one()
|
|
|
|
action_live = [a for a in db_device.actions if a.type == 'Live']
|
|
|
|
assert len(action_live) == 1
|
|
|
|
assert str(action_live[0].snapshot_uuid) == acer['uuid']
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2021-01-08 09:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_live_two_users(user: UserClient, user2: UserClient, client: Client, app: Devicehub):
|
|
|
|
"""Tests inserting a Live into the database and GETting it."""
|
|
|
|
acer = file('snapshotLive')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
acer2 = file('snapshotLive')
|
|
|
|
acer2['uuid'] = '3b6a9288-0ba6-4bdd-862a-2b1f660e7115'
|
|
|
|
snapshot2, _ = user2.post(acer2, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
|
|
|
|
acer = file('live')
|
|
|
|
live, _ = client.post(acer, res=models.Live)
|
|
|
|
db_device = Device.query.filter_by(id=device_id).one()
|
2021-01-04 14:53:58 +00:00
|
|
|
action_live = [a for a in db_device.actions if a.type == 'Live']
|
|
|
|
assert len(action_live) == 1
|
|
|
|
assert str(action_live[0].snapshot_uuid) == acer['uuid']
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2021-01-04 14:53:58 +00:00
|
|
|
|
|
|
|
|
2021-01-08 09:46:34 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_live_two_allocated(user: UserClient, user2: UserClient, client: Client, app: Devicehub):
|
|
|
|
"""Tests inserting a Live into the database and GETting it."""
|
|
|
|
acer = file('snapshotLive')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
acer2 = file('snapshotLive')
|
|
|
|
acer2['uuid'] = '3b6a9288-0ba6-4bdd-862a-2b1f660e7115'
|
|
|
|
snapshot2, _ = user2.post(acer2, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
device_id2 = snapshot2['device']['id']
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
post_request2 = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id2], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
user2.post(res=models.Allocate, data=post_request2)
|
|
|
|
|
|
|
|
acer = file('live')
|
|
|
|
live, _ = client.post(acer, res=models.Live, status=422)
|
|
|
|
message = 'Expected only one Device but multiple where found'
|
|
|
|
assert live['message'] == message
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2021-01-08 09:46:34 +00:00
|
|
|
|
|
|
|
|
2020-12-04 15:58:53 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live_without_TestDataStorage(user: UserClient, client: Client, app: Devicehub):
|
2020-12-04 15:58:53 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
If the live don't have a TestDataStorage, then save live and response None
|
|
|
|
"""
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
db_device = Device.query.filter_by(id=1).one()
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
|
|
|
actions = [a for a in acer['components'][7]['actions'] if a['type'] != 'TestDataStorage']
|
|
|
|
acer['components'][7]['actions'] = actions
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
live, _ = client.post(acer, res=models.Live)
|
2020-12-04 15:58:53 +00:00
|
|
|
assert live['severity'] == 'Warning'
|
|
|
|
description = "We don't found any TestDataStorage for disk sn: wd-wx11a80w7430"
|
|
|
|
assert live['description'] == description
|
|
|
|
db_live = models.Live.query.filter_by(id=live['id']).one()
|
|
|
|
assert db_live.usage_time_hdd is None
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-04 15:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live_without_hdd_1(user: UserClient, client: Client, app: Devicehub):
|
2020-12-04 15:58:53 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
The snapshot have hdd but the live no, and response 404
|
|
|
|
"""
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
db_device = Device.query.filter_by(id=1).one()
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
|
|
|
components = [a for a in acer['components'] if a['type'] != 'HardDrive']
|
|
|
|
acer['components'] = components
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
response, _ = client.post(acer, res=models.Live, status=404)
|
2020-12-04 15:58:53 +00:00
|
|
|
assert "The There aren't any disk in this device" in response['message']
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-04 15:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live_without_hdd_2(user: UserClient, client: Client, app: Devicehub):
|
2020-12-04 15:58:53 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
The snapshot haven't hdd and the live neither, and response 404
|
|
|
|
"""
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
components = [a for a in acer['components'] if a['type'] != 'HardDrive']
|
|
|
|
acer['components'] = components
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
db_device = Device.query.filter_by(id=1).one()
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
response, _ = client.post(acer, res=models.Live, status=404)
|
2020-12-04 15:58:53 +00:00
|
|
|
assert "The There aren't any disk in this device" in response['message']
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-04 15:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live_without_hdd_3(user: UserClient, client: Client, app: Devicehub):
|
2020-12-04 15:58:53 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
The snapshot haven't hdd and the live have, and save the live
|
|
|
|
with usage_time_allocate == 0
|
|
|
|
"""
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
|
|
|
components = [a for a in acer['components'] if a['type'] != 'HardDrive']
|
|
|
|
acer['components'] = components
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
db_device = Device.query.filter_by(id=1).one()
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
live, _ = client.post(acer, res=models.Live)
|
2020-12-04 15:58:53 +00:00
|
|
|
assert live['severity'] == 'Warning'
|
2020-12-05 19:22:38 +00:00
|
|
|
description = "Don't exist one previous live or snapshot as reference"
|
2020-12-04 15:58:53 +00:00
|
|
|
assert live['description'] == description
|
|
|
|
db_live = models.Live.query.filter_by(id=live['id']).one()
|
|
|
|
assert str(db_live.usage_time_hdd) == '195 days, 12:00:00'
|
|
|
|
assert str(db_live.usage_time_allocate) == '0:00:00'
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-04 15:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live_with_hdd_with_old_time(user: UserClient, client: Client, app: Devicehub):
|
2020-12-04 15:58:53 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
The snapshot hdd have a lifetime higher than lifetime of the live action
|
|
|
|
save the live with usage_time_allocate == 0
|
|
|
|
"""
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
db_device = Device.query.filter_by(id=1).one()
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
|
|
|
action = [a for a in acer['components'][7]['actions'] if a['type'] == 'TestDataStorage']
|
|
|
|
action[0]['lifetime'] -= 100
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
live, _ = client.post(acer, res=models.Live)
|
2020-12-04 15:58:53 +00:00
|
|
|
assert live['severity'] == 'Warning'
|
|
|
|
description = "The difference with the last live/snapshot is negative"
|
|
|
|
assert live['description'] == description
|
|
|
|
db_live = models.Live.query.filter_by(id=live['id']).one()
|
|
|
|
assert str(db_live.usage_time_hdd) == '191 days, 8:00:00'
|
|
|
|
assert str(db_live.usage_time_allocate) == '0:00:00'
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-04 15:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-12-29 16:37:13 +00:00
|
|
|
def test_live_search_last_allocate(user: UserClient, client: Client, app: Devicehub):
|
2020-12-04 15:58:53 +00:00
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
"""
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
|
|
|
hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0]
|
|
|
|
hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0]
|
|
|
|
hdd_action['lifetime'] += 1000
|
2020-12-28 14:31:57 +00:00
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
live, _ = client.post(acer, res=models.Live)
|
2020-12-04 15:58:53 +00:00
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec4"
|
|
|
|
actions = [a for a in acer['components'][7]['actions'] if a['type'] != 'TestDataStorage']
|
|
|
|
acer['components'][7]['actions'] = actions
|
2020-12-29 16:37:13 +00:00
|
|
|
live, _ = client.post(acer, res=models.Live)
|
2020-12-04 15:58:53 +00:00
|
|
|
assert live['usageTimeAllocate'] == 1000
|
2021-02-04 11:51:51 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
shutil.rmtree(tmp_snapshots)
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
|
2020-12-29 16:37:13 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
def test_save_live_json(app: Devicehub, user: UserClient, client: Client):
|
|
|
|
""" This test check if works the function save_snapshot_in_file """
|
|
|
|
acer = file('acer.happy.battery.snapshot')
|
|
|
|
snapshot, _ = user.post(acer, res=models.Snapshot)
|
|
|
|
debug = 'AAA'
|
|
|
|
acer['debug'] = debug
|
|
|
|
device_id = snapshot['device']['id']
|
|
|
|
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
|
|
|
"devices": [device_id], "description": "aaa",
|
|
|
|
"finalUserCode": "abcdefjhi",
|
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_request)
|
|
|
|
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
|
|
|
|
hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0]
|
|
|
|
hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0]
|
|
|
|
hdd_action['lifetime'] += 1000
|
|
|
|
acer.pop('elapsed')
|
|
|
|
acer['licence_version'] = '1.0.0'
|
|
|
|
live, _ = client.post(acer, res=models.Live)
|
|
|
|
|
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
path_dir_base = os.path.join(tmp_snapshots)
|
|
|
|
|
|
|
|
uuid = acer['uuid']
|
|
|
|
files = [x for x in os.listdir(path_dir_base) if uuid in x]
|
|
|
|
|
|
|
|
snapshot = {'debug': ''}
|
|
|
|
if files:
|
2021-02-04 11:23:28 +00:00
|
|
|
path_snapshot = os.path.join(path_dir_base, files[-1])
|
2020-12-29 16:37:13 +00:00
|
|
|
with open(path_snapshot) as file_snapshot:
|
|
|
|
snapshot = json.loads(file_snapshot.read())
|
|
|
|
|
2021-02-04 11:51:51 +00:00
|
|
|
shutil.rmtree(tmp_snapshots)
|
2020-12-29 16:37:13 +00:00
|
|
|
|
|
|
|
assert snapshot['debug'] == debug
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_licences(client: Client):
|
|
|
|
"""Tests inserting a Live into the database and GETting it.
|
|
|
|
"""
|
|
|
|
licences, _ = client.get('/licences/')
|
|
|
|
licences = json.loads(licences)
|
2021-01-08 09:46:34 +00:00
|
|
|
assert licences[0]['USOdyPrivacyPolicyVersion'] == '1.0.0'
|
2020-12-29 16:37:13 +00:00
|
|
|
|
|
|
|
|
2020-11-23 14:55:24 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_allocate(user: UserClient):
|
|
|
|
""" Tests allocate """
|
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
2020-11-26 17:44:08 +00:00
|
|
|
post_request = {"transaction": "ccc",
|
2020-12-01 14:33:49 +00:00
|
|
|
"finalUserCode": "aabbcc",
|
2020-11-26 17:44:08 +00:00
|
|
|
"name": "John",
|
|
|
|
"severity": "Info",
|
2020-11-27 18:14:29 +00:00
|
|
|
"endUsers": 1,
|
2020-11-26 17:44:08 +00:00
|
|
|
"devices": [device_id],
|
|
|
|
"description": "aaa",
|
2020-11-27 18:14:29 +00:00
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00",
|
2020-11-23 14:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allocate, _ = user.post(res=models.Allocate, data=post_request)
|
|
|
|
# Normal allocate
|
|
|
|
device, _ = user.get(res=Device, item=device_id)
|
|
|
|
assert device['allocated'] == True
|
|
|
|
action = [a for a in device['actions'] if a['type'] == 'Allocate'][0]
|
2020-11-26 17:44:08 +00:00
|
|
|
assert action['transaction'] == allocate['transaction']
|
2020-12-01 14:33:49 +00:00
|
|
|
assert action['finalUserCode'] == allocate['finalUserCode']
|
2020-11-23 14:55:24 +00:00
|
|
|
assert action['created'] == allocate['created']
|
2020-11-27 18:14:29 +00:00
|
|
|
assert action['startTime'] == allocate['startTime']
|
|
|
|
assert action['endUsers'] == allocate['endUsers']
|
2020-11-23 14:55:24 +00:00
|
|
|
assert action['name'] == allocate['name']
|
|
|
|
|
|
|
|
post_bad_request1 = copy.copy(post_request)
|
2020-11-27 18:14:29 +00:00
|
|
|
post_bad_request1['endUsers'] = 2
|
2020-11-23 14:55:24 +00:00
|
|
|
post_bad_request2 = copy.copy(post_request)
|
2020-11-27 18:14:29 +00:00
|
|
|
post_bad_request2['startTime'] = "2020-11-01T02:00:00+00:01"
|
2020-11-23 14:55:24 +00:00
|
|
|
post_bad_request3 = copy.copy(post_request)
|
2020-11-26 17:44:08 +00:00
|
|
|
post_bad_request3['transaction'] = "aaa"
|
2020-11-23 14:55:24 +00:00
|
|
|
res1, _ = user.post(res=models.Allocate, data=post_bad_request1, status=422)
|
|
|
|
res2, _ = user.post(res=models.Allocate, data=post_bad_request2, status=422)
|
|
|
|
res3, _ = user.post(res=models.Allocate, data=post_bad_request3, status=422)
|
|
|
|
for r in (res1, res2, res3):
|
|
|
|
assert r['code'] == 422
|
|
|
|
assert r['type'] == 'ValidationError'
|
|
|
|
|
|
|
|
|
2020-11-26 17:44:08 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_allocate_bad_dates(user: UserClient):
|
|
|
|
""" Tests allocate """
|
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
2020-12-09 11:06:43 +00:00
|
|
|
delay = timedelta(days=30)
|
|
|
|
future = datetime.now().replace(tzinfo=tzutc()) + delay
|
2020-11-26 17:44:08 +00:00
|
|
|
post_request = {"transaction": "ccc",
|
2020-12-01 14:33:49 +00:00
|
|
|
"finalUserCode": "aabbcc",
|
2020-11-26 17:44:08 +00:00
|
|
|
"name": "John",
|
|
|
|
"severity": "Info",
|
|
|
|
"end_users": 1,
|
|
|
|
"devices": [device_id],
|
|
|
|
"description": "aaa",
|
|
|
|
"start_time": future,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, _ = user.post(res=models.Allocate, data=post_request, status=422)
|
|
|
|
assert res['code'] == 422
|
|
|
|
assert res['type'] == 'ValidationError'
|
|
|
|
|
|
|
|
|
2020-11-23 14:55:24 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_deallocate(user: UserClient):
|
|
|
|
""" Tests deallocate """
|
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
2020-11-27 18:14:29 +00:00
|
|
|
post_deallocate = {"startTime": "2020-11-01T02:00:00+00:00",
|
2020-11-26 17:44:08 +00:00
|
|
|
"transaction": "ccc",
|
|
|
|
"devices": [device_id]
|
2020-11-23 14:55:24 +00:00
|
|
|
}
|
|
|
|
res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=422)
|
|
|
|
assert res['code'] == 422
|
|
|
|
assert res['type'] == 'ValidationError'
|
2020-11-27 18:14:29 +00:00
|
|
|
post_allocate = {"transaction": "ccc", "name": "John", "endUsers": 1,
|
2020-11-23 14:55:24 +00:00
|
|
|
"devices": [device_id], "description": "aaa",
|
2020-11-27 18:14:29 +00:00
|
|
|
"startTime": "2020-11-01T02:00:00+00:00",
|
|
|
|
"endTime": "2020-12-01T02:00:00+00:00"
|
2020-11-23 14:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_allocate)
|
|
|
|
device, _ = user.get(res=Device, item=device_id)
|
|
|
|
assert device['allocated'] == True
|
|
|
|
deallocate, _ = user.post(res=models.Deallocate, data=post_deallocate)
|
2020-11-27 18:14:29 +00:00
|
|
|
assert deallocate['startTime'] == post_deallocate['startTime']
|
2020-11-23 14:55:24 +00:00
|
|
|
assert deallocate['devices'][0]['id'] == device_id
|
|
|
|
assert deallocate['devices'][0]['allocated'] == False
|
|
|
|
res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=422)
|
|
|
|
assert res['code'] == 422
|
|
|
|
assert res['type'] == 'ValidationError'
|
|
|
|
|
|
|
|
|
2020-11-26 17:44:08 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_deallocate_bad_dates(user: UserClient):
|
|
|
|
""" Tests deallocate with bad date of start_time """
|
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
|
|
|
|
device_id = snapshot['device']['id']
|
2020-12-09 11:05:22 +00:00
|
|
|
delay = timedelta(days=30)
|
|
|
|
future = datetime.now().replace(tzinfo=tzutc()) + delay
|
2020-11-27 18:14:29 +00:00
|
|
|
post_deallocate = {"startTime": future,
|
2020-11-26 17:44:08 +00:00
|
|
|
"devices": [device_id]
|
|
|
|
}
|
|
|
|
post_allocate = {"devices": [device_id], "description": "aaa",
|
2020-11-27 18:14:29 +00:00
|
|
|
"startTime": "2020-11-01T02:00:00+00:00"
|
2020-11-26 17:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user.post(res=models.Allocate, data=post_allocate)
|
2020-11-26 17:51:36 +00:00
|
|
|
res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=422)
|
2020-11-26 17:44:08 +00:00
|
|
|
assert res['code'] == 422
|
|
|
|
assert res['type'] == 'ValidationError'
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2019-06-29 14:26:14 +00:00
|
|
|
@pytest.mark.parametrize('action_model_state',
|
2019-07-07 19:36:09 +00:00
|
|
|
(pytest.param(ams, id=ams[0].__name__)
|
2019-06-29 14:26:14 +00:00
|
|
|
for ams in [
|
2019-07-07 19:36:09 +00:00
|
|
|
(models.MakeAvailable, states.Trading.Available),
|
2019-06-29 14:26:14 +00:00
|
|
|
(models.Sell, states.Trading.Sold),
|
|
|
|
(models.Donate, states.Trading.Donated),
|
|
|
|
(models.Rent, states.Trading.Renting),
|
|
|
|
(models.DisposeProduct, states.Trading.ProductDisposed)
|
|
|
|
]))
|
2021-03-25 10:26:30 +00:00
|
|
|
def test_trade(action_model_state: Tuple[Type[models.Action], states.Trading], user: UserClient):
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Tests POSTing all Trade actions."""
|
2019-07-07 19:36:09 +00:00
|
|
|
# todo missing None states.Trading for after cancelling renting, for example
|
2021-03-17 09:44:58 +00:00
|
|
|
# import pdb; pdb.set_trace()
|
|
|
|
# Remove this test
|
2019-05-11 14:27:22 +00:00
|
|
|
action_model, state = action_model_state
|
2018-08-03 16:15:08 +00:00
|
|
|
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
|
2019-05-11 14:27:22 +00:00
|
|
|
action = {
|
|
|
|
'type': action_model.t,
|
2019-07-07 19:36:09 +00:00
|
|
|
'devices': [snapshot['device']['id']]
|
2018-08-03 16:15:08 +00:00
|
|
|
}
|
2019-07-07 19:36:09 +00:00
|
|
|
if issubclass(action_model, models.Trade):
|
|
|
|
action['to'] = user.user['individuals'][0]['id']
|
|
|
|
action['shippingDate'] = '2018-06-29T12:28:54'
|
|
|
|
action['invoiceNumber'] = 'ABC'
|
2019-05-11 14:27:22 +00:00
|
|
|
action, _ = user.post(action, res=models.Action)
|
|
|
|
assert action['devices'][0]['id'] == snapshot['device']['id']
|
2018-08-03 16:15:08 +00:00
|
|
|
device, _ = user.get(res=Device, item=snapshot['device']['id'])
|
2019-05-11 14:27:22 +00:00
|
|
|
assert device['actions'][-1]['id'] == action['id']
|
2018-10-13 12:53:46 +00:00
|
|
|
assert device['trading'] == state.name
|
2018-08-03 16:15:08 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 16:15:08 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
|
|
|
def test_price_custom():
|
|
|
|
computer = Desktop(serial_number='sn1', model='ml1', manufacturer='mr1',
|
|
|
|
chassis=ComputerChassis.Docking)
|
2018-10-15 09:21:21 +00:00
|
|
|
price = models.Price(price=Decimal(25.25), currency=Currency.EUR)
|
2018-08-03 16:15:08 +00:00
|
|
|
price.device = computer
|
2018-10-13 12:53:46 +00:00
|
|
|
assert computer.price == price
|
2018-08-03 16:15:08 +00:00
|
|
|
db.session.add(computer)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
client = UserClient(app, 'foo@foo.com', 'foo', response_wrapper=app.response_class)
|
|
|
|
client.login()
|
2019-05-11 14:27:22 +00:00
|
|
|
p, _ = client.get(res=models.Action, item=str(price.id))
|
2018-08-03 16:15:08 +00:00
|
|
|
assert p['device']['id'] == price.device.id == computer.id
|
|
|
|
assert p['price'] == 25.25
|
|
|
|
assert p['currency'] == Currency.EUR.name == 'EUR'
|
2018-10-13 12:53:46 +00:00
|
|
|
|
|
|
|
c, _ = client.get(res=Device, item=computer.id)
|
|
|
|
assert c['price']['id'] == p['id']
|
2018-10-15 09:21:21 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-11-17 18:22:41 +00:00
|
|
|
def test_price_custom_client(user: UserClient):
|
2018-10-16 06:46:55 +00:00
|
|
|
"""As test_price_custom but creating the price through the API."""
|
2018-11-17 18:22:41 +00:00
|
|
|
s = file('basic.snapshot')
|
|
|
|
snapshot, _ = user.post(s, res=models.Snapshot)
|
|
|
|
price, _ = user.post({
|
|
|
|
'type': 'Price',
|
|
|
|
'price': 25,
|
|
|
|
'currency': Currency.EUR.name,
|
|
|
|
'device': snapshot['device']['id']
|
2019-05-11 14:27:22 +00:00
|
|
|
}, res=models.Action)
|
2018-11-17 18:22:41 +00:00
|
|
|
assert 25 == price['price']
|
|
|
|
assert Currency.EUR.name == price['currency']
|
|
|
|
|
|
|
|
device, _ = user.get(res=Device, item=price['device']['id'])
|
|
|
|
assert 25 == device['price']['price']
|
2018-10-16 06:46:55 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-11-17 17:24:34 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
2018-11-15 12:35:19 +00:00
|
|
|
def test_erase_physical():
|
2018-11-17 17:24:34 +00:00
|
|
|
erasure = models.ErasePhysical(
|
|
|
|
device=HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar'),
|
|
|
|
method=enums.PhysicalErasureMethod.Disintegration
|
|
|
|
)
|
|
|
|
db.session.add(erasure)
|
|
|
|
db.session.commit()
|