From 7ee2f2962bb2884b1a4f333728b96f1ac1bf4bc0 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 11 May 2022 11:48:53 +0200 Subject: [PATCH 1/7] add view for download Workbench settings --- .../templates/ereuse_devicehub/base_site.html | 10 ++ .../templates/workbench/settings.html | 59 ++++++++++++ ereuse_devicehub/workbench/__init__.py | 0 ereuse_devicehub/workbench/views.py | 94 +++++++++++++++++++ examples/app.py | 2 + tests/conftest.py | 2 + 6 files changed, 167 insertions(+) create mode 100644 ereuse_devicehub/templates/workbench/settings.html create mode 100644 ereuse_devicehub/workbench/__init__.py create mode 100644 ereuse_devicehub/workbench/views.py diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index fe014107..1774e6df 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html @@ -64,6 +64,16 @@ +
  • + + + Workbench Settings + +
  • +
  • + +
  • +
  • diff --git a/ereuse_devicehub/templates/workbench/settings.html b/ereuse_devicehub/templates/workbench/settings.html new file mode 100644 index 00000000..80278615 --- /dev/null +++ b/ereuse_devicehub/templates/workbench/settings.html @@ -0,0 +1,59 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
    +

    {{ title }}

    + +
    + +
    +
    +
    + +
    +
    + +
    +
    Download your settings for Workbench
    +

    Please select one of this options

    +
    +
    + +
    +

    Download the settings for register devices and erase disks. This is less safe and more fast.

    +
    +
    +
    + +
    +

    Download the settings for register devices and erase disks. This is more safe and more slow.

    +
    +
    +
    + +
    + +
    + +
    + +
    +
    +
    +
    +{% endblock main %} diff --git a/ereuse_devicehub/workbench/__init__.py b/ereuse_devicehub/workbench/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/workbench/views.py b/ereuse_devicehub/workbench/views.py new file mode 100644 index 00000000..ccbdbdc5 --- /dev/null +++ b/ereuse_devicehub/workbench/views.py @@ -0,0 +1,94 @@ +import time + +import flask +from flask import Blueprint +from flask import current_app as app +from flask import g, make_response, request +from flask_login import login_required + +from ereuse_devicehub import auth +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.enums import SessionType +from ereuse_devicehub.resources.user.models import Session +from ereuse_devicehub.views import GenericMixView + +workbench = Blueprint('workbench', __name__, url_prefix='/workbench') + + +class SettingsView(GenericMixView): + decorators = [login_required] + template_name = 'workbench/settings.html' + page_title = "Workbench Settings" + + def dispatch_request(self): + self.get_context() + self.context.update( + { + 'page_title': self.page_title, + } + ) + + self.opt = request.values.get('opt') + if self.opt in ['register', 'soft', 'hard']: + return self.download() + + return flask.render_template(self.template_name, **self.context) + + def download(self): + self.wbContext = { + 'token': self.get_token(), + 'host': app.config['HOST'], + 'inventory': app.config['SCHEMA'], + 'benchmark': False, + 'stress_test': 1, + 'erase': '', + 'steps': 0, + 'leading_zeros': False, + } + options = {"register": self.register, "soft": self.soft, "hard": self.hard} + return options[self.opt]() + + def register(self): + data = flask.render_template('workbench/wbSettings.ini', **self.wbContext) + return self.response_download(data) + + def soft(self): + self.wbContext['erase'] = 'EraseBasic' + self.wbContext['steps'] = 1 + data = flask.render_template('workbench/wbSettings.ini', **self.wbContext) + return self.response_download(data) + + def hard(self): + self.wbContext['erase'] = 'EraseSectors' + self.wbContext['steps'] = 1 + self.wbContext['leading_zeros'] = True + data = flask.render_template('workbench/wbSettings.ini', **self.wbContext) + return self.response_download(data) + + def response_download(self, data): + bfile = str.encode(data) + output = make_response(bfile) + output.headers['Content-Disposition'] = 'attachment; filename=settings.ini' + output.headers['Content-type'] = 'text/plain' + return output + + def get_token(self): + if not g.user.sessions: + ses = Session(user=g.user) + db.session.add(ses) + db.session.commit() + + tk = '' + now = time.time() + for s in g.user.sessions: + if s.type == SessionType.Internal and (s.expired == 0 or s.expired > now): + tk = s.token + break + + assert tk != '' + + token = auth.Auth.encode(tk) + return token + + +workbench.add_url_rule('/settings/', view_func=SettingsView.as_view('settings')) diff --git a/examples/app.py b/examples/app.py index 415d86ce..b0c9b253 100644 --- a/examples/app.py +++ b/examples/app.py @@ -10,11 +10,13 @@ from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.inventory.views import devices from ereuse_devicehub.labels.views import labels from ereuse_devicehub.views import core +from ereuse_devicehub.workbench.views import workbench app = Devicehub(inventory=DevicehubConfig.DB_SCHEMA) app.register_blueprint(core) app.register_blueprint(devices) app.register_blueprint(labels) +app.register_blueprint(workbench) # configure & enable CSRF of Flask-WTF # NOTE: enable by blueprint to exclude API views diff --git a/tests/conftest.py b/tests/conftest.py index a8d04506..53fdbaa9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,6 +24,7 @@ from ereuse_devicehub.resources.enums import SessionType from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.user.models import Session, User from ereuse_devicehub.views import core +from ereuse_devicehub.workbench.views import workbench STARTT = datetime(year=2000, month=1, day=1, hour=1) """A dummy starting time to use in tests.""" @@ -58,6 +59,7 @@ def _app(config: TestConfig) -> Devicehub: app.register_blueprint(core) app.register_blueprint(devices) app.register_blueprint(labels) + app.register_blueprint(workbench) app.config["SQLALCHEMY_RECORD_QUERIES"] = True app.config['PROFILE'] = True # app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) From 0e75865757446ba53e2091339a3fe1b5232c5886 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 11 May 2022 11:58:54 +0200 Subject: [PATCH 2/7] fix settings.html --- ereuse_devicehub/templates/workbench/settings.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/templates/workbench/settings.html b/ereuse_devicehub/templates/workbench/settings.html index 80278615..6fbeb592 100644 --- a/ereuse_devicehub/templates/workbench/settings.html +++ b/ereuse_devicehub/templates/workbench/settings.html @@ -28,9 +28,9 @@

    Download the settings only for register devices.

    -
    +

    Download the settings for register devices and erase disks. This is less safe and more fast.

    @@ -38,7 +38,7 @@

    Download the settings for register devices and erase disks. This is more safe and more slow.

    From ce001b64aa66b4779f2e5c3891c66c82f69c6727 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 11 May 2022 12:39:23 +0200 Subject: [PATCH 3/7] add test for wbsettings --- tests/test_render_2_0.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 9a52b89f..38dc12cd 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -842,3 +842,27 @@ def test_action_datawipe(user3: UserClientFlask): assert dev.actions[-1].type == 'DataWipe' assert 'Action "DataWipe" created successfully!' in body assert dev.devicehub_id in body + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_wb_settings(user3: UserClientFlask): + uri = '/workbench/settings/' + body, status = user3.get(uri) + + assert status == '200 OK' + assert "Download your settings for Workbench" in body + assert "Workbench Settings" in body + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_wb_settings_register(user3: UserClientFlask): + uri = '/workbench/settings/?opt=register' + body, status = user3.get(uri) + + assert status == '200 OK' + assert "WB_BENCHMARK = False" in body + assert "WB_ERASE = \n" in body + assert "WB_ERASE_STEPS = 0" in body + assert "WB_ERASE_LEADING_ZEROS = False" in body From b8e980cabeab2490aa228824a6ab3ade698b8a7d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 11 May 2022 13:14:11 +0200 Subject: [PATCH 4/7] fixed tests --- .../templates/workbench/wbSettings.ini | 18 ++++++++++++++++++ tests/test_basic.py | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 ereuse_devicehub/templates/workbench/wbSettings.ini diff --git a/ereuse_devicehub/templates/workbench/wbSettings.ini b/ereuse_devicehub/templates/workbench/wbSettings.ini new file mode 100644 index 00000000..ea45e547 --- /dev/null +++ b/ereuse_devicehub/templates/workbench/wbSettings.ini @@ -0,0 +1,18 @@ +[settings] + +DH_TOKEN = {{token}} + +DH_HOST = {{host}} +DH_DATABASE = {{inventory}} +DEVICEHUB_URL = https://${DB_HOST}/${DB_DATABASE}/ + + +WB_BENCHMARK = {{ benchmark }} +WB_STRESS_TEST = {{ stress_test }} +WB_SMART_TEST = short + +WB_ERASE = {{ erase }} +WB_ERASE_STEPS = {{ steps }} +WB_ERASE_LEADING_ZEROS = {{ leading_zeros }} + +WB_DEBUG = True diff --git a/tests/test_basic.py b/tests/test_basic.py index 4329d996..f9960219 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -19,7 +19,7 @@ def test_dependencies(): # Simplejson has a different signature than stdlib json # should be fixed though # noinspection PyUnresolvedReferences - import simplejson + import simplejson # noqa: F401 # noinspection PyArgumentList @@ -87,6 +87,7 @@ def test_api_docs(client: Client): '/users/login/', '/users/logout/', '/versions/', + '/workbench/settings/', } assert docs['info'] == {'title': 'Devicehub', 'version': '0.2'} assert docs['components']['securitySchemes']['bearerAuth'] == { From 763d2d13619e777453f04ff0fc93e1de2d89b49a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 12 May 2022 17:19:22 +0200 Subject: [PATCH 5/7] minimalistic settings --- .../templates/workbench/settings.html | 16 ------------- .../templates/workbench/wbSettings.ini | 18 ++------------ ereuse_devicehub/workbench/views.py | 24 +++---------------- 3 files changed, 5 insertions(+), 53 deletions(-) diff --git a/ereuse_devicehub/templates/workbench/settings.html b/ereuse_devicehub/templates/workbench/settings.html index 6fbeb592..6c762f70 100644 --- a/ereuse_devicehub/templates/workbench/settings.html +++ b/ereuse_devicehub/templates/workbench/settings.html @@ -28,22 +28,6 @@

    Download the settings only for register devices.

    -
    - -
    -

    Download the settings for register devices and erase disks. This is less safe and more fast.

    -
    -
    -
    - -
    -

    Download the settings for register devices and erase disks. This is more safe and more slow.

    -
    -
    diff --git a/ereuse_devicehub/templates/workbench/wbSettings.ini b/ereuse_devicehub/templates/workbench/wbSettings.ini index ea45e547..8ce2cf40 100644 --- a/ereuse_devicehub/templates/workbench/wbSettings.ini +++ b/ereuse_devicehub/templates/workbench/wbSettings.ini @@ -1,18 +1,4 @@ [settings] -DH_TOKEN = {{token}} - -DH_HOST = {{host}} -DH_DATABASE = {{inventory}} -DEVICEHUB_URL = https://${DB_HOST}/${DB_DATABASE}/ - - -WB_BENCHMARK = {{ benchmark }} -WB_STRESS_TEST = {{ stress_test }} -WB_SMART_TEST = short - -WB_ERASE = {{ erase }} -WB_ERASE_STEPS = {{ steps }} -WB_ERASE_LEADING_ZEROS = {{ leading_zeros }} - -WB_DEBUG = True +TOKEN = {{ token }} +URL = {{ url }} diff --git a/ereuse_devicehub/workbench/views.py b/ereuse_devicehub/workbench/views.py index ccbdbdc5..ef0c5f8b 100644 --- a/ereuse_devicehub/workbench/views.py +++ b/ereuse_devicehub/workbench/views.py @@ -35,36 +35,18 @@ class SettingsView(GenericMixView): return flask.render_template(self.template_name, **self.context) def download(self): + url = "https://{}/api/".format(app.config['HOST']) self.wbContext = { 'token': self.get_token(), - 'host': app.config['HOST'], - 'inventory': app.config['SCHEMA'], - 'benchmark': False, - 'stress_test': 1, - 'erase': '', - 'steps': 0, - 'leading_zeros': False, + 'url': url, } - options = {"register": self.register, "soft": self.soft, "hard": self.hard} + options = {"register": self.register} return options[self.opt]() def register(self): data = flask.render_template('workbench/wbSettings.ini', **self.wbContext) return self.response_download(data) - def soft(self): - self.wbContext['erase'] = 'EraseBasic' - self.wbContext['steps'] = 1 - data = flask.render_template('workbench/wbSettings.ini', **self.wbContext) - return self.response_download(data) - - def hard(self): - self.wbContext['erase'] = 'EraseSectors' - self.wbContext['steps'] = 1 - self.wbContext['leading_zeros'] = True - data = flask.render_template('workbench/wbSettings.ini', **self.wbContext) - return self.response_download(data) - def response_download(self, data): bfile = str.encode(data) output = make_response(bfile) From 3a2e0bd61a818642463627c3c2e61f4e80b027b0 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 12 May 2022 17:25:02 +0200 Subject: [PATCH 6/7] fixing bugs --- ereuse_devicehub/workbench/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/workbench/views.py b/ereuse_devicehub/workbench/views.py index ef0c5f8b..95b8c009 100644 --- a/ereuse_devicehub/workbench/views.py +++ b/ereuse_devicehub/workbench/views.py @@ -29,13 +29,13 @@ class SettingsView(GenericMixView): ) self.opt = request.values.get('opt') - if self.opt in ['register', 'soft', 'hard']: + if self.opt in ['register']: return self.download() return flask.render_template(self.template_name, **self.context) def download(self): - url = "https://{}/api/".format(app.config['HOST']) + url = "https://{}/api/inventory/".format(app.config['HOST']) self.wbContext = { 'token': self.get_token(), 'url': url, From 3c02a5480f557a203de4e7ba5844ee483da11fbe Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 12 May 2022 17:42:26 +0200 Subject: [PATCH 7/7] fixed test render wbsettings --- tests/test_render_2_0.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 38dc12cd..083d82ee 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -862,7 +862,6 @@ def test_wb_settings_register(user3: UserClientFlask): body, status = user3.get(uri) assert status == '200 OK' - assert "WB_BENCHMARK = False" in body - assert "WB_ERASE = \n" in body - assert "WB_ERASE_STEPS = 0" in body - assert "WB_ERASE_LEADING_ZEROS = False" in body + assert "TOKEN = " in body + assert "URL = https://" in body + assert "/api/inventory/" in body