Skip to content

Commit d08c46e

Browse files
committed
feat: add conformance test for invalid AP2 signature
Why: - To verify that the server correctly validates AP2 mandates during checkout completion. - Ensures that invalid signatures result in a 400 Bad Request with the specific error code 'mandate_invalid_signature'. What: - Added 'test_ap2_mandate_invalid_signature' to 'Ap2MandateTest'. - Submits a completion request with a mandate string containing 'invalid_signature'. - Asserts that the response status is 400 and the error code matches the spec.
1 parent 598679c commit d08c46e

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

ap2_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,56 @@ def test_ap2_mandate_completion(self) -> None:
8888
msg="Checkout status not 'completed'",
8989
)
9090

91+
def test_ap2_mandate_invalid_signature(self) -> None:
92+
"""Test checkout completion failure with invalid AP2 signature.
93+
94+
Given a ready-to-complete checkout session,
95+
When a completion request is made with an invalid ap2 mandate signature,
96+
Then the request should fail with status 400 and error code
97+
'mandate_invalid_signature'.
98+
"""
99+
response_json = self.create_checkout_session()
100+
checkout_id = checkout.Checkout(**response_json).id
101+
102+
credential = token_credential_resp.TokenCredentialResponse(
103+
type="token", token="success_token"
104+
)
105+
instr = payment_instrument.PaymentInstrument(
106+
root=card_payment_instrument.CardPaymentInstrument(
107+
id="instr_1",
108+
brand="visa",
109+
last_digits="4242",
110+
handler_id="mock_payment_handler",
111+
handler_name="mock_payment_handler",
112+
type="card",
113+
credential=credential,
114+
)
115+
)
116+
payment_data = instr.root.model_dump(mode="json", exclude_none=True)
117+
118+
# Use trigger string for mock verification failure
119+
mandate = CheckoutMandate(root="header.payload.invalid_signature~kb_sig")
120+
ap2_data = Ap2CompleteRequest(checkout_mandate=mandate)
121+
122+
payment_payload = {
123+
"payment_data": payment_data,
124+
"risk_signals": {},
125+
"ap2": ap2_data.model_dump(mode="json", exclude_none=True),
126+
}
127+
128+
response = self.client.post(
129+
f"/checkout-sessions/{checkout_id}/complete",
130+
json=payment_payload,
131+
headers=integration_test_utils.get_headers(),
132+
)
133+
134+
self.assert_response_status(response, 400)
135+
self.assertEqual(
136+
response.json().get("code"),
137+
"mandate_invalid_signature",
138+
msg="Error code should be 'mandate_invalid_signature'",
139+
)
140+
91141

92142
if __name__ == "__main__":
93143
absltest.main()

0 commit comments

Comments
 (0)