summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-03-30 15:33:19 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-03-30 15:36:50 +0800
commitb9e0948256af817e63f901ec5277d3ef9fe36fdd (patch)
treec528e079c1e2eb05fcb742d64a71090797123023 /utils/test/testapi
parent6f84a256bbe92cf3f9f746ca0972abd9942bad43 (diff)
unify raise exception process
Change-Id: I109ef9550a57e218b6f5c9196812b33133d34134 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/testapi')
-rw-r--r--utils/test/testapi/opnfv_testapi/common/raises.py31
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/handlers.py43
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/result_handlers.py5
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py6
4 files changed, 52 insertions, 33 deletions
diff --git a/utils/test/testapi/opnfv_testapi/common/raises.py b/utils/test/testapi/opnfv_testapi/common/raises.py
new file mode 100644
index 000000000..ed3a84ee0
--- /dev/null
+++ b/utils/test/testapi/opnfv_testapi/common/raises.py
@@ -0,0 +1,31 @@
+import httplib
+
+from tornado import web
+
+
+class Raiser(object):
+ code = httplib.OK
+
+ def __init__(self, reason):
+ raise web.HTTPError(self.code, reason)
+
+
+class BadRequest(Raiser):
+ code = httplib.BAD_REQUEST
+
+
+class Forbidden(Raiser):
+ code = httplib.FORBIDDEN
+
+
+class NotFound(Raiser):
+ code = httplib.NOT_FOUND
+
+
+class Unauthorized(Raiser):
+ code = httplib.UNAUTHORIZED
+
+
+class CodeTBD(object):
+ def __init__(self, code, reason):
+ raise web.HTTPError(code, reason)
diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py
index bf8a92b54..c2b1a6476 100644
--- a/utils/test/testapi/opnfv_testapi/resources/handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py
@@ -22,13 +22,13 @@
from datetime import datetime
import functools
-import httplib
import json
from tornado import gen
from tornado import web
import models
+from opnfv_testapi.common import raises
from opnfv_testapi.tornado_swagger import swagger
DEFAULT_REPRESENTATION = "application/json"
@@ -56,9 +56,7 @@ class GenericApiHandler(web.RequestHandler):
try:
self.json_args = json.loads(self.request.body)
except (ValueError, KeyError, TypeError) as error:
- raise web.HTTPError(httplib.BAD_REQUEST,
- "Bad Json format [{}]".
- format(error))
+ raises.BadRequest("Bad Json format [{}]".format(error))
def finish_request(self, json_object=None):
if json_object:
@@ -83,13 +81,11 @@ class GenericApiHandler(web.RequestHandler):
try:
token = self.request.headers['X-Auth-Token']
except KeyError:
- raise web.HTTPError(httplib.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(httplib.FORBIDDEN,
- "Invalid Token.")
+ raises.Forbidden("Invalid Token.")
ret = yield gen.coroutine(method)(self, *args, **kwargs)
raise gen.Return(ret)
return wrapper
@@ -101,14 +97,13 @@ class GenericApiHandler(web.RequestHandler):
:param db_checks: [(table, exist, query, error)]
"""
if self.json_args is None:
- raise web.HTTPError(httplib.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(httplib.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,18 +148,16 @@ class GenericApiHandler(web.RequestHandler):
def _get_one(self, query):
data = yield self._eval_db_find_one(query)
if data is None:
- raise web.HTTPError(httplib.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))
@authenticate
def _delete(self, query):
data = yield self._eval_db_find_one(query)
if data is None:
- raise web.HTTPError(httplib.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()
@@ -172,14 +165,13 @@ class GenericApiHandler(web.RequestHandler):
@authenticate
def _update(self, query, db_keys):
if self.json_args is None:
- raise web.HTTPError(httplib.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(httplib.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
@@ -187,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(httplib.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)
@@ -206,7 +197,7 @@ class GenericApiHandler(web.RequestHandler):
request = self._update_request(request, k, v,
data.__getattribute__(k))
if not request:
- raise web.HTTPError(httplib.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/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py
index 44b9f8c07..3e78057ce 100644
--- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py
@@ -11,8 +11,8 @@ from datetime import timedelta
import httplib
from bson import objectid
-from tornado import web
+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(httplib.BAD_REQUEST,
- '{} must be int'.format(key))
+ raises.BadRequest('{} must be int'.format(key))
return value
def set_query(self):
diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py
index a2856dbd7..9d0233c77 100644
--- a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py
@@ -1,8 +1,7 @@
import functools
import httplib
-from tornado import web
-
+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
@@ -185,8 +184,7 @@ class ScenarioGURHandler(GenericScenarioHandler):
def _update_requests_rename(self, data):
data.name = self._term.get('name')
if not data.name:
- raise web.HTTPError(httplib.BAD_REQUEST,
- "new scenario name is not provided")
+ raises.BadRequest("new scenario name is not provided")
def _update_requests_add_installer(self, data):
data.installers.append(models.ScenarioInstaller.from_dict(self._term))