aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/tests
diff options
context:
space:
mode:
Diffstat (limited to 'python_moondb/tests')
-rwxr-xr-xpython_moondb/tests/unit_python/models/__init__.py0
-rw-r--r--python_moondb/tests/unit_python/models/test_meta_rules.py175
-rw-r--r--python_moondb/tests/unit_python/models/test_models.py161
3 files changed, 336 insertions, 0 deletions
diff --git a/python_moondb/tests/unit_python/models/__init__.py b/python_moondb/tests/unit_python/models/__init__.py
new file mode 100755
index 00000000..e69de29b
--- /dev/null
+++ b/python_moondb/tests/unit_python/models/__init__.py
diff --git a/python_moondb/tests/unit_python/models/test_meta_rules.py b/python_moondb/tests/unit_python/models/test_meta_rules.py
new file mode 100644
index 00000000..d8b61365
--- /dev/null
+++ b/python_moondb/tests/unit_python/models/test_meta_rules.py
@@ -0,0 +1,175 @@
+import pytest
+
+
+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)
+
+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'
+
+
+def test_add_new_meta_rule_success(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]
+ 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_set_meta_rule_succes(db):
+ # arrange
+ meta_rules = add_meta_rule()
+ meta_rule_id = list(meta_rules.keys())[0]
+ 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"]
+ }
+ # action
+ updated_meta_rule = 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"]
+
+
+def test_add_existing_meta_rule_error(db):
+ meta_rules = add_meta_rule()
+ meta_rule_id = list(meta_rules.keys())[0]
+ with pytest.raises(Exception) as exception_info:
+ 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
+ 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"]
+ }
+ meta_rules1 = add_meta_rule(value=value1)
+ meta_rule_id1 = list(meta_rules1.keys())[0]
+ values[meta_rule_id1] = value1
+ 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]
+ values[meta_rule_id2] = value2
+
+ # action
+ meta_rules = get_meta_rules()
+ # assert
+ assert isinstance(meta_rules , dict)
+ assert meta_rules
+ assert len(meta_rules) is 2
+ for meta_rule_id in meta_rules:
+ 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] == values[meta_rule_id][key]
+
+
+def test_get_specific_meta_rule_success(db):
+ # arrange
+ add_meta_rule()
+ added_meta_rules = 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_rule_id = list(meta_rules.keys())[0]
+ # assert
+ assert meta_rule_id == added_meta_rule_id
+ 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] == added_meta_rules[added_meta_rule_id][key]
+
+
+def test_delete_meta_rules_success(db):
+ # 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"]
+ }
+ meta_rules1 = 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)
+ # assert
+ meta_rules = 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'
diff --git a/python_moondb/tests/unit_python/models/test_models.py b/python_moondb/tests/unit_python/models/test_models.py
new file mode 100644
index 00000000..e56fea6b
--- /dev/null
+++ b/python_moondb/tests/unit_python/models/test_models.py
@@ -0,0 +1,161 @@
+import pytest
+
+
+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:
+ value = {
+ "name": "MLS",
+ "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 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()
+ # assert
+ assert isinstance(models, dict)
+ assert not models
+
+
+def test_get_model(db):
+ # prepare
+ add_model(model_id="mls_model_id")
+ # act
+ models = get_models()
+ # assert
+ assert isinstance(models, dict)
+ assert models # assert model is not empty
+ assert len(models) is 1
+
+
+def test_get_specific_model(db):
+ # prepare
+ add_model(model_id="mls_model_id")
+ add_model(model_id="rbac_model_id")
+ # act
+ models = get_models(model_id="mls_model_id")
+ # assert
+ assert isinstance(models, dict)
+ assert models # assert model is not empty
+ assert len(models) is 1
+
+
+def test_add_model(db):
+ # act
+ model = add_model()
+ # assert
+ assert isinstance(model, dict)
+ assert model # assert model is not empty
+ assert len(model) is 1
+
+
+def test_add_same_model_twice(db):
+ # prepare
+ add_model(model_id="model_1") # add model twice
+ # act
+ with pytest.raises(Exception) as exception_info:
+ add_model(model_id="model_1")
+ assert str(exception_info.value) == '409: Model Error'
+
+
+def test_add_model_generate_new_uuid(db):
+ model_value1 = {
+ "name": "MLS",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_1"
+ }
+ model1 = add_model(value=model_value1)
+
+ model_value2 = {
+ "name": "rbac",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_2"
+ }
+ model2 = add_model(value=model_value2)
+
+ assert list(model1)[0] != list(model2)[0]
+
+
+def test_add_models(db):
+ model_value1 = {
+ "name": "MLS",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_1"
+ }
+ models = add_model(value=model_value1)
+ assert isinstance(models, dict)
+ assert models
+ assert len(models.keys()) == 1
+ model_id = list(models.keys())[0]
+ for key in ("name", "meta_rules", "description"):
+ assert key in models[model_id]
+ assert models[model_id][key] == model_value1[key]
+
+
+def test_delete_models(db):
+ model_value1 = {
+ "name": "MLS",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_1"
+ }
+ model1 = add_model(value=model_value1)
+
+ model_value2 = {
+ "name": "rbac",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_2"
+ }
+ model2 = add_model(value=model_value2)
+
+ id = list(model1)[0]
+ delete_models(id)
+ # assert
+ models = get_models()
+ assert id not in models
+
+
+def test_update_model(db):
+ # prepare
+ model_value = {
+ "name": "MLS",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_1"
+ }
+ model = add_model(value=model_value)
+ model_id = list(model)[0]
+ new_model_value = {
+ "name": "MLS",
+ "description": "test",
+ "meta_rules": "meta_rule_mls_2"
+ }
+ # act
+ update_model(model_id=model_id, value=new_model_value)
+ # assert
+ model = 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] \ No newline at end of file