diff options
Diffstat (limited to 'python_moondb/tests/unit_python')
7 files changed, 130 insertions, 35 deletions
diff --git a/python_moondb/tests/unit_python/models/test_categories.py b/python_moondb/tests/unit_python/models/test_categories.py new file mode 100644 index 00000000..111538b4 --- /dev/null +++ b/python_moondb/tests/unit_python/models/test_categories.py @@ -0,0 +1,44 @@ +import pytest +import logging +from python_moonutilities.exceptions import * + +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"}) + assert category is not None + with pytest.raises(SubjectCategoryExisting): + add_subject_category(value={"name":"category name", "description":"description 2"}) + + +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"}) + assert category is not None + with pytest.raises(ObjectCategoryExisting): + add_object_category(value={"name":"category name", "description":"description 2"}) + + +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"}) + assert category is not None + with pytest.raises(ActionCategoryExisting): + add_action_category(value={"name":"category name", "description":"description 2"}) 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 d8b61365..585274d9 100644 --- a/python_moondb/tests/unit_python/models/test_meta_rules.py +++ b/python_moondb/tests/unit_python/models/test_meta_rules.py @@ -127,7 +127,6 @@ def test_get_meta_rule_success(db): 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 diff --git a/python_moondb/tests/unit_python/models/test_models.py b/python_moondb/tests/unit_python/models/test_models.py index e56fea6b..54c45e77 100644 --- a/python_moondb/tests/unit_python/models/test_models.py +++ b/python_moondb/tests/unit_python/models/test_models.py @@ -1,5 +1,7 @@ import pytest - +from python_moonutilities.exceptions import * +import logging +logger = logging.getLogger("moon.db.tests.test_model") def get_models(model_id=None): from python_moondb.core import ModelManager @@ -9,8 +11,9 @@ def get_models(model_id=None): 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": "MLS", + "name": name, "description": "test", "meta_rules": "meta_rule_mls_1" } @@ -27,6 +30,14 @@ def delete_models(uuid=None, name=None): 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) @@ -49,7 +60,7 @@ def test_get_model(db): assert isinstance(models, dict) assert models # assert model is not empty assert len(models) is 1 - + delete_all_models() def test_get_specific_model(db): # prepare @@ -61,7 +72,7 @@ def test_get_specific_model(db): assert isinstance(models, dict) assert models # assert model is not empty assert len(models) is 1 - + delete_all_models() def test_add_model(db): # act @@ -70,15 +81,17 @@ def test_add_model(db): assert isinstance(model, dict) assert model # assert model is not empty assert len(model) is 1 + delete_all_models() 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: + with pytest.raises(ModelExisting) as exception_info: add_model(model_id="model_1") - assert str(exception_info.value) == '409: Model Error' + delete_all_models() + #assert str(exception_info.value) == '409: Model Error' def test_add_model_generate_new_uuid(db): @@ -97,6 +110,7 @@ def test_add_model_generate_new_uuid(db): model2 = add_model(value=model_value2) assert list(model1)[0] != list(model2)[0] + delete_all_models() def test_add_models(db): @@ -113,6 +127,7 @@ 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() def test_delete_models(db): @@ -135,6 +150,7 @@ def test_delete_models(db): # assert models = get_models() assert id not in models + delete_all_models() def test_update_model(db): @@ -147,7 +163,7 @@ def test_update_model(db): model = add_model(value=model_value) model_id = list(model)[0] new_model_value = { - "name": "MLS", + "name": "MLS2", "description": "test", "meta_rules": "meta_rule_mls_2" } @@ -158,4 +174,5 @@ def test_update_model(db): 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 + assert model[model_id][key] == new_model_value[key] + delete_all_models()
\ No newline at end of file diff --git a/python_moondb/tests/unit_python/policies/mock_data.py b/python_moondb/tests/unit_python/policies/mock_data.py index 23eeef64..3e9bea93 100644 --- a/python_moondb/tests/unit_python/policies/mock_data.py +++ b/python_moondb/tests/unit_python/policies/mock_data.py @@ -1,18 +1,18 @@ -def create_meta_rule(): +def create_meta_rule(meta_rule_name="meta_rule1", category_prefix=""): meta_rule_value = { - "name": "meta_rule1", + "name": meta_rule_name, "algorithm": "name of the meta rule algorithm", - "subject_categories": ["subject_category_id1", - "subject_category_id2"], - "object_categories": ["object_category_id1"], - "action_categories": ["action_category_id1"] + "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"] } return meta_rule_value -def create_model(meta_rule_id): +def create_model(meta_rule_id, model_name="test_model"): value = { - "name": "test_model", + "name": model_name, "description": "test", "meta_rules": [meta_rule_id] @@ -20,9 +20,9 @@ def create_model(meta_rule_id): return value -def create_policy(model_id): +def create_policy(model_id, policy_name="policy_1"): value = { - "name": "policy_1", + "name": policy_name, "model_id": model_id, "genre": "authz", "description": "test", @@ -40,15 +40,15 @@ def create_pdp(pdp_ids): return value -def get_policy_id(): +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 = test_meta_rules.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 = test_models.add_model(value=create_model(meta_rule_id, model_name)) model_id = list(model.keys())[0] - value = create_policy(model_id) + value = create_policy(model_id, policy_name) policy = test_policies.add_policies(value=value) assert policy policy_id = list(policy.keys())[0] diff --git a/python_moondb/tests/unit_python/policies/test_assignments.py b/python_moondb/tests/unit_python/policies/test_assignments.py index 707632b0..1ca140e6 100755 --- a/python_moondb/tests/unit_python/policies/test_assignments.py +++ b/python_moondb/tests/unit_python/policies/test_assignments.py @@ -1,5 +1,6 @@ import policies.mock_data as mock_data - +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 @@ -93,11 +94,13 @@ def test_add_action_assignments(db): 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) 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() + 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" @@ -161,6 +164,9 @@ def test_add_object_assignments(db): 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) + def test_delete_object_assignment(db): policy_id = mock_data.get_policy_id() @@ -228,6 +234,9 @@ def test_add_subject_assignments(db): 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) + def test_delete_subject_assignment(db): policy_id = mock_data.get_policy_id() diff --git a/python_moondb/tests/unit_python/policies/test_data.py b/python_moondb/tests/unit_python/policies/test_data.py index 67fa44fb..6a57130e 100755 --- a/python_moondb/tests/unit_python/policies/test_data.py +++ b/python_moondb/tests/unit_python/policies/test_data.py @@ -1,5 +1,9 @@ import policies.mock_data as mock_data 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): @@ -142,6 +146,9 @@ def test_add_action_data(db): 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') + def test_add_action_data_with_invalid_category_id(db): policy_id = mock_data.get_policy_id() @@ -214,6 +221,9 @@ def test_add_object_data(db): 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() @@ -285,6 +295,8 @@ def test_add_subject_data(db): 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): @@ -340,6 +352,9 @@ def test_add_action(db): action_id = list(action.keys())[0] assert len(action[action_id].get('policy_list')) == 1 + with pytest.raises(ActionExisting): + add_action(policy_id=policy_id, value=value) + def test_add_action_multiple_times(db): policy_id = mock_data.get_policy_id() @@ -348,6 +363,7 @@ def test_add_action_multiple_times(db): "description": "test", } action = add_action(policy_id=policy_id, value=value) + logger.info("action : {}".format(action)) action_id = list(action.keys())[0] perimeter_id = action[action_id].get('id') assert action @@ -356,7 +372,8 @@ 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(), perimeter_id, value) + 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) + logger.info("action : {}".format(action)) assert action action_id = list(action.keys())[0] assert len(action[action_id].get('policy_list')) == 2 @@ -408,6 +425,9 @@ def test_add_object(db): 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) + def test_add_objects_multiple_times(db): policy_id = mock_data.get_policy_id() @@ -424,7 +444,7 @@ def test_add_objects_multiple_times(db): "description": "test", "policy_list": ['policy_id_3', 'policy_id_4'] } - added_object = add_object(mock_data.get_policy_id(), perimeter_id, value) + 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) assert added_object object_id = list(added_object.keys())[0] assert len(added_object[object_id].get('policy_list')) == 2 @@ -476,6 +496,9 @@ def test_add_subject(db): 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) + def test_add_subjects_multiple_times(db): policy_id = mock_data.get_policy_id() @@ -492,7 +515,7 @@ def test_add_subjects_multiple_times(db): "description": "test", "policy_list": ['policy_id_3', 'policy_id_4'] } - subject = add_subject(mock_data.get_policy_id(), perimeter_id, value) + 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) assert subject subject_id = list(subject.keys())[0] assert len(subject[subject_id].get('policy_list')) == 2 diff --git a/python_moondb/tests/unit_python/policies/test_policies.py b/python_moondb/tests/unit_python/policies/test_policies.py index 148034ef..f81f0d39 100755 --- a/python_moondb/tests/unit_python/policies/test_policies.py +++ b/python_moondb/tests/unit_python/policies/test_policies.py @@ -5,7 +5,7 @@ import pytest import policies.mock_data as mock_data - +from python_moonutilities.exceptions import * def get_policies(): from python_moondb.core import PolicyManager @@ -97,9 +97,9 @@ def test_add_policies_twice_with_same_id(db): "description": "test", } add_policies(policy_id, value) - with pytest.raises(Exception) as exception_info: + with pytest.raises(PolicyExisting) as exception_info: add_policies(policy_id, value) - assert str(exception_info.value) == '409: Policy Error' + #assert str(exception_info.value) == '409: Policy Error' def test_delete_policies(db): @@ -127,9 +127,9 @@ def test_delete_policies(db): def test_delete_policies_with_invalid_id(db): policy_id = 'policy_id_1' - with pytest.raises(Exception) as exception_info: + with pytest.raises(PolicyUnknown) as exception_info: delete_policies(policy_id) - assert str(exception_info.value) == '400: Policy Unknown' + #assert str(exception_info.value) == '400: Policy Unknown' def test_update_policy(db): @@ -156,9 +156,9 @@ def test_update_policy_with_invalid_id(db): "genre": "authz", "description": "test-3", } - with pytest.raises(Exception) as exception_info: + with pytest.raises(PolicyUnknown) as exception_info: update_policy(policy_id, value) - assert str(exception_info.value) == '400: Policy Unknown' + #assert str(exception_info.value) == '400: Policy Unknown' def test_get_policy_from_meta_rules(db): @@ -271,6 +271,9 @@ def test_add_rule(db): assert key in rules[rule_id] assert rules[rule_id][key] == value[key] + with pytest.raises(RuleExisting): + add_rule(policy_id, meta_rule_id, value) + def test_delete_rule(db): value = { |