diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index 74147824..acf6efa2 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..6c762f70 --- /dev/null +++ b/ereuse_devicehub/templates/workbench/settings.html @@ -0,0 +1,43 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
    +

    {{ title }}

    + +
    + +
    + +
    +{% endblock main %} diff --git a/ereuse_devicehub/templates/workbench/wbSettings.ini b/ereuse_devicehub/templates/workbench/wbSettings.ini new file mode 100644 index 00000000..8ce2cf40 --- /dev/null +++ b/ereuse_devicehub/templates/workbench/wbSettings.ini @@ -0,0 +1,4 @@ +[settings] + +TOKEN = {{ token }} +URL = {{ url }} 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..95b8c009 --- /dev/null +++ b/ereuse_devicehub/workbench/views.py @@ -0,0 +1,76 @@ +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']: + return self.download() + + return flask.render_template(self.template_name, **self.context) + + def download(self): + url = "https://{}/api/inventory/".format(app.config['HOST']) + self.wbContext = { + 'token': self.get_token(), + 'url': url, + } + 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 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]) 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'] == { diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 9a52b89f..083d82ee 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -842,3 +842,26 @@ 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 "TOKEN = " in body + assert "URL = https://" in body + assert "/api/inventory/" in body