]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard_v2: Refactoring of unit tests
authorRicardo Dias <rdias@suse.com>
Thu, 25 Jan 2018 16:48:03 +0000 (16:48 +0000)
committerRicardo Dias <rdias@suse.com>
Mon, 5 Mar 2018 13:07:00 +0000 (13:07 +0000)
Signed-off-by: Ricardo Dias <rdias@suse.com>
src/pybind/mgr/dashboard_v2/tests/helper.py [new file with mode: 0644]
src/pybind/mgr/dashboard_v2/tests/test_auth.py
src/pybind/mgr/dashboard_v2/tests/test_ping.py
src/pybind/mgr/dashboard_v2/tests/test_tools.py

diff --git a/src/pybind/mgr/dashboard_v2/tests/helper.py b/src/pybind/mgr/dashboard_v2/tests/helper.py
new file mode 100644 (file)
index 0000000..c6f2f79
--- /dev/null
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+
+import json
+
+import cherrypy
+from cherrypy.test import helper
+
+from ..module import Module
+from ..tools import load_controller
+
+
+class ApiControllerTestCase(helper.CPWebCase):
+    @staticmethod
+    def setup_test(controllers, authentication=True):
+        module = Module('dashboard', None, None)
+        ApiControllerTestCase._mgr_module = module
+        for ctrl in controllers:
+            cls = load_controller(module, ctrl)
+            if not authentication:
+                cls._cp_config['tools.autenticate.on'] = False
+            cherrypy.tree.mount(cls(), '/api/{}'.format(cls._cp_path_))
+
+    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 _get(self, url):
+        self._request(url, 'GET')
+
+    def _post(self, url, data=None):
+        self._request(url, 'POST', data)
+
+    def _delete(self, url, data=None):
+        self._request(url, 'DELETE', data)
+
+    def _put(self, url, data=None):
+        self._request(url, 'PUT', data)
index bde0b72e23589117610c8af58837742b474bc4f9..b55c643875d7e00f40f80f9bcdf18af111f154c4 100644 (file)
@@ -2,15 +2,14 @@
 
 from __future__ import absolute_import
 
-import json
 import time
 
+import cherrypy
 from cherrypy.lib.sessions import RamSession
-from cherrypy.test import helper
 from mock import patch
 
-from ..module import Module, cherrypy
-from ..tools import load_controller
+from .helper import ApiControllerTestCase
+from ..controllers.auth import Auth
 
 class Ping(object):
     @cherrypy.expose
@@ -19,38 +18,20 @@ class Ping(object):
         pass
 
 
-class AuthTest(helper.CPWebCase):
+class AuthTest(ApiControllerTestCase):
     @staticmethod
     def setup_server():
-        module = Module('dashboard', None, None)
-        AuthTest.Auth = load_controller(module, 'Auth')
+        ApiControllerTestCase.setup_test(['Auth'])
+        module = ApiControllerTestCase._mgr_module
 
         cherrypy.tools.autenticate = cherrypy.Tool('before_handler',
-                                                   AuthTest.Auth.check_auth)
+                                                   Auth.check_auth)
 
-        cherrypy.tree.mount(AuthTest.Auth(), "/api/auth")
         cherrypy.tree.mount(Ping(), "/api/test",
                             config={'/': {'tools.autenticate.on': True}})
         module.set_localized_config('session-expire','2')
         module.set_localized_config('username','admin')
-        pass_hash = AuthTest.Auth.password_hash('admin')
-        module.set_localized_config('password', pass_hash)
-
-    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 _delete(self, url, data=None):
-        self._request(url, 'DELETE', data)
+        module.set_localized_config('password', Auth.password_hash('admin'))
 
     def test_login_valid(self):
         sess_mock = RamSession()
@@ -58,8 +39,7 @@ class AuthTest(helper.CPWebCase):
             self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
             self.assertStatus('201 Created')
             self.assertBody('{"username": "admin"}')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY),
-                              'admin')
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
 
     def test_login_invalid(self):
         sess_mock = RamSession()
@@ -67,38 +47,35 @@ class AuthTest(helper.CPWebCase):
             self._post("/api/auth", {'username': 'admin', 'password': 'inval'})
             self.assertStatus('403 Forbidden')
             self.assertBody('{"detail": "Invalid credentials"}')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY), None)
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
 
     def test_logout(self):
         sess_mock = RamSession()
         with patch('cherrypy.session', sess_mock, create=True):
             self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY),
