Merge "allow authentication to be disabled"
[releng.git] / utils / test / testapi / opnfv_testapi / common / check.py
index e80b1c6..667578f 100644 (file)
@@ -14,13 +14,14 @@ 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 self.table in ['pods']:
+        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())
@@ -102,6 +103,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):