fix: an out-of-range cart timing is dropped, never refuses the quote
DTF-WEB-9: a cart left open for about 6 hours sent upload_ms 23556292, above the 1 h limit, and POST /quotes answered 422: the customer could not pay. The timings only feed Números, so a value out of range is now dropped; an unknown field is still refused. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -253,17 +253,31 @@ class Item(StrictModel):
|
||||
raise ValueError('Resolution warning must be acknowledged')
|
||||
return self
|
||||
|
||||
TIMING_MAX = {'page_ms': 600000, 'upload_ms': 3600000, 'upload_bytes': 10**12,
|
||||
'analysis_ms': 3600000, 'montage_ms': 3600000}
|
||||
|
||||
|
||||
class ClientTimings(StrictModel):
|
||||
"""What the browser measured on the way to a quote (item 0.12).
|
||||
|
||||
They are only numbers for Números; a cart may send none of them, and the
|
||||
server never uses them to price or accept an order.
|
||||
server never uses them to price or accept an order: a value out of range
|
||||
(a cart left open for hours, DTF-WEB-9) is dropped, never refused.
|
||||
"""
|
||||
page_ms: int | None = Field(default=None, ge=0, le=600000)
|
||||
upload_ms: int | None = Field(default=None, ge=0, le=3600000)
|
||||
upload_bytes: int | None = Field(default=None, ge=0, le=10**12)
|
||||
analysis_ms: int | None = Field(default=None, ge=0, le=3600000)
|
||||
montage_ms: int | None = Field(default=None, ge=0, le=3600000)
|
||||
page_ms: int | None = None
|
||||
upload_ms: int | None = None
|
||||
upload_bytes: int | None = None
|
||||
analysis_ms: int | None = None
|
||||
montage_ms: int | None = None
|
||||
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def drop_out_of_range(cls, data):
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
return {name: value for name, value in data.items()
|
||||
if name not in TIMING_MAX or (isinstance(value, int) and not isinstance(value, bool)
|
||||
and 0 <= value <= TIMING_MAX[name])}
|
||||
|
||||
|
||||
class QuoteRequest(StrictModel):
|
||||
|
||||
@@ -31,5 +31,22 @@ class SpeedFiguresTests(unittest.TestCase):
|
||||
self.assertTrue(all(row['average'] is None and row['samples'] == 0 for row in rows))
|
||||
|
||||
|
||||
class ClientTimingsTests(unittest.TestCase):
|
||||
def test_a_value_out_of_range_is_dropped_not_refused(self):
|
||||
"""DTF-WEB-9: a cart left open for 6 h sent upload_ms 23556292 and
|
||||
the customer could not pay."""
|
||||
from app.core.models import ClientTimings
|
||||
timings = ClientTimings.model_validate({'upload_ms': 23556292, 'analysis_ms': 5000,
|
||||
'page_ms': -1, 'montage_ms': 1.5, 'upload_bytes': True})
|
||||
self.assertEqual(timings.model_dump(), {'page_ms': None, 'upload_ms': None, 'upload_bytes': None,
|
||||
'analysis_ms': 5000, 'montage_ms': None})
|
||||
|
||||
def test_an_unknown_field_is_still_refused(self):
|
||||
from pydantic import ValidationError
|
||||
from app.core.models import ClientTimings
|
||||
with self.assertRaises(ValidationError):
|
||||
ClientTimings.model_validate({'other_ms': 1})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user