From 2d4c4d4c89ecd0d7f9778e9d2d830031b8ca21fa Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 29 Dec 2020 17:37:13 +0100 Subject: [PATCH] adding endpoint licences --- ereuse_devicehub/config.py | 3 + .../resources/licences/__init__.py | 0 .../resources/licences/licences.py | 41 +++++++++ licences.txt | 38 ++++++++ tests/conftest.py | 1 + tests/test_action.py | 87 +++++++++++++++---- 6 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 ereuse_devicehub/resources/licences/__init__.py create mode 100644 ereuse_devicehub/resources/licences/licences.py create mode 100644 licences.txt diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index d8734f65..3534ba98 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -14,6 +14,7 @@ from ereuse_devicehub.resources.device import definitions from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.enums import PriceSoftware from ereuse_devicehub.resources.versions import versions +from ereuse_devicehub.resources.licences import licences from ereuse_devicehub.resources.metric import definitions as metric_def @@ -29,6 +30,7 @@ class DevicehubConfig(Config): import_resource(documents), import_resource(inventory), import_resource(versions), + import_resource(licences), import_resource(metric_def), ),) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] @@ -48,6 +50,7 @@ class DevicehubConfig(Config): """ 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""" API_DOC_CONFIG_TITLE = 'Devicehub' API_DOC_CONFIG_VERSION = '0.2' diff --git a/ereuse_devicehub/resources/licences/__init__.py b/ereuse_devicehub/resources/licences/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/licences/licences.py b/ereuse_devicehub/resources/licences/licences.py new file mode 100644 index 00000000..8742d29a --- /dev/null +++ b/ereuse_devicehub/resources/licences/licences.py @@ -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) diff --git a/licences.txt b/licences.txt new file mode 100644 index 00000000..8137869c --- /dev/null +++ b/licences.txt @@ -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 d’hores 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" + } +] diff --git a/tests/conftest.py b/tests/conftest.py index 9969be22..d212a9b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -31,6 +31,7 @@ class TestConfig(DevicehubConfig): TESTING = True SERVER_NAME = 'localhost' TMP_SNAPSHOTS = '/tmp/snapshots' + TMP_LIVES = '/tmp/lives' @pytest.fixture(scope='session') diff --git a/tests/test_action.py b/tests/test_action.py index 314257b3..f5dd6dcb 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -1,4 +1,7 @@ +import os import ipaddress +import json +import shutil import copy import pytest @@ -12,7 +15,7 @@ from sqlalchemy.util import OrderedSet from teal.enums import Currency, Subdivision 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.resources import enums 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.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.""" acer = file('acer.happy.battery.snapshot') snapshot, _ = user.post(acer, res=models.Snapshot) @@ -267,7 +270,7 @@ def test_live(user: UserClient, app: Devicehub): hdd_action['lifetime'] += 1000 acer.pop('elapsed') 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() action_live = [a for a in db_device.actions if a.type == 'Live'] assert len(action_live) == 1 @@ -280,7 +283,7 @@ def test_live(user: UserClient, app: Devicehub): @pytest.mark.mvp @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. 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.pop('elapsed') acer['licence_version'] = '1.0.0' - live, _ = user.post(acer, res=models.Live) + 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 @@ -311,7 +314,7 @@ def test_live_without_TestDataStorage(user: UserClient, app: Devicehub): @pytest.mark.mvp @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. 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.pop('elapsed') 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'] @pytest.mark.mvp @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. 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.pop('elapsed') 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'] @pytest.mark.mvp @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. The snapshot haven't hdd and the live have, and save the live 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.pop('elapsed') acer['licence_version'] = '1.0.0' - live, _ = user.post(acer, res=models.Live) + live, _ = client.post(acer, res=models.Live) assert live['severity'] == 'Warning' description = "Don't exist one previous live or snapshot as reference" assert live['description'] == description @@ -399,7 +402,7 @@ def test_live_without_hdd_3(user: UserClient, app: Devicehub): @pytest.mark.mvp @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. The snapshot hdd have a lifetime higher than lifetime of the live action 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 acer.pop('elapsed') acer['licence_version'] = '1.0.0' - live, _ = user.post(acer, res=models.Live) + 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 @@ -433,13 +436,12 @@ def test_live_with_hdd_with_old_time(user: UserClient, app: Devicehub): @pytest.mark.mvp @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. """ acer = file('acer.happy.battery.snapshot') snapshot, _ = user.post(acer, res=models.Snapshot) device_id = snapshot['device']['id'] - db_device = Device.query.filter_by(id=1).one() post_request = {"transaction": "ccc", "name": "John", "endUsers": 1, "devices": [device_id], "description": "aaa", "finalUserCode": "abcdefjhi", @@ -454,14 +456,65 @@ def test_live_search_last_allocate(user: UserClient, app: Devicehub): hdd_action['lifetime'] += 1000 acer.pop('elapsed') 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" actions = [a for a in acer['components'][7]['actions'] if a['type'] != 'TestDataStorage'] acer['components'][7]['actions'] = actions - live, _ = user.post(acer, res=models.Live) + live, _ = client.post(acer, res=models.Live) 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.usefixtures(conftest.app_context.__name__) def test_allocate(user: UserClient):