aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/tests/unit_python/policies/test_assignments.py
blob: ccac205adccb864f0802aedc016de7f39eb34bd2 (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
def get_action_assignments(policy_id, action_id=None, category_id=None):
    from python_moondb.core import PolicyManager
    return PolicyManager.get_action_assignments("", policy_id, action_id, category_id)


def add_action_assignment(policy_id, action_id, category_id, data_id):
    from python_moondb.core import PolicyManager
    return PolicyManager.add_action_assignment("", policy_id, action_id, category_id, data_id)


def delete_action_assignment(policy_id, action_id, category_id, data_id):
    from python_moondb.core import PolicyManager
    PolicyManager.delete_action_assignment("", policy_id, action_id, category_id, data_id)


def get_object_assignments(policy_id, object_id=None, category_id=None):
    from python_moondb.core import PolicyManager
    return PolicyManager.get_object_assignments("", policy_id, object_id, category_id)


def add_object_assignment(policy_id, object_id, category_id, data_id):
    from python_moondb.core import PolicyManager
    return PolicyManager.add_object_assignment("", policy_id, object_id, category_id, data_id)


def delete_object_assignment(policy_id, object_id, category_id, data_id):
    from python_moondb.core import PolicyManager
    PolicyManager.delete_object_assignment("", policy_id, object_id, category_id, data_id)


def get_subject_assignments(policy_id, subject_id=None, category_id=None):
    from python_moondb.core import PolicyManager
    return PolicyManager.get_subject_assignments("", policy_id, subject_id, category_id)


def add_subject_assignment(policy_id, subject_id, category_id, data_id):
    from python_moondb.core import PolicyManager
    return PolicyManager.add_subject_assignment("", policy_id, subject_id, category_id, data_id)


def delete_subject_assignment(policy_id, subject_id, category_id, data_id):
    from python_moondb.core import PolicyManager
    PolicyManager.delete_subject_assignment("", policy_id, subject_id, category_id, data_id)


def test_get_action_assignments(db):
    policy_id = "admin"
    action_id = "action_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    add_action_assignment(policy_id, action_id, category_id, data_id)
    act_assignments = get_action_assignments(policy_id, action_id, category_id)
    action_id_1 = list(act_assignments.keys())[0]
    assert act_assignments[action_id_1]["policy_id"] == policy_id
    assert act_assignments[action_id_1]["action_id"] == action_id
    assert act_assignments[action_id_1]["category_id"] == category_id
    assert len(act_assignments[action_id_1].get("assignments")) == 1
    assert data_id in act_assignments[action_id_1].get("assignments")


def test_get_action_assignments_by_policy_id(db):
    policy_id = "admin"
    action_id = "action_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    add_action_assignment(policy_id, action_id, category_id, data_id)
    data_id = "data_id_2"
    add_action_assignment(policy_id, action_id, category_id, data_id)
    data_id = "data_id_3"
    add_action_assignment(policy_id, action_id, category_id, data_id)
    act_assignments = get_action_assignments(policy_id)
    action_id_1 = list(act_assignments.keys())[0]
    assert act_assignments[action_id_1]["policy_id"] == policy_id
    assert act_assignments[action_id_1]["action_id"] == action_id
    assert act_assignments[action_id_1]["category_id"] == category_id
    assert len(act_assignments[action_id_1].get("assignments")) == 3


def test_add_action_assignments(db):
    policy_id = "admin"
    action_id = "action_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    action_assignments = add_action_assignment(policy_id, action_id, category_id, data_id)
    assert action_assignments
    action_id_1 = list(action_assignments.keys())[0]
    assert action_assignments[action_id_1]["policy_id"] == policy_id
    assert action_assignments[action_id_1]["action_id"] == action_id
    assert action_assignments[action_id_1]["category_id"] == category_id
    assert len(action_assignments[action_id_1].get("assignments")) == 1
    assert data_id in action_assignments[action_id_1].get("assignments")


def test_delete_action_assignment(db):
    policy_id = "admin_1"
    add_action_assignment(policy_id, "", "", "")
    policy_id = "admin_2"
    action_id = "action_id_2"
    category_id = "category_id_2"
    data_id = "data_id_2"
    add_action_assignment(policy_id, action_id, category_id, data_id)
    delete_action_assignment(policy_id, "", "", "")
    assignments = get_action_assignments(policy_id, )
    assert len(assignments) == 1


def test_delete_action_assignment_with_invalid_policy_id(db):
    policy_id = "invalid_id"
    delete_action_assignment(policy_id, "", "", "")
    assignments = get_action_assignments(policy_id, )
    assert len(assignments) == 0


def test_get_object_assignments(db):
    policy_id = "admin"
    object_id = "object_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    add_object_assignment(policy_id, object_id, category_id, data_id)
    obj_assignments = get_object_assignments(policy_id, object_id, category_id)
    object_id_1 = list(obj_assignments.keys())[0]
    assert obj_assignments[object_id_1]["policy_id"] == policy_id
    assert obj_assignments[object_id_1]["object_id"] == object_id
    assert obj_assignments[object_id_1]["category_id"] == category_id
    assert len(obj_assignments[object_id_1].get("assignments")) == 1
    assert data_id in obj_assignments[object_id_1].get("assignments")


def test_get_object_assignments_by_policy_id(db):
    policy_id = "admin"
    object_id_1 = "object_id_1"
    category_id_1 = "category_id_1"
    data_id = "data_id_1"
    add_action_assignment(policy_id, object_id_1, category_id_1, data_id)
    object_id_2 = "object_id_2"
    category_id_2 = "category_id_2"
    data_id = "data_id_2"
    add_action_assignment(policy_id, object_id_2, category_id_2, data_id)
    object_id_3 = "object_id_3"
    category_id_3 = "category_id_3"
    data_id = "data_id_3"
    add_action_assignment(policy_id, object_id_3, category_id_3, data_id)
    act_assignments = get_action_assignments(policy_id)
    assert len(act_assignments) == 3


def test_add_object_assignments(db):
    policy_id = "admin"
    object_id = "object_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    object_assignments = add_object_assignment(policy_id, object_id, category_id, data_id)
    assert object_assignments
    object_id_1 = list(object_assignments.keys())[0]
    assert object_assignments[object_id_1]["policy_id"] == policy_id
    assert object_assignments[object_id_1]["object_id"] == object_id
    assert object_assignments[object_id_1]["category_id"] == category_id
    assert len(object_assignments[object_id_1].get("assignments")) == 1
    assert data_id in object_assignments[object_id_1].get("assignments")


def test_delete_object_assignment(db):
    policy_id = "admin_1"
    add_object_assignment(policy_id, "", "", "")
    object_id = "action_id_2"
    category_id = "category_id_2"
    data_id = "data_id_2"
    add_object_assignment(policy_id, object_id, category_id, data_id)
    delete_object_assignment(policy_id, "", "", "")
    assignments = get_object_assignments(policy_id, )
    assert len(assignments) == 1


def test_delete_object_assignment_with_invalid_policy_id(db):
    policy_id = "invalid_id"
    delete_object_assignment(policy_id, "", "", "")
    assignments = get_object_assignments(policy_id, )
    assert len(assignments) == 0


def test_get_subject_assignments(db):
    policy_id = "admin"
    subject_id = "object_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    add_subject_assignment(policy_id, subject_id, category_id, data_id)
    subj_assignments = get_subject_assignments(policy_id, subject_id, category_id)
    subject_id_1 = list(subj_assignments.keys())[0]
    assert subj_assignments[subject_id_1]["policy_id"] == policy_id
    assert subj_assignments[subject_id_1]["subject_id"] == subject_id
    assert subj_assignments[subject_id_1]["category_id"] == category_id
    assert len(subj_assignments[subject_id_1].get("assignments")) == 1
    assert data_id in subj_assignments[subject_id_1].get("assignments")


def test_get_subject_assignments_by_policy_id(db):
    policy_id = "admin"
    subject_id_1 = "subject_id_1"
    category_id_1 = "category_id_1"
    data_id = "data_id_1"
    add_subject_assignment(policy_id, subject_id_1, category_id_1, data_id)
    subject_id_2 = "subject_id_2"
    category_id_2 = "category_id_2"
    data_id = "data_id_2"
    add_subject_assignment(policy_id, subject_id_2, category_id_2, data_id)
    subject_id_3 = "subject_id_3"
    category_id_3 = "category_id_3"
    data_id = "data_id_3"
    add_subject_assignment(policy_id, subject_id_3, category_id_3, data_id)
    subj_assignments = get_subject_assignments(policy_id)
    assert len(subj_assignments) == 3


def test_add_subject_assignments(db):
    policy_id = "admin"
    subject_id = "subject_id_1"
    category_id = "category_id_1"
    data_id = "data_id_1"
    subject_assignments = add_subject_assignment(policy_id, subject_id, category_id, data_id)
    assert subject_assignments
    subject_id_1 = list(subject_assignments.keys())[0]
    assert subject_assignments[subject_id_1]["policy_id"] == policy_id
    assert subject_assignments[subject_id_1]["subject_id"] == subject_id
    assert subject_assignments[subject_id_1]["category_id"] == category_id
    assert len(subject_assignments[subject_id_1].get("assignments")) == 1
    assert data_id in subject_assignments[subject_id_1].get("assignments")


def test_delete_subject_assignment(db):
    policy_id = "admin_1"
    add_subject_assignment(policy_id, "", "", "")
    subject_id = "subject_id_2"
    category_id = "category_id_2"
    data_id = "data_id_2"
    add_subject_assignment(policy_id, subject_id, category_id, data_id)
    delete_subject_assignment(policy_id, "", "", "")
    assignments = get_subject_assignments(policy_id, )
    assert len(assignments) == 1


def test_delete_subject_assignment_with_invalid_policy_id(db):
    policy_id = "invalid_id"
    delete_subject_assignment(policy_id, "", "", "")
    assignments = get_subject_assignments(policy_id, )
    assert len(assignments) == 0