73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from messenger.worker import validate_delivery
|
|
|
|
VALID = {
|
|
"schema_version": 1,
|
|
"job_id": "11111111-1111-4111-8111-111111111111",
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": "22222222-2222-4222-8222-222222222222",
|
|
"pipeline_version": 1,
|
|
"attempt": 1,
|
|
"created_at": "2026-11-06T00:00:00Z",
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"key,value",
|
|
[
|
|
("job_id", 123),
|
|
("job_id", []),
|
|
("attachment_id", {}),
|
|
("schema_version", True),
|
|
("pipeline_version", True),
|
|
("attempt", 0),
|
|
("created_at", 123),
|
|
("created_at", "2026-11-06"),
|
|
("created_at", "2026-11-06T12Z"),
|
|
("created_at", "2026-99-99T00:00:00Z"),
|
|
("traceparent", 123),
|
|
("tracestate", []),
|
|
],
|
|
)
|
|
def test_invalid_envelopes_are_bounded_validation_errors(key, value):
|
|
with pytest.raises(ValueError):
|
|
validate_delivery(json.dumps({**VALID, key: value}))
|
|
|
|
|
|
@pytest.mark.parametrize("body", [b"\xff", "null", "[]", "{}", "not json"])
|
|
def test_invalid_json(body):
|
|
with pytest.raises(ValueError):
|
|
validate_delivery(body)
|
|
|
|
|
|
def test_valid_trace_context():
|
|
body = {
|
|
**VALID,
|
|
"traceparent": "00-" + "a" * 32 + "-" + "b" * 16 + "-01",
|
|
"tracestate": "test=value",
|
|
}
|
|
assert validate_delivery(json.dumps(body)) == body
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"body", ["[" * 2000 + "]" * 2000, "[" * 20000 + "]" * 20000, b" " * 8193 + b"{}"]
|
|
)
|
|
def test_deep_and_oversize_envelopes_reject_safely(body):
|
|
with pytest.raises(ValueError):
|
|
validate_delivery(body)
|
|
|
|
|
|
def test_uuid_uppercase_is_normalized_before_storage():
|
|
value = "AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA"
|
|
parsed = validate_delivery(json.dumps({**VALID, "job_id": value, "attachment_id": value}))
|
|
assert parsed["job_id"] == parsed["attachment_id"] == value.lower()
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["job_id", "attachment_id"])
|
|
def test_uuid_urn_is_rejected_before_storage(field):
|
|
with pytest.raises(ValueError):
|
|
validate_delivery(json.dumps({**VALID, field: "urn:uuid:" + VALID[field]}))
|