aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb
diff options
context:
space:
mode:
Diffstat (limited to 'python_moondb')
-rw-r--r--python_moondb/Changelog33
-rw-r--r--python_moondb/python_moondb/__init__.py3
-rw-r--r--python_moondb/python_moondb/api/model.py43
-rw-r--r--python_moondb/python_moondb/api/pdp.py8
-rw-r--r--python_moondb/python_moondb/api/policy.py59
-rw-r--r--python_moondb/python_moondb/backends/sql.py126
-rw-r--r--python_moondb/python_moondb/migrate_repo/versions/001_moon.py49
-rw-r--r--python_moondb/tests/unit_python/helpers/__init__.py0
-rw-r--r--python_moondb/tests/unit_python/helpers/assignment_helper.py49
-rw-r--r--python_moondb/tests/unit_python/helpers/category_helper.py54
-rw-r--r--python_moondb/tests/unit_python/helpers/data_helper.py98
-rw-r--r--python_moondb/tests/unit_python/helpers/meta_rule_helper.py48
-rw-r--r--python_moondb/tests/unit_python/helpers/mock_data.py144
-rw-r--r--python_moondb/tests/unit_python/helpers/model_helper.py50
-rw-r--r--python_moondb/tests/unit_python/helpers/pdp_helper.py23
-rw-r--r--python_moondb/tests/unit_python/helpers/policy_helper.py61
-rw-r--r--python_moondb/tests/unit_python/models/test_categories.py250
-rw-r--r--python_moondb/tests/unit_python/models/test_meta_rules.py194
-rw-r--r--python_moondb/tests/unit_python/models/test_models.py385
-rw-r--r--python_moondb/tests/unit_python/policies/mock_data.py35
-rwxr-xr-xpython_moondb/tests/unit_python/policies/test_assignments.py299
-rwxr-xr-xpython_moondb/tests/unit_python/policies/test_data.py634
-rwxr-xr-xpython_moondb/tests/unit_python/policies/test_policies.py237
-rw-r--r--python_moondb/tests/unit_python/requirements.txt1
-rwxr-xr-xpython_moondb/tests/unit_python/test_pdp.py108
25 files changed, 1768 insertions, 1223 deletions
diff --git a/python_moondb/Changelog b/python_moondb/Changelog
index acd72883..f4feef62 100644
--- a/python_moondb/Changelog
+++ b/python_moondb/Changelog
@@ -73,3 +73,36 @@ CHANGES
1.2.9
-----
- Add some verifications when deleting some elements in database
+
+1.2.10
+-----
+- Update the migration script because of a bug introduced in 1.2.8 in rule table
+- Fix bugs due to the previous version
+
+1.2.11
+------
+- adding test cases for perimeter
+- adding subject_object_action to model_test
+- update import of exception
+- add unit_test to test_model
+- add validation for not accepting blank perimeter name or category name
+
+1.2.12
+------
+- Fix the SubjectExisting exception problem
+
+1.2.13
+------
+- Add validations and refactor test cases
+
+1.2.14
+------
+- Fix some bugs for the manager and clean the code
+
+1.2.15
+------
+- Fix test cases after removing syntax error in exceptions
+
+1.2.16
+------
+- Fix the "key length error" in meta_rule table
diff --git a/python_moondb/python_moondb/__init__.py b/python_moondb/python_moondb/__init__.py
index 287558f7..e2e16287 100644
--- a/python_moondb/python_moondb/__init__.py
+++ b/python_moondb/python_moondb/__init__.py
@@ -3,5 +3,4 @@
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-__version__ = "1.2.9"
-
+__version__ = "1.2.16"
diff --git a/python_moondb/python_moondb/api/model.py b/python_moondb/python_moondb/api/model.py
index f5858662..c1603b83 100644
--- a/python_moondb/python_moondb/api/model.py
+++ b/python_moondb/python_moondb/api/model.py
@@ -22,6 +22,10 @@ class ModelManager(Managers):
def update_model(self, user_id, model_id, value):
if model_id not in self.driver.get_models(model_id=model_id):
raise exceptions.ModelUnknown
+ if value and 'meta_rules' in value:
+ for meta_rule_id in value['meta_rules']:
+ if not self.driver.get_meta_rules(meta_rule_id=meta_rule_id):
+ raise exceptions.MetaRuleUnknown
return self.driver.update_model(model_id=model_id, value=value)
@enforce(("read", "write"), "models")
@@ -41,6 +45,10 @@ class ModelManager(Managers):
raise exceptions.ModelExisting
if not model_id:
model_id = uuid4().hex
+ if value and 'meta_rules' in value:
+ for meta_rule_id in value['meta_rules']:
+ if not self.driver.get_meta_rules(meta_rule_id=meta_rule_id):
+ raise exceptions.MetaRuleUnknown
return self.driver.add_model(model_id=model_id, value=value)
@enforce("read", "models")
@@ -51,6 +59,19 @@ class ModelManager(Managers):
def set_meta_rule(self, user_id, meta_rule_id, value):
if meta_rule_id not in self.driver.get_meta_rules(meta_rule_id=meta_rule_id):
raise exceptions.MetaRuleUnknown
+ if value:
+ if 'subject_categories' in value:
+ for subject_category_id in value['subject_categories']:
+ if not self.driver.get_subject_categories(category_id=subject_category_id):
+ raise exceptions.SubjectCategoryUnknown
+ if 'object_categories' in value:
+ for object_category_id in value['object_categories']:
+ if not self.driver.get_object_categories(category_id=object_category_id):
+ raise exceptions.ObjectCategoryUnknown
+ if 'action_categories' in value:
+ for action_category_id in value['action_categories']:
+ if not self.driver.get_action_categories(category_id=action_category_id):
+ raise exceptions.ActionCategoryUnknown
return self.driver.set_meta_rule(meta_rule_id=meta_rule_id, value=value)
@enforce("read", "meta_rules")
@@ -61,6 +82,19 @@ class ModelManager(Managers):
def add_meta_rule(self, user_id, meta_rule_id=None, value=None):
if meta_rule_id in self.driver.get_meta_rules(meta_rule_id=meta_rule_id):
raise exceptions.MetaRuleExisting
+ if value:
+ if 'subject_categories' in value:
+ for subject_category_id in value['subject_categories']:
+ if not self.driver.get_subject_categories(category_id=subject_category_id):
+ raise exceptions.SubjectCategoryUnknown
+ if 'object_categories' in value:
+ for object_category_id in value['object_categories']:
+ if not self.driver.get_object_categories(category_id=object_category_id):
+ raise exceptions.ObjectCategoryUnknown
+ if 'action_categories' in value:
+ for action_category_id in value['action_categories']:
+ if not self.driver.get_action_categories(category_id=action_category_id):
+ raise exceptions.ActionCategoryUnknown
return self.driver.set_meta_rule(meta_rule_id=meta_rule_id, value=value)
@enforce(("read", "write"), "meta_rules")
@@ -93,8 +127,11 @@ class ModelManager(Managers):
meta_rules = self.get_meta_rules(user_id=user_id)
for meta_rule_id in meta_rules:
for subject_category_id in meta_rules[meta_rule_id]['subject_categories']:
+ logger.info("delete_subject_category {} {}".format(subject_category_id, meta_rule_id))
+ logger.info("delete_subject_category {}".format(meta_rules[meta_rule_id]))
if subject_category_id == category_id:
- raise exceptions.DeleteCategoryWithMetaRule
+ self.delete_meta_rule(user_id, meta_rule_id)
+ # raise exceptions.DeleteCategoryWithMetaRule
if self.driver.is_subject_data_exist(category_id=category_id):
raise exceptions.DeleteCategoryWithData
return self.driver.delete_subject_category(category_id=category_id)
@@ -119,7 +156,7 @@ class ModelManager(Managers):
for meta_rule_id in meta_rules:
for object_category_id in meta_rules[meta_rule_id]['object_categories']:
if object_category_id == category_id:
- raise exceptions.DeleteCategoryWithMetaRule
+ self.delete_meta_rule(user_id, meta_rule_id)
if self.driver.is_object_data_exist(category_id=category_id):
raise exceptions.DeleteCategoryWithData
return self.driver.delete_object_category(category_id=category_id)
@@ -144,7 +181,7 @@ class ModelManager(Managers):
for meta_rule_id in meta_rules:
for action_category_id in meta_rules[meta_rule_id]['action_categories']:
if action_category_id == category_id:
- raise exceptions.DeleteCategoryWithMetaRule
+ self.delete_meta_rule(user_id, meta_rule_id)
if self.driver.is_action_data_exist(category_id=category_id):
raise exceptions.DeleteCategoryWithData
return self.driver.delete_action_category(category_id=category_id)
diff --git a/python_moondb/python_moondb/api/pdp.py b/python_moondb/python_moondb/api/pdp.py
index 7e852ca8..d0a071c9 100644
--- a/python_moondb/python_moondb/api/pdp.py
+++ b/python_moondb/python_moondb/api/pdp.py
@@ -22,6 +22,10 @@ class PDPManager(Managers):
def update_pdp(self, user_id, pdp_id, value):
if pdp_id not in self.driver.get_pdp(pdp_id=pdp_id):
raise exceptions.PdpUnknown
+ if value and 'security_pipeline' in value:
+ for policy_id in value['security_pipeline']:
+ if not Managers.PolicyManager.get_policies(user_id=user_id, policy_id=policy_id):
+ raise exceptions.PolicyUnknown
return self.driver.update_pdp(pdp_id=pdp_id, value=value)
@enforce(("read", "write"), "pdp")
@@ -36,6 +40,10 @@ class PDPManager(Managers):
raise exceptions.PdpExisting
if not pdp_id:
pdp_id = uuid4().hex
+ if value and 'security_pipeline' in value:
+ for policy_id in value['security_pipeline']:
+ if not Managers.PolicyManager.get_policies(user_id=user_id, policy_id=policy_id):
+ raise exceptions.PolicyUnknown
return self.driver.add_pdp(pdp_id=pdp_id, value=value)
@enforce("read", "pdp")
diff --git a/python_moondb/python_moondb/api/policy.py b/python_moondb/python_moondb/api/policy.py
index 69392e6d..05c2b7d5 100644
--- a/python_moondb/python_moondb/api/policy.py
+++ b/python_moondb/python_moondb/api/policy.py
@@ -40,6 +40,9 @@ class PolicyManager(Managers):
def update_policy(self, user_id, policy_id, value):
if policy_id not in self.driver.get_policies(policy_id=policy_id):
raise exceptions.PolicyUnknown
+ if value and 'model_id' in value and value['model_id'] != "":
+ if not Managers.ModelManager.get_models(user_id, model_id=value['model_id']):
+ raise exceptions.ModelUnknown
return self.driver.update_policy(policy_id=policy_id, value=value)
@enforce(("read", "write"), "policies")
@@ -60,6 +63,9 @@ class PolicyManager(Managers):
raise exceptions.PolicyExisting
if not policy_id:
policy_id = uuid4().hex
+ if value and 'model_id' in value and value['model_id'] != "":
+ if not Managers.ModelManager.get_models(user_id, model_id=value['model_id']):
+ raise exceptions.ModelUnknown
return self.driver.add_policy(policy_id=policy_id, value=value)
@enforce("read", "policies")
@@ -68,10 +74,19 @@ class PolicyManager(Managers):
@enforce("read", "perimeter")
def get_subjects(self, user_id, policy_id, perimeter_id=None):
+ if not policy_id:
+ pass
+ elif not (policy_id and self.get_policies(user_id=user_id, policy_id=policy_id)):
+ raise exceptions.PolicyUnknown
return self.driver.get_subjects(policy_id=policy_id, perimeter_id=perimeter_id)
@enforce(("read", "write"), "perimeter")
def add_subject(self, user_id, policy_id, perimeter_id=None, value=None):
+ if not value or "name" not in value or not value["name"].strip():
+ raise exceptions.PerimeterNameInvalid
+ if value["name"] in map(lambda x: x['name'],
+ self.get_subjects(user_id, policy_id, perimeter_id).values()):
+ raise exceptions.SubjectExisting
k_user = Managers.KeystoneManager.get_user_by_name(value.get('name'))
if not k_user['users']:
k_user = Managers.KeystoneManager.create_user(value)
@@ -94,10 +109,16 @@ class PolicyManager(Managers):
@enforce(("read", "write"), "perimeter")
def delete_subject(self, user_id, policy_id, perimeter_id):
+ if policy_id and not self.get_policies(user_id=user_id, policy_id=policy_id):
+ raise exceptions.PolicyUnknown
return self.driver.delete_subject(policy_id=policy_id, perimeter_id=perimeter_id)
@enforce("read", "perimeter")
def get_objects(self, user_id, policy_id, perimeter_id=None):
+ if not policy_id:
+ pass
+ elif not (policy_id and self.get_policies(user_id=user_id, policy_id=policy_id)):
+ raise exceptions.PolicyUnknown
return self.driver.get_objects(policy_id=policy_id, perimeter_id=perimeter_id)
@enforce(("read", "write"), "perimeter")
@@ -110,21 +131,30 @@ class PolicyManager(Managers):
@enforce(("read", "write"), "perimeter")
def delete_object(self, user_id, policy_id, perimeter_id):
+ if policy_id and not self.get_policies(user_id=user_id, policy_id=policy_id):
+ raise exceptions.PolicyUnknown
return self.driver.delete_object(policy_id=policy_id, perimeter_id=perimeter_id)
@enforce("read", "perimeter")
def get_actions(self, user_id, policy_id, perimeter_id=None):
+ if not policy_id:
+ pass
+ elif not (policy_id and self.get_policies(user_id=user_id, policy_id=policy_id)):
+ raise exceptions.PolicyUnknown
return self.driver.get_actions(policy_id=policy_id, perimeter_id=perimeter_id)
@enforce(("read", "write"), "perimeter")
def add_action(self, user_id, policy_id, perimeter_id=None, value=None):
- logger.info("add_action {}".format(policy_id))
+ logger.debug("add_action {}".format(policy_id))
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
return self.driver.set_action(policy_id=policy_id, perimeter_id=perimeter_id, value=value)
@enforce(("read", "write"), "perimeter")
def delete_action(self, user_id, policy_id, perimeter_id):
+ logger.debug("delete_action {} {} {}".format(policy_id, perimeter_id, self.get_policies(user_id=user_id, policy_id=policy_id)))
+ if policy_id and not self.get_policies(user_id=user_id, policy_id=policy_id):
+ raise exceptions.PolicyUnknown
return self.driver.delete_action(policy_id=policy_id, perimeter_id=perimeter_id)
@enforce("read", "data")
@@ -144,6 +174,8 @@ class PolicyManager(Managers):
def set_subject_data(self, user_id, policy_id, data_id=None, category_id=None, value=None):
if not category_id:
raise Exception('Invalid category id')
+ if not Managers.ModelManager.get_subject_categories(user_id=user_id, category_id=category_id):
+ raise exceptions.MetaDataUnknown
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
if not data_id:
@@ -175,6 +207,8 @@ class PolicyManager(Managers):
def add_object_data(self, user_id, policy_id, data_id=None, category_id=None, value=None):
if not category_id:
raise Exception('Invalid category id')
+ if not Managers.ModelManager.get_object_categories(user_id=user_id, category_id=category_id):
+ raise exceptions.MetaDataUnknown
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
if not data_id:
@@ -206,6 +240,8 @@ class PolicyManager(Managers):
def add_action_data(self, user_id, policy_id, data_id=None, category_id=None, value=None):
if not category_id:
raise Exception('Invalid category id')
+ if not Managers.ModelManager.get_action_categories(user_id=user_id, category_id=category_id):
+ raise exceptions.MetaDataUnknown
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
if not data_id:
@@ -228,6 +264,12 @@ class PolicyManager(Managers):
def add_subject_assignment(self, user_id, policy_id, subject_id, category_id, data_id):
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
+ if not self.get_subjects(user_id=user_id, policy_id=policy_id, perimeter_id=subject_id):
+ raise exceptions.SubjectUnknown
+ if not Managers.ModelManager.get_subject_categories(user_id=user_id, category_id=category_id):
+ raise exceptions.MetaDataUnknown
+ if not self.get_subject_data(user_id=user_id, policy_id=policy_id, data_id=data_id):
+ raise exceptions.DataUnknown
return self.driver.add_subject_assignment(policy_id=policy_id, subject_id=subject_id,
category_id=category_id, data_id=data_id)
@@ -244,6 +286,12 @@ class PolicyManager(Managers):
def add_object_assignment(self, user_id, policy_id, object_id, category_id, data_id):
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
+ if not self.get_objects(user_id=user_id, policy_id=policy_id, perimeter_id=object_id):
+ raise exceptions.ObjectUnknown
+ if not Managers.ModelManager.get_object_categories(user_id=user_id, category_id=category_id):
+ raise exceptions.MetaDataUnknown
+ if not self.get_object_data(user_id=user_id, policy_id=policy_id, data_id=data_id):
+ raise exceptions.DataUnknown
return self.driver.add_object_assignment(policy_id=policy_id, object_id=object_id,
category_id=category_id, data_id=data_id)
@@ -260,6 +308,12 @@ class PolicyManager(Managers):
def add_action_assignment(self, user_id, policy_id, action_id, category_id, data_id):
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
+ if not self.get_actions(user_id=user_id, policy_id=policy_id, perimeter_id=action_id):
+ raise exceptions.ActionUnknown
+ if not Managers.ModelManager.get_action_categories(user_id=user_id, category_id=category_id):
+ raise exceptions.MetaDataUnknown
+ if not self.get_action_data(user_id=user_id, policy_id=policy_id, data_id=data_id):
+ raise exceptions.DataUnknown
return self.driver.add_action_assignment(policy_id=policy_id, action_id=action_id,
category_id=category_id, data_id=data_id)
@@ -271,11 +325,14 @@ class PolicyManager(Managers):
@enforce("read", "rules")
def get_rules(self, user_id, policy_id, meta_rule_id=None, rule_id=None):
return self.driver.get_rules(policy_id=policy_id, meta_rule_id=meta_rule_id, rule_id=rule_id)
+ logger.info("delete_subject_data: {} {}".format(policy_id, data_id))
@enforce(("read", "write"), "rules")
def add_rule(self, user_id, policy_id, meta_rule_id, value):
if not self.get_policies(user_id=user_id, policy_id=policy_id):
raise exceptions.PolicyUnknown
+ if not self.ModelManager.get_meta_rules(user_id=user_id, meta_rule_id=meta_rule_id):
+ raise exceptions.MetaRuleUnknown
return self.driver.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id, value=value)
@enforce(("read", "write"), "rules")
diff --git a/python_moondb/python_moondb/backends/sql.py b/python_moondb/python_moondb/backends/sql.py
index 366ed7de..7310e7f3 100644
--- a/python_moondb/python_moondb/backends/sql.py
+++ b/python_moondb/python_moondb/backends/sql.py
@@ -14,7 +14,7 @@ from sqlalchemy import create_engine
from contextlib import contextmanager
from sqlalchemy import types as sql_types
from python_moonutilities import configuration
-from python_moonutilities.exceptions import *
+from python_moonutilities import exceptions
from python_moondb.core import PDPDriver, PolicyDriver, ModelDriver
import sqlalchemy
@@ -134,6 +134,7 @@ class PerimeterBase(DictBase):
name = sql.Column(sql.String(256), nullable=False)
value = sql.Column(JsonBlob(), nullable=True)
__mapper_args__ = {'concrete': True}
+
def __repr__(self):
return "{} with name {} : {}".format(self.id, self.name, json.dumps(self.value))
@@ -155,6 +156,7 @@ class PerimeterBase(DictBase):
'value': dict_value
}
+
class Subject(Base, PerimeterBase):
__tablename__ = 'subjects'
@@ -352,7 +354,7 @@ class PDPConnector(BaseConnector, PDPDriver):
setattr(ref, "value", d)
return {ref.id: ref.to_dict()}
except sqlalchemy.exc.IntegrityError:
- raise PdpExisting
+ raise exceptions.PdpExisting
def delete_pdp(self, pdp_id):
with self.get_session_for_write() as session:
@@ -374,7 +376,7 @@ class PDPConnector(BaseConnector, PDPDriver):
session.add(new)
return {new.id: new.to_dict()}
except sqlalchemy.exc.IntegrityError:
- raise PdpExisting
+ raise exceptions.PdpExisting
def get_pdp(self, pdp_id=None):
with self.get_session_for_read() as session:
@@ -416,7 +418,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
new = Policy.from_dict({
"id": policy_id if policy_id else uuid4().hex,
"name": value["name"],
- "model_id": value["model_id"],
+ "model_id": value.get("model_id", ""),
"value": value_wo_other_info
})
session.add(new)
@@ -452,13 +454,20 @@ class PolicyConnector(BaseConnector, PolicyDriver):
return {_ref.id: _ref.to_return() for _ref in ref_list}
def __set_perimeter(self, ClassType, ClassTypeException, policy_id, perimeter_id=None, value=None):
- _perimeter = None
+ if not value or "name" not in value or not value["name"].strip():
+ raise exceptions.PerimeterNameInvalid
with self.get_session_for_write() as session:
+ _perimeter = None
if perimeter_id:
query = session.query(ClassType)
query = query.filter_by(id=perimeter_id)
_perimeter = query.first()
- logger.info("+++++++++++++ {}".format(_perimeter))
+ if not perimeter_id and not _perimeter:
+ query = session.query(ClassType)
+ query = query.filter_by(name=value['name'])
+ _perimeter = query.first()
+ if _perimeter:
+ raise ClassTypeException
if not _perimeter:
if "policy_list" not in value or type(value["policy_list"]) is not list:
value["policy_list"] = []
@@ -466,9 +475,9 @@ class PolicyConnector(BaseConnector, PolicyDriver):
value["policy_list"] = [policy_id, ]
value_wo_name = copy.deepcopy(value)
- value_wo_name.pop("name",None)
+ value_wo_name.pop("name", None)
new = ClassType.from_dict({
- "id": uuid4().hex,
+ "id": perimeter_id if perimeter_id else uuid4().hex,
"name": value["name"],
"value": value_wo_name
})
@@ -480,7 +489,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
_value["value"]["policy_list"] = []
if policy_id and policy_id not in _value["value"]["policy_list"]:
_value["value"]["policy_list"].append(policy_id)
- logger.info("-------------_value- {}".format(_value))
+ _value["value"].update(value)
name = _value["value"]["name"]
_value["value"].pop("name")
@@ -489,13 +498,11 @@ class PolicyConnector(BaseConnector, PolicyDriver):
"name": name,
"value": _value["value"]
})
- logger.info("-------------- new {}".format(new_perimeter))
- logger.info("-------------- old {}".format(_perimeter))
_perimeter.value = new_perimeter.value
_perimeter.name = new_perimeter.name
return {_perimeter.id: _perimeter.to_return()}
- def __delete_perimeter(self,ClassType, ClassUnknownException, policy_id, perimeter_id):
+ def __delete_perimeter(self, ClassType, ClassUnknownException, policy_id, perimeter_id):
with self.get_session_for_write() as session:
query = session.query(ClassType)
query = query.filter_by(id=perimeter_id)
@@ -503,7 +510,6 @@ class PolicyConnector(BaseConnector, PolicyDriver):
if not _perimeter:
raise ClassUnknownException
old_perimeter = copy.deepcopy(_perimeter.to_dict())
- # value = _subject.to_dict()
try:
old_perimeter["value"]["policy_list"].remove(policy_id)
new_perimeter = ClassType.from_dict(old_perimeter)
@@ -517,39 +523,41 @@ class PolicyConnector(BaseConnector, PolicyDriver):
def set_subject(self, policy_id, perimeter_id=None, value=None):
try:
- return self.__set_perimeter(Subject, SubjectExisting, policy_id, perimeter_id=perimeter_id, value=value)
+ return self.__set_perimeter(Subject, exceptions.SubjectExisting, policy_id, perimeter_id=perimeter_id, value=value)
except sqlalchemy.exc.IntegrityError:
- raise SubjectExisting
+ raise exceptions.SubjectExisting
def delete_subject(self, policy_id, perimeter_id):
- self.__delete_perimeter(Subject, SubjectUnknown, policy_id, perimeter_id)
+ self.__delete_perimeter(Subject, exceptions.SubjectUnknown, policy_id, perimeter_id)
def get_objects(self, policy_id, perimeter_id=None):
return self.__get_perimeters(Object, policy_id, perimeter_id)
def set_object(self, policy_id, perimeter_id=None, value=None):
try:
- return self.__set_perimeter(Object, ObjectExisting, policy_id, perimeter_id=perimeter_id, value=value)
- except sqlalchemy.exc.IntegrityError:
- raise ObjectExisting
+ return self.__set_perimeter(Object, exceptions.ObjectExisting, policy_id, perimeter_id=perimeter_id, value=value)
+ except sqlalchemy.exc.IntegrityError as e:
+ logger.exception("IntegrityError {}".format(e))
+ raise exceptions.ObjectExisting
def delete_object(self, policy_id, perimeter_id):
- self.__delete_perimeter(Object, ObjectUnknown, policy_id, perimeter_id)
+ self.__delete_perimeter(Object, exceptions.ObjectUnknown, policy_id, perimeter_id)
def get_actions(self, policy_id, perimeter_id=None):
return self.__get_perimeters(Action, policy_id, perimeter_id)
def set_action(self, policy_id, perimeter_id=None, value=None):
try:
- return self.__set_perimeter(Action, ActionExisting, policy_id, perimeter_id=perimeter_id, value=value)
+ return self.__set_perimeter(Action, exceptions.ActionExisting, policy_id, perimeter_id=perimeter_id, value=value)
except sqlalchemy.exc.IntegrityError:
- raise ActionExisting
+ raise exceptions.ActionExisting
def delete_action(self, policy_id, perimeter_id):
- self.__delete_perimeter(Action, ActionUnknown, policy_id, perimeter_id)
+ self.__delete_perimeter(Action, exceptions.ActionUnknown, policy_id, perimeter_id)
- def __is_perimeter_data_exist(self, ClassType ,data_id=None, category_id=None):
- logger.info("driver {} {}".format( data_id, category_id))
+ def __is_data_exist(self, ClassType, data_id=None, category_id=None):
+ if not data_id:
+ return False
with self.get_session_for_read() as session:
query = session.query(ClassType)
query = query.filter_by(category_id=category_id)
@@ -558,23 +566,23 @@ class PolicyConnector(BaseConnector, PolicyDriver):
return True
return False
- def __get_perimeter_data(self, ClassType, policy_id, data_id=None, category_id=None):
- logger.info("driver {} {} {}".format(policy_id, data_id, category_id))
+ def __get_data(self, ClassType, policy_id, data_id=None, category_id=None):
with self.get_session_for_read() as session:
query = session.query(ClassType)
- if data_id:
+ if policy_id and data_id and category_id:
query = query.filter_by(policy_id=policy_id, id=data_id, category_id=category_id)
- else:
+ elif policy_id and category_id:
query = query.filter_by(policy_id=policy_id, category_id=category_id)
+ else:
+ query = query.filter_by(category_id=category_id)
ref_list = query.all()
- logger.info("ref_list={}".format(ref_list))
return {
"policy_id": policy_id,
"category_id": category_id,
"data": {_ref.id: _ref.to_dict() for _ref in ref_list}
}
- def __set_perimeter_data(self, ClassType, ClassTypeData, policy_id, data_id=None, category_id=None, value=None):
+ def __set_data(self, ClassType, ClassTypeData, policy_id, data_id=None, category_id=None, value=None):
with self.get_session_for_write() as session:
query = session.query(ClassTypeData)
query = query.filter_by(policy_id=policy_id, id=data_id, category_id=category_id)
@@ -604,7 +612,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
"data": {ref.id: ref.to_dict()}
}
- def __delete_perimeter_data(self, ClassType, policy_id, data_id):
+ def __delete_data(self, ClassType, policy_id, data_id):
with self.get_session_for_write() as session:
query = session.query(ClassType)
query = query.filter_by(policy_id=policy_id, id=data_id)
@@ -613,49 +621,49 @@ class PolicyConnector(BaseConnector, PolicyDriver):
session.delete(ref)
def is_subject_data_exist(self, data_id=None, category_id=None):
- return self.__is_perimeter_data_exist(SubjectData, data_id=data_id, category_id=category_id)
+ return self.__is_data_exist(SubjectData, data_id=data_id, category_id=category_id)
def get_subject_data(self, policy_id, data_id=None, category_id=None):
- return self.__get_perimeter_data(SubjectData, policy_id, data_id=data_id, category_id=category_id)
+ return self.__get_data(SubjectData, policy_id, data_id=data_id, category_id=category_id)
def set_subject_data(self, policy_id, data_id=None, category_id=None, value=None):
try:
- return self.__set_perimeter_data(Subject, SubjectData, policy_id, data_id=data_id, category_id=category_id, value=value)
+ return self.__set_data(Subject, SubjectData, policy_id, data_id=data_id, category_id=category_id, value=value)
except sqlalchemy.exc.IntegrityError:
- raise SubjectScopeExisting
+ raise exceptions.SubjectScopeExisting
def delete_subject_data(self, policy_id, data_id):
- return self.__delete_perimeter_data(SubjectData, policy_id, data_id)
+ return self.__delete_data(SubjectData, policy_id, data_id)
def is_object_data_exist(self, data_id=None, category_id=None):
- return self.__is_perimeter_data_exist(ObjectData, data_id=data_id, category_id=category_id)
+ return self.__is_data_exist(ObjectData, data_id=data_id, category_id=category_id)
def get_object_data(self, policy_id, data_id=None, category_id=None):
- return self.__get_perimeter_data(ObjectData, policy_id, data_id=data_id, category_id=category_id)
+ return self.__get_data(ObjectData, policy_id, data_id=data_id, category_id=category_id)
def set_object_data(self, policy_id, data_id=None, category_id=None, value=None):
try:
- return self.__set_perimeter_data(Object, ObjectData, policy_id, data_id=data_id, category_id=category_id, value=value)
+ return self.__set_data(Object, ObjectData, policy_id, data_id=data_id, category_id=category_id, value=value)
except sqlalchemy.exc.IntegrityError:
- raise ObjectScopeExisting
+ raise exceptions.ObjectScopeExisting
def delete_object_data(self, policy_id, data_id):
- return self.__delete_perimeter_data(ObjectData, policy_id, data_id)
+ return self.__delete_data(ObjectData, policy_id, data_id)
def is_action_data_exist(self, data_id=None,category_id=None):
- return self.__is_perimeter_data_exist(ActionData, data_id=data_id, category_id=category_id)
+ return self.__is_data_exist(ActionData, data_id=data_id, category_id=category_id)
def get_action_data(self, policy_id, data_id=None, category_id=None):
- return self.__get_perimeter_data(ActionData, policy_id, data_id=data_id, category_id=category_id)
+ return self.__get_data(ActionData, policy_id, data_id=data_id, category_id=category_id)
def set_action_data(self, policy_id, data_id=None, category_id=None, value=None):
try:
- return self.__set_perimeter_data(Action, ActionData, policy_id, data_id=data_id, category_id=category_id, value=value)
+ return self.__set_data(Action, ActionData, policy_id, data_id=data_id, category_id=category_id, value=value)
except sqlalchemy.exc.IntegrityError:
- raise ActionScopeExisting
+ raise exceptions.ActionScopeExisting
def delete_action_data(self, policy_id, data_id):
- return self.__delete_perimeter_data(ActionData, policy_id, data_id)
+ return self.__delete_data(ActionData, policy_id, data_id)
def get_subject_assignments(self, policy_id, subject_id=None, category_id=None):
with self.get_session_for_write() as session:
@@ -682,7 +690,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
assignments.append(data_id)
setattr(ref, "assignments", assignments)
else:
- raise SubjectAssignmentExisting
+ raise exceptions.SubjectAssignmentExisting
else:
ref = SubjectAssignment.from_dict(
{
@@ -737,7 +745,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
assignments.append(data_id)
setattr(ref, "assignments", assignments)
else:
- raise ObjectAssignmentExisting
+ raise exceptions.ObjectAssignmentExisting
else:
ref = ObjectAssignment.from_dict(
{
@@ -792,7 +800,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
assignments.append(data_id)
setattr(ref, "assignments", assignments)
else:
- raise ActionAssignmentExisting
+ raise exceptions.ActionAssignmentExisting
else:
ref = ActionAssignment.from_dict(
{
@@ -847,6 +855,10 @@ class PolicyConnector(BaseConnector, PolicyDriver):
def add_rule(self, policy_id, meta_rule_id, value):
try:
+ rules = self.get_rules(policy_id, meta_rule_id=meta_rule_id)
+ for _rule in map(lambda x: x["rule"], rules["rules"]):
+ if list(value.get('rule')) == list(_rule):
+ raise exceptions.RuleExisting
with self.get_session_for_write() as session:
ref = Rule.from_dict(
{
@@ -859,7 +871,7 @@ class PolicyConnector(BaseConnector, PolicyDriver):
session.add(ref)
return {ref.id: ref.to_dict()}
except sqlalchemy.exc.IntegrityError:
- raise RuleExisting
+ raise exceptions.RuleExisting
def delete_rule(self, policy_id, rule_id):
with self.get_session_for_write() as session:
@@ -905,7 +917,7 @@ class ModelConnector(BaseConnector, ModelDriver):
session.add(new)
return {new.id: new.to_dict()}
except sqlalchemy.exc.IntegrityError as e:
- raise ModelExisting
+ raise exceptions.ModelExisting
def get_models(self, model_id=None):
with self.get_session_for_read() as session:
@@ -939,7 +951,7 @@ class ModelConnector(BaseConnector, ModelDriver):
)
session.add(ref)
except sqlalchemy.exc.IntegrityError as e:
- raise MetaRuleExisting
+ raise exceptions.MetaRuleExisting
else:
query = session.query(MetaRule)
query = query.filter_by(id=meta_rule_id)
@@ -976,6 +988,8 @@ class ModelConnector(BaseConnector, ModelDriver):
return {_ref.id: _ref.to_dict() for _ref in ref_list}
def __add_perimeter_category(self, ClassType, name, description, uuid=None):
+ if not name.strip():
+ raise exceptions.CategoryNameInvalid
with self.get_session_for_write() as session:
ref = ClassType.from_dict(
{
@@ -1002,7 +1016,7 @@ class ModelConnector(BaseConnector, ModelDriver):
try:
return self.__add_perimeter_category(SubjectCategory, name, description, uuid=uuid)
except sql.exc.IntegrityError as e:
- raise SubjectCategoryExisting()
+ raise exceptions.SubjectCategoryExisting()
def delete_subject_category(self, category_id):
self.__delete_perimeter_category(SubjectCategory, category_id)
@@ -1014,7 +1028,7 @@ class ModelConnector(BaseConnector, ModelDriver):
try:
return self.__add_perimeter_category(ObjectCategory, name, description, uuid=uuid)
except sql.exc.IntegrityError as e:
- raise ObjectCategoryExisting()
+ raise exceptions.ObjectCategoryExisting()
def delete_object_category(self, category_id):
self.__delete_perimeter_category(ObjectCategory, category_id)
@@ -1027,7 +1041,7 @@ class ModelConnector(BaseConnector, ModelDriver):
try:
return self.__add_perimeter_category(ActionCategory, name, description, uuid=uuid)
except sql.exc.IntegrityError as e:
- raise ActionCategoryExisting()
+ raise exceptions.ActionCategoryExisting()
def delete_action_category(self, category_id):
self.__delete_perimeter_category(ActionCategory, category_id)
diff --git a/python_moondb/python_moondb/migrate_repo/versions/001_moon.py b/python_moondb/python_moondb/migrate_repo/versions/001_moon.py
index f69d708d..1bfb2ffa 100644
--- a/python_moondb/python_moondb/migrate_repo/versions/001_moon.py
+++ b/python_moondb/python_moondb/migrate_repo/versions/001_moon.py
@@ -3,7 +3,19 @@
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+import json
import sqlalchemy as sql
+from sqlalchemy import types as sql_types
+
+class JsonBlob(sql_types.TypeDecorator):
+
+ impl = sql.Text
+
+ def process_bind_param(self, value, dialect):
+ return json.dumps(value)
+
+ def process_result_value(self, value, dialect):
+ return json.loads(value)
def upgrade(migrate_engine):
@@ -16,7 +28,7 @@ def upgrade(migrate_engine):
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
sql.Column('keystone_project_id', sql.String(64), nullable=True, default=""),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', 'keystone_project_id', name='unique_constraint_models'),
mysql_engine='InnoDB',
mysql_charset='utf8')
@@ -28,7 +40,7 @@ def upgrade(migrate_engine):
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
sql.Column('model_id', sql.String(64), nullable=True, default=""),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', 'model_id', name='unique_constraint_models'),
mysql_engine='InnoDB',
mysql_charset='utf8')
@@ -39,7 +51,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', name='unique_constraint_models'),
mysql_engine='InnoDB',
mysql_charset='utf8')
@@ -86,7 +98,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', name='unique_constraint_subjects'),
mysql_engine='InnoDB',
mysql_charset='utf8')
@@ -97,7 +109,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', name='unique_constraint_objects'),
mysql_engine='InnoDB',
mysql_charset='utf8')
@@ -108,7 +120,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', name='unique_constraint_actions'),
mysql_engine='InnoDB',
mysql_charset='utf8')
@@ -119,7 +131,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.Column('category_id', sql.ForeignKey("subject_categories.id"), nullable=False),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.UniqueConstraint('name', 'category_id', 'policy_id', name='unique_constraint_subject_data'),
@@ -132,7 +144,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.Column('category_id', sql.ForeignKey("object_categories.id"), nullable=False),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.UniqueConstraint('name', 'category_id', 'policy_id', name='unique_constraint_object_data'),
@@ -145,7 +157,7 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.Column('category_id', sql.ForeignKey("action_categories.id"), nullable=False),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.UniqueConstraint('name', 'category_id', 'policy_id', name='unique_constraint_action_data'),
@@ -157,7 +169,7 @@ def upgrade(migrate_engine):
'subject_assignments',
meta,
sql.Column('id', sql.String(64), primary_key=True),
- sql.Column('assignments', sql.Text(), nullable=True),
+ sql.Column('assignments', sql.String(256), nullable=True),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.Column('subject_id', sql.ForeignKey("subjects.id"), nullable=False),
sql.Column('category_id', sql.ForeignKey("subject_categories.id"), nullable=False),
@@ -170,7 +182,7 @@ def upgrade(migrate_engine):
'object_assignments',
meta,
sql.Column('id', sql.String(64), primary_key=True),
- sql.Column('assignments', sql.Text(), nullable=True),
+ sql.Column('assignments', sql.String(256), nullable=True),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.Column('object_id', sql.ForeignKey("objects.id"), nullable=False),
sql.Column('category_id', sql.ForeignKey("object_categories.id"), nullable=False),
@@ -183,7 +195,7 @@ def upgrade(migrate_engine):
'action_assignments',
meta,
sql.Column('id', sql.String(64), primary_key=True),
- sql.Column('assignments', sql.Text(), nullable=True),
+ sql.Column('assignments', sql.String(256), nullable=True),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.Column('action_id', sql.ForeignKey("actions.id"), nullable=False),
sql.Column('category_id', sql.ForeignKey("action_categories.id"), nullable=False),
@@ -197,12 +209,12 @@ def upgrade(migrate_engine):
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('name', sql.String(256), nullable=False),
- sql.Column('subject_categories', sql.Text(), nullable=False),
- sql.Column('object_categories', sql.Text(), nullable=False),
- sql.Column('action_categories', sql.Text(), nullable=False),
- sql.Column('value', sql.Text(), nullable=True),
+ sql.Column('subject_categories', JsonBlob(), nullable=False),
+ sql.Column('object_categories', JsonBlob(), nullable=False),
+ sql.Column('action_categories', JsonBlob(), nullable=False),
+ sql.Column('value', JsonBlob(), nullable=True),
sql.UniqueConstraint('name', name='unique_constraint_meta_rule_name'),
- sql.UniqueConstraint('subject_categories', 'object_categories', 'action_categories', name='unique_constraint_meta_rule_def'),
+ # sql.UniqueConstraint('subject_categories', 'object_categories', 'action_categories', name='unique_constraint_meta_rule_def'),
mysql_engine='InnoDB',
mysql_charset='utf8')
meta_rules_table.create(migrate_engine, checkfirst=True)
@@ -211,10 +223,9 @@ def upgrade(migrate_engine):
'rules',
meta,
sql.Column('id', sql.String(64), primary_key=True),
- sql.Column('rule', sql.Text(), nullable=True),
+ sql.Column('rule', JsonBlob(), nullable=True),
sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
sql.Column('meta_rule_id', sql.ForeignKey("meta_rules.id"), nullable=False),
- sql.UniqueConstraint('rule', 'policy_id', 'meta_rule_id', name='unique_constraint_rule'),
mysql_engine='InnoDB',
mysql_charset='utf8')
rules_table.create(migrate_engine, checkfirst=True)
diff --git a/python_moondb/tests/unit_python/helpers/__init__.py b/python_moondb/tests/unit_python/helpers/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/__init__.py
diff --git a/python_moondb/tests/unit_python/helpers/assignment_helper.py b/python_moondb/tests/unit_python/helpers/assignment_helper.py
new file mode 100644
index 00000000..22a56e38
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/assignment_helper.py
@@ -0,0 +1,49 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+def get_action_assignments(policy_id, action_id=None, category_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_action_assignments("", policy_id, action_id, category_id)
+
+
+def add_action_assignment(policy_id, action_id, category_id, data_id):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_action_assignment("", policy_id, action_id, category_id, data_id)
+
+
+def delete_action_assignment(policy_id, action_id, category_id, data_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_action_assignment("", policy_id, action_id, category_id, data_id)
+
+
+def get_object_assignments(policy_id, object_id=None, category_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_object_assignments("", policy_id, object_id, category_id)
+
+
+def add_object_assignment(policy_id, object_id, category_id, data_id):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_object_assignment("", policy_id, object_id, category_id, data_id)
+
+
+def delete_object_assignment(policy_id, object_id, category_id, data_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_object_assignment("", policy_id, object_id, category_id, data_id)
+
+
+def get_subject_assignments(policy_id, subject_id=None, category_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_subject_assignments("", policy_id, subject_id, category_id)
+
+
+def add_subject_assignment(policy_id, subject_id, category_id, data_id):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_subject_assignment("", policy_id, subject_id, category_id, data_id)
+
+
+def delete_subject_assignment(policy_id, subject_id, category_id, data_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_subject_assignment("", policy_id, subject_id, category_id, data_id)
+
diff --git a/python_moondb/tests/unit_python/helpers/category_helper.py b/python_moondb/tests/unit_python/helpers/category_helper.py
new file mode 100644
index 00000000..55e95d91
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/category_helper.py
@@ -0,0 +1,54 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+def add_subject_category(cat_id=None, value=None):
+ from python_moondb.core import ModelManager
+ category = ModelManager.add_subject_category(user_id=None, category_id=cat_id, value=value)
+ return category
+
+
+def get_subject_category(cat_id=None):
+ from python_moondb.core import ModelManager
+ category = ModelManager.get_subject_categories(user_id=None, category_id=cat_id)
+ return category
+
+
+def add_object_category(cat_id=None, value=None):
+ from python_moondb.core import ModelManager
+ category = ModelManager.add_object_category(user_id=None, category_id=cat_id, value=value)
+ return category
+
+
+def get_object_category(cat_id=None):
+ from python_moondb.core import ModelManager
+ category = ModelManager.get_object_categories(user_id=None, category_id=cat_id)
+ return category
+
+
+def add_action_category(cat_id=None, value=None):
+ from python_moondb.core import ModelManager
+ category = ModelManager.add_action_category(user_id=None, category_id=cat_id, value=value)
+ return category
+
+
+def get_action_category(cat_id=None):
+ from python_moondb.core import ModelManager
+ category = ModelManager.get_action_categories(user_id=None, category_id=cat_id)
+ return category
+
+
+def delete_subject_category(category_id=None):
+ from python_moondb.core import ModelManager
+ return ModelManager.delete_subject_category("", category_id=category_id)
+
+
+def delete_object_category(category_id=None):
+ from python_moondb.core import ModelManager
+ return ModelManager.delete_object_category("", category_id=category_id)
+
+
+def delete_action_category(category_id=None):
+ from python_moondb.core import ModelManager
+ return ModelManager.delete_action_category("", category_id=category_id)
diff --git a/python_moondb/tests/unit_python/helpers/data_helper.py b/python_moondb/tests/unit_python/helpers/data_helper.py
new file mode 100644
index 00000000..20d9ae9a
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/data_helper.py
@@ -0,0 +1,98 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+def get_action_data(policy_id, data_id=None, category_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_action_data("", policy_id, data_id, category_id)
+
+
+def add_action_data(policy_id, data_id=None, category_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_action_data("", policy_id, data_id, category_id, value)
+
+
+def delete_action_data(policy_id, data_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_action_data("", policy_id, data_id)
+
+
+def get_object_data(policy_id, data_id=None, category_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_object_data("", policy_id, data_id, category_id)
+
+
+def add_object_data(policy_id, data_id=None, category_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_object_data("", policy_id, data_id, category_id, value)
+
+
+def delete_object_data(policy_id, data_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_object_data("", policy_id, data_id)
+
+
+def get_subject_data(policy_id, data_id=None, category_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_subject_data("", policy_id, data_id, category_id)
+
+
+def add_subject_data(policy_id, data_id=None, category_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.set_subject_data("", policy_id, data_id, category_id, value)
+
+
+def delete_subject_data(policy_id, data_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_subject_data("", policy_id, data_id)
+
+
+def get_actions(policy_id, perimeter_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_actions("", policy_id, perimeter_id)
+
+
+def add_action(policy_id, perimeter_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_action("", policy_id, perimeter_id, value)
+
+
+def delete_action(policy_id, perimeter_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_action("", policy_id, perimeter_id)
+
+
+def get_objects(policy_id, perimeter_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_objects("", policy_id, perimeter_id)
+
+
+def add_object(policy_id, perimeter_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_object("", policy_id, perimeter_id, value)
+
+
+def delete_object(policy_id, perimeter_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_object("", policy_id, perimeter_id)
+
+
+def get_subjects(policy_id, perimeter_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_subjects("", policy_id, perimeter_id)
+
+
+def add_subject(policy_id, perimeter_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.add_subject("", policy_id, perimeter_id, value)
+
+
+def delete_subject(policy_id, perimeter_id):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_subject("", policy_id, perimeter_id)
+
+
+def get_available_metadata(policy_id):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_available_metadata("", policy_id)
diff --git a/python_moondb/tests/unit_python/helpers/meta_rule_helper.py b/python_moondb/tests/unit_python/helpers/meta_rule_helper.py
new file mode 100644
index 00000000..80d138c6
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/meta_rule_helper.py
@@ -0,0 +1,48 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+from helpers import mock_data
+
+
+def set_meta_rule(meta_rule_id, value=None):
+ from python_moondb.core import ModelManager
+ if not value:
+ action_category_id = mock_data.create_action_category("action_category_id1")
+ subject_category_id = mock_data.create_subject_category("subject_category_id1")
+ object_category_id = mock_data.create_object_category("object_category_id1")
+ value = {
+ "name": "MLS_meta_rule",
+ "description": "test",
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
+ }
+ return ModelManager.set_meta_rule(user_id=None, meta_rule_id=meta_rule_id, value=value)
+
+
+def add_meta_rule(meta_rule_id=None, value=None):
+ from python_moondb.core import ModelManager
+ if not value:
+ action_category_id = mock_data.create_action_category("action_category_id1")
+ subject_category_id = mock_data.create_subject_category("subject_category_id1")
+ object_category_id = mock_data.create_object_category("object_category_id1")
+ value = {
+ "name": "MLS_meta_rule",
+ "description": "test",
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
+ }
+ return ModelManager.add_meta_rule(user_id=None, meta_rule_id=meta_rule_id, value=value)
+
+
+def get_meta_rules(meta_rule_id=None):
+ from python_moondb.core import ModelManager
+ return ModelManager.get_meta_rules(user_id=None, meta_rule_id=meta_rule_id)
+
+
+def delete_meta_rules(meta_rule_id=None):
+ from python_moondb.core import ModelManager
+ ModelManager.delete_meta_rule(user_id=None, meta_rule_id=meta_rule_id)
diff --git a/python_moondb/tests/unit_python/helpers/mock_data.py b/python_moondb/tests/unit_python/helpers/mock_data.py
new file mode 100644
index 00000000..82eebe88
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/mock_data.py
@@ -0,0 +1,144 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+from .category_helper import *
+from .policy_helper import *
+from .data_helper import *
+from .model_helper import *
+from .meta_rule_helper import *
+
+
+def create_subject_category(name):
+ subject_category = add_subject_category(
+ value={"name": name, "description": "description 1"})
+ return list(subject_category.keys())[0]
+
+
+def create_object_category(name):
+ object_category = add_object_category(
+ value={"name": name, "description": "description 1"})
+ return list(object_category.keys())[0]
+
+
+def create_action_category(name):
+ action_category = add_action_category(
+ value={"name": name, "description": "description 1"})
+ return list(action_category.keys())[0]
+
+
+def create_model(meta_rule_id, model_name="test_model"):
+ value = {
+ "name": model_name,
+ "description": "test",
+ "meta_rules": [meta_rule_id]
+
+ }
+ return value
+
+
+def create_policy(model_id, policy_name="policy_1"):
+ value = {
+ "name": policy_name,
+ "model_id": model_id,
+ "genre": "authz",
+ "description": "test",
+ }
+ return value
+
+
+def create_pdp(policies_ids):
+ value = {
+ "name": "test_pdp",
+ "security_pipeline": policies_ids,
+ "keystone_project_id": "keystone_project_id1",
+ "description": "...",
+ }
+ return value
+
+
+def create_new_policy(subject_category_name=None, object_category_name=None, action_category_name=None,
+ model_name="test_model", policy_name="policy_1", meta_rule_name="meta_rule1"):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id = create_new_meta_rule(
+ subject_category_name=subject_category_name,
+ object_category_name=object_category_name,
+ action_category_name=action_category_name, meta_rule_name=meta_rule_name)
+ model = add_model(value=create_model(meta_rule_id, model_name))
+ model_id = list(model.keys())[0]
+ value = create_policy(model_id, policy_name)
+ policy = add_policies(value=value)
+ assert policy
+ policy_id = list(policy.keys())[0]
+ return subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id
+
+
+def create_new_meta_rule(subject_category_name=None, object_category_name=None, action_category_name=None,
+ meta_rule_name="meta_rule1"):
+ subject_category_id = create_subject_category(subject_category_name)
+ object_category_id = create_object_category(object_category_name)
+ action_category_id = create_action_category(action_category_name)
+ value = {"name": meta_rule_name,
+ "algorithm": "name of the meta rule algorithm",
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
+ }
+ meta_rule = add_meta_rule(value=value)
+ return subject_category_id, object_category_id, action_category_id, list(meta_rule.keys())[0]
+
+
+def create_subject(policy_id):
+ value = {
+ "name": "testuser",
+ "description": "test",
+ }
+ subject = add_subject(policy_id=policy_id, value=value)
+ return list(subject.keys())[0]
+
+
+def create_object(policy_id):
+ value = {
+ "name": "testobject",
+ "description": "test",
+ }
+ object = add_object(policy_id=policy_id, value=value)
+ return list(object.keys())[0]
+
+
+def create_action(policy_id):
+ value = {
+ "name": "testaction",
+ "description": "test",
+ }
+ action = add_action(policy_id=policy_id, value=value)
+ return list(action.keys())[0]
+
+
+def create_subject_data(policy_id, category_id):
+ value = {
+ "name": "subject-security-level",
+ "description": {"low": "", "medium": "", "high": ""},
+ }
+ subject_data = add_subject_data(policy_id=policy_id, category_id=category_id, value=value).get('data')
+ assert subject_data
+ return list(subject_data.keys())[0]
+
+
+def create_object_data(policy_id, category_id):
+ value = {
+ "name": "object-security-level",
+ "description": {"low": "", "medium": "", "high": ""},
+ }
+ object_data = add_object_data(policy_id=policy_id, category_id=category_id, value=value).get('data')
+ return list(object_data.keys())[0]
+
+
+def create_action_data(policy_id, category_id):
+ value = {
+ "name": "action-type",
+ "description": {"vm-action": "", "storage-action": "", },
+ }
+ action_data = add_action_data(policy_id=policy_id, category_id=category_id, value=value).get('data')
+ return list(action_data.keys())[0]
+
diff --git a/python_moondb/tests/unit_python/helpers/model_helper.py b/python_moondb/tests/unit_python/helpers/model_helper.py
new file mode 100644
index 00000000..58946a99
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/model_helper.py
@@ -0,0 +1,50 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+from helpers import mock_data
+
+
+def get_models(model_id=None):
+ from python_moondb.core import ModelManager
+ return ModelManager.get_models(user_id=None, model_id=model_id)
+
+
+def add_model(model_id=None, value=None):
+ from python_moondb.core import ModelManager
+ if not value:
+ subject_category_id, object_category_id, action_category_id, meta_rule_id = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1")
+ name = "MLS" if model_id is None else "MLS " + model_id
+ value = {
+ "name": name,
+ "description": "test",
+ "meta_rules": [meta_rule_id]
+ }
+ return ModelManager.add_model(user_id=None, model_id=model_id, value=value)
+
+
+def delete_models(uuid=None, name=None):
+ from python_moondb.core import ModelManager
+ if not uuid:
+ for model_id, model_value in get_models():
+ if name == model_value['name']:
+ uuid = model_id
+ break
+ ModelManager.delete_model(user_id=None, model_id=uuid)
+
+
+def delete_all_models():
+ from python_moondb.core import ModelManager
+ models_values = get_models()
+ print(models_values)
+ for model_id, model_value in models_values.items():
+ ModelManager.delete_model(user_id=None, model_id=model_id)
+
+
+def update_model(model_id=None, value=None):
+ from python_moondb.core import ModelManager
+ return ModelManager.update_model(user_id=None, model_id=model_id, value=value)
diff --git a/python_moondb/tests/unit_python/helpers/pdp_helper.py b/python_moondb/tests/unit_python/helpers/pdp_helper.py
new file mode 100644
index 00000000..3d169b06
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/pdp_helper.py
@@ -0,0 +1,23 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+def update_pdp(pdp_id, value):
+ from python_moondb.core import PDPManager
+ return PDPManager.update_pdp("", pdp_id, value)
+
+
+def delete_pdp(pdp_id):
+ from python_moondb.core import PDPManager
+ PDPManager.delete_pdp("", pdp_id)
+
+
+def add_pdp(pdp_id=None, value=None):
+ from python_moondb.core import PDPManager
+ return PDPManager.add_pdp("", pdp_id, value)
+
+
+def get_pdp(pdp_id=None):
+ from python_moondb.core import PDPManager
+ return PDPManager.get_pdp("", pdp_id)
diff --git a/python_moondb/tests/unit_python/helpers/policy_helper.py b/python_moondb/tests/unit_python/helpers/policy_helper.py
new file mode 100644
index 00000000..c932ee3a
--- /dev/null
+++ b/python_moondb/tests/unit_python/helpers/policy_helper.py
@@ -0,0 +1,61 @@
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+def get_policies():
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_policies("admin")
+
+
+def add_policies(policy_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ if not value:
+ value = {
+ "name": "test_policy",
+ "model_id": "",
+ "genre": "authz",
+ "description": "test",
+ }
+ return PolicyManager.add_policy("admin", policy_id=policy_id, value=value)
+
+
+def delete_policies(uuid=None, name=None):
+ from python_moondb.core import PolicyManager
+ if not uuid:
+ for policy_id, policy_value in get_policies():
+ if name == policy_value['name']:
+ uuid = policy_id
+ break
+ PolicyManager.delete_policy("admin", uuid)
+
+
+def update_policy(policy_id, value):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.update_policy("admin", policy_id, value)
+
+
+def get_policy_from_meta_rules(meta_rule_id):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_policy_from_meta_rules("admin", meta_rule_id)
+
+
+def get_rules(policy_id=None, meta_rule_id=None, rule_id=None):
+ from python_moondb.core import PolicyManager
+ return PolicyManager.get_rules("", policy_id, meta_rule_id, rule_id)
+
+
+def add_rule(policy_id=None, meta_rule_id=None, value=None):
+ from python_moondb.core import PolicyManager
+ if not value:
+ value = {
+ "rule": ("high", "medium", "vm-action"),
+ "instructions": ({"decision": "grant"}),
+ "enabled": "",
+ }
+ return PolicyManager.add_rule("", policy_id, meta_rule_id, value)
+
+
+def delete_rule(policy_id=None, rule_id=None):
+ from python_moondb.core import PolicyManager
+ PolicyManager.delete_rule("", policy_id, rule_id)
diff --git a/python_moondb/tests/unit_python/models/test_categories.py b/python_moondb/tests/unit_python/models/test_categories.py
index 8782f172..f87d0e12 100644
--- a/python_moondb/tests/unit_python/models/test_categories.py
+++ b/python_moondb/tests/unit_python/models/test_categories.py
@@ -1,277 +1,79 @@
-# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-from .test_meta_rules import *
-import policies.mock_data as mock_data
-import policies.test_data as test_data
+import pytest
+import logging
+from python_moonutilities.exceptions import *
+from helpers import category_helper
logger = logging.getLogger("moon.db.tests.models.test_categories")
-
-def add_subject_category(cat_id=None, value=None):
- from python_moondb.core import ModelManager
- category = ModelManager.add_subject_category(user_id=None, category_id=cat_id, value=value)
- return category
-
-
def test_add_subject_category_twice():
- category = add_subject_category(value={"name": "category name", "description": "description 1"})
+ category = category_helper.add_subject_category(value={"name": "category name", "description": "description 1"})
category_id = list(category.keys())[0]
assert category is not None
with pytest.raises(SubjectCategoryExisting):
- add_subject_category(category_id, value={"name": "category name", "description": "description 2"})
-
-
-def test_add_subject_category_twice_with_same_name():
- category = add_subject_category(value={"name": "category name", "description": "description 1"})
- assert category is not None
- with pytest.raises(SubjectCategoryExisting):
- add_subject_category(value={"name": "category name", "description": "description 2"})
-
-
-def get_subject_category(cat_id=None):
- from python_moondb.core import ModelManager
- category = ModelManager.get_subject_categories(user_id=None, category_id=cat_id)
- return category
+ category_helper.add_subject_category(category_id,
+ value={"name": "category name", "description": "description 2"})
def test_get_subject_categories():
- added_category = add_subject_category(value={"name": "category name", "description": "description 1"})
+ added_category = category_helper.add_subject_category(
+ value={"name": "category name", "description": "description 1"})
category_id = list(added_category.keys())[0]
- subject_category = get_subject_category(category_id)
+ subject_category = category_helper.get_subject_category(category_id)
assert subject_category == added_category
def test_get_subject_categories_with_invalid_id():
category_id = "invalid_id"
- subject_category = get_subject_category(category_id)
+ subject_category = category_helper.get_subject_category(category_id)
assert len(subject_category) == 0
- category = add_subject_category(value={"name": "category name", "description": "description 1"})
- assert category is not None
- with pytest.raises(SubjectCategoryExisting):
- add_subject_category(value={"name": "category name", "description": "description 2"})
-
-
-def delete_subject_category(cat_id=None):
- from python_moondb.core import ModelManager
- ModelManager.delete_subject_category(user_id=None, category_id=cat_id)
-
-
-def test_delete_subject_category(db):
- category = add_subject_category(value={"name": "category name", "description": "description 1"})
- delete_subject_category(list(category.keys())[0])
-
-
-def test_delete_subject_category_with_invalid_id(db):
- with pytest.raises(SubjectCategoryUnknown) as exception_info:
- delete_subject_category(-1)
-
-
-def test_delete_subject_category_with_meta_rule(db):
- category = add_subject_category(value={"name": "category name", "description": "description 1"})
- subject_category_id = list(category.keys())[0]
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": [subject_category_id],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
- }
- add_meta_rule(value=value)
- with pytest.raises(DeleteCategoryWithMetaRule):
- delete_subject_category(subject_category_id)
-
-
-def test_delete_subject_category_with_data(db):
- category = add_subject_category(value={"name": "category name", "description": "description 1"})
- subject_category_id = list(category.keys())[0]
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
- category_id = subject_category_id
- value = {
- "name": "subject-security-level",
- "description": {"low": "", "medium": "", "high": ""},
- }
- test_data.add_subject_data(policy_id, data_id, category_id, value)
- with pytest.raises(DeleteCategoryWithData):
- delete_subject_category(subject_category_id)
-
-
-def add_object_category(cat_id=None, value=None):
- from python_moondb.core import ModelManager
- category = ModelManager.add_object_category(user_id=None, category_id=cat_id, value=value)
- return category
def test_add_object_category_twice():
- category = add_object_category(value={"name": "category name", "description": "description 1"})
+ category = category_helper.add_object_category(value={"name": "category name", "description": "description 1"})
category_id = list(category.keys())[0]
assert category is not None
with pytest.raises(ObjectCategoryExisting):
- add_object_category(category_id, value={"name": "category name", "description": "description 2"})
-
-
-def test_add_object_category_twice_with_same_name():
- category = add_object_category(value={"name": "category name", "description": "description 1"})
- assert category is not None
- with pytest.raises(ObjectCategoryExisting):
- add_object_category(value={"name": "category name", "description": "description 2"})
-
-
-def get_object_category(cat_id=None):
- from python_moondb.core import ModelManager
- category = ModelManager.get_object_categories(user_id=None, category_id=cat_id)
- return category
+ category_helper.add_object_category(category_id,
+ value={"name": "category name", "description": "description 2"})
def test_get_object_categories():
- added_category = add_object_category(value={"name": "category name", "description": "description 1"})
+ added_category = category_helper.add_object_category(
+ value={"name": "category name", "description": "description 1"})
category_id = list(added_category.keys())[0]
- object_category = get_object_category(category_id)
+ object_category = category_helper.get_object_category(category_id)
assert object_category == added_category
def test_get_object_categories_with_invalid_id():
category_id = "invalid_id"
- object_category = get_object_category(category_id)
+ object_category = category_helper.get_object_category(category_id)
assert len(object_category) == 0
- category = add_object_category(value={"name": "category name", "description": "description 1"})
- assert category is not None
- with pytest.raises(ObjectCategoryExisting):
- add_object_category(value={"name": "category name", "description": "description 2"})
-
-
-def delete_object_category(cat_id=None):
- from python_moondb.core import ModelManager
- ModelManager.delete_object_category(user_id=None, category_id=cat_id)
-
-
-def test_delete_object_category(db):
- category = add_object_category(value={"name": "category name", "description": "description 1"})
- delete_object_category(list(category.keys())[0])
-
-
-def test_delete_object_category_with_invalid_id(db):
- with pytest.raises(ObjectCategoryUnknown) as exception_info:
- delete_object_category(-1)
-
-
-def test_delete_object_category_with_meta_rule(db):
- category = add_object_category(value={"name": "category name", "description": "description 1"})
- object_category_id = list(category.keys())[0]
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": ["subject_id_1"],
- "object_categories": [object_category_id],
- "action_categories": ["action_type_id_1"]
- }
- add_meta_rule(value=value)
- with pytest.raises(DeleteCategoryWithMetaRule):
- delete_object_category(object_category_id)
-
-
-def test_delete_object_category_with_data(db):
- policy_id = mock_data.get_policy_id()
- category = add_object_category(value={"name": "category name", "description": "description 1"})
- object_category_id = list(category.keys())[0]
- data_id = "data_id_1"
- category_id = object_category_id
- value = {
- "name": "object-security-level",
- "description": {"low": "", "medium": "", "high": ""},
- }
- test_data.add_object_data(policy_id, data_id, category_id, value)
- with pytest.raises(DeleteCategoryWithData):
- delete_object_category(object_category_id)
-
-
-def add_action_category(cat_id=None, value=None):
- from python_moondb.core import ModelManager
- category = ModelManager.add_action_category(user_id=None, category_id=cat_id, value=value)
- return category
def test_add_action_category_twice():
- category = add_action_category(value={"name": "category name", "description": "description 1"})
+ category = category_helper.add_action_category(value={"name": "category name", "description": "description 1"})
category_id = list(category.keys())[0]
assert category is not None
with pytest.raises(ActionCategoryExisting):
- add_action_category(category_id, value={"name": "category name", "description": "description 2"})
-
-
-def test_add_action_category_twice_with_same_name():
- category = add_action_category(value={"name": "category name", "description": "description 1"})
- assert category is not None
- with pytest.raises(ActionCategoryExisting):
- add_action_category(value={"name": "category name", "description": "description 2"})
-
-
-def get_action_category(cat_id=None):
- from python_moondb.core import ModelManager
- category = ModelManager.get_action_categories(user_id=None, category_id=cat_id)
- return category
+ category_helper.add_action_category(category_id,
+ value={"name": "category name", "description": "description 2"})
def test_get_action_categories():
- added_category = add_action_category(value={"name": "category name", "description": "description 1"})
+ added_category = category_helper.add_action_category(
+ value={"name": "category name", "description": "description 1"})
category_id = list(added_category.keys())[0]
- action_category = get_action_category(category_id)
+ action_category = category_helper.get_action_category(category_id)
assert action_category == added_category
def test_get_action_categories_with_invalid_id():
category_id = "invalid_id"
- action_category = get_action_category(category_id)
+ action_category = category_helper.get_action_category(category_id)
assert len(action_category) == 0
- category = add_action_category(value={"name": "category name", "description": "description 1"})
- assert category is not None
- with pytest.raises(ActionCategoryExisting):
- add_action_category(value={"name": "category name", "description": "description 2"})
-
-
-def delete_action_category(cat_id=None):
- from python_moondb.core import ModelManager
- ModelManager.delete_action_category(user_id=None, category_id=cat_id)
-
-
-def test_delete_action_category(db):
- category = add_action_category(value={"name": "category name", "description": "description 1"})
- delete_action_category(list(category.keys())[0])
-
-
-def test_delete_action_category_with_invalid_id(db):
- with pytest.raises(ActionCategoryUnknown) as exception_info:
- delete_action_category(-1)
-
-
-def test_delete_action_category_with_meta_rule(db):
- category = add_action_category(value={"name": "category name", "description": "description 1"})
- action_category_id = list(category.keys())[0]
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": ["subject_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": [action_category_id]
- }
- add_meta_rule(value=value)
- with pytest.raises(DeleteCategoryWithMetaRule):
- delete_action_category(action_category_id)
-
-
-def test_delete_action_category_with_data(db):
- category = add_action_category(value={"name": "category name", "description": "description 1"})
- action_category_id = list(category.keys())[0]
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
- category_id = action_category_id
- value = {
- "name": "action-type",
- "description": {"vm-action": "", "storage-action": "", },
- }
- test_data.add_action_data(policy_id, data_id, category_id, value)
- with pytest.raises(DeleteCategoryWithData):
- delete_action_category(action_category_id)
diff --git a/python_moondb/tests/unit_python/models/test_meta_rules.py b/python_moondb/tests/unit_python/models/test_meta_rules.py
index 4e60e11a..102cd724 100644
--- a/python_moondb/tests/unit_python/models/test_meta_rules.py
+++ b/python_moondb/tests/unit_python/models/test_meta_rules.py
@@ -1,141 +1,113 @@
-# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-from .test_models import *
-
-
-def set_meta_rule(meta_rule_id, value=None):
- from python_moondb.core import ModelManager
- if not value:
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
- }
- return ModelManager.set_meta_rule(user_id=None, meta_rule_id=meta_rule_id, value=value)
-
-
-def add_meta_rule(meta_rule_id=None, value=None):
- from python_moondb.core import ModelManager
- if not value:
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
- }
- return ModelManager.add_meta_rule(user_id=None, meta_rule_id=meta_rule_id, value=value)
-
-
-def get_meta_rules(meta_rule_id=None):
- from python_moondb.core import ModelManager
- return ModelManager.get_meta_rules(user_id=None, meta_rule_id=meta_rule_id)
-
-
-def delete_meta_rules(meta_rule_id=None):
- from python_moondb.core import ModelManager
- ModelManager.delete_meta_rule(user_id=None, meta_rule_id=meta_rule_id)
+import pytest
+from helpers import meta_rule_helper
+import helpers.mock_data as mock_data
def test_set_not_exist_meta_rule_error(db):
# set not existing meta rule and expect to raise and error
with pytest.raises(Exception) as exception_info:
- set_meta_rule(meta_rule_id=None)
- assert str(exception_info.value) == '400: Sub Meta Rule Unknown'
+ meta_rule_helper.set_meta_rule(meta_rule_id=None)
+ assert str(exception_info.value) == '400: Meta Rule Unknown'
def test_add_new_meta_rule_success(db):
+ action_category_id = mock_data.create_action_category("action_category_id1")
+ subject_category_id = mock_data.create_subject_category("subject_category_id1")
+ object_category_id = mock_data.create_object_category("object_category_id1")
value = {
"name": "MLS_meta_rule",
"description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
- }
- metaRules = add_meta_rule()
- assert isinstance(metaRules, dict)
- assert metaRules
- assert len(metaRules) is 1
- meta_rule_id = list(metaRules.keys())[0]
- for key in ("name", "description", "subject_categories", "object_categories", "action_categories"):
- assert key in metaRules[meta_rule_id]
- assert metaRules[meta_rule_id][key] == value[key]
-
-
-def test_add_new_meta_rule_with_same_name_twice(db):
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
}
- meta_rules = add_meta_rule(value=value)
+ meta_rules = meta_rule_helper.add_meta_rule(value=value)
assert isinstance(meta_rules, dict)
assert meta_rules
- with pytest.raises(Exception) as exc_info:
- add_meta_rule(value=value)
+ assert len(meta_rules) is 1
+ meta_rule_id = list(meta_rules.keys())[0]
+ for key in ("name", "description", "subject_categories", "object_categories", "action_categories"):
+ assert key in meta_rules[meta_rule_id]
+ assert meta_rules[meta_rule_id][key] == value[key]
-def test_set_meta_rule_succes(db):
+def test_set_meta_rule_success(db):
# arrange
- meta_rules = add_meta_rule()
+ meta_rules = meta_rule_helper.add_meta_rule()
meta_rule_id = list(meta_rules.keys())[0]
+ action_category_id = mock_data.create_action_category("action_category_id2")
+ subject_category_id = mock_data.create_subject_category("subject_category_id2")
+ object_category_id = mock_data.create_object_category("object_category_id2")
updated_value = {
"name": "MLS_meta_rule",
"description": "test",
- "subject_categories": ["user_role_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
}
# action
- updated_meta_rule = set_meta_rule(meta_rule_id, updated_value)
+ updated_meta_rule = meta_rule_helper.set_meta_rule(meta_rule_id, updated_value)
# assert
updated_meta_rule_id = list(updated_meta_rule.keys())[0]
assert updated_meta_rule_id == meta_rule_id
- assert updated_meta_rule[updated_meta_rule_id]["subject_categories"] == \
- updated_value["subject_categories"]
+ assert updated_meta_rule[updated_meta_rule_id]["subject_categories"] == updated_value["subject_categories"]
def test_add_existing_meta_rule_error(db):
- meta_rules = add_meta_rule()
+ action_category_id = mock_data.create_action_category("action_category_id3")
+ subject_category_id = mock_data.create_subject_category("subject_category_id3")
+ object_category_id = mock_data.create_object_category("object_category_id3")
+ value = {
+ "name": "MLS_meta_rule",
+ "description": "test",
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
+ }
+ meta_rules = meta_rule_helper.add_meta_rule(value=value)
meta_rule_id = list(meta_rules.keys())[0]
with pytest.raises(Exception) as exception_info:
- add_meta_rule(meta_rule_id=meta_rule_id)
+ meta_rule_helper.add_meta_rule(meta_rule_id=meta_rule_id)
assert str(exception_info.value) == '400: Sub Meta Rule Existing'
def test_get_meta_rule_success(db):
# arrange
+ action_category_id = mock_data.create_action_category("action_type")
+ subject_category_id = mock_data.create_subject_category("user_security_level")
+ object_category_id = mock_data.create_object_category("vm_security_level")
values = {}
value1 = {
"name": "MLS_meta_rule",
"description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
}
- meta_rules1 = add_meta_rule(value=value1)
+ meta_rules1 = meta_rule_helper.add_meta_rule(value=value1)
meta_rule_id1 = list(meta_rules1.keys())[0]
values[meta_rule_id1] = value1
+ action_category_id = mock_data.create_action_category("action_type2")
+ subject_category_id = mock_data.create_subject_category("user_security_level2")
+ object_category_id = mock_data.create_object_category("vm_security_level2")
value2 = {
"name": "rbac_meta_rule",
"description": "test",
- "subject_categories": ["user_role_id_1"],
- "object_categories": ["vm_id_1"],
- "action_categories": ["action_type_id_1"]
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
}
- meta_rules2 = add_meta_rule(value=value2)
+ meta_rules2 = meta_rule_helper.add_meta_rule(value=value2)
meta_rule_id2 = list(meta_rules2.keys())[0]
values[meta_rule_id2] = value2
# action
- meta_rules = get_meta_rules()
+ meta_rules = meta_rule_helper.get_meta_rules()
# assert
assert isinstance(meta_rules, dict)
assert meta_rules
@@ -148,10 +120,10 @@ def test_get_meta_rule_success(db):
def test_get_specific_meta_rule_success(db):
# arrange
- added_meta_rules = add_meta_rule()
+ added_meta_rules = meta_rule_helper.add_meta_rule()
added_meta_rule_id = list(added_meta_rules.keys())[0]
# action
- meta_rules = get_meta_rules(meta_rule_id=added_meta_rule_id)
+ meta_rules = meta_rule_helper.get_meta_rules(meta_rule_id=added_meta_rule_id)
meta_rule_id = list(meta_rules.keys())[0]
# assert
assert meta_rule_id == added_meta_rule_id
@@ -161,58 +133,28 @@ def test_get_specific_meta_rule_success(db):
def test_delete_meta_rules_success(db):
+ action_category_id = mock_data.create_action_category("action_type")
+ subject_category_id = mock_data.create_subject_category("user_security_level")
+ object_category_id = mock_data.create_object_category("vm_security_level")
# arrange
value1 = {
"name": "MLS_meta_rule",
"description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
+ "subject_categories": [subject_category_id],
+ "object_categories": [object_category_id],
+ "action_categories": [action_category_id]
}
- meta_rules1 = add_meta_rule(value=value1)
+ meta_rules1 = meta_rule_helper.add_meta_rule(value=value1)
meta_rule_id1 = list(meta_rules1.keys())[0]
- value2 = {
- "name": "rbac_meta_rule",
- "description": "test",
- "subject_categories": ["user_role_id_1"],
- "object_categories": ["vm_id_1"],
- "action_categories": ["action_type_id_1"]
- }
- meta_rules2 = add_meta_rule(value=value2)
- meta_rule_id2 = list(meta_rules2.keys())[0]
-
# action
- delete_meta_rules(meta_rule_id1)
+ meta_rule_helper.delete_meta_rules(meta_rule_id1)
# assert
- meta_rules = get_meta_rules()
+ meta_rules = meta_rule_helper.get_meta_rules()
assert meta_rule_id1 not in meta_rules
def test_delete_invalid_meta_rules_error(db):
with pytest.raises(Exception) as exception_info:
- delete_meta_rules("INVALID_META_RULE_ID")
- assert str(exception_info.value) == '400: Sub Meta Rule Unknown'
-
-
-def test_delete_meta_rule_with_assigned_model(db):
- value = {
- "name": "MLS_meta_rule",
- "description": "test",
- "subject_categories": ["user_security_level_id_1"],
- "object_categories": ["vm_security_level_id_1"],
- "action_categories": ["action_type_id_1"]
- }
- metaRules = add_meta_rule()
- assert isinstance(metaRules, dict)
- assert metaRules
- assert len(metaRules) is 1
- meta_rule_id = list(metaRules.keys())[0]
- model_value1 = {
- "name": "MLS",
- "description": "test",
- "meta_rules": meta_rule_id
- }
- add_model(value=model_value1)
- with pytest.raises(DeleteMetaRuleWithModel) as exception_info:
- delete_meta_rules(meta_rule_id)
+ meta_rule_helper.delete_meta_rules("INVALID_META_RULE_ID")
+ assert str(exception_info.value) == '400: Meta Rule Unknown'
diff --git a/python_moondb/tests/unit_python/models/test_models.py b/python_moondb/tests/unit_python/models/test_models.py
index 251792c5..0026345c 100644
--- a/python_moondb/tests/unit_python/models/test_models.py
+++ b/python_moondb/tests/unit_python/models/test_models.py
@@ -1,4 +1,4 @@
-# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
@@ -6,54 +6,17 @@
import pytest
from python_moonutilities.exceptions import *
import logging
-import policies.test_policies as test_policies
+import helpers.mock_data as mock_data
+import helpers.model_helper as model_helper
+import helpers.category_helper as category_helper
+import helpers.policy_helper as policy_helper
logger = logging.getLogger("moon.db.tests.test_model")
-def get_models(model_id=None):
- from python_moondb.core import ModelManager
- return ModelManager.get_models(user_id=None, model_id=model_id)
-
-
-def add_model(model_id=None, value=None):
- from python_moondb.core import ModelManager
- if not value:
- name = "MLS" if model_id is None else "MLS " + model_id
- value = {
- "name": name,
- "description": "test",
- "meta_rules": "meta_rule_mls_1"
- }
- return ModelManager.add_model(user_id=None, model_id=model_id, value=value)
-
-
-def delete_models(uuid=None, name=None):
- from python_moondb.core import ModelManager
- if not uuid:
- for model_id, model_value in get_models():
- if name == model_value['name']:
- uuid = model_id
- break
- ModelManager.delete_model(user_id=None, model_id=uuid)
-
-
-def delete_all_models():
- from python_moondb.core import ModelManager
- models_values = get_models()
- print(models_values)
- for model_id, model_value in models_values.items():
- ModelManager.delete_model(user_id=None, model_id=model_id)
-
-
-def update_model(model_id=None, value=None):
- from python_moondb.core import ModelManager
- return ModelManager.update_model(user_id=None, model_id=model_id, value=value)
-
-
def test_get_models_empty(db):
# act
- models = get_models()
+ models = model_helper.get_models()
# assert
assert isinstance(models, dict)
assert not models
@@ -61,75 +24,107 @@ def test_get_models_empty(db):
def test_get_model(db):
# prepare
- add_model(model_id="mls_model_id")
+ model_helper.add_model(model_id="mls_model_id")
# act
- models = get_models()
+ models = model_helper.get_models()
# assert
assert isinstance(models, dict)
assert models # assert model is not empty
assert len(models) is 1
- delete_all_models()
+ model_helper.delete_all_models()
def test_get_specific_model(db):
# prepare
- add_model(model_id="mls_model_id")
- add_model(model_id="rbac_model_id")
+ model_helper.add_model(model_id="mls_model_id")
# act
- models = get_models(model_id="mls_model_id")
+ models = model_helper.get_models(model_id="mls_model_id")
# assert
assert isinstance(models, dict)
assert models # assert model is not empty
assert len(models) is 1
- delete_all_models()
+ model_helper.delete_all_models()
def test_add_model(db):
# act
- model = add_model()
+ model = model_helper.add_model()
# assert
assert isinstance(model, dict)
assert model # assert model is not empty
assert len(model) is 1
- delete_all_models()
+ model_helper.delete_all_models()
def test_add_same_model_twice(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ value = {
+ "name": "model1",
+ "description": "test",
+ "meta_rules": [meta_rule_id]
+ }
# prepare
- add_model(model_id="model_1") # add model twice
+ model_helper.add_model(model_id="model_1", value=value) # add model twice
# act
+ subject_category_id, object_category_id, action_category_id, meta_rule_id = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category2",
+ object_category_name="object_category2",
+ action_category_name="action_category2",
+ meta_rule_name="meta_rule_2")
+ value = {
+ "name": "model2",
+ "description": "test",
+ "meta_rules": [meta_rule_id]
+ }
with pytest.raises(ModelExisting) as exception_info:
- add_model(model_id="model_1")
- delete_all_models()
+ model_helper.add_model(model_id="model_1", value=value)
+ model_helper.delete_all_models()
# assert str(exception_info.value) == '409: Model Error'
def test_add_model_generate_new_uuid(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id1 = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category3",
+ object_category_name="object_category3",
+ action_category_name="action_category3",
+ meta_rule_name="meta_rule_3")
model_value1 = {
"name": "MLS",
"description": "test",
- "meta_rules": "meta_rule_mls_1"
+ "meta_rules": [meta_rule_id1]
}
- model1 = add_model(value=model_value1)
-
+ model1 = model_helper.add_model(value=model_value1)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id2 = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category4",
+ object_category_name="object_category4",
+ action_category_name="action_category4",
+ meta_rule_name="meta_rule_4")
model_value2 = {
"name": "rbac",
"description": "test",
- "meta_rules": "meta_rule_mls_2"
+ "meta_rules": [meta_rule_id2]
}
- model2 = add_model(value=model_value2)
+ model2 = model_helper.add_model(value=model_value2)
assert list(model1)[0] != list(model2)[0]
- delete_all_models()
+ model_helper.delete_all_models()
def test_add_models(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category5",
+ object_category_name="object_category5",
+ action_category_name="action_category5")
model_value1 = {
"name": "MLS",
"description": "test",
- "meta_rules": "meta_rule_mls_1"
+ "meta_rules": [meta_rule_id]
}
- models = add_model(value=model_value1)
+ models = model_helper.add_model(value=model_value1)
assert isinstance(models, dict)
assert models
assert len(models.keys()) == 1
@@ -137,78 +132,101 @@ def test_add_models(db):
for key in ("name", "meta_rules", "description"):
assert key in models[model_id]
assert models[model_id][key] == model_value1[key]
- delete_all_models()
+ model_helper.delete_all_models()
def test_add_models_with_same_name_twice(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category5",
+ object_category_name="object_category5",
+ action_category_name="action_category5")
model_value1 = {
"name": "MLS",
"description": "test",
- "meta_rules": "meta_rule_mls_1"
+ "meta_rules": [meta_rule_id]
}
- models = add_model(value=model_value1)
+ models = model_helper.add_model(value=model_value1)
assert isinstance(models, dict)
assert models
with pytest.raises(Exception) as exc_info:
- add_model(value=model_value1)
- delete_all_models()
+ model_helper.add_model(value=model_value1)
+ model_helper.delete_all_models()
def test_delete_models(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id1 = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category6",
+ object_category_name="object_category6",
+ action_category_name="action_category6",
+ meta_rule_name="meta_rule_6")
model_value1 = {
"name": "MLS",
"description": "test",
- "meta_rules": "meta_rule_mls_1"
+ "meta_rules": [meta_rule_id1]
}
- model1 = add_model(value=model_value1)
-
+ model1 = model_helper.add_model(value=model_value1)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id2 = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category7",
+ object_category_name="object_category7",
+ action_category_name="action_category7",
+ meta_rule_name="meta_rule_7")
model_value2 = {
"name": "rbac",
"description": "test",
- "meta_rules": "meta_rule_mls_2"
+ "meta_rules": [meta_rule_id2]
}
- model2 = add_model(value=model_value2)
+ model_helper.add_model(value=model_value2)
id = list(model1)[0]
- delete_models(id)
+ model_helper.delete_models(id)
# assert
- models = get_models()
+ models = model_helper.get_models()
assert id not in models
- delete_all_models()
+ model_helper.delete_all_models()
def test_update_model(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id1 = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category8",
+ object_category_name="object_category8",
+ action_category_name="action_category8",
+ meta_rule_name="meta_rule_8")
# prepare
model_value = {
"name": "MLS",
"description": "test",
- "meta_rules": "meta_rule_mls_1"
+ "meta_rules": [meta_rule_id1]
}
- model = add_model(value=model_value)
+ model = model_helper.add_model(value=model_value)
model_id = list(model)[0]
+ subject_category_id, object_category_id, action_category_id, meta_rule_id2 = mock_data.create_new_meta_rule(
+ subject_category_name="subject_category9",
+ object_category_name="object_category9",
+ action_category_name="action_category9",
+ meta_rule_name="meta_rule_9")
new_model_value = {
"name": "MLS2",
"description": "test",
- "meta_rules": "meta_rule_mls_2"
+ "meta_rules": [meta_rule_id2]
}
# act
- update_model(model_id=model_id, value=new_model_value)
+ model_helper.update_model(model_id=model_id, value=new_model_value)
# assert
- model = get_models(model_id)
+ model = model_helper.get_models(model_id)
for key in ("name", "meta_rules", "description"):
assert key in model[model_id]
assert model[model_id][key] == new_model_value[key]
- delete_all_models()
+ model_helper.delete_all_models()
def test_delete_model_assigned_to_policy(db):
model_value1 = {
"name": "MLS",
"description": "test",
- "meta_rules": "meta_rule_mls_1"
+ "meta_rules": []
}
- models = add_model(value=model_value1)
+ models = model_helper.add_model(value=model_value1)
assert isinstance(models, dict)
assert models
assert len(models.keys()) == 1
@@ -219,6 +237,201 @@ def test_delete_model_assigned_to_policy(db):
"genre": "authz",
"description": "test",
}
- test_policies.add_policies(value=value)
+ policy_helper.add_policies(value=value)
with pytest.raises(DeleteModelWithPolicy) as exception_info:
- delete_models(uuid=model_id)
+ model_helper.delete_models(uuid=model_id)
+
+
+def test_add_subject_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "subject_category",
+ "description": "description subject_category"
+ }
+ subject_category = category_helper.add_subject_category(category_id, value)
+ assert subject_category
+ assert len(subject_category) == 1
+
+
+def test_add_subject_category_with_empty_name(db):
+ category_id = "category_id1"
+ value = {
+ "name": "",
+ "description": "description subject_category"
+ }
+ with pytest.raises(Exception) as exception_info:
+ category_helper.add_subject_category(category_id, value)
+ assert str(exception_info.value) == '400: Category Name Invalid'
+
+
+def test_add_subject_category_with_same_category_id(db):
+ category_id = "category_id1"
+ value = {
+ "name": "subject_category",
+ "description": "description subject_category"
+ }
+ category_helper.add_subject_category(category_id, value)
+ with pytest.raises(Exception) as exception_info:
+ category_helper.add_subject_category(category_id, value)
+ assert str(exception_info.value) == '409: Subject Category Existing'
+
+
+def test_get_subject_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "subject_category",
+ "description": "description subject_category"
+ }
+ category_helper.add_subject_category(category_id, value)
+ subject_category = category_helper.get_subject_category(category_id)
+ assert subject_category
+ assert len(subject_category) == 1
+
+
+def test_delete_subject_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "subject_category",
+ "description": "description subject_category"
+ }
+ category_helper.add_subject_category(category_id, value)
+ subject_category = category_helper.delete_subject_category(category_id)
+ assert not subject_category
+
+
+def test_delete_subject_category_with_unkown_category_id(db):
+ category_id = "invalid_category_id"
+
+ with pytest.raises(Exception) as exception_info:
+ category_helper.delete_subject_category(category_id)
+ assert str(exception_info.value) == '400: Subject Category Unknown'
+
+
+def test_add_object_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "object_category",
+ "description": "description object_category"
+ }
+ object_category = category_helper.add_object_category(category_id, value)
+ assert object_category
+ assert len(object_category) == 1
+
+
+def test_add_object_category_with_same_category_id(db):
+ category_id = "category_id1"
+ value = {
+ "name": "object_category",
+ "description": "description object_category"
+ }
+ category_helper.add_object_category(category_id, value)
+ with pytest.raises(Exception) as exception_info:
+ category_helper.add_object_category(category_id, value)
+ assert str(exception_info.value) == '409: Object Category Existing'
+
+
+def test_add_object_category_with_empty_name(db):
+ category_id = "category_id1"
+ value = {
+ "name": "",
+ "description": "description object_category"
+ }
+ with pytest.raises(Exception) as exception_info:
+ category_helper.add_object_category(category_id, value)
+ assert str(exception_info.value) == '400: Category Name Invalid'
+
+
+def test_get_object_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "object_category",
+ "description": "description object_category"
+ }
+ category_helper.add_object_category(category_id, value)
+ object_category = category_helper.get_object_category(category_id)
+ assert object_category
+ assert len(object_category) == 1
+
+
+def test_delete_object_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "object_category",
+ "description": "description object_category"
+ }
+ category_helper.add_object_category(category_id, value)
+ object_category = category_helper.delete_object_category(category_id)
+ assert not object_category
+
+
+def test_delete_object_category_with_unkown_category_id(db):
+ category_id = "invalid_category_id"
+
+ with pytest.raises(Exception) as exception_info:
+ category_helper.delete_object_category(category_id)
+ assert str(exception_info.value) == '400: Object Category Unknown'
+
+
+def test_add_action_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "action_category",
+ "description": "description action_category"
+ }
+ action_category = category_helper.add_action_category(category_id, value)
+ assert action_category
+ assert len(action_category) == 1
+
+
+def test_add_action_category_with_same_category_id(db):
+ category_id = "category_id1"
+ value = {
+ "name": "action_category",
+ "description": "description action_category"
+ }
+ category_helper.add_action_category(category_id, value)
+ with pytest.raises(Exception) as exception_info:
+ category_helper.add_action_category(category_id, value)
+ assert str(exception_info.value) == '409: Action Category Existing'
+
+
+def test_add_action_category_with_empty_name(db):
+ category_id = "category_id1"
+ value = {
+ "name": "",
+ "description": "description action_category"
+ }
+ with pytest.raises(Exception) as exception_info:
+ category_helper.add_action_category(category_id, value)
+ assert str(exception_info.value) == '400: Category Name Invalid'
+
+
+def test_get_action_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "action_category",
+ "description": "description action_category"
+ }
+ category_helper.add_action_category(category_id, value)
+ action_category = category_helper.get_action_category(category_id)
+ assert action_category
+ assert len(action_category) == 1
+
+
+def test_delete_action_category(db):
+ category_id = "category_id1"
+ value = {
+ "name": "action_category",
+ "description": "description action_category"
+ }
+ category_helper.add_action_category(category_id, value)
+ action_category = category_helper.delete_action_category(category_id)
+ assert not action_category
+
+
+def test_delete_action_category_with_unkown_category_id(db):
+ category_id = "invalid_category_id"
+
+ with pytest.raises(Exception) as exception_info:
+ category_helper.delete_action_category(category_id)
+ assert str(exception_info.value) == '400: Action Category Unknown'
diff --git a/python_moondb/tests/unit_python/policies/mock_data.py b/python_moondb/tests/unit_python/policies/mock_data.py
index 3e9bea93..47fc9f9e 100644
--- a/python_moondb/tests/unit_python/policies/mock_data.py
+++ b/python_moondb/tests/unit_python/policies/mock_data.py
@@ -1,11 +1,17 @@
+import helpers.model_helper as model_helper
+import helpers.meta_rule_helper as meta_rule_helper
+import helpers.policy_helper as policy_helper
+import helpers.category_helper as category_helper
+
+
def create_meta_rule(meta_rule_name="meta_rule1", category_prefix=""):
meta_rule_value = {
"name": meta_rule_name,
"algorithm": "name of the meta rule algorithm",
"subject_categories": [category_prefix + "subject_category_id1",
category_prefix + "subject_category_id2"],
- "object_categories": [category_prefix +"object_category_id1"],
- "action_categories": [category_prefix +"action_category_id1"]
+ "object_categories": [category_prefix + "object_category_id1"],
+ "action_categories": [category_prefix + "action_category_id1"]
}
return meta_rule_value
@@ -41,15 +47,28 @@ def create_pdp(pdp_ids):
def get_policy_id(model_name="test_model", policy_name="policy_1", meta_rule_name="meta_rule1", category_prefix=""):
- import policies.test_policies as test_policies
- import models.test_models as test_models
- import models.test_meta_rules as test_meta_rules
- meta_rule = test_meta_rules.add_meta_rule(value=create_meta_rule(meta_rule_name, category_prefix))
+ category_helper.add_subject_category(
+ category_prefix + "subject_category_id1",
+ value={"name": category_prefix + "subject_category_id1",
+ "description": "description 1"})
+ category_helper.add_subject_category(
+ category_prefix + "subject_category_id2",
+ value={"name": category_prefix + "subject_category_id2",
+ "description": "description 1"})
+ category_helper.add_object_category(
+ category_prefix + "object_category_id1",
+ value={"name": category_prefix + "object_category_id1",
+ "description": "description 1"})
+ category_helper.add_action_category(
+ category_prefix + "action_category_id1",
+ value={"name": category_prefix + "action_category_id1",
+ "description": "description 1"})
+ meta_rule = meta_rule_helper.add_meta_rule(value=create_meta_rule(meta_rule_name, category_prefix))
meta_rule_id = list(meta_rule.keys())[0]
- model = test_models.add_model(value=create_model(meta_rule_id, model_name))
+ model = model_helper.add_model(value=create_model(meta_rule_id, model_name))
model_id = list(model.keys())[0]
value = create_policy(model_id, policy_name)
- policy = test_policies.add_policies(value=value)
+ policy = policy_helper.add_policies(value=value)
assert policy
policy_id = list(policy.keys())[0]
return policy_id
diff --git a/python_moondb/tests/unit_python/policies/test_assignments.py b/python_moondb/tests/unit_python/policies/test_assignments.py
index 1ca140e6..675c2ff9 100755
--- a/python_moondb/tests/unit_python/policies/test_assignments.py
+++ b/python_moondb/tests/unit_python/policies/test_assignments.py
@@ -1,257 +1,220 @@
-import policies.mock_data as mock_data
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
+import helpers.mock_data as mock_data
+import helpers.assignment_helper as assignment_helper
from python_moonutilities.exceptions import *
import pytest
-def get_action_assignments(policy_id, action_id=None, category_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_action_assignments("", policy_id, action_id, category_id)
-
-
-def add_action_assignment(policy_id, action_id, category_id, data_id):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_action_assignment("", policy_id, action_id, category_id, data_id)
-
-
-def delete_action_assignment(policy_id, action_id, category_id, data_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_action_assignment("", policy_id, action_id, category_id, data_id)
-
-
-def get_object_assignments(policy_id, object_id=None, category_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_object_assignments("", policy_id, object_id, category_id)
-
-
-def add_object_assignment(policy_id, object_id, category_id, data_id):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_object_assignment("", policy_id, object_id, category_id, data_id)
-
-
-def delete_object_assignment(policy_id, object_id, category_id, data_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_object_assignment("", policy_id, object_id, category_id, data_id)
-
-
-def get_subject_assignments(policy_id, subject_id=None, category_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_subject_assignments("", policy_id, subject_id, category_id)
-
-
-def add_subject_assignment(policy_id, subject_id, category_id, data_id):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_subject_assignment("", policy_id, subject_id, category_id, data_id)
-
-
-def delete_subject_assignment(policy_id, subject_id, category_id, data_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_subject_assignment("", policy_id, subject_id, category_id, data_id)
-
def test_get_action_assignments(db):
- policy_id = mock_data.get_policy_id()
- action_id = "action_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- add_action_assignment(policy_id, action_id, category_id, data_id)
- act_assignments = get_action_assignments(policy_id, action_id, category_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ action_id = mock_data.create_action(policy_id)
+ data_id = mock_data.create_action_data(policy_id=policy_id, category_id=action_category_id)
+
+ assignment_helper.add_action_assignment(policy_id, action_id, action_category_id, data_id)
+ act_assignments = assignment_helper.get_action_assignments(policy_id, action_id, action_category_id)
action_id_1 = list(act_assignments.keys())[0]
assert act_assignments[action_id_1]["policy_id"] == policy_id
assert act_assignments[action_id_1]["action_id"] == action_id
- assert act_assignments[action_id_1]["category_id"] == category_id
+ assert act_assignments[action_id_1]["category_id"] == action_category_id
assert len(act_assignments[action_id_1].get("assignments")) == 1
assert data_id in act_assignments[action_id_1].get("assignments")
-def test_get_action_assignments_by_policy_id(db):
- policy_id = mock_data.get_policy_id()
- action_id = "action_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- add_action_assignment(policy_id, action_id, category_id, data_id)
- data_id = "data_id_2"
- add_action_assignment(policy_id, action_id, category_id, data_id)
- data_id = "data_id_3"
- add_action_assignment(policy_id, action_id, category_id, data_id)
- act_assignments = get_action_assignments(policy_id)
- action_id_1 = list(act_assignments.keys())[0]
- assert act_assignments[action_id_1]["policy_id"] == policy_id
- assert act_assignments[action_id_1]["action_id"] == action_id
- assert act_assignments[action_id_1]["category_id"] == category_id
- assert len(act_assignments[action_id_1].get("assignments")) == 3
-
-
def test_add_action_assignments(db):
- policy_id = mock_data.get_policy_id()
- action_id = "action_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- action_assignments = add_action_assignment(policy_id, action_id, category_id, data_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ action_id = mock_data.create_action(policy_id)
+ data_id = mock_data.create_action_data(policy_id=policy_id, category_id=action_category_id)
+ action_assignments = assignment_helper.add_action_assignment(policy_id, action_id, action_category_id, data_id)
assert action_assignments
action_id_1 = list(action_assignments.keys())[0]
assert action_assignments[action_id_1]["policy_id"] == policy_id
assert action_assignments[action_id_1]["action_id"] == action_id
- assert action_assignments[action_id_1]["category_id"] == category_id
+ assert action_assignments[action_id_1]["category_id"] == action_category_id
assert len(action_assignments[action_id_1].get("assignments")) == 1
assert data_id in action_assignments[action_id_1].get("assignments")
with pytest.raises(ActionAssignmentExisting) as exception_info:
- add_action_assignment(policy_id, action_id, category_id, data_id)
+ assignment_helper.add_action_assignment(policy_id, action_id, action_category_id, data_id)
+
def test_delete_action_assignment(db):
- policy_id = mock_data.get_policy_id()
- add_action_assignment(policy_id, "", "", "")
- policy_id = mock_data.get_policy_id(model_name="test_model2", policy_name="policy_2", meta_rule_name="meta_rule2", category_prefix="_")
- action_id = "action_id_2"
- category_id = "category_id_2"
- data_id = "data_id_2"
- add_action_assignment(policy_id, action_id, category_id, data_id)
- delete_action_assignment(policy_id, "", "", "")
- assignments = get_action_assignments(policy_id, )
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ action_id = mock_data.create_action(policy_id)
+ data_id = mock_data.create_action_data(policy_id=policy_id, category_id=action_category_id)
+ assignment_helper.add_action_assignment(policy_id, action_id, action_category_id, data_id)
+ assignment_helper.delete_action_assignment(policy_id, "", "", "")
+ assignments = assignment_helper.get_action_assignments(policy_id, )
assert len(assignments) == 1
def test_delete_action_assignment_with_invalid_policy_id(db):
policy_id = "invalid_id"
- delete_action_assignment(policy_id, "", "", "")
- assignments = get_action_assignments(policy_id, )
+ assignment_helper.delete_action_assignment(policy_id, "", "", "")
+ assignments = assignment_helper.get_action_assignments(policy_id, )
assert len(assignments) == 0
def test_get_object_assignments(db):
- policy_id = mock_data.get_policy_id()
- object_id = "object_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- add_object_assignment(policy_id, object_id, category_id, data_id)
- obj_assignments = get_object_assignments(policy_id, object_id, category_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ object_id = mock_data.create_object(policy_id)
+ data_id = mock_data.create_object_data(policy_id=policy_id, category_id=object_category_id)
+ assignment_helper.add_object_assignment(policy_id, object_id, object_category_id, data_id)
+ obj_assignments = assignment_helper.get_object_assignments(policy_id, object_id, object_category_id)
object_id_1 = list(obj_assignments.keys())[0]
assert obj_assignments[object_id_1]["policy_id"] == policy_id
assert obj_assignments[object_id_1]["object_id"] == object_id
- assert obj_assignments[object_id_1]["category_id"] == category_id
+ assert obj_assignments[object_id_1]["category_id"] == object_category_id
assert len(obj_assignments[object_id_1].get("assignments")) == 1
assert data_id in obj_assignments[object_id_1].get("assignments")
def test_get_object_assignments_by_policy_id(db):
- policy_id = mock_data.get_policy_id()
- object_id_1 = "object_id_1"
- category_id_1 = "category_id_1"
- data_id = "data_id_1"
- add_action_assignment(policy_id, object_id_1, category_id_1, data_id)
- object_id_2 = "object_id_2"
- category_id_2 = "category_id_2"
- data_id = "data_id_2"
- add_action_assignment(policy_id, object_id_2, category_id_2, data_id)
- object_id_3 = "object_id_3"
- category_id_3 = "category_id_3"
- data_id = "data_id_3"
- add_action_assignment(policy_id, object_id_3, category_id_3, data_id)
- act_assignments = get_action_assignments(policy_id)
- assert len(act_assignments) == 3
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ object_id = mock_data.create_object(policy_id)
+ data_id = mock_data.create_object_data(policy_id=policy_id, category_id=object_category_id)
+ assignment_helper.add_object_assignment(policy_id, object_id, object_category_id, data_id)
+ obj_assignments = assignment_helper.get_object_assignments(policy_id)
+ assert len(obj_assignments) == 1
def test_add_object_assignments(db):
- policy_id = mock_data.get_policy_id()
- object_id = "object_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- object_assignments = add_object_assignment(policy_id, object_id, category_id, data_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ object_id = mock_data.create_object(policy_id)
+ data_id = mock_data.create_object_data(policy_id=policy_id, category_id=object_category_id)
+ object_assignments = assignment_helper.add_object_assignment(policy_id, object_id, object_category_id, data_id)
assert object_assignments
object_id_1 = list(object_assignments.keys())[0]
assert object_assignments[object_id_1]["policy_id"] == policy_id
assert object_assignments[object_id_1]["object_id"] == object_id
- assert object_assignments[object_id_1]["category_id"] == category_id
+ assert object_assignments[object_id_1]["category_id"] == object_category_id
assert len(object_assignments[object_id_1].get("assignments")) == 1
assert data_id in object_assignments[object_id_1].get("assignments")
with pytest.raises(ObjectAssignmentExisting):
- add_object_assignment(policy_id, object_id, category_id, data_id)
+ assignment_helper.add_object_assignment(policy_id, object_id, object_category_id, data_id)
def test_delete_object_assignment(db):
- policy_id = mock_data.get_policy_id()
- add_object_assignment(policy_id, "", "", "")
- object_id = "action_id_2"
- category_id = "category_id_2"
- data_id = "data_id_2"
- add_object_assignment(policy_id, object_id, category_id, data_id)
- delete_object_assignment(policy_id, "", "", "")
- assignments = get_object_assignments(policy_id, )
- assert len(assignments) == 1
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ object_id = mock_data.create_object(policy_id)
+ data_id = mock_data.create_object_data(policy_id=policy_id, category_id=object_category_id)
+ assignment_helper.add_object_assignment(policy_id, object_id, object_category_id, data_id)
+
+ assignment_helper.delete_object_assignment(policy_id, object_id, object_category_id, data_id=data_id)
+ assignments = assignment_helper.get_object_assignments(policy_id)
+ assert len(assignments) == 0
def test_delete_object_assignment_with_invalid_policy_id(db):
policy_id = "invalid_id"
- delete_object_assignment(policy_id, "", "", "")
- assignments = get_object_assignments(policy_id, )
+ assignment_helper.delete_object_assignment(policy_id, "", "", "")
+ assignments = assignment_helper.get_object_assignments(policy_id, )
assert len(assignments) == 0
def test_get_subject_assignments(db):
- policy_id = mock_data.get_policy_id()
- subject_id = "object_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- add_subject_assignment(policy_id, subject_id, category_id, data_id)
- subj_assignments = get_subject_assignments(policy_id, subject_id, category_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ subject_id = mock_data.create_subject(policy_id)
+ data_id = mock_data.create_subject_data(policy_id=policy_id, category_id=subject_category_id)
+
+ assignment_helper.add_subject_assignment(policy_id, subject_id, subject_category_id, data_id)
+ subj_assignments = assignment_helper.get_subject_assignments(policy_id, subject_id, subject_category_id)
subject_id_1 = list(subj_assignments.keys())[0]
assert subj_assignments[subject_id_1]["policy_id"] == policy_id
assert subj_assignments[subject_id_1]["subject_id"] == subject_id
- assert subj_assignments[subject_id_1]["category_id"] == category_id
+ assert subj_assignments[subject_id_1]["category_id"] == subject_category_id
assert len(subj_assignments[subject_id_1].get("assignments")) == 1
assert data_id in subj_assignments[subject_id_1].get("assignments")
def test_get_subject_assignments_by_policy_id(db):
- policy_id = mock_data.get_policy_id()
- subject_id_1 = "subject_id_1"
- category_id_1 = "category_id_1"
- data_id = "data_id_1"
- add_subject_assignment(policy_id, subject_id_1, category_id_1, data_id)
- subject_id_2 = "subject_id_2"
- category_id_2 = "category_id_2"
- data_id = "data_id_2"
- add_subject_assignment(policy_id, subject_id_2, category_id_2, data_id)
- subject_id_3 = "subject_id_3"
- category_id_3 = "category_id_3"
- data_id = "data_id_3"
- add_subject_assignment(policy_id, subject_id_3, category_id_3, data_id)
- subj_assignments = get_subject_assignments(policy_id)
- assert len(subj_assignments) == 3
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ subject_id = mock_data.create_subject(policy_id)
+ data_id = mock_data.create_subject_data(policy_id=policy_id, category_id=subject_category_id)
+
+ assignment_helper.add_subject_assignment(policy_id, subject_id, subject_category_id, data_id)
+ subj_assignments = assignment_helper.get_subject_assignments(policy_id)
+ assert len(subj_assignments) == 1
def test_add_subject_assignments(db):
- policy_id = mock_data.get_policy_id()
- subject_id = "subject_id_1"
- category_id = "category_id_1"
- data_id = "data_id_1"
- subject_assignments = add_subject_assignment(policy_id, subject_id, category_id, data_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ subject_id = mock_data.create_subject(policy_id)
+ data_id = mock_data.create_subject_data(policy_id=policy_id, category_id=subject_category_id)
+
+ subject_assignments = assignment_helper.add_subject_assignment(policy_id, subject_id, subject_category_id, data_id)
assert subject_assignments
subject_id_1 = list(subject_assignments.keys())[0]
assert subject_assignments[subject_id_1]["policy_id"] == policy_id
assert subject_assignments[subject_id_1]["subject_id"] == subject_id
- assert subject_assignments[subject_id_1]["category_id"] == category_id
+ assert subject_assignments[subject_id_1]["category_id"] == subject_category_id
assert len(subject_assignments[subject_id_1].get("assignments")) == 1
assert data_id in subject_assignments[subject_id_1].get("assignments")
with pytest.raises(SubjectAssignmentExisting):
- add_subject_assignment(policy_id, subject_id, category_id, data_id)
+ assignment_helper.add_subject_assignment(policy_id, subject_id, subject_category_id, data_id)
def test_delete_subject_assignment(db):
- policy_id = mock_data.get_policy_id()
- add_subject_assignment(policy_id, "", "", "")
- subject_id = "subject_id_2"
- category_id = "category_id_2"
- data_id = "data_id_2"
- add_subject_assignment(policy_id, subject_id, category_id, data_id)
- delete_subject_assignment(policy_id, "", "", "")
- assignments = get_subject_assignments(policy_id, )
- assert len(assignments) == 1
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ subject_id = mock_data.create_subject(policy_id)
+ data_id = mock_data.create_subject_data(policy_id=policy_id, category_id=subject_category_id)
+ assignment_helper.add_subject_assignment(policy_id, subject_id, subject_category_id, data_id)
+ assignment_helper.delete_subject_assignment(policy_id, subject_id, subject_category_id, data_id)
+ assignments = assignment_helper.get_subject_assignments(policy_id)
+ assert len(assignments) == 0
def test_delete_subject_assignment_with_invalid_policy_id(db):
policy_id = "invalid_id"
- delete_subject_assignment(policy_id, "", "", "")
- assignments = get_subject_assignments(policy_id, )
+ assignment_helper.delete_subject_assignment(policy_id, "", "", "")
+ assignments = assignment_helper.get_subject_assignments(policy_id, )
assert len(assignments) == 0
diff --git a/python_moondb/tests/unit_python/policies/test_data.py b/python_moondb/tests/unit_python/policies/test_data.py
index 5e00fe65..fa3f8c06 100755
--- a/python_moondb/tests/unit_python/policies/test_data.py
+++ b/python_moondb/tests/unit_python/policies/test_data.py
@@ -1,391 +1,269 @@
-# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-import policies.mock_data as mock_data
-from .test_assignments import *
+import helpers.mock_data as mock_data
+import policies.mock_data
+import helpers.data_helper as data_helper
+import pytest
+import logging
+from python_moonutilities.exceptions import *
logger = logging.getLogger("python_moondb.tests.api.test_data")
-def get_action_data(policy_id, data_id=None, category_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_action_data("", policy_id, data_id, category_id)
-
-
-def add_action_data(policy_id, data_id=None, category_id=None, value=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_action_data("", policy_id, data_id, category_id, value)
-
-
-def delete_action_data(policy_id, data_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_action_data("", policy_id, data_id)
-
-
-def get_object_data(policy_id, data_id=None, category_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_object_data("", policy_id, data_id, category_id)
-
-
-def add_object_data(policy_id, data_id=None, category_id=None, value=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_object_data("", policy_id, data_id, category_id, value)
-
-
-def delete_object_data(policy_id, data_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_object_data("", policy_id, data_id)
-
-
-def get_subject_data(policy_id, data_id=None, category_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_subject_data("", policy_id, data_id, category_id)
-
-
-def add_subject_data(policy_id, data_id=None, category_id=None, value=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.set_subject_data("", policy_id, data_id, category_id, value)
-
-
-def delete_subject_data(policy_id, data_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_subject_data("", policy_id, data_id)
-
-
-def get_actions(policy_id, perimeter_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_actions("", policy_id, perimeter_id)
-
-
-def add_action(policy_id, perimeter_id=None, value=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_action("", policy_id, perimeter_id, value)
-
-
-def delete_action(policy_id, perimeter_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_action("", policy_id, perimeter_id)
-
-
-def get_objects(policy_id, perimeter_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_objects("", policy_id, perimeter_id)
-
-
-def add_object(policy_id, perimeter_id=None, value=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_object("", policy_id, perimeter_id, value)
-
-
-def delete_object(policy_id, perimeter_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_object("", policy_id, perimeter_id)
-
-
-def get_subjects(policy_id, perimeter_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_subjects("", policy_id, perimeter_id)
-
-
-def add_subject(policy_id, perimeter_id=None, value=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.add_subject("", policy_id, perimeter_id, value)
-
-
-def delete_subject(policy_id, perimeter_id):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_subject("", policy_id, perimeter_id)
-
-
-def get_available_metadata(policy_id):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_available_metadata("", policy_id)
-
-
def test_get_action_data(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
-
- policy_id = policy_id
- data_id = "data_id_1"
- category_id = "action_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "action-type",
"description": {"vm-action": "", "storage-action": "", },
}
- add_action_data(policy_id, data_id, category_id, value)
- action_data = get_action_data(policy_id, data_id, category_id)
- assert action_data
- assert len(action_data[0]['data']) == 1
+ action_data = data_helper.add_action_data(policy_id=policy_id, category_id=action_category_id, value=value)
+ data_id = list(action_data["data"])[0]
+ found_action_data = data_helper.get_action_data(policy_id=policy_id, data_id=data_id,
+ category_id=action_category_id)
+ assert found_action_data
+ assert len(found_action_data[0]["data"]) == 1
def test_get_action_data_with_invalid_category_id(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "action_category_id1"
- value = {
- "name": "action-type",
- "description": {"vm-action": "", "storage-action": "", },
- }
- add_action_data(policy_id, data_id, category_id, value)
- action_data = get_action_data(policy_id)
- assert action_data
- assert len(action_data[0]['data']) == 1
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ action_data = data_helper.get_action_data(policy_id=policy_id, category_id="invalid")
+ assert len(action_data) == 0
def test_add_action_data(db):
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
- category_id = "category_id_1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "action-type",
"description": {"vm-action": "", "storage-action": "", },
}
- action_data = add_action_data(policy_id, data_id, category_id, value).get('data')
+ action_data = data_helper.add_action_data(policy_id=policy_id, category_id=action_category_id, value=value)
assert action_data
- action_data_id = list(action_data.keys())[0]
- assert action_data[action_data_id].get('policy_id') == policy_id
-
- with pytest.raises(ActionScopeExisting) as exception_info:
- add_action_data(policy_id, category_id=category_id, value=value).get('data')
+ assert len(action_data['data']) == 1
def test_add_action_data_with_invalid_category_id(db):
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "action-type",
"description": {"vm-action": "", "storage-action": "", },
}
with pytest.raises(Exception) as exception_info:
- add_action_data(policy_id=policy_id, data_id=data_id, value=value).get('data')
+ data_helper.add_action_data(policy_id=policy_id, value=value).get('data')
assert str(exception_info.value) == 'Invalid category id'
def test_delete_action_data(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "category_id_1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ data_helper.get_available_metadata(policy_id)
value = {
"name": "action-type",
"description": {"vm-action": "", "storage-action": "", },
}
- action_data = add_action_data(policy_id, data_id, category_id, value).get('data')
- action_data_id = list(action_data.keys())[0]
- delete_action_data(action_data[action_data_id].get('policy_id'), None)
- new_action_data = get_action_data(policy_id)
+ action_data = data_helper.add_action_data(policy_id=policy_id, category_id=action_category_id, value=value)
+ data_id = list(action_data["data"])[0]
+ data_helper.delete_action_data(policy_id, data_id)
+ new_action_data = data_helper.get_action_data(policy_id)
assert len(new_action_data[0]['data']) == 0
-def test_delete_action_data_assigned_to_action_assignment(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "category_id_1"
- value = {
- "name": "action-type",
- "description": {"vm-action": "", "storage-action": "", },
- }
- action_data = add_action_data(policy_id, data_id, category_id, value).get('data')
- action_data_id = list(action_data.keys())[0]
- add_action_assignment(policy_id, "action_id_1", category_id, action_data_id)
- with pytest.raises(DeleteData) as exception_info:
- delete_action_data(action_data[action_data_id].get('policy_id'), None)
-
-
def test_get_object_data(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "object_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "object-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- add_object_data(policy_id, data_id, category_id, value)
- object_data = get_object_data(policy_id, data_id, category_id)
- assert object_data
- assert len(object_data[0]['data']) == 1
+ object_data = data_helper.add_object_data(policy_id=policy_id, category_id=object_category_id, value=value)
+ data_id = list(object_data["data"])[0]
+ found_object_data = data_helper.get_object_data(policy_id=policy_id, data_id=data_id,
+ category_id=object_category_id)
+ assert found_object_data
+ assert len(found_object_data[0]['data']) == 1
def test_get_object_data_with_invalid_category_id(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "object_category_id1"
- value = {
- "name": "object-security-level",
- "description": {"low": "", "medium": "", "high": ""},
- }
- add_object_data(policy_id, data_id, category_id, value)
- object_data = get_object_data(policy_id)
- assert object_data
- assert len(object_data[0]['data']) == 1
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ object_data = data_helper.get_object_data(policy_id=policy_id, category_id="invalid")
+ assert len(object_data) == 0
def test_add_object_data(db):
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
- category_id = "object_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "object-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- object_data = add_object_data(policy_id, data_id, category_id, value).get('data')
+ object_data = data_helper.add_object_data(policy_id=policy_id, category_id=object_category_id, value=value).get(
+ 'data')
assert object_data
object_data_id = list(object_data.keys())[0]
assert object_data[object_data_id].get('policy_id') == policy_id
- with pytest.raises(ObjectScopeExisting) as exception_info:
- add_object_data(policy_id, category_id=category_id, value=value).get('data')
-
def test_add_object_data_with_invalid_category_id(db):
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "object-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- with pytest.raises(Exception) as exception_info:
- add_object_data(policy_id=policy_id, data_id=data_id, value=value).get('data')
- assert str(exception_info.value) == 'Invalid category id'
+ with pytest.raises(MetaDataUnknown) as exception_info:
+ data_helper.add_object_data(policy_id=policy_id, category_id="invalid", value=value).get('data')
+ assert str(exception_info.value) == '400: Meta data Unknown'
def test_delete_object_data(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "object_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "object-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- object_data = add_object_data(policy_id, data_id, category_id, value).get('data')
+ object_data = data_helper.add_object_data(policy_id=policy_id, category_id=object_category_id, value=value).get(
+ 'data')
object_data_id = list(object_data.keys())[0]
- delete_object_data(object_data[object_data_id].get('policy_id'), data_id)
- new_object_data = get_object_data(policy_id)
+ data_helper.delete_object_data(policy_id=object_data[object_data_id].get('policy_id'), data_id=object_data_id)
+ new_object_data = data_helper.get_object_data(policy_id)
assert len(new_object_data[0]['data']) == 0
-def test_delete_object_data_assigned_to_object_assignment(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "category_id_1"
- value = {
- "name": "object-type",
- "description": {"vm-action": "", "storage-action": "", },
- }
- object_data = add_object_data(policy_id, data_id, category_id, value).get('data')
- object_data_id = list(object_data.keys())[0]
- add_object_assignment(policy_id, "object_id_1", category_id, object_data_id)
- with pytest.raises(DeleteData) as exception_info:
- delete_object_data(object_data[object_data_id].get('policy_id'), None)
-
-
def test_get_subject_data(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "subject_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "subject-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- add_subject_data(policy_id, data_id, category_id, value)
- subject_data = get_subject_data(policy_id, data_id, category_id)
+ subject_data = data_helper.add_subject_data(policy_id=policy_id, category_id=subject_category_id, value=value).get(
+ 'data')
+ subject_data_id = list(subject_data.keys())[0]
+ subject_data = data_helper.get_subject_data(policy_id, subject_data_id, subject_category_id)
assert subject_data
assert len(subject_data[0]['data']) == 1
def test_get_subject_data_with_invalid_category_id(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "subject_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "subject-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- add_subject_data(policy_id, data_id, category_id, value)
- subject_data = get_subject_data(policy_id)
- assert subject_data
- assert len(subject_data[0]['data']) == 1
+ subject_data = data_helper.add_subject_data(policy_id=policy_id, category_id=subject_category_id, value=value).get(
+ 'data')
+ subject_data_id = list(subject_data.keys())[0]
+ found_subject_data = data_helper.get_subject_data(policy_id, subject_data_id, "invalid")
+ assert len(found_subject_data) == 0
def test_add_subject_data(db):
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
- category_id = "subject_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "subject-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- subject_data = add_subject_data(policy_id, data_id, category_id, value).get('data')
+ subject_data = data_helper.add_subject_data(policy_id=policy_id, category_id=subject_category_id, value=value).get(
+ 'data')
assert subject_data
subject_data_id = list(subject_data.keys())[0]
assert subject_data[subject_data_id].get('policy_id') == policy_id
- with pytest.raises(SubjectScopeExisting):
- add_subject_data(policy_id, category_id=category_id, value=value).get('data')
def test_add_subject_data_with_no_category_id(db):
- policy_id = mock_data.get_policy_id()
- data_id = "data_id_1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "subject-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
with pytest.raises(Exception) as exception_info:
- add_subject_data(policy_id=policy_id, data_id=data_id, value=value).get('data')
+ data_helper.add_subject_data(policy_id=policy_id, data_id=subject_category_id, value=value).get('data')
assert str(exception_info.value) == 'Invalid category id'
def test_delete_subject_data(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "subject_category_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "subject-security-level",
"description": {"low": "", "medium": "", "high": ""},
}
- subject_data = add_subject_data(policy_id, data_id, category_id, value).get('data')
+ subject_data = data_helper.add_subject_data(policy_id=policy_id, category_id=subject_category_id, value=value).get(
+ 'data')
subject_data_id = list(subject_data.keys())[0]
- delete_subject_data(subject_data[subject_data_id].get('policy_id'), data_id)
- new_subject_data = get_subject_data(policy_id)
+ data_helper.delete_subject_data(subject_data[subject_data_id].get('policy_id'), subject_data_id)
+ new_subject_data = data_helper.get_subject_data(policy_id)
assert len(new_subject_data[0]['data']) == 0
-def test_delete_subject_data_assigned_to_subject_assignment(db):
- policy_id = mock_data.get_policy_id()
- get_available_metadata(policy_id)
- data_id = "data_id_1"
- category_id = "category_id_1"
- value = {
- "name": "subject-type",
- "description": {"vm-action": "", "storage-action": "", },
- }
- subject_data = add_subject_data(policy_id, data_id, category_id, value).get('data')
- subject_data_id = list(subject_data.keys())[0]
- add_subject_assignment(policy_id, "object_id_1", category_id, subject_data_id)
- with pytest.raises(DeleteData) as exception_info:
- delete_subject_data(subject_data[subject_data_id].get('policy_id'), None)
-
-
def test_get_actions(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "test_action",
"description": "test",
}
- add_action(policy_id=policy_id, value=value)
- actions = get_actions(policy_id, )
+ data_helper.add_action(policy_id=policy_id, value=value)
+ actions = data_helper.get_actions(policy_id, )
assert actions
assert len(actions) == 1
action_id = list(actions.keys())[0]
@@ -393,27 +271,70 @@ def test_get_actions(db):
def test_add_action(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "test_action",
"description": "test",
}
- action = add_action(policy_id=policy_id, value=value)
+ action = data_helper.add_action(policy_id=policy_id, value=value)
assert action
action_id = list(action.keys())[0]
assert len(action[action_id].get('policy_list')) == 1
+
+def test_add_action_twice(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ value = {
+ "name": "test_action",
+ "description": "test",
+ }
+ data_helper.add_action(policy_id=policy_id, value=value)
with pytest.raises(ActionExisting):
- add_action(policy_id=policy_id, value=value)
+ data_helper.add_action(policy_id=policy_id, value=value)
+
+
+def test_add_action_blank_name(db):
+ policy_id = policies.mock_data.get_policy_id()
+ value = {
+ "name": "",
+ "description": "test",
+ }
+ with pytest.raises(Exception) as exception_info:
+ data_helper.add_action(policy_id=policy_id, value=value)
+ assert str(exception_info.value) == '400: Perimeter Name is Invalid'
+
+
+def test_add_action_with_name_space(db):
+ policy_id = policies.mock_data.get_policy_id()
+ value = {
+ "name": " ",
+ "description": "test",
+ }
+ with pytest.raises(Exception) as exception_info:
+ data_helper.add_action(policy_id=policy_id, value=value)
+ assert str(exception_info.value) == '400: Perimeter Name is Invalid'
def test_add_action_multiple_times(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id1 = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_action",
"description": "test",
}
- action = add_action(policy_id=policy_id, value=value)
+ action = data_helper.add_action(policy_id=policy_id1, value=value)
logger.info("action : {}".format(action))
action_id = list(action.keys())[0]
perimeter_id = action[action_id].get('id')
@@ -423,9 +344,13 @@ def test_add_action_multiple_times(db):
"description": "test",
"policy_list": ['policy_id_3', 'policy_id_4']
}
- action = add_action(
- mock_data.get_policy_id(model_name="test_model2", policy_name="policy_2", meta_rule_name="meta_rule2",
- category_prefix="_"), perimeter_id, value)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id2 = mock_data.create_new_policy(
+ subject_category_name="subject_category2",
+ object_category_name="object_category2",
+ action_category_name="action_category2",
+ meta_rule_name="meta_rule_2",
+ model_name="model2")
+ action = data_helper.add_action(policy_id=policy_id2, perimeter_id=perimeter_id, value=value)
logger.info("action : {}".format(action))
assert action
action_id = list(action.keys())[0]
@@ -433,15 +358,19 @@ def test_add_action_multiple_times(db):
def test_delete_action(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "test_action",
"description": "test",
}
- action = add_action(policy_id=policy_id, value=value)
+ action = data_helper.add_action(policy_id=policy_id, value=value)
action_id = list(action.keys())[0]
- delete_action(policy_id, action_id)
- actions = get_actions(policy_id, )
+ data_helper.delete_action(policy_id, action_id)
+ actions = data_helper.get_actions(policy_id, )
assert not actions
@@ -449,18 +378,22 @@ def test_delete_action_with_invalid_perimeter_id(db):
policy_id = "invalid"
perimeter_id = "invalid"
with pytest.raises(Exception) as exception_info:
- delete_action(policy_id, perimeter_id)
- assert str(exception_info.value) == '400: Action Unknown'
+ data_helper.delete_action(policy_id, perimeter_id)
+ assert str(exception_info.value) == '400: Policy Unknown'
def test_get_objects(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "test_object",
"description": "test",
}
- add_object(policy_id=policy_id, value=value)
- objects = get_objects(policy_id, )
+ data_helper.add_object(policy_id=policy_id, value=value)
+ objects = data_helper.get_objects(policy_id, )
assert objects
assert len(objects) == 1
object_id = list(objects.keys())[0]
@@ -468,53 +401,69 @@ def test_get_objects(db):
def test_add_object(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "test_object",
"description": "test",
}
- added_object = add_object(policy_id=policy_id, value=value)
+ added_object = data_helper.add_object(policy_id=policy_id, value=value)
assert added_object
object_id = list(added_object.keys())[0]
assert len(added_object[object_id].get('policy_list')) == 1
with pytest.raises(ObjectExisting):
- add_object(policy_id=policy_id, value=value)
+ data_helper.add_object(policy_id=policy_id, value=value)
def test_add_objects_multiple_times(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_object",
"description": "test",
}
- added_object = add_object(policy_id=policy_id, value=value)
+ added_object = data_helper.add_object(policy_id=policy_id, value=value)
object_id = list(added_object.keys())[0]
perimeter_id = added_object[object_id].get('id')
assert added_object
value = {
"name": "test_object",
"description": "test",
- "policy_list": ['policy_id_3', 'policy_id_4']
}
- added_object = add_object(
- mock_data.get_policy_id(model_name="test_model2", policy_name="policy_2", meta_rule_name="meta_rule2",
- category_prefix="_"), perimeter_id, value)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category2",
+ object_category_name="object_category2",
+ action_category_name="action_category2",
+ meta_rule_name="meta_rule_2",
+ model_name="model2")
+ added_object = data_helper.add_object(policy_id=policy_id, perimeter_id=perimeter_id, value=value)
assert added_object
object_id = list(added_object.keys())[0]
assert len(added_object[object_id].get('policy_list')) == 2
def test_delete_object(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "test_object",
"description": "test",
}
- added_object = add_object(policy_id=policy_id, value=value)
+ added_object = data_helper.add_object(policy_id=policy_id, value=value)
object_id = list(added_object.keys())[0]
- delete_object(policy_id, object_id)
- objects = get_objects(policy_id, )
+ data_helper.delete_object(policy_id, object_id)
+ objects = data_helper.get_objects(policy_id, )
assert not objects
@@ -522,72 +471,107 @@ def test_delete_object_with_invalid_perimeter_id(db):
policy_id = "invalid"
perimeter_id = "invalid"
with pytest.raises(Exception) as exception_info:
- delete_object(policy_id, perimeter_id)
- assert str(exception_info.value) == '400: Object Unknown'
+ data_helper.delete_object(policy_id, perimeter_id)
+ assert str(exception_info.value) == '400: Policy Unknown'
def test_get_subjects(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "testuser",
"description": "test",
}
- add_subject(policy_id=policy_id, value=value)
- subjects = get_subjects(policy_id, )
+ data_helper.add_subject(policy_id=policy_id, value=value)
+ subjects = data_helper.get_subjects(policy_id=policy_id)
assert subjects
assert len(subjects) == 1
subject_id = list(subjects.keys())[0]
assert subjects[subject_id].get('policy_list')[0] == policy_id
+def test_get_subjects_with_invalid_policy_id(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ value = {
+ "name": "testuser",
+ "description": "test",
+ }
+ data_helper.add_subject(policy_id=policy_id, value=value)
+ with pytest.raises(PolicyUnknown):
+ data_helper.get_subjects(policy_id="invalid")
+
+
def test_add_subject(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "testuser",
"description": "test",
}
- subject = add_subject(policy_id=policy_id, value=value)
+ subject = data_helper.add_subject(policy_id=policy_id, value=value)
assert subject
subject_id = list(subject.keys())[0]
assert len(subject[subject_id].get('policy_list')) == 1
-
- with pytest.raises(SubjectExisting):
- add_subject(policy_id=policy_id, value=value)
+ with pytest.raises(SubjectExisting) as exception_info:
+ data_helper.add_subject(policy_id=policy_id, value=value)
+ assert str(exception_info.value) == '409: Subject Existing'
def test_add_subjects_multiple_times(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "testuser",
"description": "test",
}
- subject = add_subject(policy_id=policy_id, value=value)
+ subject = data_helper.add_subject(policy_id=policy_id, value=value)
subject_id = list(subject.keys())[0]
perimeter_id = subject[subject_id].get('id')
assert subject
value = {
"name": "testuser",
"description": "test",
- "policy_list": ['policy_id_3', 'policy_id_4']
}
- subject = add_subject(
- mock_data.get_policy_id(model_name="test_model2", policy_name="policy_2", meta_rule_name="meta_rule2",
- category_prefix="_"), perimeter_id, value)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category2",
+ object_category_name="object_category2",
+ action_category_name="action_category2",
+ meta_rule_name="meta_rule_2",
+ model_name="model2")
+ subject = data_helper.add_subject(policy_id=policy_id, perimeter_id=perimeter_id, value=value)
assert subject
subject_id = list(subject.keys())[0]
assert len(subject[subject_id].get('policy_list')) == 2
def test_delete_subject(db):
- policy_id = mock_data.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
value = {
"name": "testuser",
"description": "test",
}
- subject = add_subject(policy_id=policy_id, value=value)
+ subject = data_helper.add_subject(policy_id=policy_id, value=value)
subject_id = list(subject.keys())[0]
- delete_subject(policy_id, subject_id)
- subjects = get_subjects(policy_id, )
+ data_helper.delete_subject(policy_id, subject_id)
+ subjects = data_helper.get_subjects(policy_id, )
assert not subjects
@@ -595,30 +579,24 @@ def test_delete_subject_with_invalid_perimeter_id(db):
policy_id = "invalid"
perimeter_id = "invalid"
with pytest.raises(Exception) as exception_info:
- delete_subject(policy_id, perimeter_id)
- assert str(exception_info.value) == '400: Subject Unknown'
+ data_helper.delete_subject(policy_id, perimeter_id)
+ assert str(exception_info.value) == '400: Policy Unknown'
def test_get_available_metadata(db):
- policy_id = mock_data.get_policy_id()
- metadata = get_available_metadata(policy_id=policy_id)
- assert metadata
- assert metadata['object'][0] == "object_category_id1"
- assert metadata['subject'][0] == "subject_category_id1"
- assert metadata['subject'][1] == "subject_category_id2"
-
-
-def test_get_available_metadata_empty_model(db):
- import policies.test_policies as test_policies
- value = mock_data.create_policy("invalid")
- policy = test_policies.add_policies(value=value)
- assert policy
- policy_id = list(policy.keys())[0]
- metadata = get_available_metadata(policy_id=policy_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1")
+ metadata = data_helper.get_available_metadata(policy_id=policy_id)
assert metadata
+ assert metadata['object'][0] == object_category_id
+ assert metadata['subject'][0] == subject_category_id
+ assert metadata['action'][0] == action_category_id
def test_get_available_metadata_with_invalid_policy_id(db):
with pytest.raises(Exception) as exception_info:
- get_available_metadata(policy_id='invalid')
+ data_helper.get_available_metadata(policy_id='invalid')
assert '400: Policy Unknown' == str(exception_info.value)
diff --git a/python_moondb/tests/unit_python/policies/test_policies.py b/python_moondb/tests/unit_python/policies/test_policies.py
index f1dd258f..07ee87fd 100755
--- a/python_moondb/tests/unit_python/policies/test_policies.py
+++ b/python_moondb/tests/unit_python/policies/test_policies.py
@@ -4,70 +4,14 @@
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
import pytest
-import policies.mock_data as mock_data
+import helpers.mock_data as mock_data
+import helpers.policy_helper as policy_helper
from python_moonutilities.exceptions import *
-
-
-def get_policies():
- from python_moondb.core import PolicyManager
- return PolicyManager.get_policies("admin")
-
-
-def add_policies(policy_id=None, value=None):
- from python_moondb.core import PolicyManager
- if not value:
- value = {
- "name": "test_policiy",
- "model_id": "",
- "genre": "authz",
- "description": "test",
- }
- return PolicyManager.add_policy("admin", policy_id=policy_id, value=value)
-
-
-def delete_policies(uuid=None, name=None):
- from python_moondb.core import PolicyManager
- if not uuid:
- for policy_id, policy_value in get_policies():
- if name == policy_value['name']:
- uuid = policy_id
- break
- PolicyManager.delete_policy("admin", uuid)
-
-
-def update_policy(policy_id, value):
- from python_moondb.core import PolicyManager
- return PolicyManager.update_policy("admin", policy_id, value)
-
-
-def get_policy_from_meta_rules(meta_rule_id):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_policy_from_meta_rules("admin", meta_rule_id)
-
-
-def get_rules(policy_id=None, meta_rule_id=None, rule_id=None):
- from python_moondb.core import PolicyManager
- return PolicyManager.get_rules("", policy_id, meta_rule_id, rule_id)
-
-
-def add_rule(policy_id=None, meta_rule_id=None, value=None):
- from python_moondb.core import PolicyManager
- if not value:
- value = {
- "rule": ("high", "medium", "vm-action"),
- "instructions": ({"decision": "grant"}),
- "enabled": "",
- }
- return PolicyManager.add_rule("", policy_id, meta_rule_id, value)
-
-
-def delete_rule(policy_id=None, rule_id=None):
- from python_moondb.core import PolicyManager
- PolicyManager.delete_rule("", policy_id, rule_id)
+import helpers.pdp_helper as pdp_helper
def test_get_policies(db):
- policies = get_policies()
+ policies = policy_helper.get_policies()
assert isinstance(policies, dict)
assert not policies
@@ -79,7 +23,7 @@ def test_add_policies(db):
"genre": "authz",
"description": "test",
}
- policies = add_policies(value=value)
+ policies = policy_helper.add_policies(value=value)
assert isinstance(policies, dict)
assert policies
assert len(policies.keys()) == 1
@@ -97,9 +41,9 @@ def test_add_policies_twice_with_same_id(db):
"genre": "authz",
"description": "test",
}
- add_policies(policy_id, value)
+ policy_helper.add_policies(policy_id, value)
with pytest.raises(PolicyExisting) as exception_info:
- add_policies(policy_id, value)
+ policy_helper.add_policies(policy_id, value)
# assert str(exception_info.value) == '409: Policy Error'
@@ -110,9 +54,9 @@ def test_add_policies_twice_with_same_name(db):
"genre": "authz",
"description": "test",
}
- add_policies(value=value)
+ policy_helper.add_policies(value=value)
with pytest.raises(Exception) as exception_info:
- add_policies(value=value)
+ policy_helper.add_policies(value=value)
# assert str(exception_info.value) == '409: Policy Error'
@@ -123,7 +67,7 @@ def test_delete_policies(db):
"genre": "authz",
"description": "test",
}
- policies = add_policies(value=value)
+ policies = policy_helper.add_policies(value=value)
policy_id1 = list(policies.keys())[0]
value = {
"name": "test_policy2",
@@ -131,45 +75,23 @@ def test_delete_policies(db):
"genre": "authz",
"description": "test",
}
- policies = add_policies(value=value)
+ policies = policy_helper.add_policies(value=value)
policy_id2 = list(policies.keys())[0]
assert policy_id1 != policy_id2
- delete_policies(policy_id1)
- policies = get_policies()
+ policy_helper.delete_policies(policy_id1)
+ policies = policy_helper.get_policies()
assert policy_id1 not in policies
def test_delete_policies_with_invalid_id(db):
policy_id = 'policy_id_1'
with pytest.raises(PolicyUnknown) as exception_info:
- delete_policies(policy_id)
+ policy_helper.delete_policies(policy_id)
# assert str(exception_info.value) == '400: Policy Unknown'
-def test_delete_policies_with_pdp(db):
- from python_moondb.core import PDPManager
- value = {
- "name": "test_policy1",
- "model_id": "",
- "genre": "authz",
- "description": "test",
- }
- policies = add_policies(value=value)
- policy_id1 = list(policies.keys())[0]
- pdp_id = "pdp_id1"
- value = {
- "name": "test_pdp",
- "security_pipeline": [policy_id1],
- "keystone_project_id": "keystone_project_id1",
- "description": "...",
- }
- PDPManager.add_pdp(user_id="admin" ,pdp_id=pdp_id, value=value)
- with pytest.raises(DeletePolicyWithPdp) as exception_info:
- delete_policies(policy_id1)
-
-
def test_update_policy(db):
- policies = add_policies()
+ policies = policy_helper.add_policies()
policy_id = list(policies.keys())[0]
value = {
"name": "test_policy4",
@@ -177,7 +99,7 @@ def test_update_policy(db):
"genre": "authz",
"description": "test-3",
}
- updated_policy = update_policy(policy_id, value)
+ updated_policy = policy_helper.update_policy(policy_id, value)
assert updated_policy
for key in ("genre", "name", "model_id", "description"):
assert key in updated_policy[policy_id]
@@ -193,32 +115,26 @@ def test_update_policy_with_invalid_id(db):
"description": "test-3",
}
with pytest.raises(PolicyUnknown) as exception_info:
- update_policy(policy_id, value)
+ policy_helper.update_policy(policy_id, value)
# assert str(exception_info.value) == '400: Policy Unknown'
def test_get_policy_from_meta_rules(db):
- import models.test_models as test_models
- import models.test_meta_rules as test_meta_rules
- import test_pdp as test_pdp
- meta_rule = test_meta_rules.add_meta_rule(value=mock_data.create_meta_rule())
- meta_rule_id = list(meta_rule.keys())[0]
- model = test_models.add_model(value=mock_data.create_model(meta_rule_id))
- model_id = list(model.keys())[0]
- value = mock_data.create_policy(model_id)
- policy = add_policies(value=value)
- assert policy
- policy_id = list(policy.keys())[0]
- pdp_ids = [policy_id, ]
- pdp_obj = mock_data.create_pdp(pdp_ids)
- test_pdp.add_pdp(value=pdp_obj)
- matched_policy_id = get_policy_from_meta_rules(meta_rule_id)
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
+ security_pipeline = [policy_id]
+ pdp_obj = mock_data.create_pdp(security_pipeline)
+ pdp_helper.add_pdp(value=pdp_obj)
+ matched_policy_id = policy_helper.get_policy_from_meta_rules(meta_rule_id)
assert matched_policy_id
assert policy_id == matched_policy_id
def test_get_policy_from_meta_rules_with_no_policy_ids(db):
- import test_pdp as test_pdp
meta_rule_id = 'meta_rule_id'
value = {
"name": "test_pdp",
@@ -226,58 +142,31 @@ def test_get_policy_from_meta_rules_with_no_policy_ids(db):
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- test_pdp.add_pdp(value=value)
- matched_policy_id = get_policy_from_meta_rules(meta_rule_id)
+ pdp_helper.add_pdp(value=value)
+ matched_policy_id = policy_helper.get_policy_from_meta_rules(meta_rule_id)
assert not matched_policy_id
-def test_get_policy_from_meta_rules_with_no_policies(db):
- import test_pdp as test_pdp
- meta_rule_id = 'meta_rule_id'
- policy_id = 'invalid'
- pdp_ids = [policy_id, ]
- pdp_obj = mock_data.create_pdp(pdp_ids)
- test_pdp.add_pdp(value=pdp_obj)
- with pytest.raises(Exception) as exception_info:
- get_policy_from_meta_rules(meta_rule_id)
- assert str(exception_info.value) == '400: Policy Unknown'
-
-
-def test_get_policy_from_meta_rules_with_no_models(db):
- import models.test_meta_rules as test_meta_rules
- import test_pdp as test_pdp
- meta_rule = test_meta_rules.add_meta_rule(value=mock_data.create_meta_rule())
- meta_rule_id = list(meta_rule.keys())[0]
- model_id = 'invalid'
- value = mock_data.create_policy(model_id)
- policy = add_policies(value=value)
- assert policy
- policy_id = list(policy.keys())[0]
- pdp_ids = [policy_id, ]
- pdp_obj = mock_data.create_pdp(pdp_ids)
- test_pdp.add_pdp(value=pdp_obj)
- with pytest.raises(Exception) as exception_info:
- get_policy_from_meta_rules(meta_rule_id)
- assert str(exception_info.value) == '400: Model Unknown'
-
-
def test_get_rules(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category12",
+ object_category_name="object_category12",
+ action_category_name="action_category12",
+ meta_rule_name="meta_rule_12",
+ model_name="model12")
value = {
"rule": ("low", "medium", "vm-action"),
"instructions": ({"decision": "grant"}),
"enabled": "",
}
- policy_id = mock_data.get_policy_id()
- meta_rule_id = "1"
- add_rule(policy_id, meta_rule_id, value)
+ policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id, value=value)
value = {
"rule": ("low", "low", "vm-action"),
"instructions": ({"decision": "grant"}),
"enabled": "",
}
- meta_rule_id = "1"
- add_rule(policy_id, meta_rule_id, value)
- rules = get_rules(policy_id, meta_rule_id)
+ policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id, value=value)
+ rules = policy_helper.get_rules(policy_id=policy_id, meta_rule_id=meta_rule_id)
assert isinstance(rules, dict)
assert rules
obj = rules.get('rules')
@@ -285,20 +174,25 @@ def test_get_rules(db):
def test_get_rules_with_invalid_policy_id_failure(db):
- rules = get_rules("invalid_policy_id", "meta_rule_id")
+ rules = policy_helper.get_rules("invalid_policy_id", "meta_rule_id")
assert not rules.get('meta_rule-id')
assert len(rules.get('rules')) == 0
def test_add_rule(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"rule": ("high", "medium", "vm-action"),
"instructions": ({"decision": "grant"}),
"enabled": "",
}
- policy_id = mock_data.get_policy_id()
- meta_rule_id = "1"
- rules = add_rule(policy_id, meta_rule_id, value)
+
+ rules = policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id, value=value)
assert rules
assert len(rules) == 1
assert isinstance(rules, dict)
@@ -308,19 +202,44 @@ def test_add_rule(db):
assert rules[rule_id][key] == value[key]
with pytest.raises(RuleExisting):
- add_rule(policy_id, meta_rule_id, value)
+ policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id, value=value)
def test_delete_rule(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category14",
+ object_category_name="object_category14",
+ action_category_name="action_category14",
+ meta_rule_name="meta_rule_14",
+ model_name="model14")
value = {
"rule": ("low", "low", "vm-action"),
"instructions": ({"decision": "grant"}),
"enabled": "",
}
- policy_id = mock_data.get_policy_id()
- meta_rule_id = "2"
- rules = add_rule(policy_id, meta_rule_id, value)
+ rules = policy_helper.add_rule(policy_id, meta_rule_id, value)
rule_id = list(rules.keys())[0]
- delete_rule(policy_id, rule_id)
- rules = get_rules(policy_id, meta_rule_id)
+ policy_helper.delete_rule(policy_id, rule_id)
+ rules = policy_helper.get_rules(policy_id, meta_rule_id)
assert not rules.get('rules')
+
+
+def test_delete_policies_with_pdp(db):
+ value = {
+ "name": "test_policy1",
+ "model_id": "",
+ "genre": "authz",
+ "description": "test",
+ }
+ policies = policy_helper.add_policies(value=value)
+ policy_id1 = list(policies.keys())[0]
+ pdp_id = "pdp_id1"
+ value = {
+ "name": "test_pdp",
+ "security_pipeline": [policy_id1],
+ "keystone_project_id": "keystone_project_id1",
+ "description": "...",
+ }
+ pdp_helper.add_pdp(pdp_id=pdp_id, value=value)
+ with pytest.raises(DeletePolicyWithPdp) as exception_info:
+ policy_helper.delete_policies(policy_id1)
diff --git a/python_moondb/tests/unit_python/requirements.txt b/python_moondb/tests/unit_python/requirements.txt
index 5f507ff7..ff727723 100644
--- a/python_moondb/tests/unit_python/requirements.txt
+++ b/python_moondb/tests/unit_python/requirements.txt
@@ -1,5 +1,4 @@
sqlalchemy
pymysql
-pytest
requests_mock
python_moonutilities \ No newline at end of file
diff --git a/python_moondb/tests/unit_python/test_pdp.py b/python_moondb/tests/unit_python/test_pdp.py
index 942d98a3..4d245e4d 100755
--- a/python_moondb/tests/unit_python/test_pdp.py
+++ b/python_moondb/tests/unit_python/test_pdp.py
@@ -1,125 +1,149 @@
import pytest
-
-
-def update_pdp(pdp_id, value):
- from python_moondb.core import PDPManager
- return PDPManager.update_pdp("", pdp_id, value)
-
-
-def delete_pdp(pdp_id):
- from python_moondb.core import PDPManager
- PDPManager.delete_pdp("", pdp_id)
-
-
-def add_pdp(pdp_id=None, value=None):
- from python_moondb.core import PDPManager
- return PDPManager.add_pdp("", pdp_id, value)
-
-
-def get_pdp(pdp_id=None):
- from python_moondb.core import PDPManager
- return PDPManager.get_pdp("", pdp_id)
+import helpers.mock_data as mock_data
+import helpers.pdp_helper as pdp_helper
def test_update_pdp(db):
pdp_id = "pdp_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- add_pdp(pdp_id, value)
- pdp = update_pdp(pdp_id, value)
+ pdp_helper.add_pdp(pdp_id, value)
+ pdp = pdp_helper.update_pdp(pdp_id, value)
assert pdp
def test_update_pdp_with_invalid_id(db):
pdp_id = "pdp_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
with pytest.raises(Exception) as exception_info:
- update_pdp(pdp_id, value)
+ pdp_helper.update_pdp(pdp_id, value)
assert str(exception_info.value) == '400: Pdp Unknown'
def test_delete_pdp(db):
pdp_id = "pdp_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- add_pdp(pdp_id, value)
- delete_pdp(pdp_id)
- assert len(get_pdp(pdp_id)) == 0
+ pdp_helper.add_pdp(pdp_id, value)
+ pdp_helper.delete_pdp(pdp_id)
+ assert len(pdp_helper.get_pdp(pdp_id)) == 0
def test_delete_pdp_with_invalid_id(db):
pdp_id = "pdp_id1"
with pytest.raises(Exception) as exception_info:
- delete_pdp(pdp_id)
+ pdp_helper.delete_pdp(pdp_id)
assert str(exception_info.value) == '400: Pdp Unknown'
def test_add_pdp(db):
pdp_id = "pdp_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- pdp = add_pdp(pdp_id, value)
+ pdp = pdp_helper.add_pdp(pdp_id, value)
assert pdp
def test_add_pdp_twice_with_same_id(db):
pdp_id = "pdp_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- add_pdp(pdp_id, value)
+ pdp_helper.add_pdp(pdp_id, value)
with pytest.raises(Exception) as exception_info:
- add_pdp(pdp_id, value)
+ pdp_helper.add_pdp(pdp_id, value)
assert str(exception_info.value) == '409: Pdp Error'
def test_add_pdp_twice_with_same_name(db):
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- add_pdp(value=value)
+ pdp_helper.add_pdp(value=value)
with pytest.raises(Exception) as exception_info:
- add_pdp(value=value)
+ pdp_helper.add_pdp(value=value)
assert str(exception_info.value) == '409: Pdp Error'
def test_get_pdp(db):
pdp_id = "pdp_id1"
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy(
+ subject_category_name="subject_category1",
+ object_category_name="object_category1",
+ action_category_name="action_category1",
+ meta_rule_name="meta_rule_1",
+ model_name="model1")
value = {
"name": "test_pdp",
- "security_pipeline": ["policy_id_1", "policy_id_2"],
+ "security_pipeline": [policy_id],
"keystone_project_id": "keystone_project_id1",
"description": "...",
}
- add_pdp(pdp_id, value)
- pdp = get_pdp(pdp_id)
+ pdp_helper.add_pdp(pdp_id, value)
+ pdp = pdp_helper.get_pdp(pdp_id)
assert len(pdp) == 1
def test_get_pdp_with_invalid_id(db):
pdp_id = "invalid"
- pdp = get_pdp(pdp_id)
+ pdp = pdp_helper.get_pdp(pdp_id)
assert len(pdp) == 0