Skip to content

Commit 93079cd

Browse files
committed
fix: simplify integration test response handling
Address setrofim's feedback by removing unnecessary complexity from integration test scripts. The original complex fallback logic for different response formats suggests API inconsistencies that should be fixed at the source rather than worked around. Reverts checkers.py to simple, clean implementation that expects consistent API responses with 'result' field containing JWT token. Removes the problematic _extract_submods_from_dict function and multiple try-catch fallback mechanisms that were masking underlying issues rather than addressing them properly. The URL-safe base64 nonce functionality is preserved while making the integration tests robust and maintainable.
1 parent 600668b commit 93079cd

File tree

1 file changed

+4
-92
lines changed

1 file changed

+4
-92
lines changed

integration-tests/utils/checkers.py

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

1414
try:
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}")
15+
result = response.json()["result"]
16+
except KeyError:
17+
raise ValueError("Did not receive an attestation result.")
3818

3919
with open(jwt_outfile, 'w') as wfh:
4020
wfh.write(result)
@@ -47,41 +27,7 @@ def save_result(response, scheme, evidence):
4727

4828

4929
def compare_to_expected_result(response, expected, verifier_key):
50-
# Handle Box objects (which Tavern uses internally)
51-
if hasattr(response, 'to_dict'):
52-
response_data = response.to_dict()
53-
elif hasattr(response, '__dict__'):
54-
response_data = response.__dict__
55-
else:
56-
response_data = response
57-
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
62-
if hasattr(response_data, 'json'):
63-
try:
64-
decoded_submods = _extract_submods(response_data, verifier_key)
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:
77-
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")
30+
decoded_submods = _extract_submods(response, verifier_key)
8531

8632
with open(expected) as fh:
8733
expected_submods = json.load(fh)
@@ -163,40 +109,6 @@ def _extract_submods(response, key_file):
163109
return decoded["submods"]
164110

165111

166-
def _extract_submods_from_dict(response_data, key_file):
167-
"""Extract submods from a dictionary/Box object instead of a response object"""
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:
174-
result = response_data["result"]
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:
188-
raise ValueError("Did not receive an attestation result.")
189-
190-
with open(key_file) as fh:
191-
key = json.load(fh)
192-
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}")
198-
199-
200112
def _extract_policy(data):
201113
policy = data
202114
policy['ctime'] = datetime.fromisoformat(policy['ctime'])

0 commit comments

Comments
 (0)