-                              'admin')
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
             self._delete("/api/auth")
             self.assertStatus('204 No Content')
             self.assertBody('')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY), None)
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
 
     def test_session_expire(self):
         sess_mock = RamSession()
         with patch('cherrypy.session', sess_mock, create=True):
             self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
             self.assertStatus('201 Created')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY),
-                              'admin')
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
             self._post("/api/test/ping")
             self.assertStatus('200 OK')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY),
-                              'admin')
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
             time.sleep(3)
             self._post("/api/test/ping")
             self.assertStatus('401 Unauthorized')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY), None)
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
 
     def test_unauthorized(self):
         sess_mock = RamSession()
         with patch('cherrypy.session', sess_mock, create=True):
             self._post("/api/test/ping")
             self.assertStatus('401 Unauthorized')
-            self.assertEquals(sess_mock.get(AuthTest.Auth.SESSION_KEY), None)
+            self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
index 721431b2f267efc0a3a3b3c40e51a7651674f47a..748e415ce12ab8e3b8ce981304be6a44eb518d88 100644 (file)
@@ -2,44 +2,17 @@
 
 from __future__ import absolute_import
 
-import json
+from .helper import ApiControllerTestCase
 
-from cherrypy.test import helper
-
-from ..controllers.auth import Auth
-from ..module import Module, cherrypy
-from ..tools import load_controller
-
-class SimpleCPTest(helper.CPWebCase):
+class SimpleCPTest(ApiControllerTestCase):
     @staticmethod
     def setup_server():
-
-        cherrypy.tools.autenticate = cherrypy.Tool('before_handler',
-                                                   Auth.check_auth)
-        module = Module('attic', None, None)
-        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)
+        ApiControllerTestCase.setup_test(['Ping', 'Echo', 'EchoArgs'],
+                                         authentication=False)
 
     def test_ping(self):
         self.getPage("/api/ping")
-        self.assertStatus('401 Unauthorized')
+        self.assertStatus('200 OK')
 
     def test_echo(self):
         self._post("/api/echo2", {'msg': 'Hello World'})
index c9057afad37a8e2c8aa1c977d04fb4ba5f524cba..a8a2feac224a5bbb7c447389741031a866340072 100644 (file)
@@ -1,12 +1,13 @@
+# -*- coding: utf-8 -*-
 from __future__ import absolute_import
 
 import json
 
 import cherrypy
 from cherrypy.lib.sessions import RamSession
-from cherrypy.test import helper
 from mock import patch
 
+from .helper import ApiControllerTestCase
 from ..tools import RESTController
 
 
@@ -33,15 +34,16 @@ class FooResource(RESTController):
 class Root(object):
     foo = FooResource()
 
-class RESTControllerTest(helper.CPWebCase):
+class RESTControllerTest(ApiControllerTestCase):
     @staticmethod
     def setup_server():
+        ApiControllerTestCase.setup_test([])
         cherrypy.tree.mount(Root())
 
     def test_empty(self):
-        self.getPage("/foo", method='DELETE')
+        self._delete("/foo")
         self.assertStatus(204)
-        self.getPage("/foo")
+        self._get("/foo")
         self.assertStatus('200 OK')
         self.assertHeader('Content-Type', 'application/json')
         self.assertBody('[]')
@@ -51,16 +53,12 @@ class RESTControllerTest(helper.CPWebCase):
         with patch('cherrypy.session', sess_mock, create=True):
             body = json.dumps({'a': 'b'})
             for _ in range(5):
-
-                self.getPage("/foo", method='POST', body=body, headers=[
-                    ('Content-Type', 'application/json'),
-                    ('Content-Length', str(len(body)))
-                ])
+                self._post("/foo", {'a': 'b'})
                 self.assertBody(body)
                 self.assertStatus(201)
                 self.assertHeader('Content-Type', 'application/json')
 
-            self.getPage("/foo")
+            self._get("/foo")
             self.assertStatus('200 OK')
             self.assertHeader('Content-Type', 'application/json')
             self.assertBody(json.dumps([{'a': 'b'}] * 5))