From 5b2b99e43eba2e7dfd2694ce165c1945642c3838 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Fri, 20 Oct 2017 15:21:59 +0800 Subject: criteria field must be 'PASS/FAIL' criteria must be fix with 'PASS/FAIL', case insensitive or else, BADREQUEST exception will be raises JIRA: RELENG-327 Change-Id: Ic648b9d38937fc63924e42cfdf5345cdaa32e1ed Signed-off-by: SerenaFeng --- testapi/opnfv_testapi/common/check.py | 13 +++++++++++++ testapi/opnfv_testapi/common/message.py | 4 ++++ testapi/opnfv_testapi/resources/handlers.py | 1 + testapi/opnfv_testapi/resources/result_handlers.py | 8 +++++++- testapi/opnfv_testapi/tests/unit/resources/test_result.py | 9 ++++++++- 5 files changed, 33 insertions(+), 2 deletions(-) (limited to 'testapi') diff --git a/testapi/opnfv_testapi/common/check.py b/testapi/opnfv_testapi/common/check.py index e80b1c6..1dff37a 100644 --- a/testapi/opnfv_testapi/common/check.py +++ b/testapi/opnfv_testapi/common/check.py @@ -102,6 +102,19 @@ def carriers_exist(xstep): return wrap +def values_check(xstep): + @functools.wraps(xstep) + def wrap(self, *args, **kwargs): + checks = kwargs.pop('values_check', {}) + if checks: + for field, check, options in checks: + if not check(field, options): + raises.BadRequest(message.invalid_value(field, options)) + ret = yield gen.coroutine(xstep)(self, *args, **kwargs) + raise gen.Return(ret) + return wrap + + def new_not_exists(xstep): @functools.wraps(xstep) def wrap(self, *args, **kwargs): diff --git a/testapi/opnfv_testapi/common/message.py b/testapi/opnfv_testapi/common/message.py index 8b5c3fb..3e14f72 100644 --- a/testapi/opnfv_testapi/common/message.py +++ b/testapi/opnfv_testapi/common/message.py @@ -26,6 +26,10 @@ def missing(name): return '{} Missing'.format(name) +def invalid_value(name, options): + return '{} must be in {}'.format(name, options) + + def exist(key, value): return '{} [{}] {}'.format(key, value, exist_base) diff --git a/testapi/opnfv_testapi/resources/handlers.py b/testapi/opnfv_testapi/resources/handlers.py index 8e5dab2..6c7a819 100644 --- a/testapi/opnfv_testapi/resources/handlers.py +++ b/testapi/opnfv_testapi/resources/handlers.py @@ -79,6 +79,7 @@ class GenericApiHandler(web.RequestHandler): @check.valid_token @check.no_body @check.miss_fields + @check.values_check @check.carriers_exist @check.new_not_exists def _create(self, **kwargs): diff --git a/testapi/opnfv_testapi/resources/result_handlers.py b/testapi/opnfv_testapi/resources/result_handlers.py index e202f5c..a258528 100644 --- a/testapi/opnfv_testapi/resources/result_handlers.py +++ b/testapi/opnfv_testapi/resources/result_handlers.py @@ -215,12 +215,18 @@ class ResultsCLHandler(GenericResultHandler): return {'project_name': self.json_args.get('project_name'), 'name': self.json_args.get('case_name')} + def options_check(field, options): + return self.json_args.get(field).upper() in options + miss_fields = ['pod_name', 'project_name', 'case_name'] carriers = [('pods', pod_query), ('projects', project_query), ('testcases', testcase_query)] + values_check = [('criteria', options_check, ['PASS', 'FAIL'])] - self._create(miss_fields=miss_fields, carriers=carriers) + self._create(miss_fields=miss_fields, + carriers=carriers, + values_check=values_check) class ResultsUploadHandler(ResultsCLHandler): diff --git a/testapi/opnfv_testapi/tests/unit/resources/test_result.py b/testapi/opnfv_testapi/tests/unit/resources/test_result.py index 1df31f3..6c1a07a 100644 --- a/testapi/opnfv_testapi/tests/unit/resources/test_result.py +++ b/testapi/opnfv_testapi/tests/unit/resources/test_result.py @@ -62,7 +62,7 @@ class TestResultBase(base.TestBase): self.version = 'C' self.build_tag = 'v3.0' self.scenario = 'odl-l2' - self.criteria = 'passed' + self.criteria = 'PASS' self.trust_indicator = result_models.TI(0.7) self.start_date = str(datetime.now()) self.stop_date = str(datetime.now() + timedelta(minutes=1)) @@ -170,6 +170,13 @@ class TestResultCreate(TestResultBase): req.case_name = None return req + @executor.create(httplib.BAD_REQUEST, + message.invalid_value('criteria', ['PASS', 'FAIL'])) + def test_invalid_criteria(self): + req = self.req_d + req.criteria = 'invalid' + return req + @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noPod(self): req = self.req_d -- cgit 1.2.3-korg