From ec578193869a89ae6d04df92724a55c127ae281b Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Thu, 25 Jan 2018 11:44:01 +0000 Subject: [PATCH] mgr/dashboard_v2: Very simple ping example Signed-off-by: Ricardo Dias --- .../mgr/dashboard_v2/controllers/ping.py | 28 +++++++++++++ .../mgr/dashboard_v2/tests/test_ping.py | 40 +++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/pybind/mgr/dashboard_v2/controllers/ping.py diff --git a/src/pybind/mgr/dashboard_v2/controllers/ping.py b/src/pybind/mgr/dashboard_v2/controllers/ping.py new file mode 100644 index 00000000000..f3d96d85700 --- /dev/null +++ b/src/pybind/mgr/dashboard_v2/controllers/ping.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import + +import cherrypy + +from ..restresource import RESTResource +from ..tools import ApiController, AuthRequired + + +@ApiController('ping') +@AuthRequired() +class Ping(object): + @cherrypy.expose + def default(self, *args): + return "pong" + + +@ApiController('echo1') +class EchoArgs(RESTResource): + @RESTResource.args_from_json + def create(self, msg): + return {'echo': msg} + + +@ApiController('echo2') +class Echo(RESTResource): + def create(self, data): + return {'echo': data['msg']} diff --git a/src/pybind/mgr/dashboard_v2/tests/test_ping.py b/src/pybind/mgr/dashboard_v2/tests/test_ping.py index cca2241ba97..721431b2f26 100644 --- a/src/pybind/mgr/dashboard_v2/tests/test_ping.py +++ b/src/pybind/mgr/dashboard_v2/tests/test_ping.py @@ -2,18 +2,50 @@ from __future__ import absolute_import +import json + from cherrypy.test import helper +from ..controllers.auth import Auth from ..module import Module, cherrypy +from ..tools import load_controller class SimpleCPTest(helper.CPWebCase): @staticmethod def setup_server(): + + cherrypy.tools.autenticate = cherrypy.Tool('before_handler', + Auth.check_auth) module = Module('attic', None, None) - cherrypy.tree.mount(Module.HelloWorld(module), "/api/hello") + Ping = load_controller(module, 'Ping') + Echo = load_controller(module, 'Echo') + EchoArgs = load_controller(module, 'EchoArgs') + cherrypy.tree.mount(Ping(), "/api/ping") + cherrypy.tree.mount(Echo(), "/api/echo2") + cherrypy.tree.mount(EchoArgs(), "/api/echo1") + + def _request(self, url, method, data=None): + if not data: + b = None + h = None + else: + b = json.dumps(data) + h = [('Content-Type', 'application/json'), + ('Content-Length', str(len(b)))] + self.getPage(url, method=method, body=b, headers=h) + + def _post(self, url, data=None): + self._request(url, 'POST', data) def test_ping(self): - self.getPage("/api/hello/ping") - self.assertStatus('200 OK') - self.assertBody('"pong"') + self.getPage("/api/ping") + self.assertStatus('401 Unauthorized') + + def test_echo(self): + self._post("/api/echo2", {'msg': 'Hello World'}) + self.assertStatus('201 Created') + + def test_echo_args(self): + self._post("/api/echo1", {'msg': 'Hello World'}) + self.assertStatus('201 Created') -- 2.39.5