diff options
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/common')
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/check.py | 35 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/config.py | 19 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/common/constants.py | 5 |
3 files changed, 42 insertions, 17 deletions
diff --git a/utils/test/testapi/opnfv_testapi/common/check.py b/utils/test/testapi/opnfv_testapi/common/check.py index 24ba876a9..009d3d46c 100644 --- a/utils/test/testapi/opnfv_testapi/common/check.py +++ b/utils/test/testapi/opnfv_testapi/common/check.py @@ -8,14 +8,49 @@ ############################################################################## import functools +import cas from tornado import gen from tornado import web +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 login(method): + @web.asynchronous + @gen.coroutine + @functools.wraps(method) + def wrapper(self, *args, **kwargs): + ticket = self.get_query_argument('ticket', default=None) + if ticket: + client = cas.CASClient(version='2', + server_url=CONF.lfid_cas_url, + service_url=CONF.ui_url) + (user, attrs, _) = client.verify_ticket(ticket=ticket) + print 'login user: {}'.format(user) + login_user = { + 'user': user, + 'email': attrs.get('mail'), + 'fullname': attrs.get('field_lf_full_name'), + 'groups': constants.TESTAPI_USERS + attrs.get('group', []) + } + q_user = {'user': user} + db_user = yield dbapi.db_find_one(constants.USER_TABLE, q_user) + if not db_user: + dbapi.db_save(constants.USER_TABLE, login_user) + else: + dbapi.db_update(constants.USER_TABLE, q_user, login_user) + + self.clear_cookie(constants.TESTAPI_ID) + self.set_secure_cookie(constants.TESTAPI_ID, user) + ret = yield gen.coroutine(method)(self, *args, **kwargs) + raise gen.Return(ret) + return wrapper + + def authenticate(method): @web.asynchronous @gen.coroutine diff --git a/utils/test/testapi/opnfv_testapi/common/config.py b/utils/test/testapi/opnfv_testapi/common/config.py index 4cd53c619..140e49283 100644 --- a/utils/test/testapi/opnfv_testapi/common/config.py +++ b/utils/test/testapi/opnfv_testapi/common/config.py @@ -16,14 +16,10 @@ import sys class Config(object): def __init__(self): - self.config_file = None + self.config_file = '/etc/opnfv_testapi/config.ini' self._set_config_file() self._parse() self._parse_per_page() - self.static_path = os.path.join( - os.path.dirname(os.path.normpath(__file__)), - os.pardir, - 'static') def _parse(self): if not os.path.exists(self.config_file): @@ -56,23 +52,12 @@ class Config(object): return value def _set_config_file(self): - if not self._set_sys_config_file(): - self._set_default_config_file() - - def _set_sys_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) - try: + if hasattr(args, 'config_file') and args.config_file: self.config_file = args.config_file - finally: - return self.config_file is not None - - def _set_default_config_file(self): - is_venv = os.getenv('VIRTUAL_ENV') - self.config_file = os.path.join('/' if not is_venv else is_venv, - 'etc/opnfv_testapi/config.ini') CONF = Config() diff --git a/utils/test/testapi/opnfv_testapi/common/constants.py b/utils/test/testapi/opnfv_testapi/common/constants.py new file mode 100644 index 000000000..b37ebb3d6 --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/common/constants.py @@ -0,0 +1,5 @@ +TESTAPI_ID = 'testapi_id' +CSRF_TOKEN = 'csrf_token' +ROLE = 'role' +TESTAPI_USERS = ['opnfv-testapi-users'] +USER_TABLE = 'users' |