diff options
Diffstat (limited to 'python_moondb/tests/unit_python/models')
4 files changed, 0 insertions, 1136 deletions
diff --git a/python_moondb/tests/unit_python/models/__init__.py b/python_moondb/tests/unit_python/models/__init__.py deleted file mode 100755 index e69de29b..00000000 --- a/python_moondb/tests/unit_python/models/__init__.py +++ /dev/null diff --git a/python_moondb/tests/unit_python/models/test_categories.py b/python_moondb/tests/unit_python/models/test_categories.py deleted file mode 100644 index 39dc4c71..00000000 --- a/python_moondb/tests/unit_python/models/test_categories.py +++ /dev/null @@ -1,111 +0,0 @@ -# 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 pytest -import logging -from python_moonutilities.exceptions import * -from helpers import category_helper - -logger = logging.getLogger("moon.db.tests.models.test_categories") - - -def test_add_subject_category_twice(): - 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): - category_helper.add_subject_category(category_id, - value={"name": "category name", - "description": "description 2"}) - - -def test_add_subject_category_name_space(): - with pytest.raises(CategoryNameInvalid) as exp: - category = category_helper.add_subject_category(value={"name": " ", "description": - "description 1"}) - assert exp.value.code == 400 - assert exp.value.description == 'The given category name is invalid.' - - -def test_get_subject_categories(): - added_category = category_helper.add_subject_category( - value={"name": "category name", "description": "description 1"}) - category_id = list(added_category.keys())[0] - 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 = category_helper.get_subject_category(category_id) - assert len(subject_category) == 0 - - -def test_add_object_category_twice(): - 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): - category_helper.add_object_category(category_id, - value={"name": "category name", - "description": "description 2"}) - - -def test_add_object_category_name_space(): - with pytest.raises(CategoryNameInvalid) as exp: - category = category_helper.add_object_category(value={"name": " ", "description": - "description 1"}) - assert exp.value.code == 400 - assert exp.value.description == 'The given category name is invalid.' - - -def test_get_object_categories(): - added_category = category_helper.add_object_category( - value={"name": "category name", "description": "description 1"}) - category_id = list(added_category.keys())[0] - 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 = category_helper.get_object_category(category_id) - assert len(object_category) == 0 - - -def test_add_action_category_twice(): - 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) as exp_info: - category_helper.add_action_category(category_id, - value={"name": "category name", - "description": "description 2"}) - assert str(exp_info.value)=='409: Action Category Existing' - - -def test_add_action_category_name_space(): - with pytest.raises(CategoryNameInvalid) as exp: - category = category_helper.add_action_category(value={"name": " ", "description": - "description 1"}) - assert exp.value.code == 400 - assert exp.value.description == 'The given category name is invalid.' - - -def test_get_action_categories(): - added_category = category_helper.add_action_category( - value={"name": "category name", "description": "description 1"}) - category_id = list(added_category.keys())[0] - 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 = category_helper.get_action_category(category_id) - assert len(action_category) == 0 diff --git a/python_moondb/tests/unit_python/models/test_meta_rules.py b/python_moondb/tests/unit_python/models/test_meta_rules.py deleted file mode 100644 index 3b2b5b0e..00000000 --- a/python_moondb/tests/unit_python/models/test_meta_rules.py +++ /dev/null @@ -1,403 +0,0 @@ -# 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 pytest -from helpers import meta_rule_helper -from helpers import policy_helper -import helpers.mock_data as mock_data -import helpers.model_helper as model_helper -from python_moonutilities.exceptions import * -from uuid import uuid4 - - -def test_update_not_exist_meta_rule_error(db): - # set not existing meta rule and expect to raise and error - with pytest.raises(MetaRuleUnknown) as exception_info: - meta_rule_helper.update_meta_rule(meta_rule_id=None) - assert str(exception_info.value) == '400: Meta Rule Unknown' - - -def test_update_meta_rule_connected_with_policy_and_rule(): - 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") - subject_data_id = mock_data.create_subject_data(policy_id=policy_id, - category_id=subject_category_id) - object_data_id = mock_data.create_object_data(policy_id=policy_id, - category_id=object_category_id) - action_data_id = mock_data.create_action_data(policy_id=policy_id, - category_id=action_category_id) - - value = { - "rule": (subject_data_id, object_data_id, action_data_id), - "instructions": ({"decision": "grant"}), - "enabled": "", - } - - rules = policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id, value=value) - assert rules - assert len(rules) == 1 - - 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": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - with pytest.raises(MetaRuleUpdateError) as exception_info: - updated_meta_rule = meta_rule_helper.update_meta_rule(meta_rule_id, updated_value) - assert str(exception_info.value) == '400: Meta_Rule Update Error' - - -def test_update_meta_rule_connected_with_policy(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") - 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") - 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) - assert isinstance(meta_rules, dict) - assert meta_rules - 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_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": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - meta_rules = meta_rule_helper.add_meta_rule(value=value) - assert isinstance(meta_rules, dict) - assert meta_rules - 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_meta_rule_with_blank_name(db): - action_category_id = mock_data.create_action_category(uuid4().hex) - subject_category_id = mock_data.create_subject_category(uuid4().hex) - object_category_id = mock_data.create_object_category(uuid4().hex) - value = { - "name": "", - "description": "test", - "subject_categories": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - with pytest.raises(MetaRuleContentError) as exception_info: - meta_rule_helper.add_meta_rule(value=value) - assert str(exception_info.value) == '400: Meta Rule Error' - - -def test_update_meta_rule_success(db): - # arrange - 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": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - # action - updated_meta_rule = meta_rule_helper.update_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_update_meta_rule_with_existed_categories_combination(db): - action_category_id1 = mock_data.create_action_category(uuid4().hex) - subject_category_id1 = mock_data.create_subject_category(uuid4().hex) - object_category_id1 = mock_data.create_object_category(uuid4().hex) - meta_rule_name1=uuid4().hex - value1 = { - "name": meta_rule_name1, - "description": "test", - "subject_categories": [subject_category_id1], - "object_categories": [object_category_id1], - "action_categories": [action_category_id1] - } - meta_rules = meta_rule_helper.add_meta_rule(value=value1) - - action_category_id2 = mock_data.create_action_category(uuid4().hex) - subject_category_id2 = mock_data.create_subject_category(uuid4().hex) - object_category_id2 = mock_data.create_object_category(uuid4().hex) - meta_rule_name2 = uuid4().hex - value2 = { - "name": meta_rule_name2, - "description": "test", - "subject_categories": [subject_category_id2], - "object_categories": [object_category_id2], - "action_categories": [action_category_id2] - } - meta_rules = meta_rule_helper.add_meta_rule(value=value2) - meta_rule_id2 = list(meta_rules.keys())[0] - value1['name']=value2['name'] - with pytest.raises(MetaRuleExisting) as exception_info: - updated_meta_rule = meta_rule_helper.update_meta_rule(meta_rule_id2, value1) - assert str(exception_info.value) == '409: Meta Rule Existing' - assert exception_info.value.description=="Same categories combination existed" - - -def test_update_meta_rule_with_different_categories_combination_but_same_data(db): - action_category_id1 = mock_data.create_action_category(uuid4().hex) - subject_category_id1 = mock_data.create_subject_category(uuid4().hex) - object_category_id1 = mock_data.create_object_category(uuid4().hex) - meta_rule_name1=uuid4().hex - value1 = { - "name": meta_rule_name1, - "description": "test", - "subject_categories": [subject_category_id1], - "object_categories": [object_category_id1], - "action_categories": [action_category_id1] - } - meta_rules = meta_rule_helper.add_meta_rule(value=value1) - - action_category_id2 = mock_data.create_action_category(uuid4().hex) - subject_category_id2 = mock_data.create_subject_category(uuid4().hex) - object_category_id2 = mock_data.create_object_category(uuid4().hex) - meta_rule_name2 = uuid4().hex - value2 = { - "name": meta_rule_name2, - "description": "test", - "subject_categories": [subject_category_id2], - "object_categories": [object_category_id2], - "action_categories": [action_category_id2] - } - meta_rules = meta_rule_helper.add_meta_rule(value=value2) - meta_rule_id2 = list(meta_rules.keys())[0] - value1['name']=value2['name'] - value1['object_categories']+=[object_category_id1] - updated_meta_rule = meta_rule_helper.update_meta_rule(meta_rule_id2, value1) - assert meta_rule_id2 in updated_meta_rule - - -def test_add_existing_meta_rule_error(db): - 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(MetaRuleExisting) as exception_info: - meta_rule_helper.add_meta_rule(meta_rule_id=meta_rule_id) - assert str(exception_info.value) == '409: Meta Rule Existing' - - -def test_add_meta_rule_with_existing_name_error(db): - action_category_id = mock_data.create_action_category(uuid4().hex) - subject_category_id = mock_data.create_subject_category(uuid4().hex) - object_category_id = mock_data.create_object_category(uuid4().hex) - name = uuid4().hex - value = { - "name": name, - "description": "test", - "subject_categories": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - meta_rule_helper.add_meta_rule(value=value) - action_category_id = mock_data.create_action_category(uuid4().hex) - subject_category_id = mock_data.create_subject_category(uuid4().hex) - object_category_id = mock_data.create_object_category(uuid4().hex) - value = { - "name": name, - "description": 'test', - "subject_categories": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - with pytest.raises(MetaRuleExisting) as exception_info: - meta_rule_helper.add_meta_rule(value=value) - assert str(exception_info.value) == '409: Meta Rule Existing' - assert exception_info.value.description == 'The meta rule already exists.' - - -def test_add_meta_rule_with_existing_categories_combination(db): - action_category_id = mock_data.create_action_category(uuid4().hex) - subject_category_id = mock_data.create_subject_category(uuid4().hex) - object_category_id = mock_data.create_object_category(uuid4().hex) - name = uuid4().hex - value = { - "name": name, - "description": "test", - "subject_categories": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - meta_rule_helper.add_meta_rule(value=value) - value['name'] = uuid4().hex - with pytest.raises(MetaRuleExisting) as exception_info: - meta_rule_helper.add_meta_rule(value=value) - assert str(exception_info.value) == '409: Meta Rule Existing' - assert exception_info.value.description == "Same categories combination existed" - - -def test_add_meta_rule_with_different_categories_combination_but_same_data(db): - action_category_id = mock_data.create_action_category(uuid4().hex) - subject_category_id = mock_data.create_subject_category(uuid4().hex) - object_category_id1 = mock_data.create_object_category(uuid4().hex) - object_category_id2 = mock_data.create_object_category(uuid4().hex) - - name1 = uuid4().hex - value = { - "name": name1, - "description": "test", - "subject_categories": [subject_category_id], - "object_categories": [object_category_id1], - "action_categories": [action_category_id] - } - meta_rule_helper.add_meta_rule(value=value) - name2 = uuid4().hex - value['name'] = name2 - value['object_categories'] += [object_category_id2] - meta_rules = meta_rule_helper.add_meta_rule(value=value) - bool_found_meta_rule = 0 - for meta_rule_id in meta_rules: - if meta_rules[meta_rule_id]['name'] == name2: - bool_found_meta_rule = 1 - break - assert bool_found_meta_rule - - -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": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - 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": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - 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 = meta_rule_helper.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 - added_meta_rules = meta_rule_helper.add_meta_rule() - added_meta_rule_id = list(added_meta_rules.keys())[0] - # action - 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 - 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): - 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": [subject_category_id], - "object_categories": [object_category_id], - "action_categories": [action_category_id] - } - meta_rules1 = meta_rule_helper.add_meta_rule(value=value1) - meta_rule_id1 = list(meta_rules1.keys())[0] - - # action - meta_rule_helper.delete_meta_rules(meta_rule_id1) - # assert - meta_rules = meta_rule_helper.get_meta_rules() - assert meta_rule_id1 not in meta_rules - - -def test_delete_meta_rules_with_model(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - - with pytest.raises(DeleteMetaRuleWithModel) as exception_info: - meta_rule_helper.delete_meta_rules(meta_rule_id) - assert str(exception_info.value) == '400: Meta rule With Model Error' - - -def test_delete_invalid_meta_rules_error(db): - with pytest.raises(MetaRuleUnknown) as exception_info: - 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 deleted file mode 100644 index 1b171069..00000000 --- a/python_moondb/tests/unit_python/models/test_models.py +++ /dev/null @@ -1,622 +0,0 @@ -# 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 pytest -from python_moonutilities.exceptions import * -import logging -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 -import helpers.assignment_helper as assignment_helper -from uuid import uuid4 - -logger = logging.getLogger("moon.db.tests.test_model") - - -def test_get_models_empty(db): - # act - models = model_helper.get_models() - # assert - assert isinstance(models, dict) - assert not models - - -def test_get_model(db): - # prepare - model_helper.add_model(model_id="mls_model_id") - # act - models = model_helper.get_models() - # assert - assert isinstance(models, dict) - assert models # assert model is not empty - assert len(models) is 1 - model_helper.delete_all_models() - - -def test_get_specific_model(db): - # prepare - model_helper.add_model(model_id="mls_model_id") - # act - 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 - model_helper.delete_all_models() - - -def test_add_model(db): - # act - model = model_helper.add_model() - # assert - assert isinstance(model, dict) - assert model # assert model is not empty - assert len(model) is 1 - 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 - 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: - 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_id1] - } - 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_id2] - } - model2 = model_helper.add_model(value=model_value2) - - assert list(model1)[0] != list(model2)[0] - 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_id] - } - models = model_helper.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] - 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_id] - } - models = model_helper.add_model(value=model_value1) - assert isinstance(models, dict) - assert models - with pytest.raises(Exception) as exception_info: - model_helper.add_model(value=model_value1) - model_helper.delete_all_models() - assert str(exception_info.value) == '409: Model Error' - -def test_add_model_with_existed_meta_rules_list(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id = mock_data.create_new_meta_rule( - subject_category_name=uuid4().hex, - object_category_name=uuid4().hex, - action_category_name=uuid4().hex) - model_value1 = { - "name": uuid4().hex, - "description": "test", - "meta_rules": [meta_rule_id] - } - models = model_helper.add_model(value=model_value1) - assert isinstance(models, dict) - assert models - model_value1 = { - "name": uuid4().hex, - "description": "test", - "meta_rules": [meta_rule_id] - } - with pytest.raises(Exception) as exception_info: - model_helper.add_model(value=model_value1) - model_helper.delete_all_models() - assert str(exception_info.value) == '409: Model Error' - assert str(exception_info.value.description)=='Meta Rules List Existed in another Model' - - -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_id1] - } - 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_id2] - } - model_helper.add_model(value=model_value2) - - id = list(model1)[0] - model_helper.delete_models(id) - # assert - models = model_helper.get_models() - assert id not in 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_id1] - } - 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_id2] - } - # act - model_helper.update_model(model_id=model_id, value=new_model_value) - # assert - 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] - model_helper.delete_all_models() - - -def test_delete_model_assigned_to_policy(db): - model_value1 = { - "name": "MLS", - "description": "test", - "meta_rules": [] - } - models = model_helper.add_model(value=model_value1) - assert isinstance(models, dict) - assert models - assert len(models.keys()) == 1 - model_id = list(models.keys())[0] - value = { - "name": "test_policy", - "model_id": model_id, - "genre": "authz", - "description": "test", - } - policy_helper.add_policies(value=value) - with pytest.raises(DeleteModelWithPolicy) as exception_info: - model_helper.delete_models(uuid=model_id) - assert str(exception_info.value) == '400: Model With Policy Error' - - -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_categories_with_existed_name(db): - name = uuid4().hex - value = { - "name": name, - "description": "description subject_category" - } - subject_category = category_helper.add_subject_category(value=value) - assert subject_category - assert len(subject_category) == 1 - - value = { - "name": name, - "description": "description subject_category" - } - with pytest.raises(SubjectCategoryExisting) as exception_info: - category_helper.add_subject_category(value=value) - assert str(exception_info.value) == '409: Subject Category Existing' - - -def test_add_subject_category_with_empty_name(db): - category_id = "category_id1" - value = { - "name": "", - "description": "description subject_category" - } - with pytest.raises(CategoryNameInvalid) 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(SubjectCategoryExisting) 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_data(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - - mock_data.create_subject_data(policy_id, subject_category_id) - - with pytest.raises(DeleteSubjectCategoryWithMetaRule) as exception_info: - category_helper.delete_subject_category(subject_category_id) - assert str(exception_info.value) == '400: Subject Category With Meta Rule Error' - - -def test_delete_subject_category_with_assignment(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - - subject_id = mock_data.create_subject(policy_id) - data_id = mock_data.create_subject_data(policy_id, subject_category_id) - assignment_helper.add_subject_assignment(policy_id, subject_id, subject_category_id, data_id) - - with pytest.raises(DeleteSubjectCategoryWithMetaRule) as exception_info: - category_helper.delete_subject_category(subject_category_id) - assert str(exception_info.value) == '400: Subject Category With Meta Rule Error' - - -def test_delete_subject_category_with_rule(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id) - - with pytest.raises(DeleteSubjectCategoryWithMetaRule) as exception_info: - category_helper.delete_subject_category(subject_category_id) - assert str(exception_info.value) == '400: Subject Category With Meta Rule Error' - - -def test_delete_subject_category_with_unkown_category_id(db): - category_id = "invalid_category_id" - - with pytest.raises(SubjectCategoryUnknown) 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_categories_with_existed_name(db): - name = uuid4().hex - value = { - "name": name, - "description": "description object_category" - } - object_category = category_helper.add_object_category(value=value) - assert object_category - assert len(object_category) == 1 - with pytest.raises(ObjectCategoryExisting) as exception_info: - category_helper.add_object_category(value=value) - assert str(exception_info.value) == '409: Object Category Existing' - - -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(ObjectCategoryExisting) 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(CategoryNameInvalid) 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_data(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - mock_data.create_subject_data(policy_id, subject_category_id) - - mock_data.create_object_data(policy_id, object_category_id) - - with pytest.raises(DeleteObjectCategoryWithMetaRule) as exception_info: - category_helper.delete_object_category(object_category_id) - assert str(exception_info.value) == '400: Object Category With Meta Rule Error' - - -def test_delete_object_category_with_assignment(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - - object_id = mock_data.create_object(policy_id) - data_id = mock_data.create_object_data(policy_id, object_category_id) - assignment_helper.add_object_assignment(policy_id, object_id, object_category_id, data_id) - - with pytest.raises(DeleteObjectCategoryWithMetaRule) as exception_info: - category_helper.delete_object_category(object_category_id) - assert str(exception_info.value) == '400: Object Category With Meta Rule Error' - - -def test_delete_object_category_with_rule(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id) - - with pytest.raises(DeleteObjectCategoryWithMetaRule) as exception_info: - category_helper.delete_object_category(object_category_id) - assert str(exception_info.value) == '400: Object Category With Meta Rule Error' - - -def test_delete_object_category_with_unkown_category_id(db): - category_id = "invalid_category_id" - - with pytest.raises(ObjectCategoryUnknown) 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_categories_with_existed_name(db): - name = uuid4().hex - value = { - "name": name, - "description": "description action_category" - } - action_category = category_helper.add_action_category(value=value) - assert action_category - assert len(action_category) == 1 - with pytest.raises(ActionCategoryExisting) as exception_info: - category_helper.add_action_category(value=value) - assert str(exception_info.value) == '409: Action Category Existing' - - -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(ActionCategoryExisting) 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(CategoryNameInvalid) 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_data(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - mock_data.create_subject_data(policy_id, subject_category_id) - - mock_data.create_action_data(policy_id, action_category_id) - - with pytest.raises(DeleteActionCategoryWithMetaRule) as exception_info: - category_helper.delete_action_category(action_category_id) - assert str(exception_info.value) == '400: Action Category With Meta Rule Error' - - -def test_delete_action_category_with_assignment(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - - action_id = mock_data.create_action(policy_id) - data_id = mock_data.create_action_data(policy_id, action_category_id) - assignment_helper.add_action_assignment(policy_id, action_id, action_category_id, data_id) - - with pytest.raises(DeleteActionCategoryWithMetaRule) as exception_info: - category_helper.delete_action_category(action_category_id) - assert str(exception_info.value) == '400: Action Category With Meta Rule Error' - - -def test_delete_action_category_with_rule(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - policy_helper.add_rule(policy_id=policy_id, meta_rule_id=meta_rule_id) - - with pytest.raises(DeleteActionCategoryWithMetaRule) as exception_info: - category_helper.delete_action_category(action_category_id) - assert str(exception_info.value) == '400: Action Category With Meta Rule Error' - - -def test_delete_action_category_with_unkown_category_id(db): - category_id = "invalid_category_id" - - with pytest.raises(ActionCategoryUnknown) as exception_info: - category_helper.delete_action_category(category_id) - assert str(exception_info.value) == '400: Action Category Unknown' - - -def test_delete_data_categories_connected_to_meta_rule(db): - subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = mock_data.create_new_policy() - with pytest.raises(DeleteSubjectCategoryWithMetaRule) as exception_info: - category_helper.delete_subject_category(subject_category_id) - assert str(exception_info.value) == '400: Subject Category With Meta Rule Error' - - with pytest.raises(DeleteObjectCategoryWithMetaRule) as exception_info: - category_helper.delete_object_category(object_category_id) - assert str(exception_info.value) == '400: Object Category With Meta Rule Error' - - with pytest.raises(DeleteActionCategoryWithMetaRule) as exception_info: - category_helper.delete_action_category(action_category_id) - assert str(exception_info.value) == '400: Action Category With Meta Rule Error' |