Skip to content

Commit 1477028

Browse files
committed
chore(kuttl): improve error output in python scripts
1 parent e0fecaa commit 1477028

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

tests/templates/kuttl/aas-user-info/test-regorule.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ def assertions(
88
username, response, opa_attribute, expected_groups, expected_attributes={}
99
):
1010
assert "result" in response
11-
assert opa_attribute in response["result"]
11+
result = response["result"]
12+
assert opa_attribute in result, f"expected {opa_attribute} in {result}"
1213

1314
# repeated the right hand side for better output on error
14-
assert "customAttributes" in response["result"][opa_attribute]
15-
assert "groups" in response["result"][opa_attribute]
16-
assert "id" in response["result"][opa_attribute]
17-
assert "username" in response["result"][opa_attribute]
15+
assert "customAttributes" in result[opa_attribute]
16+
assert "groups" in result[opa_attribute]
17+
assert "id" in result[opa_attribute]
18+
assert "username" in result[opa_attribute]
1819

1920
# todo: split out group assertions
2021
print(f"Testing for {username} in groups {expected_groups}")
21-
groups = sorted(response["result"][opa_attribute]["groups"])
22+
groups = sorted(result[opa_attribute]["groups"])
2223
expected_groups = sorted(expected_groups)
2324
assert groups == expected_groups, f"got {groups}, expected: {expected_groups}"
2425

2526
# todo: split out customAttribute assertions
2627
print(f"Testing for {username} with customAttributes {expected_attributes}")
27-
custom_attributes = response["result"][opa_attribute]["customAttributes"]
28+
custom_attributes = result[opa_attribute]["customAttributes"]
2829
assert (
2930
custom_attributes == expected_attributes
3031
), f"got {custom_attributes}, expected: {expected_attributes}"
@@ -37,9 +38,12 @@ def assertions(
3738
params = {"strict-builtin-errors": "true"}
3839

3940
def make_request(payload):
40-
return requests.post(
41-
args["url"], data=json.dumps(payload), params=params
42-
).json()
41+
response = requests.post(args["url"], data=json.dumps(payload), params=params)
42+
expected_status_code = 200
43+
assert (
44+
response.status_code == expected_status_code
45+
), f"got {response.status_code}, expected: {expected_status_code}"
46+
return response.json()
4347

4448
for subject_id in ["alice", "bob"]:
4549
try:

tests/templates/kuttl/keycloak-user-info/test-regorule.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ def assertions(
1414
username, response, opa_attribute, expected_groups, expected_attributes={}
1515
):
1616
assert "result" in response
17-
assert opa_attribute in response["result"]
17+
result = response["result"]
18+
assert opa_attribute in result, f"expected {opa_attribute} in {result}"
1819

1920
# repeated the right hand side for better output on error
20-
assert "customAttributes" in response["result"][opa_attribute]
21-
assert "groups" in response["result"][opa_attribute]
22-
assert "id" in response["result"][opa_attribute]
23-
assert "username" in response["result"][opa_attribute]
21+
assert "customAttributes" in result[opa_attribute]
22+
assert "groups" in result[opa_attribute]
23+
assert "id" in result[opa_attribute]
24+
assert "username" in result[opa_attribute]
2425

2526
# todo: split out group assertions
2627
print(f"Testing for {username} in groups {expected_groups}")
27-
groups = sorted(response["result"][opa_attribute]["groups"])
28+
groups = sorted(result[opa_attribute]["groups"])
2829
expected_groups = sorted(expected_groups)
2930
assert groups == expected_groups, f"got {groups}, expected: {expected_groups}"
3031

3132
# todo: split out customAttribute assertions
3233
print(f"Testing for {username} with customAttributes {expected_attributes}")
33-
custom_attributes = response["result"][opa_attribute]["customAttributes"]
34+
custom_attributes = result[opa_attribute]["customAttributes"]
3435
assert (
3536
custom_attributes == expected_attributes
3637
), f"got {custom_attributes}, expected: {expected_attributes}"
@@ -43,9 +44,12 @@ def assertions(
4344
params = {"strict-builtin-errors": "true"}
4445

4546
def make_request(payload):
46-
return requests.post(
47-
args["url"], data=json.dumps(payload), params=params
48-
).json()
47+
response = requests.post(args["url"], data=json.dumps(payload), params=params)
48+
expected_status_code = 200
49+
assert (
50+
response.status_code == expected_status_code
51+
), f"got {response.status_code}, expected: {expected_status_code}"
52+
return response.json()
4953

5054
for username, groups in users_and_groups.items():
5155
try:

0 commit comments

Comments
 (0)