diff --git a/tests/unit/test_system.py b/tests/unit/test_system.py new file mode 100644 index 0000000..7cbdbec --- /dev/null +++ b/tests/unit/test_system.py @@ -0,0 +1,70 @@ +from __future__ import unicode_literals +import json +import httpretty +from httpretty import GET + +# TEMP: Temporary hack until httpretty string checking is fixed +if httpretty.compat.PY3: + httpretty.core.basestring = (bytes, str) + +try: + import unittest2 as unittest # Python2.6 +except ImportError: + import unittest + +import trovebox + +class TestSystem(unittest.TestCase): + test_host = "test.example.com" + + def setUp(self): + self.client = trovebox.Trovebox(host=self.test_host) + + @staticmethod + def _return_value(result, message="", code=200): + return json.dumps({"message": message, "code": code, "result": result}) + +class TestSystemVersion(TestSystem): + test_result = {"api": "v2", + "database": "2.0.0"} + + @httpretty.activate + def test_version(self): + """Check that the version dictionary is returned correctly""" + httpretty.register_uri(GET, uri="http://test.example.com/system/version.json", + body=self._return_value(self.test_result), + status=200) + response = self.client.system.version() + + self.assertEqual(response, self.test_result) + +class TestSystemDiagnostics(TestSystem): + test_result = {'database': [{'label': 'failure', + 'message': 'Could not properly connect to the database.', + 'status': False}], + } + + @httpretty.activate + def test_diagnostics_pass(self): + """Check that the diagnostics dictionary is returned correctly on success""" + httpretty.register_uri(GET, uri="http://test.example.com/system/diagnostics.json", + body=self._return_value(self.test_result), + status=200) + response = self.client.system.diagnostics() + + self.assertEqual(response, self.test_result) + + @httpretty.activate + def test_diagnostics_fail(self): + """ + Check that the diagnostics dictionary is returned correctly on failure. + Although the JSON code is 500, no exception should be raised. + """ + # On failure, the diagnostics endpoint returns a JSON code of 500 + # and a response status code of 200. + httpretty.register_uri(GET, uri="http://test.example.com/system/diagnostics.json", + body=self._return_value(self.test_result, code=500), + status=200) + response = self.client.system.diagnostics() + + self.assertEqual(response, self.test_result) diff --git a/trovebox/__init__.py b/trovebox/__init__.py index 73e2af5..e49df85 100644 --- a/trovebox/__init__.py +++ b/trovebox/__init__.py @@ -9,6 +9,7 @@ from trovebox.api import api_tag from trovebox.api import api_album from trovebox.api import api_action from trovebox.api import api_activity +from trovebox.api import api_system LATEST_API_VERSION = 2 @@ -41,3 +42,4 @@ class Trovebox(Http): self.action = api_action.ApiAction(self) self.activities = api_activity.ApiActivities(self) self.activity = api_activity.ApiActivity(self) + self.system = api_system.ApiSystem(self) diff --git a/trovebox/api/api_system.py b/trovebox/api/api_system.py new file mode 100644 index 0000000..eef087d --- /dev/null +++ b/trovebox/api/api_system.py @@ -0,0 +1,27 @@ +""" +api_system.py : Trovebox System API Classes +""" +from .api_base import ApiBase + +class ApiSystem(ApiBase): + """ Definitions of /system/ API endpoints """ + def version(self, **kwds): + """ + Endpoint: /system/version.json + + Returns a dictionary containing the various server version strings + """ + return self._client.get("/system/version.json", **kwds)["result"] + + def diagnostics(self, **kwds): + """ + Endpoint: /system/diagnostics.json + + Runs a set of diagnostic tests on the server. + Returns a dictionary containing the results. + """ + # Don't process the result automatically, since this raises an exception + # on failure, which doesn't provide the cause of the failure + self._client.get("/system/diagnostics.json", process_response=False, + **kwds) + return self._client.last_response.json()["result"]