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
|
# Software Name: MOON
# Version: 5.4
# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0
# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.
import json
from uuid import uuid4
import hug
import pytest
from helpers import data_builder as builder
from helpers import policy_helper
from moon_utilities import exceptions
# subject_categories_test
def get_subject_data(policy_id, category_id=None):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}
if category_id is None:
req = hug.test.get(data, "/policies/{}/subject_data".format(policy_id), headers=auth_headers)
else:
req = hug.test.get(data, "/policies/{}/subject_data/{}".format(policy_id, category_id), headers=auth_headers)
subject_data = req.data
return req, subject_data
def add_subject_data(name):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
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)
body = {
"name": name,
"description": "description of {}".format(name)
}
req = hug.test.post(data, "/policies/{}/subject_data/{}".format(policy_id, subject_category_id),
body=json.dumps(body),
headers={'Content-Type': 'application/json', "X-Api-Key": get_api_key_for_user("admin")})
subject_data = req.data
return req, subject_data
def delete_subject_data(policy_id, category_id, data_id):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}
req = hug.test.delete(data, "/policies/{}/subject_data/{}/{}".format(policy_id, category_id,
data_id), headers=auth_headers)
return req
def test_get_subject_data():
policy = policy_helper.add_policies()
policy_id = next(iter(policy))
req, subject_data = get_subject_data(policy_id)
assert req.status == hug.HTTP_200
assert isinstance(subject_data, dict)
assert "subject_data" in subject_data
def test_add_subject_data():
req, subject_data = add_subject_data("testuser")
assert req.status == hug.HTTP_200
assert isinstance(subject_data, dict)
value = subject_data["subject_data"]['data']
assert "subject_data" in subject_data
id = list(value.keys())[0]
assert value[id]['name'] == "testuser"
assert value[id]['description'] == "description of {}".format("testuser")
def test_add_subject_data_invalid_name():
with pytest.raises(exceptions.DataContentError) as exception_info:
req, subject_data = add_subject_data(" ")
# assert req.status == hug.HTTP_400
assert '400: Data Content Error' == str(exception_info.value)
with pytest.raises(exceptions.DataContentError) as exception_info:
req, subject_data = add_subject_data("")
# assert req.status == hug.HTTP_400
assert '400: Data Content Error' == str(exception_info.value)
def test_delete_subject_data():
subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy()
data_id = builder.create_subject_data(policy_id, subject_category_id)
success_req = delete_subject_data(policy_id, subject_category_id, data_id)
assert success_req.status == hug.HTTP_200
def test_add_subject_data_with_forbidden_char_in_user():
with pytest.raises(exceptions.ValidationContentError) as exception_info:
req, subject_data = add_subject_data("<a>")
# assert '400: Invalid Content' == str(exception_info.value)
assert "Key: 'name', [Forbidden characters in string]" == str(exception_info.value)
# assert req.status == hug.HTTP_400
# assert req.data["message"] == "Key: 'name', [Forbidden characters in string]"
def test_delete_subject_data_without_policy_id():
success_req = delete_subject_data("", "", "")
assert success_req.status == hug.HTTP_405
# ---------------------------------------------------------------------------
# object_categories_test
def get_object_data(policy_id, category_id=None):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}
if category_id is None:
req = hug.test.get(data, "/policies/{}/object_data".format(policy_id), headers=auth_headers)
else:
req = hug.test.get(data, "/policies/{}/object_data/{}".format(policy_id, category_id), headers=auth_headers)
object_data = req.data
return req, object_data
def add_object_data(name):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
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)
body = {
"name": name,
"description": "description of {}".format(name)
}
req = hug.test.post(data, "/policies/{}/object_data/{}".format(policy_id, object_category_id),
body=json.dumps(body), headers={'Content-Type': 'application/json',
"X-Api-Key": get_api_key_for_user("admin")})
object_data = req.data
return req, object_data
def delete_object_data(policy_id, category_id, data_id):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}
req = hug.test.delete(data, "/policies/{}/object_data/{}/{}".format(policy_id, category_id,
data_id), headers=auth_headers)
return req
def test_get_object_data():
policy = policy_helper.add_policies()
policy_id = next(iter(policy))
req, object_data = get_object_data(policy_id)
assert req.status == hug.HTTP_200
assert isinstance(object_data, dict)
assert "object_data" in object_data
def test_add_object_data():
req, object_data = add_object_data("testuser")
assert req.status == hug.HTTP_200
assert isinstance(object_data, dict)
value = object_data["object_data"]['data']
assert "object_data" in object_data
_id = list(value.keys())[0]
assert value[_id]['name'] == "testuser"
assert value[_id]['description'] == "description of {}".format("testuser")
def test_add_object_data_invalid_name():
with pytest.raises(exceptions.DataContentError) as exception_info:
req, object_data = add_object_data(" ")
# assert req.status == hug.HTTP_400
assert '400: Data Content Error' == str(exception_info.value)
with pytest.raises(exceptions.DataContentError):
req, object_data = add_object_data("")
# assert req.status == hug.HTTP_400
assert '400: Data Content Error' == str(exception_info.value)
def test_delete_object_data():
subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy()
data_id = builder.create_object_data(policy_id, object_category_id)
success_req = delete_object_data(policy_id, data_id, object_category_id)
assert success_req.status == hug.HTTP_200
def test_add_object_data_with_forbidden_char_in_user():
with pytest.raises(exceptions.ValidationContentError) as exception_info:
req, subject_data = add_object_data("<a>")
# assert req.status == hug.HTTP_400
# assert req.data["message"] == "Key: 'name', [Forbidden characters in string]"
# assert '400: Invalid Content' == str(exception_info.value)
assert "Key: 'name', [Forbidden characters in string]" == str(exception_info.value)
def test_delete_object_data_without_policy_id():
success_req = delete_object_data("", "", "")
assert success_req.status == hug.HTTP_405
# ---------------------------------------------------------------------------
# action_categories_test
def get_action_data(policy_id, category_id=None):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}
if category_id is None:
req = hug.test.get(data, "/policies/{}/action_data".format(policy_id),
headers=auth_headers)
else:
req = hug.test.get(data, "/policies/{}/action_data/{}".format(policy_id, category_id),
headers=auth_headers)
action_data = req.data
return req, action_data
def add_action_data(name):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
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)
body = {
"name": name,
"description": "description of {}".format(name)
}
req = hug.test.post(data, "/policies/{}/action_data/{}".format(policy_id, action_category_id),
body=json.dumps(body),
headers={'Content-Type': 'application/json',
"X-Api-Key": get_api_key_for_user("admin")})
action_data = req.data
return req, action_data
def delete_action_data(policy_id, categorgy_id, data_id):
from moon_manager.api import data
from moon_utilities.auth_functions import get_api_key_for_user
auth_headers = {"X-Api-Key": get_api_key_for_user("admin")}
req = hug.test.delete(data, "/policies/{}/action_data/{}/{}".format(policy_id, categorgy_id,
data_id), headers=auth_headers)
return req
def test_get_action_data():
policy = policy_helper.add_policies()
policy_id = next(iter(policy))
req, action_data = get_action_data(policy_id)
assert req.status == hug.HTTP_200
assert isinstance(action_data, dict)
assert "action_data" in action_data
def test_add_action_data():
req, action_data = add_action_data("testuser")
assert req.status == hug.HTTP_200
assert isinstance(action_data, dict)
value = action_data["action_data"]['data']
assert "action_data" in action_data
id = list(value.keys())[0]
assert value[id]['name'] == "testuser"
assert value[id]['description'] == "description of {}".format("testuser")
def test_add_action_data_invalid_name():
with pytest.raises(exceptions.DataContentError)as exception_info:
req, action_data = add_action_data(" ")
# assert req.status == hug.HTTP_400
assert '400: Data Content Error' == str(exception_info.value)
with pytest.raises(exceptions.DataContentError) as exception_info:
req, action_data = add_action_data("")
# assert req.status == hug.HTTP_400
assert '400: Data Content Error' == str(exception_info.value)
def test_delete_action_data():
subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy()
data_id = builder.create_action_data(policy_id, action_category_id)
success_req = delete_action_data(policy_id, data_id, action_category_id)
assert success_req.status == hug.HTTP_200
def test_add_action_data_with_forbidden_char_in_user():
with pytest.raises(exceptions.ValidationContentError) as exception_info:
req, action_data = add_action_data("<a>")
# assert req.status == hug.HTTP_400
# assert req.data["message"] == "Key: 'name', [Forbidden characters in string]"
# assert '400: Invalid Content' == str(exception_info.value)
assert "Key: 'name', [Forbidden characters in string]" == str(exception_info.value)
def test_delete_action_data_without_policy_id():
success_req = delete_action_data("", "", "")
assert success_req.status == hug.HTTP_405
# ---------------------------------------------------------------------------
|