aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/tests/unit_python/models/test_categories.py
blob: 8782f172423f0a08bf13a62fce5ec070c528a9bb (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright 2018 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

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_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


def test_get_subject_categories():
    added_category = add_subject_category(value={"name": "category name", "description": "description 1"})
    category_id = list(added_category.keys())[0]
    subject_category = 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)
    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_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


def test_get_object_categories():
    added_category = add_object_category(value={"name": "category name", "description": "description 1"})
    category_id = list(added_category.keys())[0]
    object_category = 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)
    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_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


def test_get_action_categories():
    added_category = add_action_category(value={"name": "category name", "description": "description 1"})
    category_id = list(added_category.keys())[0]
    action_category = 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)
    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)