--- /dev/null
+# -*- 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)
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
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()
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()
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)
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'})
+# -*- 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
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('[]')
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))