This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/tests/test_action.py

3024 lines
102 KiB
Python
Raw Normal View History

2022-05-16 07:53:22 +00:00
import copy
2020-12-29 16:37:13 +00:00
import json
2022-05-16 07:53:22 +00:00
import os
2020-12-29 16:37:13 +00:00
import shutil
2020-11-26 17:44:08 +00:00
from datetime import datetime, timedelta
2018-10-15 09:21:21 +00:00
from decimal import Decimal
2022-05-16 07:53:22 +00:00
from io import BytesIO
2021-03-17 17:17:43 +00:00
from json.decoder import JSONDecodeError
from typing import Tuple
2018-06-10 16:47:49 +00:00
2022-05-16 07:53:22 +00:00
import pytest
from dateutil.tz import tzutc
from flask import current_app as app
from flask import g
from pytest import raises
2018-07-14 14:41:22 +00:00
from sqlalchemy.util import OrderedSet
2022-05-16 07:53:22 +00:00
from ereuse_devicehub.client import Client, UserClient
2018-04-30 17:58:19 +00:00
from ereuse_devicehub.db import db
from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.resources import enums
from ereuse_devicehub.resources.action import models
2022-05-16 07:53:22 +00:00
from ereuse_devicehub.resources.agent.models import Person
from ereuse_devicehub.resources.device import states
2022-05-16 07:53:22 +00:00
from ereuse_devicehub.resources.device.models import (
Desktop,
Device,
GraphicCard,
HardDrive,
RamModule,
SolidStateDrive,
)
from ereuse_devicehub.resources.enums import (
ComputerChassis,
Severity,
TestDataStorageLength,
)
from ereuse_devicehub.resources.lot.models import Lot
2021-09-03 14:05:55 +00:00
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
2022-05-16 07:53:22 +00:00
from ereuse_devicehub.resources.user.models import User
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.teal.enums import Currency
from tests import conftest
2022-05-16 07:53:22 +00:00
from tests.conftest import create_user, file, json_encode, yaml2json
2018-04-30 17:58:19 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2018-04-30 17:58:19 +00:00
def test_author():
"""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
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
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
2018-06-10 16:47:49 +00:00
def test_erase_basic():
erasure = models.EraseBasic(
2018-06-10 16:47:49 +00:00
device=HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar'),
2022-05-16 07:53:22 +00:00
steps=[models.StepZero(**conftest.T), models.StepRandom(**conftest.T)],
**conftest.T,
2018-06-10 16:47:49 +00:00
)
db.session.add(erasure)
db.session.commit()
db_erasure = models.EraseBasic.query.one()
2018-06-10 16:47:49 +00:00
assert erasure == db_erasure
assert next(iter(db_erasure.device.actions)) == erasure
assert not erasure.standards, 'EraseBasic themselves do not have standards'
2018-06-10 16:47:49 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
2018-06-10 16:47:49 +00:00
def test_validate_device_data_storage():
"""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):
models.EraseBasic(
2022-05-16 07:53:22 +00:00
device=GraphicCard(
serial_number='foo', manufacturer='bar', model='foo-bar'
),
2018-06-10 16:47:49 +00:00
clean_with_zeros=True,
2022-05-16 07:53:22 +00:00
**conftest.T,
2018-06-10 16:47:49 +00:00
)
pytest.fail(
'EraseBasic.device must be a DataStorage '
'but you passed <GraphicCard None model=\'foo-bar\' S/N=\'foo\'>'
)
2018-06-10 16:47:49 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_erase_sectors_steps_erasure_standards_hmg_is5():
erasure = models.EraseSectors(
2022-05-16 07:53:22 +00:00
device=SolidStateDrive(
serial_number='foo', manufacturer='bar', model='foo-bar'
),
2018-06-10 16:47:49 +00:00
steps=[
models.StepZero(**conftest.T),
models.StepRandom(**conftest.T),
2022-05-16 07:53:22 +00:00
models.StepRandom(**conftest.T),
],
2022-05-16 07:53:22 +00:00
**conftest.T,
2018-06-10 16:47:49 +00:00
)
db.session.add(erasure)
db.session.commit()
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
assert {enums.ErasureStandards.HMG_IS5} == erasure.standards
2018-06-10 16:47:49 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_test_data_storage_working():
"""Tests TestDataStorage with the resulting properties in Device."""
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
test = models.TestDataStorage(
device=hdd,
severity=Severity.Error,
2018-06-10 16:47:49 +00:00
elapsed=timedelta(minutes=25),
length=TestDataStorageLength.Short,
status=':-(',
2022-05-16 07:53:22 +00:00
lifetime=timedelta(days=120),
2018-06-10 16:47:49 +00:00
)
db.session.add(test)
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=':-(',
2022-05-16 07:53:22 +00:00
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
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
2018-06-10 16:47:49 +00:00
def test_install():
hdd = HardDrive(serial_number='sn')
2022-05-16 07:53:22 +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()
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_update_components_action_one():
2022-05-16 07:53:22 +00:00
computer = Desktop(
serial_number='sn1',
model='ml1',
manufacturer='mr1',
chassis=ComputerChassis.Tower,
)
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
computer.components.add(hdd)
# Add action
test = models.StressTest(elapsed=timedelta(minutes=1))
computer.actions_one.add(test)
assert test.device == computer
assert next(iter(test.components)) == hdd, 'Action has to have new components'
# Remove action
computer.actions_one.clear()
assert not test.device
assert not test.components, 'Action has to loose the components'
# 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)
ram = RamModule()
computer.components.add(ram)
assert len(test.components) == 1
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_update_components_action_multiple():
2022-05-16 07:53:22 +00:00
computer = Desktop(
serial_number='sn1',
model='ml1',
manufacturer='mr1',
chassis=ComputerChassis.Tower,
)
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
computer.components.add(hdd)
ready = models.Ready()
assert not ready.devices
assert not ready.components
# Add
computer.actions_multiple.add(ready)
assert ready.devices == OrderedSet([computer])
assert next(iter(ready.components)) == hdd
# Remove
computer.actions_multiple.remove(ready)
assert not ready.devices
assert not ready.components
# init / replace collection
ready.devices = OrderedSet([computer])
assert ready.devices
assert ready.components
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_update_parent():
2022-05-16 07:53:22 +00:00
computer = Desktop(
serial_number='sn1',
model='ml1',
manufacturer='mr1',
chassis=ComputerChassis.Tower,
)
hdd = HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar')
computer.components.add(hdd)
# Add
benchmark = models.BenchmarkDataStorage()
benchmark.device = hdd
assert benchmark.parent == computer
assert not benchmark.components
# Remove
benchmark.device = None
assert not benchmark.parent
@pytest.mark.mvp
2022-09-01 09:10:37 +00:00
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
2022-05-16 07:53:22 +00:00
@pytest.mark.parametrize(
'action_model_state',
(
pytest.param(ams, id=ams[0].__class__.__name__)
for ams in [
(models.ToPrepare, states.Physical.ToPrepare),
(models.Prepare, states.Physical.Prepare),
(models.ToRepair, states.Physical.ToRepair),
(models.Ready, states.Physical.Ready),
]
),
)
def test_generic_action(
2022-09-01 09:10:37 +00:00
action_model_state: Tuple[models.ToPrepare, states.Trading], user2: UserClient
2022-05-16 07:53:22 +00:00
):
"""Tests POSTing all generic actions."""
2022-09-01 09:10:37 +00:00
user = user2
action_model, state = action_model_state
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2022-09-01 09:10:37 +00:00
abstract = Device.query.filter(Device.id == snapshot['device']['id']).one()
real = abstract.binding.device
action = {'type': action_model.t, 'devices': [real.id]}
action, _ = user.post(action, res=models.Action)
2022-09-01 09:10:37 +00:00
assert action['devices'][0]['id'] == real.id
device, _ = user.get(res=Device, item=real.dhid)
assert device['actions'][-1]['id'] == action['id']
assert device['physical'] == state.name
# Check if the update of device is changed
assert snapshot['device']['updated'] != device['updated']
2021-09-22 12:33:46 +00:00
@pytest.mark.mvp
2022-09-01 09:10:37 +00:00
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
2022-05-16 07:53:22 +00:00
@pytest.mark.parametrize(
'action_model',
(
pytest.param(ams, id=ams.__class__.__name__)
for ams in [models.Recycling, models.Use, models.Refurbish, models.Management]
),
)
def test_simple_status_actions(action_model: models.Action, user2: UserClient):
2021-09-28 09:25:57 +00:00
"""Simple test of status action."""
2022-09-01 09:10:37 +00:00
user = user2
2021-09-28 09:25:57 +00:00
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2022-09-01 09:10:37 +00:00
abstract = Device.query.filter(Device.id == snap['device']['id']).one()
real = abstract.binding.device
2021-09-28 09:25:57 +00:00
2022-09-01 09:10:37 +00:00
action = {'type': action_model.t, 'devices': [real.id]}
2021-09-28 09:25:57 +00:00
action, _ = user.post(action, res=models.Action)
2022-09-01 09:10:37 +00:00
device, _ = user.get(res=Device, item=real.dhid)
2021-09-28 09:25:57 +00:00
assert device['actions'][-1]['id'] == action['id']
assert action['author']['id'] == user.user['id']
assert action['rol_user']['id'] == user.user['id']
@pytest.mark.mvp
2022-05-16 07:53:22 +00:00
@pytest.mark.parametrize(
'action_model',
(
pytest.param(ams, id=ams.__class__.__name__)
for ams in [models.Recycling, models.Use, models.Refurbish, models.Management]
),
)
def test_outgoinlot_status_actions(
action_model: models.Action, user: UserClient, user2: UserClient
):
2021-09-28 09:25:57 +00:00
"""Test of status actions in outgoinlot."""
2021-09-22 12:33:46 +00:00
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2021-09-28 09:25:57 +00:00
device, _ = user.get(res=Device, item=snap['device']['devicehubID'])
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device['id'])]
)
2021-09-28 09:25:57 +00:00
request_post = {
'type': 'Trade',
'devices': [device['id']],
'userFromEmail': user.email,
'userToEmail': user2.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
action = {'type': action_model.t, 'devices': [device['id']]}
2021-09-22 12:33:46 +00:00
action, _ = user.post(action, res=models.Action)
device, _ = user.get(res=Device, item=snap['device']['devicehubID'])
2021-09-28 09:25:57 +00:00
assert device['actions'][-1]['id'] == action['id']
assert action['author']['id'] == user.user['id']
assert action['rol_user']['id'] == user2.user['id']
# Remove device from lot
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{},
res=Lot,
item='{}/devices'.format(lot['id']),
query=[('id', device['id'])],
status=200,
)
2021-09-28 09:25:57 +00:00
action = {'type': action_model.t, 'devices': [device['id']]}
action, _ = user.post(action, res=models.Action)
device, _ = user.get(res=Device, item=snap['device']['devicehubID'])
assert device['actions'][-1]['id'] == action['id']
assert action['author']['id'] == user.user['id']
2021-11-11 21:08:28 +00:00
assert action['rol_user']['id'] == user2.user['id']
2021-09-28 09:25:57 +00:00
@pytest.mark.mvp
2022-05-16 07:53:22 +00:00
@pytest.mark.parametrize(
'action_model',
(
pytest.param(ams, id=ams.__class__.__name__)
for ams in [models.Recycling, models.Use, models.Refurbish, models.Management]
),
)
def test_incominglot_status_actions(
action_model: models.Action, user: UserClient, user2: UserClient
):
2021-09-28 09:25:57 +00:00
"""Test of status actions in outgoinlot."""
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device, _ = user.get(res=Device, item=snap['device']['devicehubID'])
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device['id'])]
)
2021-09-28 09:25:57 +00:00
request_post = {
'type': 'Trade',
'devices': [device['id']],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
action = {'type': action_model.t, 'devices': [device['id']]}
action, _ = user.post(action, res=models.Action)
device, _ = user.get(res=Device, item=snap['device']['devicehubID'])
2021-09-22 12:33:46 +00:00
assert device['actions'][-1]['id'] == action['id']
2021-09-28 09:25:57 +00:00
assert action['author']['id'] == user.user['id']
assert action['rol_user']['id'] == user.user['id']
2021-09-22 12:33:46 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_history_status_actions(user: UserClient, user2: UserClient):
"""Test for check the status actions."""
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device = Device.query.filter_by(id=snap['device']['id']).one()
# Case 1
action = {'type': models.Recycling.t, 'devices': [device.id]}
action, _ = user.post(action, res=models.Action)
assert str(device.actions[-1].id) == action['id']
assert action['id'] == str(device.status.id)
assert device.status.t == models.Recycling.t
assert [action['id']] == [str(ac.id) for ac in device.history_status]
2021-11-11 21:08:28 +00:00
# Case 2
action2 = {'type': models.Refurbish.t, 'devices': [device.id]}
action2, _ = user.post(action2, res=models.Action)
assert action2['id'] == str(device.status.id)
assert device.status.t == models.Refurbish.t
assert [action2['id']] == [str(ac.id) for ac in device.history_status]
2021-11-11 21:08:28 +00:00
# Case 3
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device.id)]
)
request_post = {
'type': 'Trade',
'devices': [device.id],
'userFromEmail': user.email,
'userToEmail': user2.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
action3 = {'type': models.Use.t, 'devices': [device.id]}
action3, _ = user.post(action3, res=models.Action)
assert action3['id'] == str(device.status.id)
assert device.status.t == models.Use.t
2022-05-16 07:53:22 +00:00
assert [action2['id'], action3['id']] == [
str(ac.id) for ac in device.history_status
]
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_use_changing_owner(user: UserClient, user2: UserClient):
"""Check if is it possible to do a use action for one device
2022-05-16 07:53:22 +00:00
when you are not the owner.
"""
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device = Device.query.filter_by(id=snap['device']['id']).one()
assert device.owner.email == user.email
# Trade
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device.id)]
)
request_post = {
'type': 'Trade',
'devices': [device.id],
'userFromEmail': user.email,
'userToEmail': user2.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
# Doble confirmation and change of owner
2022-05-16 07:53:22 +00:00
request_confirm = {'type': 'Confirm', 'action': trade.id, 'devices': [device.id]}
user2.post(res=models.Action, data=request_confirm)
assert device.owner.email == user2.email
# Adding action Use
action3 = {'type': models.Use.t, 'devices': [device.id]}
action3, _ = user.post(action3, res=models.Action)
assert action3['id'] == str(device.status.id)
assert device.status.t == models.Use.t
assert device.owner.email == user2.email
2021-09-29 19:12:46 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_recycling_container(user: UserClient):
2021-10-18 09:20:05 +00:00
"""Test of status action recycling for a container."""
2021-09-29 19:12:46 +00:00
lot, _ = user.post({'name': 'MyLotOut'}, res=Lot)
2022-05-16 07:53:22 +00:00
url = ('http://www.ereuse.org/',)
2021-09-29 19:12:46 +00:00
request_post = {
'filename': 'test.pdf',
'hash': 'bbbbbbbb',
'url': url,
'weight': 150,
2022-05-16 07:53:22 +00:00
'lot': lot['id'],
2021-09-29 19:12:46 +00:00
}
tradedocument, _ = user.post(res=TradeDocument, data=request_post)
2022-05-16 07:53:22 +00:00
action = {
'type': models.Recycling.t,
'devices': [],
'documents': [tradedocument['id']],
}
2021-09-29 19:12:46 +00:00
action, _ = user.post(action, res=models.Action)
trade = TradeDocument.query.one()
assert str(trade.actions[0].id) == action['id']
2021-09-22 12:33:46 +00:00
@pytest.mark.mvp
2022-09-01 09:10:37 +00:00
@pytest.mark.usefixtures(conftest.app_context.__name__)
2022-05-16 07:53:22 +00:00
@pytest.mark.parametrize(
'action_model',
(
pytest.param(ams, id=ams.__class__.__name__)
for ams in [models.Recycling, models.Use, models.Refurbish, models.Management]
),
)
2021-10-18 09:20:05 +00:00
def test_status_without_lot(action_model: models.Action, user: UserClient):
"""Test of status actions for devices without lot."""
2021-09-22 12:33:46 +00:00
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2022-09-01 09:10:37 +00:00
abstract = Device.query.filter_by(id=snap['device']['id']).first()
device_id = abstract.binding.device.id
action = {'type': action_model.t, 'devices': [device_id]}
2021-10-18 09:20:05 +00:00
action, _ = user.post(action, res=models.Action)
2022-09-01 09:10:37 +00:00
device, _ = user.get(res=Device, item=abstract.dhid)
2021-10-18 09:20:05 +00:00
assert device['actions'][-1]['id'] == action['id']
@pytest.mark.mvp
2022-09-01 09:10:37 +00:00
@pytest.mark.usefixtures(conftest.app_context.__name__)
2022-05-16 07:53:22 +00:00
@pytest.mark.parametrize(
'action_model',
(
pytest.param(ams, id=ams.__class__.__name__)
for ams in [models.Recycling, models.Use, models.Refurbish, models.Management]
),
)
def test_status_in_temporary_lot(
action_model: models.Action, user: UserClient, app: Devicehub
):
2021-10-18 09:20:05 +00:00
"""Test of status actions for devices in a temporary lot."""
snap, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2022-09-01 09:10:37 +00:00
abstract = Device.query.filter_by(id=snap['device']['id']).first()
device_id = abstract.binding.device.id
2021-10-18 09:20:05 +00:00
lot, _ = user.post({'name': 'MyLotOut'}, res=Lot)
2022-05-16 07:53:22 +00:00
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device_id)]
)
2021-10-18 09:20:05 +00:00
action = {'type': action_model.t, 'devices': [device_id]}
2021-09-22 12:33:46 +00:00
action, _ = user.post(action, res=models.Action)
2022-09-01 09:10:37 +00:00
device, _ = user.get(res=Device, item=abstract.dhid)
2021-09-22 12:33:46 +00:00
assert device['actions'][-1]['id'] == action['id']
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2020-12-29 16:37:13 +00:00
def test_live(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it."""
acer = file('acer.happy.battery.snapshot')
snapshot, _ = user.post(acer, res=models.Snapshot)
2021-07-02 13:25:19 +00:00
acer = yaml2json('acer.happy.battery.snapshot')
device_id = snapshot['device']['id']
2022-05-16 07:53:22 +00:00
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)
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
snapshot, _ = client.post(acer, res=models.Live)
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 action_live[0].usage_time_hdd == timedelta(hours=hdd_action['lifetime'])
assert action_live[0].usage_time_allocate == timedelta(hours=1000)
assert action_live[0].final_user_code == post_request['finalUserCode']
assert action_live[0].serial_number == 'wd-wx11a80w7430'
2020-12-28 14:31:57 +00:00
assert action_live[0].licence_version == '1.0'
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
@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']
2022-05-16 07:53:22 +00:00
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",
2021-01-04 14:53:58 +00:00
}
user.post(res=models.Allocate, data=post_request)
2021-07-02 13:25:19 +00:00
acer = yaml2json('live')
2021-01-04 14:53:58 +00:00
live, _ = client.post(acer, res=models.Live)
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)
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer2 = yaml2json('snapshotLive')
acer2['uuid'] = '3b6a9288-0ba6-4bdd-862a-2b1f660e7115'
2021-07-02 13:25:19 +00:00
snapshot2, _ = user2.post(json_encode(acer2), res=models.Snapshot)
device_id = snapshot['device']['id']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('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
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer2 = yaml2json('snapshotLive')
acer2['uuid'] = '3b6a9288-0ba6-4bdd-862a-2b1f660e7115'
2021-07-02 13:25:19 +00:00
snapshot2, _ = user2.post(json_encode(acer2), res=models.Snapshot)
device_id = snapshot['device']['id']
device_id2 = snapshot2['device']['id']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('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)
@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):
"""Tests inserting a Live into the database and GETting it.
2022-05-16 07:53:22 +00:00
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']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('acer.happy.battery.snapshot')
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
2022-05-16 07:53:22 +00:00
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)
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)
@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):
"""Tests inserting a Live into the database and GETting it.
2022-05-16 07:53:22 +00:00
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']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('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
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)
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)
@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):
"""Tests inserting a Live into the database and GETting it.
2022-05-16 07:53:22 +00:00
The snapshot haven't hdd and the live neither, and response 404
"""
2021-07-02 13:25:19 +00:00
acer = yaml2json('acer.happy.battery.snapshot')
components = [a for a in acer['components'] if a['type'] != 'HardDrive']
acer['components'] = components
2021-07-02 13:25:19 +00:00
snapshot, _ = user.post(json_encode(acer), res=models.Snapshot)
device_id = snapshot['device']['id']
2022-05-16 07:53:22 +00:00
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)
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)
@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):
"""Tests inserting a Live into the database and GETting it.
2022-05-16 07:53:22 +00:00
The snapshot haven't hdd and the live have, and save the live
with usage_time_allocate == 0
"""
2021-07-02 13:25:19 +00:00
acer = yaml2json('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
2021-07-05 13:04:44 +00:00
snapshot, _ = user.post(json_encode(acer), res=models.Snapshot)
device_id = snapshot['device']['id']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('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)
assert live['severity'] == 'Warning'
2020-12-05 19:22:38 +00:00
description = "Don't exist one previous live or snapshot as reference"
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)
@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):
"""Tests inserting a Live into the database and GETting it.
2022-05-16 07:53:22 +00:00
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']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('acer.happy.battery.snapshot')
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
2022-05-16 07:53:22 +00:00
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)
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)
@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):
2022-05-16 07:53:22 +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']
2022-05-16 07:53:22 +00:00
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)
2021-07-02 13:25:19 +00:00
acer = yaml2json('acer.happy.battery.snapshot')
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)
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec4"
2022-05-16 07:53:22 +00:00
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)
assert live['usageTimeAllocate'] == 1000
2021-02-04 11:51:51 +00:00
tmp_snapshots = app.config['TMP_LIVES']
shutil.rmtree(tmp_snapshots)
2020-12-29 16:37:13 +00:00
@pytest.mark.mvp
def test_save_live_json(app: Devicehub, user: UserClient, client: Client):
2022-05-16 07:53:22 +00:00
"""This test check if works the function save_snapshot_in_file"""
2021-07-02 13:25:19 +00:00
acer = yaml2json('acer.happy.battery.snapshot')
snapshot, _ = user.post(json_encode(acer), res=models.Snapshot)
2020-12-29 16:37:13 +00:00
debug = 'AAA'
acer['debug'] = debug
device_id = snapshot['device']['id']
2022-05-16 07:53:22 +00:00
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",
2020-12-29 16:37:13 +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
2021-03-24 13:17:18 +00:00
2020-12-29 16:37:13 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_licences(client: Client):
2022-05-16 07:53:22 +00:00
"""Tests inserting a Live into the database and GETting it."""
2020-12-29 16:37:13 +00:00
licences, _ = client.get('/licences/')
licences = json.loads(licences)
assert licences[0]['USOdyPrivacyPolicyVersion'] == '1.0.0'
2020-12-29 16:37:13 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_allocate(user: UserClient):
2022-05-16 07:53:22 +00:00
"""Tests allocate"""
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2022-09-01 09:10:37 +00:00
abstract = Device.query.filter_by(id=snapshot['device']['id']).one()
device_id = abstract.binding.device.id
devicehub_id = abstract.dhid
2022-05-16 07:53:22 +00:00
post_request = {
"transaction": "ccc",
"finalUserCode": "aabbcc",
"name": "John",
"severity": "Info",
"endUsers": 1,
"devices": [device_id],
"description": "aaa",
"startTime": "2020-11-01T02:00:00+00:00",
"endTime": "2020-12-01T02:00:00+00:00",
}
allocate, _ = user.post(res=models.Allocate, data=post_request)
# Normal allocate
2021-03-08 21:43:24 +00:00
device, _ = user.get(res=Device, item=devicehub_id)
assert device['allocated'] is 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']
assert action['created'] == allocate['created']
2020-11-27 18:14:29 +00:00
assert action['startTime'] == allocate['startTime']
assert action['endUsers'] == allocate['endUsers']
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
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"
post_bad_request3 = copy.copy(post_request)
2020-11-26 17:44:08 +00:00
post_bad_request3['transaction'] = "aaa"
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):
2022-05-16 07:53:22 +00:00
"""Tests allocate"""
2020-11-26 17:44:08 +00:00
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
2022-05-16 07:53:22 +00:00
post_request = {
"transaction": "ccc",
"finalUserCode": "aabbcc",
"name": "John",
"severity": "Info",
"end_users": 1,
"devices": [device_id],
"description": "aaa",
"start_time": future,
2020-11-26 17:44:08 +00:00
}
res, _ = user.post(res=models.Allocate, data=post_request, status=422)
assert res['code'] == 422
assert res['type'] == 'ValidationError'
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_deallocate(user: UserClient):
2022-05-16 07:53:22 +00:00
"""Tests deallocate"""
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2022-09-01 09:10:37 +00:00
abstract = Device.query.filter_by(id=snapshot['device']['id']).one()
device_id = abstract.binding.device.id
devicehub_id = abstract.dhid
2022-05-16 07:53:22 +00:00
post_deallocate = {
"startTime": "2020-11-01T02:00:00+00:00",
"transaction": "ccc",
"devices": [device_id],
}
res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=422)
assert res['code'] == 422
assert res['type'] == 'ValidationError'
2022-05-16 07:53:22 +00:00
post_allocate = {
"transaction": "ccc",
"name": "John",
"endUsers": 1,
"devices": [device_id],
"description": "aaa",
"startTime": "2020-11-01T02:00:00+00:00",
"endTime": "2020-12-01T02:00:00+00:00",
}
user.post(res=models.Allocate, data=post_allocate)
2021-03-08 21:43:24 +00:00
device, _ = user.get(res=Device, item=devicehub_id)
assert device['allocated'] is True
deallocate, _ = user.post(res=models.Deallocate, data=post_deallocate)
2020-11-27 18:14:29 +00:00
assert deallocate['startTime'] == post_deallocate['startTime']
assert deallocate['devices'][0]['id'] == device_id
assert deallocate['devices'][0]['allocated'] is 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):
2022-05-16 07:53:22 +00:00
"""Tests deallocate with bad date of start_time"""
2020-11-26 17:44:08 +00:00
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id']
2022-05-16 07:53:22 +00:00
delay = timedelta(days=30)
2020-12-09 11:05:22 +00:00
future = datetime.now().replace(tzinfo=tzutc()) + delay
2022-05-16 07:53:22 +00:00
post_deallocate = {"startTime": future, "devices": [device_id]}
post_allocate = {
"devices": [device_id],
"description": "aaa",
"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'
2021-03-17 17:17:43 +00:00
@pytest.mark.mvp
2021-04-03 15:46:40 +00:00
@pytest.mark.xfail(reason='Old functionality')
2021-03-24 13:17:18 +00:00
def test_trade_endpoint(user: UserClient, user2: UserClient):
2021-03-17 17:17:43 +00:00
"""Tests POST one simple Trade between 2 users of the system."""
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device, _ = user.get(res=Device, item=snapshot['device']['id'])
assert device['id'] == snapshot['device']['id']
request_post = {
2021-03-18 09:59:09 +00:00
'userTo': user2.user['email'],
2021-03-17 17:17:43 +00:00
'price': 1.0,
'date': "2020-12-01T02:00:00+00:00",
2022-05-16 07:53:22 +00:00
'devices': [snapshot['device']['id']],
2021-03-17 17:17:43 +00:00
}
2021-03-18 09:59:09 +00:00
action, _ = user.post(res=models.Trade, data=request_post)
2021-03-17 17:17:43 +00:00
with raises(JSONDecodeError):
device1, _ = user.get(res=Device, item=device['id'])
device2, _ = user2.get(res=Device, item=device['id'])
assert device2['id'] == device['id']
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2021-10-01 11:18:56 +00:00
def test_trade_without_device(user: UserClient, user2: UserClient):
"""Test one offer with automatic confirmation and without user to"""
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
request_post = {
'type': 'Trade',
'userFromEmail': user.email,
2021-10-01 11:18:56 +00:00
'userToEmail': user2.email,
'lot': lot['id'],
2021-10-01 11:18:56 +00:00
'confirms': True,
}
2021-10-01 11:18:56 +00:00
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
assert str(trade.lot.id) == lot['id']
2021-03-24 13:17:18 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2021-04-07 08:49:05 +00:00
def test_offer_without_to(user: UserClient):
2021-05-10 09:53:31 +00:00
"""Test one offer with automatic confirmation and without user to"""
2021-04-07 08:49:05 +00:00
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device = Device.query.filter_by(id=snapshot['device']['id']).one()
2021-05-10 09:53:31 +00:00
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device.id)]
)
2021-04-08 17:10:08 +00:00
# check the owner of the device
assert device.owner.email == user.email
for c in device.components:
assert c.owner.email == user.email
2021-04-08 17:10:08 +00:00
2021-04-07 08:49:05 +00:00
request_post = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-05-10 09:53:31 +00:00
'devices': [device.id],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
2021-04-07 08:49:05 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
2021-05-10 09:53:31 +00:00
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': False,
2022-05-16 07:53:22 +00:00
'code': 'MAX',
2021-04-07 08:49:05 +00:00
}
user.post(res=models.Action, data=request_post)
2021-04-19 19:31:22 +00:00
trade = models.Trade.query.one()
2021-04-08 17:10:08 +00:00
assert device in trade.devices
2021-04-19 17:33:55 +00:00
# assert trade.confirm_transfer
users = [ac.user for ac in trade.acceptances]
assert trade.user_to == device.owner
2021-04-08 17:10:08 +00:00
assert request_post['code'].lower() in device.owner.email
assert device.owner.active is False
assert device.owner.phantom is True
2021-04-19 17:33:55 +00:00
assert trade.user_to in users
assert trade.user_from in users
2021-04-08 17:10:08 +00:00
assert device.owner.email != user.email
for c in device.components:
assert c.owner.email != user.email
# check if the user_from is owner of the devices
request_post = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-05-10 09:53:31 +00:00
'devices': [device.id],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
2021-05-10 09:53:31 +00:00
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': False,
2022-05-16 07:53:22 +00:00
'code': 'MAX',
}
user.post(res=models.Action, data=request_post, status=422)
2021-04-19 19:31:22 +00:00
trade = models.Trade.query.one()
# Check if the new phantom account is reused and not duplicated
computer = file('1-device-with-components.snapshot')
snapshot2, _ = user.post(computer, res=models.Snapshot)
2021-04-14 11:02:39 +00:00
device2 = Device.query.filter_by(id=snapshot2['device']['id']).one()
lot2 = Lot('MyLot2')
lot2.owner_id = user.user['id']
2021-04-14 11:02:39 +00:00
lot2.devices.add(device2)
db.session.add(lot2)
db.session.flush()
request_post2 = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-05-10 09:53:31 +00:00
'devices': [device2.id],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot2.id,
2021-05-27 13:55:39 +00:00
'confirms': False,
2022-05-16 07:53:22 +00:00
'code': 'MAX',
}
user.post(res=models.Action, data=request_post2)
assert User.query.filter_by(email=device.owner.email).count() == 1
2021-04-08 17:10:08 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2021-04-19 17:33:55 +00:00
def test_offer_without_from(user: UserClient, user2: UserClient):
2021-04-08 17:10:08 +00:00
"""Test one offer without confirmation and without user from"""
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
lot = Lot('MyLot')
lot.owner_id = user.user['id']
device = Device.query.filter_by(id=snapshot['device']['id']).one()
# check the owner of the device
assert device.owner.email == user.email
assert device.owner.email != user2.email
lot.devices.add(device)
db.session.add(lot)
db.session.flush()
request_post = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-05-10 09:53:31 +00:00
'devices': [device.id],
2021-06-01 13:59:54 +00:00
'userToEmail': user2.email,
2021-04-08 17:10:08 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot.id,
2021-05-27 13:55:39 +00:00
'confirms': False,
2022-05-16 07:53:22 +00:00
'code': 'MAX',
2021-04-08 17:10:08 +00:00
}
2021-05-10 09:53:31 +00:00
action, _ = user2.post(res=models.Action, data=request_post, status=422)
2021-06-01 13:59:54 +00:00
request_post['userToEmail'] = user.email
2021-05-10 09:53:31 +00:00
action, _ = user.post(res=models.Action, data=request_post)
2021-04-19 17:33:55 +00:00
trade = models.Trade.query.one()
2021-04-08 17:10:08 +00:00
2021-04-19 17:33:55 +00:00
phantom_user = trade.user_from
2021-04-08 17:10:08 +00:00
assert request_post['code'].lower() in phantom_user.email
assert phantom_user.active is False
assert phantom_user.phantom is True
2021-04-19 17:33:55 +00:00
# assert trade.confirm_transfer
2021-04-08 17:10:08 +00:00
2021-04-19 17:33:55 +00:00
users = [ac.user for ac in trade.acceptances]
assert trade.user_to in users
assert trade.user_from in users
2021-05-10 09:53:31 +00:00
assert user.email in trade.devices[0].owner.email
assert device.owner.email != user2.email
assert device.owner.email == user.email
2021-04-08 17:10:08 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_offer_without_users(user: UserClient):
"""Test one offer with doble confirmation"""
user2 = User(email='baz@baz.cxm', password='baz')
user2.individuals.add(Person(name='Tommy'))
db.session.add(user2)
db.session.commit()
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
lot = Lot('MyLot')
lot.owner_id = user.user['id']
device = Device.query.filter_by(id=snapshot['device']['id']).one()
lot.devices.add(device)
db.session.add(lot)
db.session.flush()
request_post = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-04-08 17:10:08 +00:00
'devices': [device.id],
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot.id,
2021-05-27 13:55:39 +00:00
'confirms': False,
2022-05-16 07:53:22 +00:00
'code': 'MAX',
2021-04-08 17:10:08 +00:00
}
action, response = user.post(res=models.Action, data=request_post, status=422)
2021-06-09 09:27:12 +00:00
txt = 'you need one user from or user to for to do a trade'
2021-04-08 17:10:08 +00:00
assert txt in action['message']['_schema']
2021-04-07 08:49:05 +00:00
2021-03-24 13:17:18 +00:00
2021-04-07 08:49:05 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_offer(user: UserClient):
"""Test one offer with doble confirmation"""
2021-03-24 13:17:18 +00:00
user2 = User(email='baz@baz.cxm', password='baz')
user2.individuals.add(Person(name='Tommy'))
db.session.add(user2)
db.session.commit()
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
lot = Lot('MyLot')
lot.owner_id = user.user['id']
device = Device.query.filter_by(id=snapshot['device']['id']).one()
2021-04-08 17:10:08 +00:00
assert device.owner.email == user.email
assert device.owner.email != user2.email
2021-03-24 13:17:18 +00:00
lot.devices.add(device)
db.session.add(lot)
db.session.flush()
request_post = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-04-14 11:02:39 +00:00
'devices': [],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
'userToEmail': user2.email,
2021-04-03 15:46:40 +00:00
'price': 10,
2021-03-24 13:17:18 +00:00
'date': "2020-12-01T02:00:00+00:00",
2021-04-07 08:49:05 +00:00
'lot': lot.id,
2021-05-27 13:55:39 +00:00
'confirms': True,
2021-03-24 13:17:18 +00:00
}
2021-04-08 17:10:08 +00:00
2021-03-24 13:17:18 +00:00
action, _ = user.post(res=models.Action, data=request_post)
2021-04-09 18:11:43 +00:00
# no there are transfer of devices
2021-04-08 17:10:08 +00:00
assert device.owner.email == user.email
assert device.owner.email != user2.email
2021-03-24 13:17:18 +00:00
2021-04-19 19:31:22 +00:00
2021-04-14 11:02:39 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_offer_without_devices(user: UserClient):
"""Test one offer with doble confirmation"""
user2 = User(email='baz@baz.cxm', password='baz')
user2.individuals.add(Person(name='Tommy'))
db.session.add(user2)
db.session.commit()
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
request_post = {
2021-04-19 17:33:55 +00:00
'type': 'Trade',
2021-04-14 11:02:39 +00:00
'devices': [],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
'userToEmail': user2.email,
2021-04-14 11:02:39 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': True,
2021-04-14 11:02:39 +00:00
}
user.post(res=models.Action, data=request_post)
# no there are transfer of devices
2021-03-17 17:17:43 +00:00
2021-04-19 19:31:22 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_price_custom():
2022-05-16 07:53:22 +00:00
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)
price.device = computer
assert computer.price == price
db.session.add(computer)
db.session.commit()
client = UserClient(app, 'foo@foo.com', 'foo', response_wrapper=app.response_class)
client.login()
p, _ = client.get(res=models.Action, item=str(price.id))
assert p['device']['id'] == price.device.id == computer.id
assert p['price'] == 25.25
assert p['currency'] == Currency.EUR.name == 'EUR'
2021-03-08 21:43:24 +00:00
c, _ = client.get(res=Device, item=computer.devicehub_id)
assert c['price']['id'] == p['id']
2018-10-15 09:21:21 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_erase_physical():
erasure = models.ErasePhysical(
device=HardDrive(serial_number='foo', manufacturer='bar', model='foo-bar'),
2022-05-16 07:53:22 +00:00
method=enums.PhysicalErasureMethod.Disintegration,
)
db.session.add(erasure)
db.session.commit()
2021-04-21 12:44:34 +00:00
2022-05-16 07:53:22 +00:00
2021-04-22 09:10:49 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_endpoint_confirm(user: UserClient, user2: UserClient):
"""Check the normal creation and visualization of one confirmation trade"""
2021-05-10 09:53:31 +00:00
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id']
2021-04-22 09:10:49 +00:00
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device_id)]
)
2021-05-10 09:53:31 +00:00
2021-04-22 09:10:49 +00:00
request_post = {
'type': 'Trade',
2021-05-10 09:53:31 +00:00
'devices': [device_id],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
'userToEmail': user2.email,
2021-04-22 09:10:49 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': True,
2021-04-22 09:10:49 +00:00
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2021-05-10 09:53:31 +00:00
assert trade.devices[0].owner.email == user.email
2022-05-16 07:53:22 +00:00
request_confirm = {'type': 'Confirm', 'action': trade.id, 'devices': [device_id]}
2021-04-22 09:10:49 +00:00
user2.post(res=models.Action, data=request_confirm)
user2.post(res=models.Action, data=request_confirm, status=422)
assert len(trade.acceptances) == 2
2021-05-10 09:53:31 +00:00
assert trade.devices[0].owner.email == user2.email
2021-04-22 09:10:49 +00:00
2021-04-30 15:54:03 +00:00
2021-04-22 09:10:49 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_confirm_revoke(user: UserClient, user2: UserClient):
2021-05-10 09:53:31 +00:00
"""Check the normal revoke of one confirmation"""
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id']
2021-04-22 09:10:49 +00:00
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
2022-05-16 07:53:22 +00:00
user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=[('id', device_id)]
)
2021-05-10 09:53:31 +00:00
2021-04-22 09:10:49 +00:00
request_post = {
'type': 'Trade',
2021-05-10 09:53:31 +00:00
'devices': [device_id],
2021-06-01 13:59:54 +00:00
'userFromEmail': user.email,
'userToEmail': user2.email,
2021-04-22 09:10:49 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': True,
2021-04-22 09:10:49 +00:00
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2021-11-12 16:46:39 +00:00
device = trade.devices[0]
2021-04-22 09:10:49 +00:00
2022-05-16 07:53:22 +00:00
request_confirm = {'type': 'Confirm', 'action': trade.id, 'devices': [device_id]}
2021-04-22 09:10:49 +00:00
request_revoke = {
2021-04-30 15:54:03 +00:00
'type': 'Revoke',
'action': trade.id,
2021-05-10 09:53:31 +00:00
'devices': [device_id],
2021-04-22 09:10:49 +00:00
}
2021-04-30 15:54:03 +00:00
# Normal confirmation
user2.post(res=models.Action, data=request_confirm)
# Normal revoke
user2.post(res=models.Action, data=request_revoke)
2021-04-30 15:54:03 +00:00
2021-11-12 16:46:39 +00:00
# You can to do one confirmation next of one revoke
user2.post(res=models.Action, data=request_confirm)
assert len(trade.acceptances) == 4
assert device.trading(trade.lot) == "TradeConfirmed"
2021-05-10 09:53:31 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_usecase_confirmation(user: UserClient, user2: UserClient):
"""Example of one usecase about confirmation"""
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
snap3, _ = user.post(file('asus-1001pxd.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
snap4, _ = user.post(
file('desktop-9644w8n-lenovo-0169622.snapshot'), res=models.Snapshot
)
snap5, _ = user.post(
file('laptop-hp_255_g3_notebook-hewlett-packard-cnd52270fw.snapshot'),
res=models.Snapshot,
)
2021-05-10 09:53:31 +00:00
snap6, _ = user.post(file('1-device-with-components.snapshot'), res=models.Snapshot)
snap7, _ = user.post(file('asus-eee-1000h.snapshot.11'), res=models.Snapshot)
snap8, _ = user.post(file('complete.export.snapshot'), res=models.Snapshot)
snap9, _ = user.post(file('real-hp-quad-core.snapshot.11'), res=models.Snapshot)
snap10, _ = user.post(file('david.lshw.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
('id', snap3['device']['id']),
('id', snap4['device']['id']),
('id', snap5['device']['id']),
('id', snap6['device']['id']),
('id', snap7['device']['id']),
('id', snap8['device']['id']),
('id', snap9['device']['id']),
('id', snap10['device']['id']),
]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:7]
)
2021-05-10 09:53:31 +00:00
2021-11-12 16:46:39 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-05-25 09:35:03 +00:00
# for the SCRAP to confirm it
2021-05-10 09:53:31 +00:00
request_post = {
'type': 'Trade',
'devices': [],
2021-06-01 13:59:54 +00:00
'userFromEmail': user2.email,
'userToEmail': user.email,
2021-05-10 09:53:31 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': True,
2021-05-10 09:53:31 +00:00
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2021-06-01 13:59:54 +00:00
# l_after, _ = user.get(res=Lot, item=lot['id'])
2021-05-10 09:53:31 +00:00
# the SCRAP confirms 3 of the 10 devices in its outgoing lot
request_confirm = {
'type': 'Confirm',
'action': trade.id,
2022-05-16 07:53:22 +00:00
'devices': [
snap1['device']['id'],
snap2['device']['id'],
snap3['device']['id'],
],
2021-05-10 09:53:31 +00:00
}
assert trade.devices[0].actions[-2].t == 'Trade'
assert trade.devices[0].actions[-1].t == 'Confirm'
assert trade.devices[0].actions[-1].user == trade.user_to
user2.post(res=models.Action, data=request_confirm)
2021-05-10 09:53:31 +00:00
assert trade.devices[0].actions[-1].t == 'Confirm'
assert trade.devices[0].actions[-1].user == trade.user_from
n_actions = len(trade.devices[0].actions)
# check validation error
request_confirm = {
'type': 'Confirm',
'action': trade.id,
2022-05-16 07:53:22 +00:00
'devices': [snap10['device']['id']],
2021-05-10 09:53:31 +00:00
}
user2.post(res=models.Action, data=request_confirm, status=422)
# The manager add 3 device more into the lot
2022-05-16 07:53:22 +00:00
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[7:]
)
2021-05-10 09:53:31 +00:00
assert trade.devices[-1].actions[-2].t == 'Trade'
assert trade.devices[-1].actions[-1].t == 'Confirm'
assert trade.devices[-1].actions[-1].user == trade.user_to
assert len(trade.devices[0].actions) == n_actions
# the SCRAP confirms the rest of devices
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [
snap4['device']['id'],
snap5['device']['id'],
snap6['device']['id'],
snap7['device']['id'],
snap8['device']['id'],
snap9['device']['id'],
2022-05-16 07:53:22 +00:00
snap10['device']['id'],
],
2021-05-10 09:53:31 +00:00
}
user2.post(res=models.Action, data=request_confirm)
assert trade.devices[-1].actions[-3].t == 'Trade'
assert trade.devices[-1].actions[-1].t == 'Confirm'
assert trade.devices[-1].actions[-1].user == trade.user_from
assert len(trade.devices[0].actions) == n_actions
2021-11-10 17:59:44 +00:00
# The manager remove one device of the lot and automaticaly
2021-05-10 09:53:31 +00:00
# is create one revoke action
device_10 = trade.devices[-1]
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-06-09 09:27:12 +00:00
assert len(trade.lot.devices) == len(trade.devices) == 10
2021-05-10 09:53:31 +00:00
assert device_10.actions[-1].t == 'Revoke'
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-05-10 09:53:31 +00:00
assert device_10.actions[-1].t == 'Revoke'
# the SCRAP confirms the revoke action
request_confirm_revoke = {
2021-11-10 17:59:44 +00:00
'type': 'Revoke',
'action': trade.id,
2022-05-16 07:53:22 +00:00
'devices': [snap10['device']['id']],
2021-05-10 09:53:31 +00:00
}
user2.post(res=models.Action, data=request_confirm_revoke)
2021-11-10 17:59:44 +00:00
assert device_10.actions[-1].t == 'Revoke'
2021-05-10 09:53:31 +00:00
assert device_10.actions[-2].t == 'Revoke'
2021-06-09 09:27:12 +00:00
# assert len(trade.lot.devices) == len(trade.devices) == 9
# assert not device_10 in trade.devices
2021-05-10 09:53:31 +00:00
2021-05-25 09:35:03 +00:00
# check validation error
2021-05-10 09:53:31 +00:00
request_confirm_revoke = {
2021-11-10 17:59:44 +00:00
'type': 'Revoke',
'action': trade.id,
2022-05-16 07:53:22 +00:00
'devices': [snap9['device']['id']],
2021-05-10 09:53:31 +00:00
}
# The manager add again device_10
2021-06-09 09:27:12 +00:00
# assert len(trade.devices) == 9
2022-05-16 07:53:22 +00:00
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-05-10 09:53:31 +00:00
assert device_10.actions[-1].t == 'Confirm'
assert device_10 in trade.devices
assert len(trade.devices) == 10
# the SCRAP confirms the action trade for device_10
request_reconfirm = {
'type': 'Confirm',
'action': trade.id,
2022-05-16 07:53:22 +00:00
'devices': [snap10['device']['id']],
2021-05-10 09:53:31 +00:00
}
user2.post(res=models.Action, data=request_reconfirm)
assert device_10.actions[-1].t == 'Confirm'
assert device_10.actions[-1].user == trade.user_from
assert device_10.actions[-2].t == 'Confirm'
assert device_10.actions[-2].user == trade.user_to
2021-11-10 17:59:44 +00:00
assert device_10.actions[-3].t == 'Revoke'
2021-06-09 09:27:12 +00:00
# assert len(device_10.actions) == 13
2021-05-10 09:53:31 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_confirmRevoke(user: UserClient, user2: UserClient):
"""Example of one usecase about confirmation"""
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
snap3, _ = user.post(file('asus-1001pxd.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
snap4, _ = user.post(
file('desktop-9644w8n-lenovo-0169622.snapshot'), res=models.Snapshot
)
snap5, _ = user.post(
file('laptop-hp_255_g3_notebook-hewlett-packard-cnd52270fw.snapshot'),
res=models.Snapshot,
)
2021-05-10 09:53:31 +00:00
snap6, _ = user.post(file('1-device-with-components.snapshot'), res=models.Snapshot)
snap7, _ = user.post(file('asus-eee-1000h.snapshot.11'), res=models.Snapshot)
snap8, _ = user.post(file('complete.export.snapshot'), res=models.Snapshot)
snap9, _ = user.post(file('real-hp-quad-core.snapshot.11'), res=models.Snapshot)
snap10, _ = user.post(file('david.lshw.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
('id', snap3['device']['id']),
('id', snap4['device']['id']),
('id', snap5['device']['id']),
('id', snap6['device']['id']),
('id', snap7['device']['id']),
('id', snap8['device']['id']),
('id', snap9['device']['id']),
('id', snap10['device']['id']),
]
lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices)
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-05-10 09:53:31 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
2021-06-01 13:59:54 +00:00
'userFromEmail': user2.email,
'userToEmail': user.email,
2021-05-10 09:53:31 +00:00
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
2021-05-27 13:55:39 +00:00
'confirms': True,
2021-05-10 09:53:31 +00:00
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
# the SCRAP confirms all of devices
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [
2022-05-16 07:53:22 +00:00
snap1['device']['id'],
snap2['device']['id'],
2021-05-10 09:53:31 +00:00
snap3['device']['id'],
snap4['device']['id'],
snap5['device']['id'],
snap6['device']['id'],
snap7['device']['id'],
snap8['device']['id'],
snap9['device']['id'],
2022-05-16 07:53:22 +00:00
snap10['device']['id'],
],
2021-05-10 09:53:31 +00:00
}
user2.post(res=models.Action, data=request_confirm)
assert trade.devices[-1].actions[-3].t == 'Trade'
assert trade.devices[-1].actions[-1].t == 'Confirm'
assert trade.devices[-1].actions[-1].user == trade.user_from
2022-05-16 07:53:22 +00:00
# The manager remove one device of the lot and automaticaly
2021-05-10 09:53:31 +00:00
# is create one revoke action
device_10 = trade.devices[-1]
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-06-09 09:27:12 +00:00
# assert len(trade.lot.devices) == len(trade.devices) == 9
# assert not device_10 in trade.devices
2021-05-10 09:53:31 +00:00
assert device_10.actions[-1].t == 'Revoke'
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-05-10 09:53:31 +00:00
assert device_10.actions[-1].t == 'Revoke'
2021-06-09 09:27:12 +00:00
# assert device_10.actions[-2].t == 'Confirm'
2021-05-10 09:53:31 +00:00
# The manager add again device_10
2021-06-09 09:27:12 +00:00
# assert len(trade.devices) == 9
2022-05-16 07:53:22 +00:00
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-05-10 09:53:31 +00:00
2021-06-09 09:27:12 +00:00
# assert device_10.actions[-1].t == 'Confirm'
2021-05-10 09:53:31 +00:00
assert device_10 in trade.devices
assert len(trade.devices) == 10
2022-09-26 15:18:10 +00:00
# TODO??? the SCRAP confirms the revoke action
# request_confirm_revoke = {
# 'type': 'ConfirmRevoke',
# 'action': device_10.actions[-2].id,
# 'devices': [snap10['device']['id']],
# }
2021-05-10 09:53:31 +00:00
# check validation error
2021-06-09 09:27:12 +00:00
# user2.post(res=models.Action, data=request_confirm_revoke, status=422)
2021-05-10 09:53:31 +00:00
# the SCRAP confirms the action trade for device_10
2021-06-09 09:27:12 +00:00
# request_reconfirm = {
2022-05-16 07:53:22 +00:00
# 'type': 'Confirm',
# 'action': trade.id,
# 'devices': [
# snap10['device']['id']
# ]
2021-06-09 09:27:12 +00:00
# }
# user2.post(res=models.Action, data=request_reconfirm)
# assert device_10.actions[-1].t == 'Confirm'
# assert device_10.actions[-1].user == trade.user_from
# assert device_10.actions[-2].t == 'Confirm'
# assert device_10.actions[-2].user == trade.user_to
# assert device_10.actions[-3].t == 'Revoke'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case1(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2021-11-11 21:08:28 +00:00
lot = trade.lot
device = trade.devices[0]
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device.actions[-2].t == 'Trade'
assert device.actions[-1].t == 'Confirm'
assert device.actions[-1].user == trade.user_to
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
user.delete(
{}, res=Lot, item='{}/devices'.format(lot.id), query=devices[:-1], status=200
)
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device not in trade.lot.devices
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-11-11 21:08:28 +00:00
assert device.actions[-2].t == 'Confirm'
assert device.actions[-1].t == 'Revoke'
assert device.actions[-1].user == trade.user_to
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case2(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device2 = trade.devices
assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to
assert device2.actions[-2].t == 'Trade'
assert device2.actions[-1].t == 'Confirm'
assert device2.actions[-1].user == trade.user_to
request_revoke = {
'type': 'Revoke',
2021-05-10 09:53:31 +00:00
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device1.id, device2.id],
2021-05-10 09:53:31 +00:00
}
2021-06-11 10:25:58 +00:00
# Normal revoke
user.post(res=models.Action, data=request_revoke)
2021-11-11 21:08:28 +00:00
assert device1.actions[-2].t == 'Confirm'
assert device1.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device1.actions[-1].user == trade.user_to
2021-11-11 21:08:28 +00:00
assert device2.actions[-2].t == 'Confirm'
assert device2.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device2.actions[-1].user == trade.user_to
2021-11-12 16:46:39 +00:00
assert device1.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case3(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device2 = trade.devices
assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to
assert device2.actions[-2].t == 'Trade'
assert device2.actions[-1].t == 'Confirm'
assert device2.actions[-1].user == trade.user_from
2022-05-16 07:53:22 +00:00
lot, _ = user2.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device2.actions[-2].t == 'Confirm'
assert device2.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device2.actions[-1].user == trade.user_from
2021-11-12 16:46:39 +00:00
assert device2.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case4(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2021-06-11 13:49:31 +00:00
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
]
lot1, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot2, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device2 = trade.devices
assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to
assert device2.actions[-2].t == 'Trade'
assert device2.actions[-1].t == 'Confirm'
assert device2.actions[-1].user == trade.user_from
request_revoke = {
'type': 'Revoke',
'action': trade.id,
'devices': [device2.id],
}
# Normal revoke
user2.post(res=models.Action, data=request_revoke)
assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to
2021-11-11 21:08:28 +00:00
assert device2.actions[-2].t == 'Confirm'
assert device2.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device2.actions[-1].user == trade.user_from
2021-11-12 16:46:39 +00:00
assert device2.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case5(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2021-06-11 13:49:31 +00:00
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
]
lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
device1, device2 = trade.devices
assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to
assert device2.actions[-2].t == 'Trade'
assert device2.actions[-1].t == 'Confirm'
assert device2.actions[-1].user == trade.user_to
2022-05-16 07:53:22 +00:00
lot, _ = user2.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-06-11 10:25:58 +00:00
assert device2.actions[-2].t == 'Confirm'
assert device2.actions[-1].t == 'Revoke'
assert device2.actions[-1].user == trade.user_from
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device2.id],
}
# Normal revoke
user.post(res=models.Action, data=request_confirm_revoke)
assert device2.actions[-2].t == 'Revoke'
2021-11-11 21:08:28 +00:00
assert device2.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device2.actions[-1].user == trade.user_to
2021-11-12 16:46:39 +00:00
assert device2.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case6(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [
('id', snap1['device']['id']),
('id', snap2['device']['id']),
]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device2 = trade.devices
assert device1.actions[-2].t == 'Trade'
assert device1.actions[-1].t == 'Confirm'
assert device1.actions[-1].user == trade.user_to
assert device2.actions[-2].t == 'Trade'
assert device2.actions[-1].t == 'Confirm'
assert device2.actions[-1].user == trade.user_from
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-06-11 10:25:58 +00:00
assert device2.actions[-2].t == 'Confirm'
assert device2.actions[-1].t == 'Revoke'
assert device2.actions[-1].user == trade.user_to
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device2.id],
}
# Normal revoke
user2.post(res=models.Action, data=request_confirm_revoke)
assert device2.actions[-2].t == 'Revoke'
2021-11-11 21:08:28 +00:00
assert device2.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device2.actions[-1].user == trade.user_from
2021-11-12 16:46:39 +00:00
assert device2.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case7(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
devices = [('id', snap1['device']['id'])]
2022-05-16 07:53:22 +00:00
lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
device = trade.devices[0]
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user2.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices, status=200
)
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user2.post(res=models.Action, data=request_confirm_revoke)
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_from
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_to
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_from
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_to
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case8(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
devices = [('id', snap1['device']['id'])]
2022-05-16 07:53:22 +00:00
lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
device = trade.devices[0]
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user2.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
request_revoke = {
'type': 'Revoke',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user.post(res=models.Action, data=request_revoke)
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user2.post(res=models.Action, data=request_confirm_revoke)
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_from
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_to
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_from
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_to
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case9(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [('id', snap1['device']['id']), ('id', snap2['device']['id'])]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device = trade.devices
assert device.owner == trade.user_from
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
assert device.owner == trade.user_to
2022-05-16 07:53:22 +00:00
lot, _ = user2.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user.post(res=models.Action, data=request_confirm_revoke)
assert device.owner == trade.user_from
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_to
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_from
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_to
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_from
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case10(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [('id', snap1['device']['id']), ('id', snap2['device']['id'])]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device = trade.devices
assert device.owner == trade.user_from
2021-11-12 16:46:39 +00:00
# assert device.trading(trade.lot) == 'Confirm'
2021-06-11 10:25:58 +00:00
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
# Normal confirm
user.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
# assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
assert device.owner == trade.user_to
request_revoke = {
'type': 'Revoke',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user2.post(res=models.Action, data=request_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'Revoke'
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user.post(res=models.Action, data=request_confirm_revoke)
assert device.owner == trade.user_from
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_to
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_from
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_to
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_from
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case11(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2021-06-11 13:49:31 +00:00
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
devices = [('id', snap1['device']['id']), ('id', snap2['device']['id'])]
lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
device1, device = trade.devices
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'Confirm'
2021-06-11 10:25:58 +00:00
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
user2.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
lot, _ = user2.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'Revoke'
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user.post(res=models.Action, data=request_confirm_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_to
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_from
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_from
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_to
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case12(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
2021-06-11 13:49:31 +00:00
snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
devices = [('id', snap1['device']['id']), ('id', snap2['device']['id'])]
lot, _ = user.post({}, res=Lot, item='{}/devices'.format(lot['id']), query=devices)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
device1, device = trade.devices
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'Confirm'
2021-06-11 10:25:58 +00:00
# Normal confirm
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
user2.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
request_revoke = {
'type': 'Revoke',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user2.post(res=models.Action, data=request_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'Revoke'
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user.post(res=models.Action, data=request_confirm_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_to
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_from
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_from
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_to
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case13(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [('id', snap1['device']['id']), ('id', snap2['device']['id'])]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device = trade.devices
2021-11-12 16:46:39 +00:00
assert device1.trading(trade.lot) == 'NeedConfirmation'
assert device.trading(trade.lot) == 'Confirm'
2021-06-11 10:25:58 +00:00
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
user.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device1.trading(trade.lot) == 'Confirm'
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
lot, _ = user.delete(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:], status=200
)
2021-11-12 16:46:39 +00:00
assert device1.trading(trade.lot) == 'Confirm'
assert device.trading(trade.lot) == 'Revoke'
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user2.post(res=models.Action, data=request_confirm_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_from
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_to
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_to
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_from
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_trade_case14(user: UserClient, user2: UserClient):
# the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
# The manager add 7 device into the lot
snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
devices = [('id', snap1['device']['id']), ('id', snap2['device']['id'])]
lot, _ = user.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[:-1]
)
2021-06-11 10:25:58 +00:00
2022-05-16 07:53:22 +00:00
# the manager shares the temporary lot with the SCRAP as an incoming lot
2021-06-11 10:25:58 +00:00
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
2022-05-16 07:53:22 +00:00
lot, _ = user2.post(
{}, res=Lot, item='{}/devices'.format(lot['id']), query=devices[-1:]
)
2021-06-11 10:25:58 +00:00
device1, device = trade.devices
2021-11-12 16:46:39 +00:00
assert device1.trading(trade.lot) == 'NeedConfirmation'
assert device.trading(trade.lot) == 'Confirm'
2021-06-11 10:25:58 +00:00
# Normal confirm
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [device.id],
}
user.post(res=models.Action, data=request_confirm)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'TradeConfirmed'
2021-06-11 10:25:58 +00:00
request_revoke = {
'type': 'Revoke',
'action': trade.id,
'devices': [device.id],
}
# Normal revoke
user.post(res=models.Action, data=request_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'Revoke'
2021-06-11 10:25:58 +00:00
request_confirm_revoke = {
2021-11-11 21:08:28 +00:00
'type': 'Revoke',
'action': trade.id,
2021-06-11 10:25:58 +00:00
'devices': [device.id],
}
user2.post(res=models.Action, data=request_confirm_revoke)
2021-11-12 16:46:39 +00:00
assert device.trading(trade.lot) == 'RevokeConfirmed'
2021-06-11 10:25:58 +00:00
2021-11-11 21:08:28 +00:00
assert device.actions[-1].t == 'Revoke'
2021-06-11 10:25:58 +00:00
assert device.actions[-1].user == trade.user_from
assert device.actions[-2].t == 'Revoke'
assert device.actions[-2].user == trade.user_to
assert device.actions[-3].t == 'Confirm'
assert device.actions[-3].user == trade.user_to
assert device.actions[-4].t == 'Confirm'
assert device.actions[-4].user == trade.user_from
assert device.actions[-5].t == 'Trade'
assert device.actions[-5].author == trade.user_to
2021-07-06 14:03:03 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2021-07-28 13:59:11 +00:00
def test_action_web_erase(user: UserClient, client: Client):
import hashlib
2022-05-16 07:53:22 +00:00
2021-07-28 13:59:11 +00:00
from ereuse_devicehub.resources.documents import documents
2022-05-16 07:53:22 +00:00
2021-07-28 13:59:11 +00:00
bfile = BytesIO(b'abc')
hash3 = hashlib.sha3_256(bfile.read()).hexdigest()
2021-07-26 09:54:05 +00:00
snap, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
request = {
'type': 'DataWipe',
'devices': [snap['device']['id']],
'name': 'borrado universal',
'severity': 'Info',
'description': 'nada que describir',
'url': 'http://www.google.com/',
'documentId': '33',
'endTime': '2021-07-07T22:00:00.000Z',
'filename': 'Certificado de borrado1.pdf',
'hash': hash3,
'success': 1,
'software': "Blanco",
}
2021-11-11 21:08:28 +00:00
2021-07-26 09:54:05 +00:00
user.post(res=models.Action, data=request)
2021-07-29 10:45:43 +00:00
action = models.DataWipe.query.one()
2021-07-26 09:54:05 +00:00
for dev in action.devices:
assert action in dev.actions
2021-07-26 09:55:12 +00:00
assert action.document.file_hash == request['hash']
2021-07-28 13:59:11 +00:00
bfile = BytesIO(b'abc')
2022-05-16 07:53:22 +00:00
response, _ = client.post(
res=documents.DocumentDef.t,
item='stamps/',
content_type='multipart/form-data',
accept='text/html',
data={'docUpload': [(bfile, 'example.csv')]},
status=200,
)
2021-07-28 13:59:11 +00:00
assert "alert alert-info" in response
assert "100% coincidence." in response
2022-09-26 15:19:14 +00:00
assert "alert alert-danger" not in response
2021-09-03 14:05:55 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
2021-09-08 12:17:48 +00:00
def test_moveOnDocument(user: UserClient, user2: UserClient):
2021-09-03 14:05:55 +00:00
lotIn, _ = user.post({'name': 'MyLotIn'}, res=Lot)
lotOut, _ = user.post({'name': 'MyLotOut'}, res=Lot)
2022-05-16 07:53:22 +00:00
url = (
2022-09-26 15:18:10 +00:00
'http://www.ereuse.org/apapaapaapaapaapaapaapaapaapaapapaapaapaapaapaapaapaapaapaapapaapaapaapaapaapaapaapaap',
2022-05-16 07:53:22 +00:00
)
2021-09-03 14:05:55 +00:00
request_post1 = {
'filename': 'test.pdf',
'hash': 'bbbbbbbb',
'url': url,
'weight': 150,
2022-05-16 07:53:22 +00:00
'lot': lotIn['id'],
2021-09-03 14:05:55 +00:00
}
tradedocument_from, _ = user.post(res=TradeDocument, data=request_post1)
2021-09-08 12:17:48 +00:00
id_hash = 'aaaaaaaaa'
2021-09-03 14:05:55 +00:00
request_post2 = {
'filename': 'test.pdf',
2021-09-08 12:17:48 +00:00
'hash': id_hash,
2021-09-03 14:05:55 +00:00
'url': url,
'weight': 0,
2022-05-16 07:53:22 +00:00
'lot': lotOut['id'],
2021-09-03 14:05:55 +00:00
}
tradedocument_to, _ = user.post(res=TradeDocument, data=request_post2)
request_trade = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lotIn['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_trade)
description = 'This is a good description'
2021-09-03 14:05:55 +00:00
request_moveOn = {
2021-09-08 12:17:48 +00:00
'type': 'MoveOnDocument',
2021-09-03 14:05:55 +00:00
'weight': 15,
'container_from': tradedocument_from['id'],
'container_to': id_hash,
2022-05-16 07:53:22 +00:00
'description': description,
2021-09-03 14:05:55 +00:00
}
doc, _ = user.post(res=models.Action, data=request_moveOn)
2021-09-06 10:45:47 +00:00
assert doc['weight'] == request_moveOn['weight']
assert doc['container_from']['id'] == tradedocument_from['id']
assert doc['container_to']['id'] == tradedocument_to['id']
2021-09-08 12:17:48 +00:00
2022-05-16 07:53:22 +00:00
mvs = models.MoveOnDocument.query.filter().first()
trade_from = TradeDocument.query.filter_by(id=tradedocument_from['id']).one()
trade_to = TradeDocument.query.filter_by(id=tradedocument_to['id']).one()
assert trade_from in mvs.documents
assert trade_to in mvs.documents
assert description == mvs.description
2021-09-08 12:17:48 +00:00
tradedocument_to, _ = user.post(res=TradeDocument, data=request_post2)
user.post(res=models.Action, data=request_moveOn, status=422)
2021-10-05 07:56:57 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_delete_devices(user: UserClient):
"""This action deactive one device and simulate than one devices is delete."""
snap, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot)
2022-05-16 07:53:22 +00:00
request = {
'type': 'Delete',
'devices': [snap['device']['id']],
'name': 'borrado universal',
'severity': 'Info',
'description': 'duplicity of devices',
'endTime': '2021-07-07T22:00:00.000Z',
}
2021-11-19 11:44:52 +00:00
2021-10-05 09:55:43 +00:00
action, _ = user.post(res=models.Action, data=request)
2021-10-05 12:33:28 +00:00
# Check get one device
2021-10-05 10:17:07 +00:00
user.get(res=Device, item=snap['device']['devicehubID'], status=404)
2021-10-05 09:55:43 +00:00
db_device = Device.query.filter_by(id=snap['device']['id']).one()
2021-10-05 07:56:57 +00:00
2021-10-05 09:55:43 +00:00
action_delete = sorted(db_device.actions, key=lambda x: x.created)[-1]
2021-10-05 07:56:57 +00:00
2021-10-05 09:55:43 +00:00
assert action_delete.t == 'Delete'
assert str(action_delete.id) == action['id']
assert db_device.active is False
2021-10-05 12:33:28 +00:00
# Check use of filter from frontend
url = '/devices/?filter={"type":["Computer"]}'
devices, res = user.get(url, None)
assert len(devices['items']) == 0
2021-10-06 10:11:05 +00:00
2021-11-19 11:44:52 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_delete_devices_check_sync(user: UserClient):
"""This action deactive one device and simulate than one devices is delete."""
file_snap1 = file('1-device-with-components.snapshot')
file_snap2 = file('2-device-with-components.snapshot')
2021-11-22 10:14:27 +00:00
snap, _ = user.post(file_snap1, res=models.Snapshot)
2022-05-16 07:53:22 +00:00
request = {
'type': 'Delete',
'devices': [snap['device']['id']],
'name': 'borrado universal',
'severity': 'Info',
'description': 'duplicity of devices',
'endTime': '2021-07-07T22:00:00.000Z',
}
2021-11-19 11:44:52 +00:00
action, _ = user.post(res=models.Action, data=request)
device1 = Device.query.filter_by(id=snap['device']['id']).one()
2021-11-22 10:14:27 +00:00
snap2, _ = user.post(file_snap2, res=models.Snapshot)
2022-05-16 07:53:22 +00:00
request2 = {
'type': 'Delete',
'devices': [snap2['device']['id']],
'name': 'borrado universal',
'severity': 'Info',
'description': 'duplicity of devices',
'endTime': '2021-07-07T22:00:00.000Z',
}
2021-11-19 11:44:52 +00:00
action2, _ = user.post(res=models.Action, data=request2)
device2 = Device.query.filter_by(id=snap2['device']['id']).one()
2021-11-22 10:14:27 +00:00
# check than device2 is an other device than device1
assert device2.id != device1.id
# check than device2 have the components of device1
2022-05-16 07:53:22 +00:00
assert (
len(
[
x
for x in device2.components
if device1.id
in [y.device.id for y in x.actions if hasattr(y, 'device')]
]
)
2023-02-10 19:09:22 +00:00
== 0
2022-05-16 07:53:22 +00:00
)
2021-11-19 11:44:52 +00:00
2021-11-22 11:01:12 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_delete_devices_permitions(user: UserClient, user2: UserClient):
"""This action deactive one device and simulate than one devices is delete."""
file_snap = file('1-device-with-components.snapshot')
snap, _ = user.post(file_snap, res=models.Snapshot)
2022-05-16 07:53:22 +00:00
request = {
'type': 'Delete',
'devices': [snap['device']['id']],
'name': 'borrado universal',
'severity': 'Info',
'description': 'duplicity of devices',
'endTime': '2021-07-07T22:00:00.000Z',
}
2021-11-22 11:01:12 +00:00
action, _ = user2.post(res=models.Action, data=request, status=422)
2021-10-01 16:01:39 +00:00
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_moveOnDocument_bug168(user: UserClient, user2: UserClient):
"""If you use one moveOnDocument in a trade Document. Next you can not drop this document."""
lotIn, _ = user.post({'name': 'MyLotIn'}, res=Lot)
lotOut, _ = user.post({'name': 'MyLotOut'}, res=Lot)
2022-05-16 07:53:22 +00:00
url = (
2022-09-26 15:18:10 +00:00
'http://www.ereuse.org/apapaapaapaapaapaapaapaapaapaapapaapaapaapaapaapaapaapaapaapapaapaapaapaapaapaapaapaap',
2022-05-16 07:53:22 +00:00
)
2021-10-01 16:01:39 +00:00
request_post1 = {
'filename': 'test.pdf',
'hash': 'bbbbbbbb',
'url': url,
'weight': 150,
2022-05-16 07:53:22 +00:00
'lot': lotIn['id'],
2021-10-01 16:01:39 +00:00
}
tradedocument_from, _ = user.post(res=TradeDocument, data=request_post1)
id_hash = 'aaaaaaaaa'
request_post2 = {
'filename': 'test.pdf',
'hash': id_hash,
'url': url,
'weight': 0,
2022-05-16 07:53:22 +00:00
'lot': lotOut['id'],
2021-10-01 16:01:39 +00:00
}
tradedocument_to, _ = user.post(res=TradeDocument, data=request_post2)
request_trade = {
'type': 'Trade',
'devices': [],
'userFromEmail': user2.email,
'userToEmail': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lotIn['id'],
'confirms': True,
}
user.post(res=models.Action, data=request_trade)
description = 'This is a good description'
request_moveOn = {
'type': 'MoveOnDocument',
2021-10-04 11:34:49 +00:00
'weight': 4,
2021-10-01 16:01:39 +00:00
'container_from': tradedocument_from['id'],
'container_to': id_hash,
2022-05-16 07:53:22 +00:00
'description': description,
2021-10-01 16:01:39 +00:00
}
user.post(res=models.Action, data=request_moveOn)
2021-10-04 11:34:49 +00:00
trade_document1 = TradeDocument.query.filter_by(id=tradedocument_from['id']).one()
trade_document2 = TradeDocument.query.filter_by(id=tradedocument_to['id']).one()
assert trade_document1.total_weight == 150.0
assert trade_document2.total_weight == 4.0
assert trade_document1.trading == 'Confirm'
2022-09-26 15:19:14 +00:00
assert trade_document2.trading is None
2021-10-01 16:01:39 +00:00
tradedocument, _ = user.delete(res=TradeDocument, item=tradedocument_to['id'])