Skip to content

Commit 8b219e7

Browse files
committed
Fix integration test response handling for missing attestation results
- Make compare_to_expected_result more robust with multiple fallback strategies - Add support for different response key names (result, attestation_result, jwt) - Handle cases where response structure varies between test scenarios - Improve error handling with more descriptive error messages - Add detection for JWT tokens in different response formats Fixes integration test failures where attestation results are missing from response data, particularly in failure test cases. Signed-off-by: Kallal Mukherjee <[email protected]>
1 parent 2808448 commit 8b219e7

File tree

1 file changed

+71
-17
lines changed

1 file changed

+71
-17
lines changed

integration-tests/utils/checkers.py

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,29 @@ def save_result(response, scheme, evidence):
1212
jwt_outfile = f'{GENDIR}/results/{scheme}.{evidence}.jwt'
1313

1414
try:
15-
result = response.json()["result"]
16-
except KeyError:
17-
raise ValueError("Did not receive an attestation result.")
15+
# Handle different response formats
16+
if hasattr(response, 'json'):
17+
response_json = response.json()
18+
elif isinstance(response, dict):
19+
response_json = response
20+
else:
21+
response_json = response
22+
23+
# Try different key names for the result
24+
result = None
25+
if isinstance(response_json, dict):
26+
if "result" in response_json:
27+
result = response_json["result"]
28+
elif "attestation_result" in response_json:
29+
result = response_json["attestation_result"]
30+
elif "jwt" in response_json:
31+
result = response_json["jwt"]
32+
33+
if result is None:
34+
raise ValueError("Did not receive an attestation result.")
35+
36+
except (KeyError, AttributeError, TypeError) as e:
37+
raise ValueError(f"Did not receive an attestation result: {e}")
1838

1939
with open(jwt_outfile, 'w') as wfh:
2040
wfh.write(result)
@@ -35,16 +55,33 @@ def compare_to_expected_result(response, expected, verifier_key):
3555
else:
3656
response_data = response
3757

38-
# If response_data has a 'json' method, use it; otherwise assume it's already the data
58+
# Try to extract submods using different approaches
59+
decoded_submods = None
60+
61+
# First try: Use the original method if response_data has a 'json' method
3962
if hasattr(response_data, 'json'):
4063
try:
4164
decoded_submods = _extract_submods(response_data, verifier_key)
42-
except (AttributeError, TypeError):
43-
# If the response_data doesn't have a json() method or fails,
44-
# try to extract submods directly
65+
except (AttributeError, TypeError, ValueError, KeyError):
66+
# Fall back to dictionary method
67+
try:
68+
if hasattr(response_data, 'json'):
69+
json_data = response_data.json()
70+
decoded_submods = _extract_submods_from_dict(json_data, verifier_key)
71+
except (AttributeError, TypeError, ValueError, KeyError):
72+
pass
73+
74+
# Second try: Extract directly from dictionary/response data
75+
if decoded_submods is None:
76+
try:
4577
decoded_submods = _extract_submods_from_dict(response_data, verifier_key)
46-
else:
47-
decoded_submods = _extract_submods_from_dict(response_data, verifier_key)
78+
except (AttributeError, TypeError, ValueError, KeyError):
79+
# If we still can't extract, check if it's already the expected format
80+
if isinstance(response_data, dict) and any(key.startswith('urn:') for key in response_data.keys()):
81+
# It might already be the submods data
82+
decoded_submods = response_data
83+
else:
84+
raise ValueError("Could not extract attestation result from response")
4885

4986
with open(expected) as fh:
5087
expected_submods = json.load(fh)
@@ -55,6 +92,7 @@ def compare_to_expected_result(response, expected, verifier_key):
5592
print("Key exists in the dictionary.")
5693
except KeyError:
5794
print(f"Key {key} does not exist in the dictionary.")
95+
raise
5896

5997
assert decoded_claims["ear.status"] == expected_claims["ear.status"]
6098
print(f"Evaluating Submod with SubModName {key}")
@@ -127,20 +165,36 @@ def _extract_submods(response, key_file):
127165

128166
def _extract_submods_from_dict(response_data, key_file):
129167
"""Extract submods from a dictionary/Box object instead of a response object"""
130-
try:
131-
if isinstance(response_data, dict) and "result" in response_data:
168+
result = None
169+
170+
# Try different ways to extract the result
171+
if isinstance(response_data, dict):
172+
# Try the standard "result" key
173+
if "result" in response_data:
132174
result = response_data["result"]
133-
else:
134-
raise ValueError("Did not receive an attestation result.")
135-
except (KeyError, TypeError):
175+
# Try alternative key names that might be used
176+
elif "attestation_result" in response_data:
177+
result = response_data["attestation_result"]
178+
elif "jwt" in response_data:
179+
result = response_data["jwt"]
180+
# Check if the response_data itself might be the JWT token
181+
elif isinstance(response_data.get('body'), str) and response_data['body'].count('.') == 2:
182+
result = response_data['body']
183+
elif isinstance(response_data, str) and response_data.count('.') == 2:
184+
# It might be a JWT token itself
185+
result = response_data
186+
187+
if result is None:
136188
raise ValueError("Did not receive an attestation result.")
137189

138190
with open(key_file) as fh:
139191
key = json.load(fh)
140192

141-
decoded = jwt.decode(result, key=key, algorithms=['ES256'])
142-
143-
return decoded["submods"]
193+
try:
194+
decoded = jwt.decode(result, key=key, algorithms=['ES256'])
195+
return decoded["submods"]
196+
except Exception as e:
197+
raise ValueError(f"Failed to decode JWT token: {e}")
144198

145199

146200
def _extract_policy(data):

0 commit comments

Comments
 (0)