diff options
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/resources')
6 files changed, 47 insertions, 50 deletions
diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py index 8255b526a..c2b1a6476 100644 --- a/utils/test/testapi/opnfv_testapi/resources/handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py @@ -28,9 +28,11 @@ from tornado import gen from tornado import web import models -from opnfv_testapi.common import constants +from opnfv_testapi.common import raises from opnfv_testapi.tornado_swagger import swagger +DEFAULT_REPRESENTATION = "application/json" + class GenericApiHandler(web.RequestHandler): def __init__(self, application, request, **kwargs): @@ -50,18 +52,16 @@ class GenericApiHandler(web.RequestHandler): if self.request.method != "GET" and self.request.method != "DELETE": if self.request.headers.get("Content-Type") is not None: if self.request.headers["Content-Type"].startswith( - constants.DEFAULT_REPRESENTATION): + DEFAULT_REPRESENTATION): try: self.json_args = json.loads(self.request.body) except (ValueError, KeyError, TypeError) as error: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, - "Bad Json format [{}]". - format(error)) + raises.BadRequest("Bad Json format [{}]".format(error)) def finish_request(self, json_object=None): if json_object: self.write(json.dumps(json_object)) - self.set_header("Content-Type", constants.DEFAULT_REPRESENTATION) + self.set_header("Content-Type", DEFAULT_REPRESENTATION) self.finish() def _create_response(self, resource): @@ -81,19 +81,15 @@ class GenericApiHandler(web.RequestHandler): try: token = self.request.headers['X-Auth-Token'] except KeyError: - raise web.HTTPError(constants.HTTP_UNAUTHORIZED, - "No Authentication Header.") + raises.Unauthorized("No Authentication Header.") query = {'access_token': token} check = yield self._eval_db_find_one(query, 'tokens') if not check: - raise web.HTTPError(constants.HTTP_FORBIDDEN, - "Invalid Token.") + raises.Forbidden("Invalid Token.") ret = yield gen.coroutine(method)(self, *args, **kwargs) raise gen.Return(ret) return wrapper - @web.asynchronous - @gen.coroutine @authenticate def _create(self, miss_checks, db_checks, **kwargs): """ @@ -101,14 +97,13 @@ class GenericApiHandler(web.RequestHandler): :param db_checks: [(table, exist, query, error)] """ if self.json_args is None: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, "no body") + raises.BadRequest('no body') data = self.table_cls.from_dict(self.json_args) for miss in miss_checks: miss_data = data.__getattribute__(miss) if miss_data is None or miss_data == '': - raise web.HTTPError(constants.HTTP_BAD_REQUEST, - '{} missing'.format(miss)) + raises.BadRequest('{} missing'.format(miss)) for k, v in kwargs.iteritems(): data.__setattr__(k, v) @@ -117,7 +112,7 @@ class GenericApiHandler(web.RequestHandler): check = yield self._eval_db_find_one(query(data), table) if (exist and not check) or (not exist and check): code, message = error(data) - raise web.HTTPError(code, message) + raises.CodeTBD(code, message) if self.table != 'results': data.creation_date = datetime.now() @@ -153,37 +148,30 @@ class GenericApiHandler(web.RequestHandler): def _get_one(self, query): data = yield self._eval_db_find_one(query) if data is None: - raise web.HTTPError(constants.HTTP_NOT_FOUND, - "[{}] not exist in table [{}]" - .format(query, self.table)) + raises.NotFound("[{}] not exist in table [{}]" + .format(query, self.table)) self.finish_request(self.format_data(data)) - @web.asynchronous - @gen.coroutine @authenticate def _delete(self, query): data = yield self._eval_db_find_one(query) if data is None: - raise web.HTTPError(constants.HTTP_NOT_FOUND, - "[{}] not exit in table [{}]" - .format(query, self.table)) + raises.NotFound("[{}] not exit in table [{}]" + .format(query, self.table)) yield self._eval_db(self.table, 'remove', query) self.finish_request() - @web.asynchronous - @gen.coroutine @authenticate def _update(self, query, db_keys): if self.json_args is None: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, "No payload") + raises.BadRequest("No payload") # check old data exist from_data = yield self._eval_db_find_one(query) if from_data is None: - raise web.HTTPError(constants.HTTP_NOT_FOUND, - "{} could not be found in table [{}]" - .format(query, self.table)) + raises.NotFound("{} could not be found in table [{}]" + .format(query, self.table)) data = self.table_cls.from_dict(from_data) # check new data exist @@ -191,9 +179,8 @@ class GenericApiHandler(web.RequestHandler): if not equal: to_data = yield self._eval_db_find_one(new_query) if to_data is not None: - raise web.HTTPError(constants.HTTP_FORBIDDEN, - "{} already exists in table [{}]" - .format(new_query, self.table)) + raises.Forbidden("{} already exists in table [{}]" + .format(new_query, self.table)) # we merge the whole document """ edit_request = self._update_requests(data) @@ -210,7 +197,7 @@ class GenericApiHandler(web.RequestHandler): request = self._update_request(request, k, v, data.__getattribute__(k)) if not request: - raise web.HTTPError(constants.HTTP_FORBIDDEN, "Nothing to update") + raises.Forbidden("Nothing to update") edit_request = data.format() edit_request.update(request) diff --git a/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py b/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py index 65c27f60a..fd9ce3eb5 100644 --- a/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py @@ -6,8 +6,9 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import httplib + import handlers -from opnfv_testapi.common import constants from opnfv_testapi.tornado_swagger import swagger import pod_models @@ -46,7 +47,7 @@ class PodCLHandler(GenericPodHandler): def error(data): message = '{} already exists as a pod'.format(data.name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.table, False, query, error)] diff --git a/utils/test/testapi/opnfv_testapi/resources/project_handlers.py b/utils/test/testapi/opnfv_testapi/resources/project_handlers.py index f3521961d..087bb8af2 100644 --- a/utils/test/testapi/opnfv_testapi/resources/project_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/project_handlers.py @@ -6,8 +6,9 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import httplib + import handlers -from opnfv_testapi.common import constants from opnfv_testapi.tornado_swagger import swagger import project_models @@ -48,7 +49,7 @@ class ProjectCLHandler(GenericProjectHandler): def error(data): message = '{} already exists as a project'.format(data.name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.table, False, query, error)] diff --git a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py index d41ba4820..3e78057ce 100644 --- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py @@ -8,11 +8,11 @@ ############################################################################## from datetime import datetime from datetime import timedelta +import httplib from bson import objectid -from tornado import web -from opnfv_testapi.common import constants +from opnfv_testapi.common import raises from opnfv_testapi.resources import handlers from opnfv_testapi.resources import result_models from opnfv_testapi.tornado_swagger import swagger @@ -30,8 +30,7 @@ class GenericResultHandler(handlers.GenericApiHandler): try: value = int(value) except: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, - '{} must be int'.format(key)) + raises.BadRequest('{} must be int'.format(key)) return value def set_query(self): @@ -146,14 +145,14 @@ class ResultsCLHandler(GenericResultHandler): def pod_error(data): message = 'Could not find pod [{}]'.format(data.pod_name) - return constants.HTTP_NOT_FOUND, message + return httplib.NOT_FOUND, message def project_query(data): return {'name': data.project_name} def project_error(data): message = 'Could not find project [{}]'.format(data.project_name) - return constants.HTTP_NOT_FOUND, message + return httplib.NOT_FOUND, message def testcase_query(data): return {'project_name': data.project_name, 'name': data.case_name} @@ -161,7 +160,7 @@ class ResultsCLHandler(GenericResultHandler): def testcase_error(data): message = 'Could not find testcase [{}] in project [{}]'\ .format(data.case_name, data.project_name) - return constants.HTTP_NOT_FOUND, message + return httplib.NOT_FOUND, message miss_checks = ['pod_name', 'project_name', 'case_name'] db_checks = [('pods', True, pod_query, pod_error), diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py index 083bf59fc..9d0233c77 100644 --- a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py @@ -1,4 +1,7 @@ -from opnfv_testapi.common import constants +import functools +import httplib + +from opnfv_testapi.common import raises from opnfv_testapi.resources import handlers import opnfv_testapi.resources.scenario_models as models from opnfv_testapi.tornado_swagger import swagger @@ -80,7 +83,7 @@ class ScenariosCLHandler(GenericScenarioHandler): def error(data): message = '{} already exists as a scenario'.format(data.name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.table, False, query, error)] @@ -158,18 +161,21 @@ class ScenarioGURHandler(GenericScenarioHandler): return data.format() def _iter_installers(xstep): + @functools.wraps(xstep) def magic(self, data): [xstep(self, installer) for installer in self._filter_installers(data.installers)] return magic def _iter_versions(xstep): + @functools.wraps(xstep) def magic(self, installer): [xstep(self, version) for version in (self._filter_versions(installer.versions))] return magic def _iter_projects(xstep): + @functools.wraps(xstep) def magic(self, version): [xstep(self, project) for project in (self._filter_projects(version.projects))] @@ -177,6 +183,8 @@ class ScenarioGURHandler(GenericScenarioHandler): def _update_requests_rename(self, data): data.name = self._term.get('name') + if not data.name: + raises.BadRequest("new scenario name is not provided") def _update_requests_add_installer(self, data): data.installers.append(models.ScenarioInstaller.from_dict(self._term)) diff --git a/utils/test/testapi/opnfv_testapi/resources/testcase_handlers.py b/utils/test/testapi/opnfv_testapi/resources/testcase_handlers.py index 3debd6918..1211a0573 100644 --- a/utils/test/testapi/opnfv_testapi/resources/testcase_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/testcase_handlers.py @@ -6,7 +6,8 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from opnfv_testapi.common import constants +import httplib + from opnfv_testapi.resources import handlers from opnfv_testapi.resources import testcase_models from opnfv_testapi.tornado_swagger import swagger @@ -58,12 +59,12 @@ class TestcaseCLHandler(GenericTestcaseHandler): def p_error(data): message = 'Could not find project [{}]'.format(data.project_name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message def tc_error(data): message = '{} already exists as a testcase in project {}'\ .format(data.name, data.project_name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.db_projects, True, p_query, p_error), |