test: verify level-four failures and history index
This commit is contained in:
@@ -14,12 +14,13 @@ import urllib.request
|
||||
from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
from src.db import database_url
|
||||
|
||||
|
||||
URL = os.environ.get("DATABASE_URL")
|
||||
URL = database_url() if os.environ.get("DATABASE_URL") or os.environ.get("DATABASE_URL_FILE") else None
|
||||
|
||||
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL or DATABASE_URL_FILE to a disposable PostgreSQL database")
|
||||
class AuthTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
@@ -81,7 +82,7 @@ class AuthTests(unittest.TestCase):
|
||||
status = response.status
|
||||
response_headers = response.headers
|
||||
data = response.read()
|
||||
assert status == expected, (method, path, expected, status)
|
||||
assert status == expected, (method, path.split("?", 1)[0], expected, status)
|
||||
request_id = response_headers.get("X-Request-Id")
|
||||
assert request_id
|
||||
payload = json.loads(data) if data else None
|
||||
@@ -140,6 +141,15 @@ class AuthTests(unittest.TestCase):
|
||||
self.assertEqual(headers.get("WWW-Authenticate"), 'Basic realm="messenger", charset="UTF-8"')
|
||||
self.call("POST", "/api/v1/auth/sessions", authorization="Basic " + encoded, body={"unexpected": True}, expected=400)
|
||||
|
||||
def test_huge_page_limit_is_bad_request(self):
|
||||
user, password = self.user()
|
||||
session, _ = self.login(user["username"], password)
|
||||
bearer = "Bearer " + session["token"]
|
||||
for path in ("/api/v1/chats", f"/api/v1/chats/{uuid4()}/messages"):
|
||||
self.call("GET", path + "?limit=" + "9" * 4301, authorization=bearer, expected=400)
|
||||
self.call("GET", "/api/v1/chats?limit=0001", authorization=bearer)
|
||||
self.call("GET", "/api/v1/chats?limit=%D9%A0", authorization=bearer, expected=400)
|
||||
|
||||
def test_z_short_ttl_survives_server_restart_without_extension(self):
|
||||
self.stop_server()
|
||||
self.start_server(ttl="2")
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Opt-in HTTP checks that stop only disposable Compose dependencies."""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client
|
||||
|
||||
|
||||
def compose(*args):
|
||||
subprocess.run(
|
||||
["docker", "compose", "--env-file", ".env", *args],
|
||||
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
|
||||
@unittest.skipUnless(os.environ.get("CHECK_COMPOSE_FAILURES") == "1", "opt in on the disposable Compose stack")
|
||||
class ComposeFailureTests(unittest.TestCase):
|
||||
def test_cache_bypass_database_refusal_and_revocation(self):
|
||||
client = Client(base_url="http://127.0.0.1:8080", timeout=15)
|
||||
actor = client.user("outage")
|
||||
client.call("GET", "/api/v1/users/me", actor=actor, schema="User") # fill User cache
|
||||
|
||||
try:
|
||||
compose("stop", "valkey")
|
||||
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
finally:
|
||||
compose("up", "-d", "--no-deps", "--wait", "valkey")
|
||||
|
||||
try:
|
||||
compose("stop", "postgres")
|
||||
client.call("GET", "/health/ready", expected=503)
|
||||
client.call("GET", "/api/v1/users/me", actor=actor, expected=503)
|
||||
finally:
|
||||
compose("up", "-d", "--no-deps", "--wait", "postgres")
|
||||
|
||||
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
client.call("DELETE", "/api/v1/auth/sessions/current", actor=actor, expected=204)
|
||||
try:
|
||||
compose("restart", "valkey")
|
||||
finally:
|
||||
compose("up", "-d", "--no-deps", "--wait", "valkey")
|
||||
client.call("GET", "/api/v1/users/me", actor=actor, expected=401)
|
||||
print("Valkey outage: 200; PostgreSQL outage: readiness/auth 503; revoked token after Valkey restart: 401")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -5,17 +5,20 @@ import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import patch
|
||||
from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
|
||||
from src.store import Store
|
||||
from src.db import database_url
|
||||
from src.store import Store, connection
|
||||
|
||||
|
||||
URL = os.environ.get("DATABASE_URL")
|
||||
URL = database_url() if os.environ.get("DATABASE_URL") or os.environ.get("DATABASE_URL_FILE") else None
|
||||
|
||||
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL or DATABASE_URL_FILE to a disposable PostgreSQL database")
|
||||
class DatabaseTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
@@ -80,6 +83,37 @@ class DatabaseTests(unittest.TestCase):
|
||||
self.store.chat(eve["id"], chat["id"])
|
||||
self.assertEqual(hidden.exception.status_code, 404)
|
||||
|
||||
def test_membership_failure_rolls_back_inserted_chat(self):
|
||||
alice, bob = self.user(), self.user()
|
||||
title = "rollback-" + uuid4().hex
|
||||
|
||||
class FailingConnection:
|
||||
def __init__(self, wrapped):
|
||||
self.wrapped = wrapped
|
||||
self.memberships = 0
|
||||
|
||||
def execute(self, query, params=None):
|
||||
if query.startswith("INSERT INTO memberships"):
|
||||
self.memberships += 1
|
||||
if self.memberships == 2:
|
||||
raise RuntimeError("synthetic second membership failure")
|
||||
return self.wrapped.execute(query, params)
|
||||
|
||||
@contextmanager
|
||||
def failing_connection():
|
||||
with connection() as conn:
|
||||
yield FailingConnection(conn)
|
||||
|
||||
with patch("src.store.connection", failing_connection):
|
||||
with self.assertRaisesRegex(RuntimeError, "synthetic second membership failure"):
|
||||
self.store.create_chat(alice["id"], title, [alice["id"], bob["id"]])
|
||||
with psycopg.connect(URL) as conn:
|
||||
self.assertEqual(conn.execute("SELECT count(*) FROM chats WHERE title=%s", (title,)).fetchone()[0], 0)
|
||||
self.assertEqual(conn.execute(
|
||||
"SELECT count(*) FROM memberships WHERE user_id IN (%s,%s)",
|
||||
(alice["id"], bob["id"]),
|
||||
).fetchone()[0], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user