Files
mtusi2026lab3/student-tests/cursor_validation.py
T

33 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Проверка диапазона дат cursor: некорректная дата должна давать 400, а не 500."""
import base64
import json
import sys
import uuid
from pathlib import Path
from urllib.parse import quote
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client, arguments, run
def main():
args = arguments(__doc__)
client = Client(args.base_url, ca_file=args.ca_file)
alice, _bob, _eve, chat = client.bootstrap()
checks = 0
for kind, scope, path in (
("messages", chat["id"], "/api/v1/chats/" + chat["id"] + "/messages"),
("chats", alice["id"], "/api/v1/chats"),
):
for timestamp, expected in ((-10**17, 400), (-62135596800000001, 400), (0, 200)):
payload = {"k": kind, "s": scope, "t": timestamp, "i": str(uuid.uuid4())}
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
client.call("GET", path + "?cursor=" + quote(cursor, safe=""), expected, actor=alice)
checks += 1
print("PASS: %d cursor date boundary checks; invalid dates return 400." % checks)
if __name__ == "__main__":
run(main)