forked from Universal-Commerce-Protocol/conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_test.py
More file actions
265 lines (231 loc) · 8.79 KB
/
webhook_test.py
File metadata and controls
265 lines (231 loc) · 8.79 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
# Copyright 2026 UCP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for Webhook notifications in UCP SDK Server."""
import time
from absl.testing import absltest
import integration_test_utils
from ucp_sdk.models.schemas.shopping import fulfillment_resp
from ucp_sdk.models.schemas.shopping.payment_resp import (
PaymentResponse as Payment,
)
# Rebuild models to resolve forward references
fulfillment_resp.Checkout.model_rebuild(
_types_namespace={"PaymentResponse": Payment}
)
class WebhookTest(integration_test_utils.IntegrationTestBase):
"""Tests for Webhook notifications."""
def setUp(self) -> None:
"""Set up the webhook server and configuration."""
super().setUp()
port = integration_test_utils.FLAGS.mock_webhook_port
self.webhook_server = integration_test_utils.MockWebhookServer(port=port)
self.webhook_server.start()
self.webhook_url = (
f"http://localhost:{port}/webhooks/partners/test_partner/events/order"
)
def tearDown(self) -> None:
"""Stop the webhook server and clean up."""
self.webhook_server.stop()
super().tearDown()
def test_webhook_event_stream(self) -> None:
"""Test that the server sends order_placed and order_shipped events.
Given a mock webhook server is running,
When a checkout is completed with a webhook_url (via Agent Profile),
Then the server should send an 'order_placed' event.
When the order is subsequently shipped,
Then the server should send an 'order_shipped' event.
"""
# 1. Create checkout (webhook URL passed via UCP-Agent header)
checkout_data = self.create_checkout_session(headers=self.get_headers())
checkout_obj = fulfillment_resp.Checkout(**checkout_data)
checkout_id = checkout_obj.id
# 2. Complete Checkout
complete_response = self.complete_checkout_session(checkout_id)
order_id = complete_response["order"]["id"]
# 3. Trigger Shipping
headers = self.get_headers()
headers["Simulation-Secret"] = (
integration_test_utils.FLAGS.simulation_secret
)
ship_response = self.client.post(
f"/testing/simulate-shipping/{order_id}",
headers=headers,
)
self.assert_response_status(ship_response, 200)
# 4. Verify Webhook Events
# Poll for events to arrive (up to 2 seconds)
for _ in range(20):
if len(self.webhook_server.events) >= 2:
break
time.sleep(0.1)
events = self.webhook_server.events
self.assertGreaterEqual(
len(events),
2,
f"Expected at least 2 events, got {len(events)}",
)
# Verify order_placed event
placed_event = next(
(e for e in events if e["payload"]["event_type"] == "order_placed"),
None,
)
self.assertIsNotNone(placed_event, "Missing order_placed event")
self.assertEqual(placed_event["payload"]["checkout_id"], checkout_id)
self.assertEqual(placed_event["payload"]["order"]["id"], order_id)
# Verify order_shipped event
shipped_event = next(
(e for e in events if e["payload"]["event_type"] == "order_shipped"),
None,
)
self.assertIsNotNone(shipped_event, "Missing order_shipped event")
self.assertEqual(shipped_event["payload"]["checkout_id"], checkout_id)
self.assertEqual(shipped_event["payload"]["order"]["id"], order_id)
fulfillment_events = shipped_event["payload"]["order"]["fulfillment"].get(
"events", []
)
self.assertTrue(
any(e["type"] == "shipped" for e in fulfillment_events),
"order_shipped event did not contain shipment info in order data",
)
def test_webhook_order_address_known_customer(self) -> None:
"""Test that webhook contains correct address for known customer/address."""
buyer_info = {"fullName": "John Doe", "email": "john.doe@example.com"}
checkout_data = self.create_checkout_session(buyer=buyer_info)
checkout_obj = fulfillment_resp.Checkout(**checkout_data)
# Trigger fulfillment update to inject address
self.update_checkout_session(
checkout_obj, fulfillment={"methods": [{"type": "shipping"}]}
)
# Fetch to get injected destinations
response = self.client.get(
self.get_shopping_url(f"/checkout-sessions/{checkout_obj.id}"),
headers=self.get_headers(),
)
checkout_data = response.json()
checkout_obj = fulfillment_resp.Checkout(**checkout_data)
if (
checkout_obj.fulfillment
and checkout_obj.fulfillment.root.methods
and checkout_obj.fulfillment.root.methods[0].destinations
):
method = checkout_obj.fulfillment.root.methods[0]
dest_id = method.destinations[0].root.id
# Select destination first to calculate options
self.update_checkout_session(
checkout_obj,
fulfillment={
"methods": [{"type": "shipping", "selected_destination_id": dest_id}]
},
)
# Fetch again to get options
response = self.client.get(
self.get_shopping_url(f"/checkout-sessions/{checkout_obj.id}"),
headers=self.get_headers(),
)
checkout_obj = fulfillment_resp.Checkout(**response.json())
method = checkout_obj.fulfillment.root.methods[0]
if method.groups and method.groups[0].options:
option_id = method.groups[0].options[0].id
self.update_checkout_session(
checkout_obj,
fulfillment={
"methods": [
{
"type": "shipping",
"selected_destination_id": dest_id,
"groups": [{"selected_option_id": option_id}],
}
]
},
)
complete_response = self.complete_checkout_session(checkout_obj.id)
order_id = complete_response["order"]["id"]
for _ in range(20):
if len(self.webhook_server.events) >= 1:
break
time.sleep(0.1)
event = next(
(
e
for e in self.webhook_server.events
if e["payload"]["order"]["id"] == order_id
),
None,
)
self.assertIsNotNone(event)
expectations = event["payload"]["order"]["fulfillment"]["expectations"]
self.assertTrue(expectations)
self.assertEqual(expectations[0]["destination"]["address_country"], "US")
def test_webhook_order_address_new_address(self) -> None:
"""Test that webhook contains correct address when a new one is provided."""
buyer_info = {"fullName": "John Doe", "email": "john.doe@example.com"}
checkout_data = self.create_checkout_session(buyer=buyer_info)
checkout_obj = fulfillment_resp.Checkout(**checkout_data)
new_address = {
"id": "dest_new_webhook",
"address_country": "CA",
"postal_code": "M5V 2H1",
"street_address": "Webhook St",
}
# Send address to get options
fulfillment_payload = {
"methods": [
{
"type": "shipping",
"destinations": [new_address],
"selected_destination_id": "dest_new_webhook",
}
]
}
self.update_checkout_session(checkout_obj, fulfillment=fulfillment_payload)
# Fetch to get options
response = self.client.get(
self.get_shopping_url(f"/checkout-sessions/{checkout_obj.id}"),
headers=self.get_headers(),
)
checkout_obj = fulfillment_resp.Checkout(**response.json())
method = checkout_obj.fulfillment.root.methods[0]
if method.groups and method.groups[0].options:
option_id = method.groups[0].options[0].id
# Select option
fulfillment_payload["methods"][0]["groups"] = [
{"selected_option_id": option_id}
]
fulfillment_payload["methods"][0]["type"] = "shipping"
self.update_checkout_session(
checkout_obj, fulfillment=fulfillment_payload
)
complete_response = self.complete_checkout_session(checkout_obj.id)
order_id = complete_response["order"]["id"]
for _ in range(20):
if len(self.webhook_server.events) >= 1:
break
time.sleep(0.1)
event = next(
(
e
for e in self.webhook_server.events
if e["payload"]["order"]["id"] == order_id
),
None,
)
self.assertIsNotNone(event)
expectations = event["payload"]["order"]["fulfillment"]["expectations"]
self.assertTrue(expectations)
self.assertEqual(expectations[0]["destination"]["address_country"], "CA")
self.assertEqual(
expectations[0]["destination"]["street_address"], "Webhook St"
)
if __name__ == "__main__":
absltest.main()