2018-09-20 14:40:41 +00:00
|
|
|
import itertools
|
|
|
|
import json
|
2022-12-12 09:36:33 +00:00
|
|
|
import uuid
|
2018-06-20 21:18:15 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import click
|
|
|
|
import click_spinner
|
2019-01-23 15:55:04 +00:00
|
|
|
import ereuse_utils.cli
|
2022-06-07 13:24:45 +00:00
|
|
|
import jwt
|
2018-06-20 21:18:15 +00:00
|
|
|
import yaml
|
2018-10-23 20:54:27 +00:00
|
|
|
from ereuse_utils.test import ANY
|
2018-08-03 16:15:08 +00:00
|
|
|
|
2018-06-20 21:18:15 +00:00
|
|
|
from ereuse_devicehub.client import UserClient
|
|
|
|
from ereuse_devicehub.db import db
|
2022-06-07 13:24:45 +00:00
|
|
|
from ereuse_devicehub.parser.models import SnapshotsLog
|
2019-05-11 14:27:22 +00:00
|
|
|
from ereuse_devicehub.resources.action import models as m
|
2018-08-03 16:15:08 +00:00
|
|
|
from ereuse_devicehub.resources.agent.models import Person
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
2022-06-07 13:24:45 +00:00
|
|
|
from ereuse_devicehub.resources.enums import SessionType
|
2018-09-20 16:25:47 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2018-06-20 21:18:15 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
|
|
|
from ereuse_devicehub.resources.user import User
|
2021-04-15 19:16:32 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import Session
|
2018-06-20 21:18:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Dummy:
|
2022-06-07 13:24:45 +00:00
|
|
|
TAGS = ('tag1', 'tag2', 'tag3')
|
2018-09-20 14:40:41 +00:00
|
|
|
"""Tags to create."""
|
|
|
|
ET = (
|
2019-02-03 16:12:53 +00:00
|
|
|
('DT-AAAAA', 'A0000000000001'),
|
|
|
|
('DT-BBBBB', 'A0000000000002'),
|
|
|
|
('DT-CCCCC', 'A0000000000003'),
|
|
|
|
('DT-BRRAB', '04970DA2A15984'),
|
2022-06-07 13:24:45 +00:00
|
|
|
('DT-XXXXX', '04e4bc5af95980'),
|
2018-09-20 14:40:41 +00:00
|
|
|
)
|
|
|
|
"""eTags to create."""
|
2018-10-05 12:35:51 +00:00
|
|
|
ORG = 'eReuse.org CAT', '-t', 'G-60437761', '-c', 'ES'
|
2018-09-20 14:40:41 +00:00
|
|
|
"""An organization to create."""
|
2018-06-20 21:18:15 +00:00
|
|
|
|
|
|
|
def __init__(self, app) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.app = app
|
2022-06-07 13:24:45 +00:00
|
|
|
self.app.cli.command('dummy', short_help='Creates dummy devices and users.')(
|
|
|
|
self.run
|
|
|
|
)
|
|
|
|
|
|
|
|
@click.option(
|
|
|
|
'--tag-url',
|
|
|
|
'-tu',
|
|
|
|
type=ereuse_utils.cli.URL(scheme=True, host=True, path=False),
|
|
|
|
default='http://localhost:8081',
|
|
|
|
help='The base url (scheme and host) of the tag provider.',
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'--tag-token',
|
|
|
|
'-tt',
|
|
|
|
type=click.UUID,
|
|
|
|
default='899c794e-1737-4cea-9232-fdc507ab7106',
|
|
|
|
help='The token provided by the tag provider. It is an UUID.',
|
|
|
|
)
|
|
|
|
@click.confirmation_option(
|
|
|
|
prompt='This command (re)creates the DB from scratch.'
|
|
|
|
'Do you want to continue?'
|
|
|
|
)
|
2019-01-23 15:55:04 +00:00
|
|
|
def run(self, tag_url, tag_token):
|
2018-09-20 14:40:41 +00:00
|
|
|
runner = self.app.test_cli_runner()
|
2022-06-07 13:24:45 +00:00
|
|
|
self.app.init_db(
|
|
|
|
'Dummy', 'ACME', 'acme-id', tag_url, tag_token, erase=True, common=True
|
|
|
|
)
|
2018-10-08 08:37:32 +00:00
|
|
|
print('Creating stuff...'.ljust(30), end='')
|
2022-06-07 13:06:18 +00:00
|
|
|
assert SnapshotsLog.query.filter().all() == []
|
2018-06-20 21:18:15 +00:00
|
|
|
with click_spinner.spinner():
|
2019-02-11 20:34:45 +00:00
|
|
|
out = runner.invoke('org', 'add', *self.ORG).output
|
2018-09-20 14:40:41 +00:00
|
|
|
org_id = json.loads(out)['id']
|
2021-02-10 12:40:24 +00:00
|
|
|
user1 = self.user_client('user@dhub.com', '1234', 'user1')
|
|
|
|
user2 = self.user_client('user2@dhub.com', '1234', 'user2')
|
|
|
|
user3 = self.user_client('user3@dhub.com', '1234', 'user3')
|
|
|
|
user4 = self.user_client('user4@dhub.com', '1234', 'user4')
|
2019-12-16 19:38:41 +00:00
|
|
|
|
2018-09-20 14:40:41 +00:00
|
|
|
# todo put user's agent into Org
|
2018-09-20 07:28:52 +00:00
|
|
|
for id in self.TAGS:
|
2019-12-16 19:38:41 +00:00
|
|
|
user1.post({'id': id}, res=Tag)
|
2018-09-20 14:40:41 +00:00
|
|
|
for id, sec in self.ET:
|
2022-06-07 13:24:45 +00:00
|
|
|
runner.invoke(
|
|
|
|
'tag',
|
|
|
|
'add',
|
|
|
|
id,
|
|
|
|
'-p',
|
|
|
|
'https://t.devicetag.io',
|
|
|
|
'-s',
|
|
|
|
sec,
|
|
|
|
'-u',
|
|
|
|
user1.user["id"],
|
|
|
|
'-o',
|
|
|
|
org_id,
|
|
|
|
)
|
2018-10-16 06:46:55 +00:00
|
|
|
# create tag for pc-laudem
|
2022-06-07 13:24:45 +00:00
|
|
|
runner.invoke(
|
|
|
|
'tag',
|
|
|
|
'add',
|
|
|
|
'tagA',
|
|
|
|
'-p',
|
|
|
|
'https://t.devicetag.io',
|
|
|
|
'-u',
|
|
|
|
user1.user["id"],
|
|
|
|
'-s',
|
|
|
|
'tagA-secondary',
|
|
|
|
)
|
2018-07-02 10:52:54 +00:00
|
|
|
files = tuple(Path(__file__).parent.joinpath('files').iterdir())
|
|
|
|
print('done.')
|
2018-10-16 06:46:55 +00:00
|
|
|
sample_pc = None # We treat this one as a special sample for demonstrations
|
2022-11-24 11:21:42 +00:00
|
|
|
pcs = set()
|
2018-07-02 10:52:54 +00:00
|
|
|
with click.progressbar(files, label='Creating devices...'.ljust(28)) as bar:
|
|
|
|
for path in bar:
|
|
|
|
with path.open() as f:
|
|
|
|
snapshot = yaml.load(f)
|
2022-12-12 09:36:33 +00:00
|
|
|
if snapshot['device']['type'] in ['Desktop', 'Laptop']:
|
|
|
|
snapshot['device']['system_uuid'] = uuid.uuid4()
|
2022-06-07 13:24:45 +00:00
|
|
|
s, _ = user1.post(res=m.Snapshot, data=self.json_encode(snapshot))
|
2018-10-16 06:46:55 +00:00
|
|
|
if s.get('uuid', None) == 'ec23c11b-80b6-42cd-ac5c-73ba7acddbc4':
|
|
|
|
sample_pc = s['device']['id']
|
2021-03-08 21:43:24 +00:00
|
|
|
sample_pc_devicehub_id = s['device']['devicehubID']
|
2018-10-16 06:46:55 +00:00
|
|
|
else:
|
|
|
|
pcs.add(s['device']['id'])
|
2022-06-07 13:24:45 +00:00
|
|
|
if (
|
|
|
|
s.get('uuid', None) == 'de4f495e-c58b-40e1-a33e-46ab5e84767e'
|
|
|
|
): # oreo
|
2018-11-17 17:24:34 +00:00
|
|
|
# Make one hdd ErasePhysical
|
2022-06-07 13:24:45 +00:00
|
|
|
hdd = next(
|
|
|
|
hdd for hdd in s['components'] if hdd['type'] == 'HardDrive'
|
|
|
|
)
|
|
|
|
user1.post(
|
|
|
|
{
|
|
|
|
'type': 'ErasePhysical',
|
|
|
|
'method': 'Shred',
|
|
|
|
'device': hdd['id'],
|
|
|
|
},
|
|
|
|
res=m.Action,
|
|
|
|
)
|
2018-10-16 06:46:55 +00:00
|
|
|
assert sample_pc
|
|
|
|
print('PC sample is', sample_pc)
|
2018-09-20 14:40:41 +00:00
|
|
|
# Link tags and eTags
|
2022-06-07 13:24:45 +00:00
|
|
|
for tag, pc in zip(
|
|
|
|
(self.TAGS[1], self.TAGS[2], self.ET[0][0], self.ET[1][1]), pcs
|
|
|
|
):
|
2019-12-16 19:38:41 +00:00
|
|
|
user1.put({}, res=Tag, item='{}/device/{}'.format(tag, pc), status=204)
|
2018-09-20 14:40:41 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# Perform generic actions
|
2022-06-07 13:24:45 +00:00
|
|
|
for pc, model in zip(
|
|
|
|
pcs, {m.ToRepair, m.Repair, m.ToPrepare, m.Ready, m.ToPrepare, m.Prepare}
|
|
|
|
):
|
2019-12-16 19:38:41 +00:00
|
|
|
user1.post({'type': model.t, 'devices': [pc]}, res=m.Action)
|
2018-09-20 14:40:41 +00:00
|
|
|
|
|
|
|
# Perform a Sell to several devices
|
2021-04-09 12:43:20 +00:00
|
|
|
# user1.post(
|
2022-06-07 13:24:45 +00:00
|
|
|
# {
|
|
|
|
# 'type': m.Sell.t,
|
|
|
|
# 'to': user1.user['individuals'][0]['id'],
|
|
|
|
# 'devices': list(itertools.islice(pcs, len(pcs) // 2))
|
|
|
|
# },
|
|
|
|
# res=m.Action)
|
2019-12-23 16:36:20 +00:00
|
|
|
|
|
|
|
lot_user, _ = user1.post({'name': 'LoteStephan'}, res=Lot)
|
|
|
|
|
2020-03-17 11:14:56 +00:00
|
|
|
lot_user2, _ = user2.post({'name': 'LoteSergio'}, res=Lot)
|
2019-12-23 16:36:20 +00:00
|
|
|
|
2020-03-17 11:14:56 +00:00
|
|
|
lot_user3, _ = user3.post({'name': 'LoteManos'}, res=Lot)
|
2019-12-23 16:36:20 +00:00
|
|
|
|
2020-03-17 11:14:56 +00:00
|
|
|
lot_user4, _ = user4.post({'name': 'LoteJordi'}, res=Lot)
|
2018-09-20 14:40:41 +00:00
|
|
|
|
2022-06-07 13:24:45 +00:00
|
|
|
lot, _ = user1.post(
|
|
|
|
{},
|
|
|
|
res=Lot,
|
|
|
|
item='{}/devices'.format(lot_user['id']),
|
|
|
|
query=[('id', pc) for pc in itertools.islice(pcs, 1, 4)],
|
|
|
|
)
|
2021-11-18 10:52:59 +00:00
|
|
|
# assert len(lot['devices'])
|
2018-09-20 14:40:41 +00:00
|
|
|
|
2022-06-07 13:24:45 +00:00
|
|
|
lot2, _ = user2.post(
|
|
|
|
{},
|
|
|
|
res=Lot,
|
|
|
|
item='{}/devices'.format(lot_user2['id']),
|
|
|
|
query=[('id', pc) for pc in itertools.islice(pcs, 4, 6)],
|
|
|
|
)
|
2019-12-21 16:51:29 +00:00
|
|
|
|
2022-06-07 13:24:45 +00:00
|
|
|
lot3, _ = user3.post(
|
|
|
|
{},
|
|
|
|
res=Lot,
|
|
|
|
item='{}/devices'.format(lot_user3['id']),
|
|
|
|
query=[('id', pc) for pc in itertools.islice(pcs, 11, 14)],
|
|
|
|
)
|
2020-07-07 12:58:55 +00:00
|
|
|
|
2022-06-07 13:24:45 +00:00
|
|
|
lot4, _ = user4.post(
|
|
|
|
{},
|
|
|
|
res=Lot,
|
|
|
|
item='{}/devices'.format(lot_user4['id']),
|
|
|
|
query=[('id', pc) for pc in itertools.islice(pcs, 14, 16)],
|
|
|
|
)
|
2019-12-21 16:51:29 +00:00
|
|
|
|
2018-09-21 09:25:22 +00:00
|
|
|
# Keep this at the bottom
|
2019-12-16 19:38:41 +00:00
|
|
|
inventory, _ = user1.get(res=Device)
|
2018-10-04 08:59:31 +00:00
|
|
|
assert len(inventory['items'])
|
2018-09-29 10:24:22 +00:00
|
|
|
|
2022-07-21 12:01:20 +00:00
|
|
|
# i, _ = user1.get(res=Device, query=[('search', 'intel')])
|
|
|
|
# assert len(i['items']) in [14, 12]
|
|
|
|
# i, _ = user1.get(res=Device, query=[('search', 'pc')])
|
|
|
|
# assert len(i['items']) in [17, 14]
|
2018-10-16 06:46:55 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# Let's create a set of actions for the pc device
|
2018-10-16 06:46:55 +00:00
|
|
|
# Make device Ready
|
|
|
|
|
2019-12-16 19:38:41 +00:00
|
|
|
user1.post({'type': m.ToPrepare.t, 'devices': [sample_pc]}, res=m.Action)
|
|
|
|
user1.post({'type': m.Prepare.t, 'devices': [sample_pc]}, res=m.Action)
|
|
|
|
user1.post({'type': m.Ready.t, 'devices': [sample_pc]}, res=m.Action)
|
2022-06-07 13:24:45 +00:00
|
|
|
user1.post(
|
|
|
|
{'type': m.Price.t, 'device': sample_pc, 'currency': 'EUR', 'price': 85},
|
|
|
|
res=m.Action,
|
|
|
|
)
|
2019-12-21 16:51:29 +00:00
|
|
|
|
2018-10-16 06:46:55 +00:00
|
|
|
# todo test reserve
|
2021-04-09 12:43:20 +00:00
|
|
|
# user1.post( # Sell device
|
2022-06-07 13:24:45 +00:00
|
|
|
# {
|
|
|
|
# 'type': m.Sell.t,
|
|
|
|
# 'to': user1.user['individuals'][0]['id'],
|
|
|
|
# 'devices': [sample_pc]
|
|
|
|
# },
|
|
|
|
# res=m.Action)
|
2018-10-16 06:46:55 +00:00
|
|
|
# todo Receive
|
|
|
|
|
2021-03-08 21:43:24 +00:00
|
|
|
user1.get(res=Device, item=sample_pc_devicehub_id) # Test
|
2018-10-23 20:54:27 +00:00
|
|
|
anonymous = self.app.test_client()
|
2021-03-08 21:43:24 +00:00
|
|
|
html, _ = anonymous.get(res=Device, item=sample_pc_devicehub_id, accept=ANY)
|
2023-02-02 09:58:09 +00:00
|
|
|
assert 'hewlett-packard' in html
|
2018-10-23 20:54:27 +00:00
|
|
|
|
2018-10-16 06:46:55 +00:00
|
|
|
# For netbook: to preapre -> torepair -> to dispose -> disposed
|
2018-07-02 10:52:54 +00:00
|
|
|
print('⭐ Done.')
|
2018-06-20 21:18:15 +00:00
|
|
|
|
2021-02-10 12:40:24 +00:00
|
|
|
def user_client(self, email: str, password: str, name: str):
|
2021-02-10 12:05:43 +00:00
|
|
|
user = User(email=email, password=password)
|
2019-12-16 19:38:41 +00:00
|
|
|
|
2019-12-23 16:36:20 +00:00
|
|
|
user.individuals.add(Person(name=name))
|
|
|
|
db.session.add(user)
|
2021-04-15 19:16:32 +00:00
|
|
|
session_external = Session(user=user, type=SessionType.External)
|
|
|
|
session_internal = Session(user=user, type=SessionType.Internal)
|
|
|
|
db.session.add(session_internal)
|
|
|
|
db.session.add(session_external)
|
2019-12-16 19:38:41 +00:00
|
|
|
|
2018-06-20 21:18:15 +00:00
|
|
|
db.session.commit()
|
2022-06-07 13:24:45 +00:00
|
|
|
client = UserClient(
|
|
|
|
self.app, user.email, password, response_wrapper=self.app.response_class
|
|
|
|
)
|
2018-08-03 16:15:08 +00:00
|
|
|
client.login()
|
2018-06-20 21:18:15 +00:00
|
|
|
return client
|
2021-07-05 13:04:44 +00:00
|
|
|
|
|
|
|
def json_encode(self, dev: str) -> dict:
|
|
|
|
"""Encode json."""
|
|
|
|
data = {"type": "Snapshot"}
|
2022-06-07 13:24:45 +00:00
|
|
|
data['data'] = jwt.encode(
|
|
|
|
dev,
|
|
|
|
self.app.config['JWT_PASS'],
|
|
|
|
algorithm="HS256",
|
|
|
|
json_encoder=ereuse_utils.JSONEncoder,
|
2021-07-05 13:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return data
|