forked from Universal-Commerce-Protocol/conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_test.py
More file actions
244 lines (203 loc) · 8.05 KB
/
validation_test.py
File metadata and controls
244 lines (203 loc) · 8.05 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
# 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.
"""Validation tests for the UCP SDK Server."""
from absl.testing import absltest
import integration_test_utils
from ucp_sdk.models.schemas.shopping import (
checkout_update_request as checkout_update_req,
)
from ucp_sdk.models.schemas.shopping import checkout as checkout
from ucp_sdk.models.schemas.shopping import payment_update_request
from ucp_sdk.models.schemas.shopping.payment import (
Payment,
)
from ucp_sdk.models.schemas.shopping.types import item_update_request
from ucp_sdk.models.schemas.shopping.types import line_item_update_request
# Rebuild models to resolve forward references
checkout.Checkout.model_rebuild(_types_namespace={"Payment": Payment})
class ValidationTest(integration_test_utils.IntegrationTestBase):
"""Tests for input validation and error handling.
Validated Paths:
- POST /checkout-sessions
- PUT /checkout-sessions/{id}
- POST /checkout-sessions/{id}/complete
"""
def test_out_of_stock(self) -> None:
"""Test validation for out-of-stock items.
Given a product with 0 inventory,
When a checkout creation request is made for this item,
Then the server should return a 400 Bad Request error indicating
insufficient stock.
"""
# Get out of stock item from config
out_of_stock_item = self.conformance_config.get(
"out_of_stock_item",
{"id": "out_of_stock_item_1", "title": "Out of Stock Item"},
)
create_payload = self.create_checkout_payload(
item_id=out_of_stock_item["id"],
title=out_of_stock_item["title"],
)
response = self.client.post(
self.get_shopping_url("/checkout-sessions"),
json=create_payload.model_dump(
mode="json", by_alias=True, exclude_none=True
),
headers=integration_test_utils.get_headers(),
)
self.assert_response_status(response, 400)
self.assertIn(
"Insufficient stock",
response.text,
msg="Expected 'Insufficient stock' message",
)
def test_update_inventory_validation(self) -> None:
"""Test that inventory validation is enforced on update.
Given an existing checkout session with a valid quantity,
When the line item quantity is updated to exceed available stock,
Then the server should return a 400 Bad Request error indicating
insufficient stock.
"""
response_json = self.create_checkout_session()
checkout_obj = checkout.Checkout(**response_json)
checkout_id = checkout_obj.id
# Update to excessive quantity (e.g. 10000)
item_update = item_update_request.ItemUpdateRequest(
id=checkout_obj.line_items[0].item.id,
title=checkout_obj.line_items[0].item.title,
)
line_item_update = line_item_update_request.LineItemUpdateRequest(
id=checkout_obj.line_items[0].id,
item=item_update,
quantity=10001,
)
payment_update = payment_update_request.PaymentUpdateRequest(
instruments=checkout_obj.payment.instruments,
handlers=[
h.model_dump(mode="json", exclude_none=True)
for h in checkout_obj.payment.instruments
],
)
update_payload = checkout_update_req.CheckoutUpdateRequest(
id=checkout_id,
currency=checkout_obj.currency,
line_items=[line_item_update],
payment=payment_update,
)
response = self.client.put(
self.get_shopping_url(f"/checkout-sessions/{checkout_id}"),
json=update_payload.model_dump(
mode="json", by_alias=True, exclude_none=True
),
headers=integration_test_utils.get_headers(),
)
self.assert_response_status(response, 400)
self.assertIn(
"stock", response.text.lower(), msg="Expected 'stock' message"
)
def test_product_not_found(self) -> None:
"""Test validation for non-existent products.
Given a request for a product ID that does not exist in the catalog,
When a checkout creation request is made,
Then the server should return a 400 Bad Request error indicating the product
was not found.
"""
non_existent_item = self.conformance_config.get(
"non_existent_item",
{"id": "non_existent_item_1", "title": "Non-existent Item"},
)
create_payload = self.create_checkout_payload(
item_id=non_existent_item["id"],
title=non_existent_item["title"],
)
response = self.client.post(
self.get_shopping_url("/checkout-sessions"),
json=create_payload.model_dump(
mode="json", by_alias=True, exclude_none=True
),
headers=integration_test_utils.get_headers(),
)
self.assert_response_status(response, 400)
self.assertIn(
"not found", response.text.lower(), msg="Expected 'not found' message"
)
def test_payment_failure(self) -> None:
"""Test handling of payment failures.
Given a checkout session ready for completion,
When a payment instrument with a known failing token ('fail_token') is
submitted,
Then the server should return a 402 Payment Required error.
"""
response_json = self.create_checkout_session(handlers=[])
checkout_id = checkout.Checkout(**response_json).id
# Use the helper to get valid structure, but request the failing instrument
# 'instr_fail' is loaded from payment_instruments.csv
payment_payload = integration_test_utils.get_valid_payment_payload(
instrument_id="instr_fail"
)
response = self.client.post(
self.get_shopping_url(f"/checkout-sessions/{checkout_id}/complete"),
json=payment_payload,
headers=integration_test_utils.get_headers(),
)
self.assert_response_status(response, 402)
def test_complete_without_fulfillment(self) -> None:
"""Test completion rejection when fulfillment is missing.
Given a newly created checkout session without fulfillment details,
When a completion request is submitted,
Then the server should return a 400 Bad Request error.
"""
response_json = self.create_checkout_session(select_fulfillment=False)
checkout_id = response_json["id"]
payment_payload = integration_test_utils.get_valid_payment_payload()
response = self.client.post(
self.get_shopping_url(f"/checkout-sessions/{checkout_id}/complete"),
json=payment_payload,
headers=integration_test_utils.get_headers(),
)
self.assert_response_status(response, 400)
self.assertIn(
"Fulfillment address and option must be selected",
response.text,
msg="Expected error message for missing fulfillment",
)
def test_structured_error_messages(self) -> None:
"""Test that error responses conform to the Message schema.
Given a request that triggers an error (e.g., out of stock),
When the server responds with an error code (400),
Then the response body should contain a structured 'detail' field describing
the error.
"""
# Get out of stock item from config
out_of_stock_item = self.conformance_config.get(
"out_of_stock_item",
{"id": "out_of_stock_item_1", "title": "Out of Stock Item"},
)
create_payload = self.create_checkout_payload(
item_id=out_of_stock_item["id"],
)
response = self.client.post(
self.get_shopping_url("/checkout-sessions"),
json=create_payload.model_dump(
mode="json", by_alias=True, exclude_none=True
),
headers=integration_test_utils.get_headers(),
)
self.assert_response_status(response, 400)
# Check for structured error
data = response.json()
self.assertTrue(data.get("detail"), "Error response missing 'detail' field")
self.assertIn("Insufficient stock", data["detail"])
if __name__ == "__main__":
absltest.main()