-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_cedar_integration_tests.py
More file actions
392 lines (333 loc) · 20.7 KB
/
test_cedar_integration_tests.py
File metadata and controls
392 lines (333 loc) · 20.7 KB
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
from parameterized import parameterized
import unittest
from typing import List
import cedarpy
from shared import pretty_format, load_file_as_json, load_file_as_str
def custom_name_func(testcase_func, param_num, param):
# print(f'{type(param.args)} {param.args}')
return "%s_%s__%s" %(
testcase_func.__name__,
param_num,
parameterized.to_safe_name("__".join(parameterized.to_safe_name(str(x)) for x in param.args)),
)
def get_authz_test_params_for_test_suite(test_kind: str, test_suite: str) -> list:
"""Get authorization test params for a cedar-integration-tests test suite
:param test_kind is one of the kinds of tests organized by directory in the cedar-integration-tests/tests
directory, e.g. example_use_cases_doc
:param test_suite is the test suite's 'id', which is the name of the file without the `.json`, e.g. '1a'
"""
# Load the test data
cedar_int_tests_base = "resources/cedar-integration-tests"
test_def: dict = load_file_as_json(f"{cedar_int_tests_base}/tests/{test_kind}/{test_suite}.json")
print(f'loading tests defined for use case: {test_suite}')
policies_file_name: str = test_def['policies']
entities_file_name: str = test_def['entities']
schema_file_name: str = test_def['schema']
should_validate: bool = test_def['shouldValidate']
request_models: List[dict] = test_def['requests']
policies: str = load_file_as_str(f"{cedar_int_tests_base}/{policies_file_name}")
entities: list = load_file_as_json(f"{cedar_int_tests_base}/{entities_file_name}")
schema: object = load_file_as_str(f"{cedar_int_tests_base}/{schema_file_name}")
testing_params = []
for request_model in request_models:
testing_params.append((policies,
entities,
schema,
should_validate,
request_model))
print(f'selected {len(testing_params)} test cases for {test_suite}:\n{pretty_format(testing_params)}')
return testing_params
class BaseDataDrivenCedarIntegrationTestCase(unittest.TestCase):
def exec_authz_query_with_assertions(self,
policies: str,
entities: list,
schema: str,
should_validate: bool,
request_model: dict) -> None:
# Validate policies against schema if should_validate is True
if should_validate:
validation_result = cedarpy.validate_policies(policies, schema)
self.assertTrue(validation_result.validation_passed,
f"Expected policies to validate but got errors: "
f"{[str(e) for e in validation_result.errors]}")
print(f"executing authz request model:\n{pretty_format(request_model)}")
request = {
'principal': f"{request_model['principal']['type']}::\"{request_model['principal']['id']}\"",
'action': f"{request_model['action']['type']}::\"{request_model['action']['id']}\"",
'resource': f"{request_model['resource']['type']}::\"{request_model['resource']['id']}\"",
'context': request_model.get('context', {}),
}
authz_result: cedarpy.AuthzResult = cedarpy.is_authorized(request=request, policies=policies, entities=entities,
schema=schema,
verbose=True)
description = request_model['description']
self.assertEqual(request_model['decision'], authz_result.decision.value.lower(),
msg=f'unexpected decision for query desc: {description}')
# 'reason' spelling is correct here, but a debatable choice as it's a list
# 'reason' matches the (Rust) Decision enum but Java API has exposed as reasons (plural)
self.assertEqual(request_model['reason'], authz_result.diagnostics.reasons,
msg=f'unexpected errors for query desc: {description}')
self.assertEqual(request_model['errors'], authz_result.diagnostics.errors,
msg=f'unexpected errors for query desc: {description}')
class CedarExampleUseCasesIntegrationTestCase(BaseDataDrivenCedarIntegrationTestCase):
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "1a"),
name_func=custom_name_func)
def test_example_use_cases_doc_1a(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "2a"),
name_func=custom_name_func)
def test_example_use_cases_doc_2a(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "2b"),
name_func=custom_name_func)
def test_example_use_cases_doc_2b(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "2c"),
name_func=custom_name_func)
def test_example_use_cases_doc_2c(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "3a"),
name_func=custom_name_func)
def test_example_use_cases_doc_3a(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "3b"),
name_func=custom_name_func)
def test_example_use_cases_doc_3b(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "3c"),
name_func=custom_name_func)
def test_example_use_cases_doc_3c(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "4a"),
name_func=custom_name_func)
def test_example_use_cases_doc_4a(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
# Test is not present in cedar-integration-tests release/4.1.x
# @parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "4c"),
# name_func=custom_name_func)
# @unittest.skip(reason="A couple of requests failing here; true reason TBD")
# def test_example_use_cases_doc_4c(self,
# policies: str,
# entities: list,
# schema: str,
# should_validate: bool, # ignored; currently don't have the equivalent
# query: dict):
#
# self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
# should_validate=should_validate,
# request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "4d"),
name_func=custom_name_func)
def test_example_use_cases_doc_4d(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "4e"),
name_func=custom_name_func)
def test_example_use_cases_doc_4e(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "4f"),
name_func=custom_name_func)
def test_example_use_cases_doc_4f(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("example_use_cases", "5b"),
name_func=custom_name_func)
def test_example_use_cases_doc_5b(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
class CedarIPIntegrationTestCase(BaseDataDrivenCedarIntegrationTestCase):
@parameterized.expand(get_authz_test_params_for_test_suite("ip", "1"),
name_func=custom_name_func)
def test_ip_1(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("ip", "2"),
name_func=custom_name_func)
def test_ip_2(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("ip", "3"),
name_func=custom_name_func)
def test_ip_3(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
class CedarMultiIntegrationTestCase(BaseDataDrivenCedarIntegrationTestCase):
@parameterized.expand(get_authz_test_params_for_test_suite("multi", "1"),
name_func=custom_name_func)
def test_multi_1(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("multi", "2"),
name_func=custom_name_func)
def test_multi_2(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("multi", "3"),
name_func=custom_name_func)
def test_multi_3(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("multi", "4"),
name_func=custom_name_func)
@unittest.skip(reason="12 pass, 1 fails")
def test_multi_4(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("multi", "5"),
name_func=custom_name_func)
@unittest.skip(reason="Depends on unspecified principal, which is (currently) unsupported by is_authorized, i.e. principal is a required parameter")
def test_multi_5(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
class CedarDecimalIntegrationTestCase(BaseDataDrivenCedarIntegrationTestCase):
@parameterized.expand(get_authz_test_params_for_test_suite("decimal", "1"),
name_func=custom_name_func)
def test_decimal_1(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)
@parameterized.expand(get_authz_test_params_for_test_suite("decimal", "2"),
name_func=custom_name_func)
def test_decimal_2(self,
policies: str,
entities: list,
schema: str,
should_validate: bool, # ignored; currently don't have the equivalent
query: dict):
self.exec_authz_query_with_assertions(policies=policies, entities=entities, schema=schema,
should_validate=should_validate,
request_model=query)