diff options
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/common')
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/__init__.py | 8 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/check.py | 148 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/config.py | 63 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/constants.py | 4 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/message.py | 62 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/raises.py | 43 |
6 files changed, 0 insertions, 328 deletions
diff --git a/utils/test/testapi/opnfv_testapi/common/__init__.py b/utils/test/testapi/opnfv_testapi/common/__init__.py deleted file mode 100644 index 05c0c9392..000000000 --- a/utils/test/testapi/opnfv_testapi/common/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -############################################################################## -# Copyright (c) 2015 Orange -# guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## diff --git a/utils/test/testapi/opnfv_testapi/common/check.py b/utils/test/testapi/opnfv_testapi/common/check.py deleted file mode 100644 index 667578fed..000000000 --- a/utils/test/testapi/opnfv_testapi/common/check.py +++ /dev/null @@ -1,148 +0,0 @@ -############################################################################## -# Copyright (c) 2017 ZTE Corp -# feng.xiaowei@zte.com.cn -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## -import functools -import re - -from tornado import gen - -from opnfv_testapi.common import constants -from opnfv_testapi.common import message -from opnfv_testapi.common import raises -from opnfv_testapi.common.config import CONF -from opnfv_testapi.db import api as dbapi - - -def is_authorized(method): - @functools.wraps(method) - def wrapper(self, *args, **kwargs): - if CONF.api_authenticate and self.table in ['pods']: - testapi_id = self.get_secure_cookie(constants.TESTAPI_ID) - if not testapi_id: - raises.Unauthorized(message.not_login()) - user_info = yield dbapi.db_find_one('users', {'user': testapi_id}) - if not user_info: - raises.Unauthorized(message.not_lfid()) - kwargs['owner'] = testapi_id - ret = yield gen.coroutine(method)(self, *args, **kwargs) - raise gen.Return(ret) - return wrapper - - -def valid_token(method): - @functools.wraps(method) - def wrapper(self, *args, **kwargs): - if self.auth and self.table == 'results': - try: - token = self.request.headers['X-Auth-Token'] - except KeyError: - raises.Unauthorized(message.unauthorized()) - query = {'access_token': token} - check = yield dbapi.db_find_one('tokens', query) - if not check: - raises.Forbidden(message.invalid_token()) - ret = yield gen.coroutine(method)(self, *args, **kwargs) - raise gen.Return(ret) - return wrapper - - -def not_exist(xstep): - @functools.wraps(xstep) - def wrap(self, *args, **kwargs): - query = kwargs.get('query') - data = yield dbapi.db_find_one(self.table, query) - if not data: - raises.NotFound(message.not_found(self.table, query)) - ret = yield gen.coroutine(xstep)(self, data, *args, **kwargs) - raise gen.Return(ret) - - return wrap - - -def no_body(xstep): - @functools.wraps(xstep) - def wrap(self, *args, **kwargs): - if self.json_args is None: - raises.BadRequest(message.no_body()) - ret = yield gen.coroutine(xstep)(self, *args, **kwargs) - raise gen.Return(ret) - - return wrap - - -def miss_fields(xstep): - @functools.wraps(xstep) - def wrap(self, *args, **kwargs): - fields = kwargs.pop('miss_fields', []) - if fields: - for miss in fields: - miss_data = self.json_args.get(miss) - if miss_data is None or miss_data == '': - raises.BadRequest(message.missing(miss)) - ret = yield gen.coroutine(xstep)(self, *args, **kwargs) - raise gen.Return(ret) - return wrap - - -def carriers_exist(xstep): - @functools.wraps(xstep) - def wrap(self, *args, **kwargs): - carriers = kwargs.pop('carriers', {}) - if carriers: - for table, query in carriers: - exist = yield dbapi.db_find_one(table, query()) - if not exist: - raises.Forbidden(message.not_found(table, query())) - ret = yield gen.coroutine(xstep)(self, *args, **kwargs) - raise gen.Return(ret) - 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): - query = kwargs.get('query') - if query: - query_data = query() - if self.table == 'pods': - if query_data.get('name') is not None: - query_data['name'] = re.compile(query_data.get('name'), - re.IGNORECASE) - to_data = yield dbapi.db_find_one(self.table, query_data) - if to_data: - raises.Forbidden(message.exist(self.table, query())) - ret = yield gen.coroutine(xstep)(self, *args, **kwargs) - raise gen.Return(ret) - return wrap - - -def updated_one_not_exist(xstep): - @functools.wraps(xstep) - def wrap(self, data, *args, **kwargs): - db_keys = kwargs.pop('db_keys', []) - query = self._update_query(db_keys, data) - if query: - to_data = yield dbapi.db_find_one(self.table, query) - if to_data: - raises.Forbidden(message.exist(self.table, query)) - ret = yield gen.coroutine(xstep)(self, data, *args, **kwargs) - raise gen.Return(ret) - return wrap diff --git a/utils/test/testapi/opnfv_testapi/common/config.py b/utils/test/testapi/opnfv_testapi/common/config.py deleted file mode 100644 index f888b07be..000000000 --- a/utils/test/testapi/opnfv_testapi/common/config.py +++ /dev/null @@ -1,63 +0,0 @@ -############################################################################## -# Copyright (c) 2015 Orange -# guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -# feng.xiaowei@zte.com.cn remove prepare_put_request 5-30-2016 -############################################################################## -import ConfigParser -import argparse -import os -import sys - - -class Config(object): - - def __init__(self): - self.config_file = '/etc/opnfv_testapi/config.ini' - self._set_config_file() - self._parse() - self._parse_per_page() - - def _parse(self): - if not os.path.exists(self.config_file): - raise Exception("%s not found" % self.config_file) - - config = ConfigParser.RawConfigParser() - config.read(self.config_file) - self._parse_section(config) - - def _parse_section(self, config): - [self._parse_item(config, section) for section in (config.sections())] - - def _parse_item(self, config, section): - [setattr(self, '{}_{}'.format(section, k), self._parse_value(v)) - for k, v in config.items(section)] - - def _parse_per_page(self): - if not hasattr(self, 'api_results_per_page'): - self.api_results_per_page = 20 - - @staticmethod - def _parse_value(value): - try: - value = int(value) - except Exception: - if str(value).lower() == 'true': - value = True - elif str(value).lower() == 'false': - value = False - return value - - def _set_config_file(self): - parser = argparse.ArgumentParser() - parser.add_argument("-c", "--config-file", dest='config_file', - help="Config file location", metavar="FILE") - args, _ = parser.parse_known_args(sys.argv) - if hasattr(args, 'config_file') and args.config_file: - self.config_file = args.config_file - - -CONF = Config() diff --git a/utils/test/testapi/opnfv_testapi/common/constants.py b/utils/test/testapi/opnfv_testapi/common/constants.py deleted file mode 100644 index 70c922383..000000000 --- a/utils/test/testapi/opnfv_testapi/common/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -TESTAPI_ID = 'testapi_id' -CSRF_TOKEN = 'csrf_token' -ROLE = 'role' -TESTAPI_USERS = ['opnfv-testapi-users'] diff --git a/utils/test/testapi/opnfv_testapi/common/message.py b/utils/test/testapi/opnfv_testapi/common/message.py deleted file mode 100644 index 3e14f7258..000000000 --- a/utils/test/testapi/opnfv_testapi/common/message.py +++ /dev/null @@ -1,62 +0,0 @@ -############################################################################## -# Copyright (c) 2017 ZTE Corp -# feng.xiaowei@zte.com.cn -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## -not_found_base = 'Could Not Found' -exist_base = 'Already Exists' - - -def key_error(key): - return "KeyError: '{}'".format(key) - - -def no_body(): - return 'No Body' - - -def not_found(key, value): - return '{} {} [{}]'.format(not_found_base, key, value) - - -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) - - -def bad_format(error): - return 'Bad Format [{}]'.format(error) - - -def unauthorized(): - return 'No Authentication Header' - - -def invalid_token(): - return 'Invalid Token' - - -def not_login(): - return 'TestAPI id is not provided' - - -def not_lfid(): - return 'Not a valid Linux Foundation Account' - - -def no_update(): - return 'Nothing to update' - - -def must_int(name): - return '{} must be int'.format(name) diff --git a/utils/test/testapi/opnfv_testapi/common/raises.py b/utils/test/testapi/opnfv_testapi/common/raises.py deleted file mode 100644 index 55c58c9e2..000000000 --- a/utils/test/testapi/opnfv_testapi/common/raises.py +++ /dev/null @@ -1,43 +0,0 @@ -############################################################################## -# Copyright (c) 2017 ZTE Corp -# feng.xiaowei@zte.com.cn -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## -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 Conflict(Raiser): - code = httplib.CONFLICT - - -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) |