diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2015-03-10 12:17:21 +0100 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2015-03-10 12:17:21 +0100 |
commit | cf5151aecf6552faec6232ef0ba57070b546a10d (patch) | |
tree | cac749b72a218e3f4e5bfab83fc37f2b3aa7745b | |
parent | 4f78cd8c245e129dc4e09c0ec39b3152b7109e7f (diff) |
Add base handler for test resources.
-rw-r--r-- | app/handlers/test_base.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/app/handlers/test_base.py b/app/handlers/test_base.py new file mode 100644 index 0000000..e82e368 --- /dev/null +++ b/app/handlers/test_base.py @@ -0,0 +1,91 @@ +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +"""The base for the test related handlers.""" + +try: + import simplejson as json +except ImportError: + import json + +import handlers.base as hbase +import handlers.common as hcommon +import handlers.response as hresponse +import utils.validator as validator + + +# pylint: disable=too-many-public-methods +class TestBaseHandler(hbase.BaseHandler): + """Base class for test related API handlers. + + This class provides methods and functions common to all the test API + endpoint. + """ + + def __init__(self, application, request, **kwargs): + super(TestBaseHandler, self).__init__(application, request, **kwargs) + + @staticmethod + def _token_validation_func(): + return hcommon.valid_token_tests + + def execute_put(self, *args, **kwargs): + """Execute the PUT pre-operations.""" + response = None + + if self.validate_req_token("PUT"): + if kwargs and kwargs.get("id", None): + valid_request = self._valid_post_request() + + if valid_request == 200: + try: + json_obj = json.loads(self.request.body.decode("utf8")) + + valid_json, j_reason = validator.is_valid_json( + json_obj, self._valid_keys("PUT")) + if valid_json: + kwargs["json_obj"] = json_obj + kwargs["db_options"] = self.settings["dboptions"] + kwargs["reason"] = j_reason + response = self._put(*args, **kwargs) + else: + response = hresponse.HandlerResponse(400) + if j_reason: + response.reason = ( + "Provided JSON is not valid: %s" % + j_reason) + else: + response.reason = "Provided JSON is not valid" + except ValueError, ex: + self.log.exception(ex) + error = "No JSON data found in the PUT request" + self.log.error(error) + response = hresponse.HandlerResponse(422) + response.reason = error + else: + response = hresponse.HandlerResponse(valid_request) + response.reason = ( + "%s: %s" % + ( + self._get_status_message(valid_request), + "Use %s as the content type" % self.content_type + ) + ) + else: + response = hresponse.HandlerResponse(400) + response.reason = "No ID specified" + else: + response = hresponse.HandlerResponse(403) + response.reason = hcommon.NOT_VALID_TOKEN + + return response |