aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/tests/unit_python/models/test_meta_rules.py
blob: d8b6136529f112d524daf983972d40aa7d8e3641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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'