aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_perimeter.py
blob: 322d90c616eb330244d6b4ba4bc39d4860f62ff0 (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
# import moon_manager
# import moon_manager.api
import json
import api.utilities as utilities
from helpers import data_builder as builder
from uuid import uuid4


def get_subjects(client):
    req = client.get("/subjects")
    subjects = utilities.get_json(req.data)
    return req, subjects


def add_subjects(client, name):
    subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy(
        subject_category_name="subject_category1" + uuid4().hex,
        object_category_name="object_category1" + uuid4().hex,
        action_category_name="action_category1" + uuid4().hex,
        meta_rule_name="meta_rule_1" + uuid4().hex,
        model_name="model1" + uuid4().hex)
    data = {
        "name": name + uuid4().hex,
        "description": "description of {}".format(name),
        "password": "password for {}".format(name),
        "email": "{}@moon".format(name)
    }
    req = client.post("/policies/{}/subjects".format(policy_id), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    subjects = utilities.get_json(req.data)
    return req, subjects


def delete_subject(client):
    subjects = get_subjects(client)
    value = subjects[1]['subjects']
    id = list(value.keys())[0]
    policy_id = builder.get_policy_id_with_subject_assignment()
    return client.delete("/policies/{}/subjects/{}".format(policy_id, id))


def delete_subjects_without_perimeter_id(client):
    req = client.delete("/subjects/{}".format(""))
    return req


def test_perimeter_get_subject():
    client = utilities.register_client()
    req, subjects = get_subjects(client)
    assert req.status_code == 200
    assert isinstance(subjects, dict)
    assert "subjects" in subjects


def test_perimeter_add_subject():
    client = utilities.register_client()
    req, subjects = add_subjects(client, "testuser")
    value = list(subjects["subjects"].values())[0]
    assert req.status_code == 200
    assert "subjects" in subjects
    assert value["name"] is not None
    assert value["email"] is not None


def test_perimeter_add_subject_without_name():
    client = utilities.register_client()
    data = {
        "name": "",
        "description": "description of {}".format(""),
        "password": "password for {}".format(""),
        "email": "{}@moon".format("")
    }
    req = client.post("/policies/{}/subjects".format("111"), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [Empty String]"


def test_perimeter_add_subject_with_name_contain_spaces():
    client = utilities.register_client()
    data = {
        "name": "test user",
        "description": "description of {}".format("test user"),
        "password": "password for {}".format("test user"),
        "email": "{}@moon".format("test user")
    }
    req = client.post("/policies/{}/subjects".format("111"), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [String contains space]"


def test_perimeter_delete_subject():
    client = utilities.register_client()
    req = delete_subject(client)
    assert req.status_code == 200


def test_perimeter_delete_subjects_without_perimeter_id():
    client = utilities.register_client()
    req = delete_subjects_without_perimeter_id(client)
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "400: Subject Unknown"


def get_objects(client):
    req = client.get("/objects")
    objects = utilities.get_json(req.data)
    return req, objects


def add_objects(client, name):
    subject_category_id, object_category_id, action_category_id, meta_rule_id, policyId = builder.create_new_policy(
        subject_category_name="subject_category1" + uuid4().hex,
        object_category_name="object_category1" + uuid4().hex,
        action_category_name="action_category1" + uuid4().hex,
        meta_rule_name="meta_rule_1" + uuid4().hex,
        model_name="model1" + uuid4().hex)
    data = {
        "name": name + uuid4().hex,
        "description": "description of {}".format(name),
    }
    req = client.post("/policies/{}/objects/".format(policyId), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    objects = utilities.get_json(req.data)
    return req, objects


def delete_object(client):
    objects = get_objects(client)
    value = objects[1]['objects']
    id = list(value.keys())[0]
    policy_id = builder.get_policy_id_with_object_assignment()
    return client.delete("/policies/{}/objects/{}".format(policy_id, id))


def delete_objects_without_perimeter_id(client):
    req = client.delete("/objects/{}".format(""))
    return req


def test_perimeter_get_object():
    client = utilities.register_client()
    req, objects = get_objects(client)
    assert req.status_code == 200
    assert isinstance(objects, dict)
    assert "objects" in objects


def test_perimeter_add_object():
    client = utilities.register_client()
    req, objects = add_objects(client, "testuser")
    value = list(objects["objects"].values())[0]
    assert req.status_code == 200
    assert "objects" in objects
    assert value['name'] is not None


def test_perimeter_add_object_without_name():
    client = utilities.register_client()
    data = {
        "name": "",
        "description": "description of {}".format(""),
    }
    req = client.post("/policies/{}/objects/".format("111"), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [Empty String]"


def test_perimeter_add_object_with_name_contain_spaces():
    client = utilities.register_client()
    data = {
        "name": "test user",
        "description": "description of {}".format("test user"),
    }
    req = client.post("/policies/{}/objects/".format("111"), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [String contains space]"


def test_perimeter_delete_object():
    client = utilities.register_client()
    req = delete_object(client)
    assert req.status_code == 200


def test_perimeter_delete_objects_without_perimeter_id():
    client = utilities.register_client()
    req = delete_objects_without_perimeter_id(client)
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "400: Object Unknown"


def get_actions(client):
    req = client.get("/actions")
    actions = utilities.get_json(req.data)
    return req, actions


def add_actions(client, name):
    subject_category_id, object_category_id, action_category_id, meta_rule_id, policyId = builder.create_new_policy(
        subject_category_name="subject_category1" + uuid4().hex,
        object_category_name="object_category1" + uuid4().hex,
        action_category_name="action_category1" + uuid4().hex,
        meta_rule_name="meta_rule_1" + uuid4().hex,
        model_name="model1" + uuid4().hex)
    data = {
        "name": name + uuid4().hex,
        "description": "description of {}".format(name),
    }
    req = client.post("/policies/{}/actions".format(policyId), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    actions = utilities.get_json(req.data)
    return req, actions


def delete_actions(client):
    actions = get_actions(client)
    value = actions[1]['actions']
    id = list(value.keys())[0]
    policy_id = builder.get_policy_id_with_action_assignment()
    return client.delete("/policies/{}/actions/{}".format(policy_id, id))


def delete_actions_without_perimeter_id(client):
    req = client.delete("/actions/{}".format(""))
    return req


def test_perimeter_get_actions():
    client = utilities.register_client()
    req, actions = get_actions(client)
    assert req.status_code == 200
    assert isinstance(actions, dict)
    assert "actions" in actions


def test_perimeter_add_actions():
    client = utilities.register_client()
    req, actions = add_actions(client, "testuser")
    value = list(actions["actions"].values())[0]
    assert req.status_code == 200
    assert "actions" in actions
    assert value['name'] is not None


def test_perimeter_add_actions_without_name():
    client = utilities.register_client()
    data = {
        "name": "",
        "description": "description of {}".format(""),
    }
    req = client.post("/policies/{}/actions".format("111"), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [Empty String]"


def test_perimeter_add_actions_with_name_contain_spaces():
    client = utilities.register_client()
    data = {
        "name": "test user",
        "description": "description of {}".format("test user"),
    }
    req = client.post("/policies/{}/actions".format("111"), data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [String contains space]"


def test_perimeter_delete_actions():
    client = utilities.register_client()
    req = delete_actions(client)
    assert req.status_code == 200


def test_perimeter_delete_actions_without_perimeter_id():
    client = utilities.register_client()
    req = delete_actions_without_perimeter_id(client)
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "400: Action Unknown"