adding endpoint licences

This commit is contained in:
Cayo Puigdefabregas 2020-12-29 17:37:13 +01:00
parent fdd5b9bb77
commit 2d4c4d4c89
6 changed files with 153 additions and 17 deletions

View File

@ -14,6 +14,7 @@ from ereuse_devicehub.resources.device import definitions
from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.documents import documents
from ereuse_devicehub.resources.enums import PriceSoftware from ereuse_devicehub.resources.enums import PriceSoftware
from ereuse_devicehub.resources.versions import versions from ereuse_devicehub.resources.versions import versions
from ereuse_devicehub.resources.licences import licences
from ereuse_devicehub.resources.metric import definitions as metric_def from ereuse_devicehub.resources.metric import definitions as metric_def
@ -29,6 +30,7 @@ class DevicehubConfig(Config):
import_resource(documents), import_resource(documents),
import_resource(inventory), import_resource(inventory),
import_resource(versions), import_resource(versions),
import_resource(licences),
import_resource(metric_def), import_resource(metric_def),
),) ),)
PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str]
@ -48,6 +50,7 @@ class DevicehubConfig(Config):
""" """
TMP_SNAPSHOTS = config('TMP_SNAPSHOTS', '/tmp/snapshots') TMP_SNAPSHOTS = config('TMP_SNAPSHOTS', '/tmp/snapshots')
TMP_LIVES = config('TMP_LIVES', '/tmp/lives')
"""This var is for save a snapshots in json format when fail something""" """This var is for save a snapshots in json format when fail something"""
API_DOC_CONFIG_TITLE = 'Devicehub' API_DOC_CONFIG_TITLE = 'Devicehub'
API_DOC_CONFIG_VERSION = '0.2' API_DOC_CONFIG_VERSION = '0.2'

View File

@ -0,0 +1,41 @@
from typing import Callable, Iterable, Tuple
from flask.json import jsonify
from teal.resource import Resource, View
class LicenceView(View):
def get(self, *args, **kwargs):
"""Get version of DeviceHub and ereuse-tag."""
with open('licences.txt') as f:
licences = f.read()
ret = jsonify(licences)
ret.status_code = 200
return ret
class LicencesDef(Resource):
__type__ = 'Licence'
SCHEMA = None
VIEW = None # We do not want to create default / documents endpoint
AUTH = False
def __init__(self, app,
import_name=__name__,
static_folder=None,
static_url_path=None,
template_folder=None,
url_prefix=None,
subdomain=None,
url_defaults=None,
root_path=None,
cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()):
super().__init__(app, import_name, static_folder, static_url_path, template_folder,
url_prefix, subdomain, url_defaults, root_path, cli_commands)
get = {'GET'}
d = {}
licence_view = LicenceView.as_view('LicenceView', definition=self)
self.add_url_rule('/', defaults=d, view_func=licence_view, methods=get)

38
licences.txt Normal file
View File

@ -0,0 +1,38 @@
[
{
"WorkbenchDesktopVersion": "v.1",
"USOdyPrivacyPolicyVersion": "v.1",
"Language": "CAT",
"Description": "Recollim informació bàsica bla bla bla"
},
{
"WorkbenchesktopVersion": "v.1",
"USOdyPrivacyPolicyVersion": "v.2",
"Language": "CAT",
"Description": "Recollim informació bàsica bla bla bla i a partir de tal data les dades dhores dús les usem també per estimar la durabilitat"
},
{
"WorkbenchDesktopVersion": "v.1.1",
"USOdyPrivacyPolicyVersion": "v.3",
"Language": "CAT",
"Description": "Recollim informació bàsica bla bla bla pero ara també recollim la versió del sistema operatiu"
},
{
"WorkbenchDesktopVersion": "v.1",
"USOdyPrivacyPolicyVersion": "v.1",
"Language": "EN",
"Description": "We collect basic information blah blah blah"
},
{
"WorkbenchDesktopVersion": "v.1",
"USOdyPrivacyPolicyVersion": "v.2",
"Language": "EN",
"Description": "We collect basic information blah blah blah and from that date we also use the usage time data to estimate durability"
},
{
"WorkbenchDesktopVersion": "v.1.1",
"USOdyPrivacyPolicyVersion": "v.3",
"Language": "EN",
"Description": "We collect basic information blah blah blah but now we also collect the operating system version"
}
]

View File

@ -31,6 +31,7 @@ class TestConfig(DevicehubConfig):
TESTING = True TESTING = True
SERVER_NAME = 'localhost' SERVER_NAME = 'localhost'
TMP_SNAPSHOTS = '/tmp/snapshots' TMP_SNAPSHOTS = '/tmp/snapshots'
TMP_LIVES = '/tmp/lives'
@pytest.fixture(scope='session') @pytest.fixture(scope='session')

