aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/tests/unit_python/models/test_models.py
blob: 0026345cc9cc0f6ddf5296084e49b3d200c4e243 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# 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

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 exc_info:
        model_helper.add_model(value=model_value1)
    model_helper.delete_all_models()


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)


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_category_with_empty_name(db):
    category_id = "category_id1"
    value = {
        "name": "",
        "description": "description subject_category"
    }
    with pytest.raises(Exception) 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(Exception) 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_unkown_category_id(db):
    category_id = "invalid_category_id"

    with pytest.raises(Exception) 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_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(Exception) 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(Exception) 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_unkown_category_id(db):
    category_id = "invalid_category_id"

    with pytest.raises(Exception) 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_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(Exception) 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(Exception) 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_unkown_category_id(db):
    category_id = "invalid_category_id"

    with pytest.raises(Exception) as exception_info:
        category_helper.delete_action_category(category_id)
    assert str(exception_info.value) == '400: Action Category Unknown'