View File

@ -1,4 +1,7 @@
import os
import ipaddress import ipaddress
import json
import shutil
import copy import copy
import pytest import pytest
@ -12,7 +15,7 @@ from sqlalchemy.util import OrderedSet
from teal.enums import Currency, Subdivision from teal.enums import Currency, Subdivision
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.client import UserClient from ereuse_devicehub.client import UserClient, Client
from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.resources import enums from ereuse_devicehub.resources import enums
from ereuse_devicehub.resources.action import models from ereuse_devicehub.resources.action import models
@ -248,7 +251,7 @@ def test_generic_action(action_model_state: Tuple[models.Action, states.Trading]
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live(user: UserClient, app: Devicehub): def test_live(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it.""" """Tests inserting a Live into the database and GETting it."""
acer = file('acer.happy.battery.snapshot') acer = file('acer.happy.battery.snapshot')
snapshot, _ = user.post(acer, res=models.Snapshot) snapshot, _ = user.post(acer, res=models.Snapshot)
@ -267,7 +270,7 @@ def test_live(user: UserClient, app: Devicehub):
hdd_action['lifetime'] += 1000 hdd_action['lifetime'] += 1000
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
snapshot, _ = user.post(acer, res=models.Live) snapshot, _ = client.post(acer, res=models.Live)
db_device = Device.query.filter_by(id=1).one() db_device = Device.query.filter_by(id=1).one()
action_live = [a for a in db_device.actions if a.type == 'Live'] action_live = [a for a in db_device.actions if a.type == 'Live']
assert len(action_live) == 1 assert len(action_live) == 1
@ -280,7 +283,7 @@ def test_live(user: UserClient, app: Devicehub):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live_without_TestDataStorage(user: UserClient, app: Devicehub): def test_live_without_TestDataStorage(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it. """Tests inserting a Live into the database and GETting it.
If the live don't have a TestDataStorage, then save live and response None If the live don't have a TestDataStorage, then save live and response None
""" """
@ -301,7 +304,7 @@ def test_live_without_TestDataStorage(user: UserClient, app: Devicehub):
acer['components'][7]['actions'] = actions acer['components'][7]['actions'] = actions
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
live, _ = user.post(acer, res=models.Live) live, _ = client.post(acer, res=models.Live)
assert live['severity'] == 'Warning' assert live['severity'] == 'Warning'
description = "We don't found any TestDataStorage for disk sn: wd-wx11a80w7430" description = "We don't found any TestDataStorage for disk sn: wd-wx11a80w7430"
assert live['description'] == description assert live['description'] == description
@ -311,7 +314,7 @@ def test_live_without_TestDataStorage(user: UserClient, app: Devicehub):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live_without_hdd_1(user: UserClient, app: Devicehub): def test_live_without_hdd_1(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it. """Tests inserting a Live into the database and GETting it.
The snapshot have hdd but the live no, and response 404 The snapshot have hdd but the live no, and response 404
""" """
@ -332,13 +335,13 @@ def test_live_without_hdd_1(user: UserClient, app: Devicehub):
acer['components'] = components acer['components'] = components
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
response, _ = user.post(acer, res=models.Live, status=404) response, _ = client.post(acer, res=models.Live, status=404)
assert "The There aren't any disk in this device" in response['message'] assert "The There aren't any disk in this device" in response['message']
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live_without_hdd_2(user: UserClient, app: Devicehub): def test_live_without_hdd_2(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it. """Tests inserting a Live into the database and GETting it.
The snapshot haven't hdd and the live neither, and response 404 The snapshot haven't hdd and the live neither, and response 404
""" """
@ -359,13 +362,13 @@ def test_live_without_hdd_2(user: UserClient, app: Devicehub):
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3" acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
response, _ = user.post(acer, res=models.Live, status=404) response, _ = client.post(acer, res=models.Live, status=404)
assert "The There aren't any disk in this device" in response['message'] assert "The There aren't any disk in this device" in response['message']
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live_without_hdd_3(user: UserClient, app: Devicehub): def test_live_without_hdd_3(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it. """Tests inserting a Live into the database and GETting it.
The snapshot haven't hdd and the live have, and save the live The snapshot haven't hdd and the live have, and save the live
with usage_time_allocate == 0 with usage_time_allocate == 0
@ -388,7 +391,7 @@ def test_live_without_hdd_3(user: UserClient, app: Devicehub):
acer = file('acer.happy.battery.snapshot') acer = file('acer.happy.battery.snapshot')
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
live, _ = user.post(acer, res=models.Live) live, _ = client.post(acer, res=models.Live)
assert live['severity'] == 'Warning' assert live['severity'] == 'Warning'
description = "Don't exist one previous live or snapshot as reference" description = "Don't exist one previous live or snapshot as reference"
assert live['description'] == description assert live['description'] == description
@ -399,7 +402,7 @@ def test_live_without_hdd_3(user: UserClient, app: Devicehub):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live_with_hdd_with_old_time(user: UserClient, app: Devicehub): def test_live_with_hdd_with_old_time(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it. """Tests inserting a Live into the database and GETting it.
The snapshot hdd have a lifetime higher than lifetime of the live action The snapshot hdd have a lifetime higher than lifetime of the live action
save the live with usage_time_allocate == 0 save the live with usage_time_allocate == 0
@ -422,7 +425,7 @@ def test_live_with_hdd_with_old_time(user: UserClient, app: Devicehub):
action[0]['lifetime'] -= 100 action[0]['lifetime'] -= 100
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
live, _ = user.post(acer, res=models.Live) live, _ = client.post(acer, res=models.Live)
assert live['severity'] == 'Warning' assert live['severity'] == 'Warning'
description = "The difference with the last live/snapshot is negative" description = "The difference with the last live/snapshot is negative"
assert live['description'] == description assert live['description'] == description
@ -433,13 +436,12 @@ def test_live_with_hdd_with_old_time(user: UserClient, app: Devicehub):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_live_search_last_allocate(user: UserClient, app: Devicehub): def test_live_search_last_allocate(user: UserClient, client: Client, app: Devicehub):
"""Tests inserting a Live into the database and GETting it. """Tests inserting a Live into the database and GETting it.
""" """
acer = file('acer.happy.battery.snapshot') acer = file('acer.happy.battery.snapshot')
snapshot, _ = user.post(acer, res=models.Snapshot) snapshot, _ = user.post(acer, res=models.Snapshot)
device_id = snapshot['device']['id'] device_id = snapshot['device']['id']
db_device = Device.query.filter_by(id=1).one()
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1, post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
"devices": [device_id], "description": "aaa", "devices": [device_id], "description": "aaa",
"finalUserCode": "abcdefjhi", "finalUserCode": "abcdefjhi",
@ -454,14 +456,65 @@ def test_live_search_last_allocate(user: UserClient, app: Devicehub):
hdd_action['lifetime'] += 1000 hdd_action['lifetime'] += 1000
acer.pop('elapsed') acer.pop('elapsed')
acer['licence_version'] = '1.0.0' acer['licence_version'] = '1.0.0'
live, _ = user.post(acer, res=models.Live) live, _ = client.post(acer, res=models.Live)
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec4" acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec4"
actions = [a for a in acer['components'][7]['actions'] if a['type'] != 'TestDataStorage'] actions = [a for a in acer['components'][7]['actions'] if a['type'] != 'TestDataStorage']
acer['components'][7]['actions'] = actions acer['components'][7]['actions'] = actions
live, _ = user.post(acer, res=models.Live) live, _ = client.post(acer, res=models.Live)
assert live['usageTimeAllocate'] == 1000 assert live['usageTimeAllocate'] == 1000
@pytest.mark.mvp
def test_save_live_json(app: Devicehub, user: UserClient, client: Client):
""" This test check if works the function save_snapshot_in_file """
acer = file('acer.happy.battery.snapshot')
snapshot, _ = user.post(acer, res=models.Snapshot)
debug = 'AAA'
acer['debug'] = debug
device_id = snapshot['device']['id']
post_request = {"transaction": "ccc", "name": "John", "endUsers": 1,
"devices": [device_id], "description": "aaa",
"finalUserCode": "abcdefjhi",
"startTime": "2020-11-01T02:00:00+00:00",
"endTime": "2020-12-01T02:00:00+00:00"
}
user.post(res=models.Allocate, data=post_request)
acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3"
hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0]
hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0]
hdd_action['lifetime'] += 1000
acer.pop('elapsed')
acer['licence_version'] = '1.0.0'
live, _ = client.post(acer, res=models.Live)
tmp_snapshots = app.config['TMP_LIVES']
path_dir_base = os.path.join(tmp_snapshots)
uuid = acer['uuid']
files = [x for x in os.listdir(path_dir_base) if uuid in x]
snapshot = {'debug': ''}
if files:
path_snapshot = os.path.join(path_dir_base, files[0])
with open(path_snapshot) as file_snapshot:
snapshot = json.loads(file_snapshot.read())
shutil.rmtree(tmp_snapshots)
assert snapshot['debug'] == debug
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_licences(client: Client):
"""Tests inserting a Live into the database and GETting it.
"""
licences, _ = client.get('/licences/')
licences = json.loads(licences)
assert licences[0]['USOdyPrivacyPolicyVersion'] == 'v.1'
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_allocate(user: UserClient): def test_allocate(user: UserClient):