feat: implement persistent messenger with PostgreSQL and Redis
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
\set ON_ERROR_STOP on
|
||||
BEGIN;
|
||||
\timing on
|
||||
|
||||
ANALYZE messages;
|
||||
|
||||
SELECT
|
||||
chat_id,
|
||||
COUNT(*) AS messages_count
|
||||
FROM messages
|
||||
GROUP BY chat_id
|
||||
ORDER BY messages_count DESC
|
||||
LIMIT 5;
|
||||
|
||||
SELECT id AS benchmark_chat_id
|
||||
FROM chats
|
||||
WHERE id IN (
|
||||
SELECT chat_id
|
||||
FROM messages
|
||||
GROUP BY chat_id
|
||||
ORDER BY COUNT(*) DESC
|
||||
LIMIT 1
|
||||
)
|
||||
LIMIT 1
|
||||
\gset
|
||||
|
||||
\echo 'TEST A: existing composite index'
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'benchmark_chat_id'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 50;
|
||||
|
||||
\echo 'TEST B: additional index'
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_only
|
||||
ON messages (chat_id);
|
||||
|
||||
ANALYZE messages;
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'benchmark_chat_id'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 50;
|
||||
|
||||
\echo 'Index sizes'
|
||||
|
||||
SELECT
|
||||
indexrelname,
|
||||
pg_size_pretty(pg_relation_size(indexrelid)) AS size
|
||||
FROM pg_stat_user_indexes
|
||||
WHERE relname = 'messages'
|
||||
ORDER BY indexrelname;
|
||||
|
||||
ROLLBACK;
|
||||
@@ -0,0 +1,40 @@
|
||||
\set ON_ERROR_STOP on
|
||||
\timing on
|
||||
|
||||
BEGIN;
|
||||
|
||||
DROP INDEX IF EXISTS idx_messages_chat_created_id;
|
||||
DROP INDEX IF EXISTS idx_messages_chat_id_only;
|
||||
|
||||
\echo 'WRITE WITHOUT SECONDARY INDEXES'
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
INSERT INTO messages (id, chat_id, sender_id, text)
|
||||
SELECT
|
||||
gen_random_uuid(),
|
||||
c.id,
|
||||
c.created_by,
|
||||
'Write benchmark without index'
|
||||
FROM chats c
|
||||
CROSS JOIN generate_series(1, 10000)
|
||||
LIMIT 10000;
|
||||
|
||||
ROLLBACK;
|
||||
|
||||
BEGIN;
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_only ON messages (chat_id);
|
||||
|
||||
\echo 'WRITE WITH EXISTING INDEXES'
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
INSERT INTO messages (id, chat_id, sender_id, text)
|
||||
SELECT
|
||||
gen_random_uuid(),
|
||||
c.id,
|
||||
c.created_by,
|
||||
'Write benchmark with index'
|
||||
FROM chats c
|
||||
CROSS JOIN generate_series(1, 10000)
|
||||
LIMIT 10000;
|
||||
|
||||
ROLLBACK;
|
||||
@@ -0,0 +1,120 @@
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import json
|
||||
import secrets
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
BASE = "http://" + "127.0.0.1:8080"
|
||||
PASSWORD = "StrongPassword123!"
|
||||
|
||||
|
||||
def call(method, path, data=None, authorization=None):
|
||||
headers = {}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if authorization:
|
||||
headers["Authorization"] = authorization
|
||||
|
||||
req = urllib.request.Request(
|
||||
BASE + path,
|
||||
data=json.dumps(data).encode() if data is not None else None,
|
||||
headers=headers,
|
||||
method=method,
|
||||
)
|
||||
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=15)
|
||||
except urllib.error.HTTPError as exc:
|
||||
response = exc
|
||||
|
||||
with response:
|
||||
raw = response.read()
|
||||
return response.status, json.loads(raw) if raw else None
|
||||
|
||||
|
||||
def register(username):
|
||||
return call("POST", "/api/v1/users", {
|
||||
"username": username,
|
||||
"display_name": "Concurrent User",
|
||||
"password": PASSWORD,
|
||||
})[0]
|
||||
|
||||
|
||||
print("1. Concurrent registration")
|
||||
|
||||
username = "race_" + secrets.token_hex(5)
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
||||
statuses = list(executor.map(register, [username] * 10))
|
||||
|
||||
assert statuses.count(201) == 1, statuses
|
||||
assert statuses.count(409) == 9, statuses
|
||||
print("PASS: one registration, nine conflicts")
|
||||
|
||||
|
||||
print("2. Incorrect password")
|
||||
|
||||
credentials = base64.b64encode(
|
||||
(username + ":WrongPassword123!").encode()
|
||||
).decode()
|
||||
|
||||
status, _ = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
authorization="Basic " + credentials,
|
||||
)
|
||||
|
||||
assert status == 401, status
|
||||
print("PASS: incorrect password rejected")
|
||||
|
||||
|
||||
print("3. Invalid Bearer token")
|
||||
|
||||
status, _ = call(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
authorization="Bearer invalid-token",
|
||||
)
|
||||
|
||||
assert status == 401, status
|
||||
print("PASS: invalid token rejected")
|
||||
for bad_token in ("é", "abc def", "", "x" * 513, "abc\tdef"):
|
||||
status, _ = call("GET", "/api/v1/users/me", authorization="Bearer " + bad_token)
|
||||
assert status == 401, (repr(bad_token), status)
|
||||
print("PASS: malformed and non-ASCII Bearer tokens rejected")
|
||||
|
||||
|
||||
print("4. Unknown chat member")
|
||||
|
||||
credentials = base64.b64encode(
|
||||
(username + ":" + PASSWORD).encode()
|
||||
).decode()
|
||||
|
||||
status, result = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
authorization="Basic " + credentials,
|
||||
)
|
||||
|
||||
assert status == 201, status
|
||||
token = result["token"]
|
||||
user_id = result["user"]["id"]
|
||||
|
||||
import uuid
|
||||
|
||||
status, _ = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
{
|
||||
"title": "Rollback test",
|
||||
"member_ids": [user_id, str(uuid.uuid4())],
|
||||
},
|
||||
authorization="Bearer " + token,
|
||||
)
|
||||
|
||||
assert status == 400, status
|
||||
print("PASS: unknown member rejected")
|
||||
|
||||
|
||||
print("\nALL ADVANCED API CHECKS PASSED")
|
||||
@@ -0,0 +1,149 @@
|
||||
import base64
|
||||
import json
|
||||
import secrets
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
BASE_URL = "http://" + "127.0.0.1:8080"
|
||||
|
||||
username = "test_" + secrets.token_hex(5)
|
||||
password = "StrongPassword123!"
|
||||
|
||||
def request(method, path, body=None, authorization=None):
|
||||
headers = {}
|
||||
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
body = json.dumps(body).encode("utf-8")
|
||||
|
||||
if authorization:
|
||||
headers["Authorization"] = authorization
|
||||
|
||||
req = urllib.request.Request(
|
||||
BASE_URL + path,
|
||||
data=body,
|
||||
headers=headers,
|
||||
method=method,
|
||||
)
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as response:
|
||||
data = response.read()
|
||||
return response.status, dict(response.headers), (
|
||||
json.loads(data) if data else None
|
||||
)
|
||||
except urllib.error.HTTPError as error:
|
||||
data = error.read()
|
||||
return error.code, dict(error.headers), (
|
||||
json.loads(data) if data else None
|
||||
)
|
||||
|
||||
|
||||
def check(name, actual, expected):
|
||||
result = "PASS" if actual == expected else "FAIL"
|
||||
print(f"{result}: {name}: {actual} (expected {expected})")
|
||||
if actual != expected:
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
print("1. Registration")
|
||||
|
||||
status, headers, user = request(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
{
|
||||
"username": username,
|
||||
"display_name": "Test User",
|
||||
"password": password,
|
||||
},
|
||||
)
|
||||
print("Registration status:", status)
|
||||
print("Registration headers:", headers)
|
||||
print("Registration body:", user)
|
||||
check("registration", status, 201)
|
||||
|
||||
assert "password" not in user
|
||||
assert "password_hash" not in user
|
||||
assert "X-Request-Id" in headers
|
||||
|
||||
print("User ID:", user["id"])
|
||||
|
||||
|
||||
print("\n2. Basic login")
|
||||
|
||||
credentials = base64.b64encode(
|
||||
f"{username}:{password}".encode()
|
||||
).decode()
|
||||
|
||||
status, headers, session = request(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
authorization="Basic " + credentials,
|
||||
)
|
||||
check("login", status, 201)
|
||||
|
||||
token = session["token"]
|
||||
assert session["token_type"] == "Bearer"
|
||||
assert len(token) >= 40
|
||||
|
||||
|
||||
print("\n3. Access with Bearer token")
|
||||
|
||||
status, headers, me = request(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
authorization="Bearer " + token,
|
||||
)
|
||||
check("authorized request", status, 200)
|
||||
assert me["id"] == user["id"]
|
||||
|
||||
|
||||
print("\n4. Access without token")
|
||||
|
||||
status, headers, body = request(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
)
|
||||
check("missing token", status, 401)
|
||||
assert any(
|
||||
key.lower() == "www-authenticate"
|
||||
and value == 'Bearer realm="messenger"'
|
||||
for key, value in headers.items()
|
||||
), f"Неверный WWW-Authenticate: {headers}"
|
||||
|
||||
|
||||
print("\n5. X-User-Id cannot authorize")
|
||||
|
||||
req = urllib.request.Request(
|
||||
BASE_URL + "/api/v1/users/me",
|
||||
headers={"X-User-Id": user["id"]},
|
||||
)
|
||||
|
||||
try:
|
||||
urllib.request.urlopen(req, timeout=10)
|
||||
raise AssertionError("X-User-Id unexpectedly authorized")
|
||||
except urllib.error.HTTPError as error:
|
||||
check("X-User-Id rejected", error.code, 401)
|
||||
|
||||
|
||||
print("\n6. Logout")
|
||||
|
||||
status, headers, body = request(
|
||||
"DELETE",
|
||||
"/api/v1/auth/sessions/current",
|
||||
authorization="Bearer " + token,
|
||||
)
|
||||
check("logout", status, 204)
|
||||
|
||||
|
||||
print("\n7. Reuse revoked token")
|
||||
|
||||
status, headers, body = request(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
authorization="Bearer " + token,
|
||||
)
|
||||
check("revoked token rejected", status, 401)
|
||||
|
||||
|
||||
print("\nALL AUTH CHECKS PASSED")
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Backup/restore в новый изолированный контейнер и новый volume.
|
||||
|
||||
Запускать без параллельных писателей. Backup остаётся в backups/ (игнорируется
|
||||
Git), контейнер после проверки останавливается, volume сохраняется.
|
||||
"""
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def run(args, **kwargs):
|
||||
return subprocess.run(args, check=True, **kwargs)
|
||||
|
||||
|
||||
def main():
|
||||
suffix = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ') + '-' + uuid4().hex[:6]
|
||||
container = 'lab2-audit-restore-' + suffix.lower()
|
||||
volume = container + '-data'
|
||||
source = ['docker', 'compose', 'exec', '-T', 'postgres']
|
||||
target = ['docker', 'exec', '-i', container]
|
||||
query = """SELECT 'users', count(*) FROM users
|
||||
UNION ALL SELECT 'chats', count(*) FROM chats
|
||||
UNION ALL SELECT 'chat_members', count(*) FROM chat_members
|
||||
UNION ALL SELECT 'messages', count(*) FROM messages
|
||||
UNION ALL SELECT 'sessions', count(*) FROM sessions ORDER BY 1;
|
||||
SELECT id, md5(text) FROM messages ORDER BY id LIMIT 5;"""
|
||||
|
||||
def summary(prefix):
|
||||
return run(prefix + ['psql', '-X', '-v', 'ON_ERROR_STOP=1', '-U', 'messenger',
|
||||
'-d', 'messenger', '-Atc', query],
|
||||
capture_output=True, text=True).stdout
|
||||
|
||||
before = summary(source)
|
||||
Path('backups').mkdir(exist_ok=True)
|
||||
backup = Path('backups') / (suffix + '.dump')
|
||||
with backup.open('xb') as out:
|
||||
backup.chmod(0o600)
|
||||
run(source + ['pg_dump', '-U', 'messenger', '-d', 'messenger', '-Fc'], stdout=out)
|
||||
run(['docker', 'run', '-d', '--name', container, '--network', 'none',
|
||||
'--mount', 'type=volume,src=' + volume + ',dst=/var/lib/postgresql/data',
|
||||
'-e', 'POSTGRES_HOST_AUTH_METHOD=trust', '-e', 'POSTGRES_USER=messenger',
|
||||
'-e', 'POSTGRES_DB=messenger', 'postgres:16.8-bookworm'], capture_output=True)
|
||||
try:
|
||||
for _ in range(60):
|
||||
ready = subprocess.run(target + ['pg_isready', '-U', 'messenger', '-d', 'messenger'],
|
||||
capture_output=True)
|
||||
if ready.returncode == 0:
|
||||
break
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise AssertionError('restore PostgreSQL did not start')
|
||||
start = time.monotonic()
|
||||
with backup.open('rb') as src:
|
||||
run(target + ['pg_restore', '--exit-on-error', '-U', 'messenger',
|
||||
'-d', 'messenger'], stdin=src)
|
||||
elapsed = time.monotonic() - start
|
||||
restored = summary(target)
|
||||
after = summary(source)
|
||||
assert before == after, 'source changed during backup; retry without writers'
|
||||
assert before == restored, 'restored counts or samples differ'
|
||||
evidence = {
|
||||
'date_utc': suffix, 'container': container, 'volume': volume,
|
||||
'network': 'none', 'backup': str(backup),
|
||||
'pg_restore_seconds': round(elapsed, 3),
|
||||
'source_counts_and_sample_md5': before.splitlines(),
|
||||
'restored_counts_and_sample_md5': restored.splitlines(),
|
||||
'result': 'PASS',
|
||||
'limitation': 'counts for all five tables and five message id/text hashes; not full RTO or full DB checksum',
|
||||
}
|
||||
Path('evidence/backup_restore.json').write_text(json.dumps(evidence, indent=2) + '\n')
|
||||
print(json.dumps(evidence, indent=2))
|
||||
finally:
|
||||
run(['docker', 'stop', container], capture_output=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,99 @@
|
||||
import base64
|
||||
import json
|
||||
import secrets
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
BASE = "http://" + "127.0.0.1:8080"
|
||||
username = "failure_" + secrets.token_hex(4)
|
||||
password = "StrongPassword123!"
|
||||
|
||||
|
||||
def request(method, path, body=None, authorization=None):
|
||||
headers = {}
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if authorization:
|
||||
headers["Authorization"] = authorization
|
||||
|
||||
req = urllib.request.Request(
|
||||
BASE + path,
|
||||
data=json.dumps(body).encode() if body is not None else None,
|
||||
headers=headers,
|
||||
method=method,
|
||||
)
|
||||
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=20)
|
||||
except urllib.error.HTTPError as exc:
|
||||
response = exc
|
||||
|
||||
with response:
|
||||
raw = response.read()
|
||||
return response.status, json.loads(raw) if raw else None
|
||||
|
||||
|
||||
status, _ = request("POST", "/api/v1/users", {
|
||||
"username": username,
|
||||
"display_name": "Failure Test",
|
||||
"password": password,
|
||||
})
|
||||
assert status == 201, status
|
||||
|
||||
basic = base64.b64encode(
|
||||
(username + ":" + password).encode()
|
||||
).decode()
|
||||
|
||||
status, result = request(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
authorization="Basic " + basic,
|
||||
)
|
||||
assert status == 201, status
|
||||
|
||||
bearer = "Bearer " + result["token"]
|
||||
|
||||
status, _ = request(
|
||||
"GET", "/api/v1/users/me", authorization=bearer
|
||||
)
|
||||
assert status == 200, status
|
||||
|
||||
print("PASS: token works before database failure")
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
["docker", "compose", "stop", "postgres"],
|
||||
check=True,
|
||||
)
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
status, _ = request(
|
||||
"GET", "/api/v1/users/me", authorization=bearer
|
||||
)
|
||||
|
||||
assert status == 503, status
|
||||
print("PASS: database failure does not grant access:", status)
|
||||
|
||||
status, _ = request("GET", "/health/ready")
|
||||
assert status == 503, status
|
||||
|
||||
finally:
|
||||
subprocess.run(
|
||||
["docker", "compose", "start", "postgres"],
|
||||
check=True,
|
||||
)
|
||||
|
||||
for _ in range(30):
|
||||
try:
|
||||
status, _ = request("GET", "/api/v1/users/me", authorization=bearer)
|
||||
if status == 200:
|
||||
break
|
||||
except (OSError, TimeoutError):
|
||||
pass
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise AssertionError("PostgreSQL did not recover")
|
||||
print("ALL DATABASE FAILURE CHECKS PASSED; original session recovered")
|
||||
@@ -0,0 +1,179 @@
|
||||
import base64
|
||||
import json
|
||||
import secrets
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
BASE_URL = "http://" + "127.0.0.1:8080"
|
||||
|
||||
|
||||
def request(method, path, body=None, token=None, basic=None):
|
||||
headers = {}
|
||||
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
|
||||
if token is not None:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
|
||||
if basic is not None:
|
||||
credentials = base64.b64encode(
|
||||
basic.encode("utf-8")
|
||||
).decode("ascii")
|
||||
headers["Authorization"] = "Basic " + credentials
|
||||
|
||||
req = urllib.request.Request(
|
||||
BASE_URL + path,
|
||||
data=json.dumps(body).encode("utf-8") if body is not None else None,
|
||||
headers=headers,
|
||||
method=method,
|
||||
)
|
||||
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=15)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
|
||||
with response:
|
||||
raw = response.read()
|
||||
data = json.loads(raw) if raw else None
|
||||
return response.status, data
|
||||
|
||||
|
||||
def check(label, actual, expected):
|
||||
if actual != expected:
|
||||
raise AssertionError(
|
||||
f"{label}: expected {expected}, got {actual}"
|
||||
)
|
||||
print(f"PASS: {label}: {actual}")
|
||||
|
||||
|
||||
password = "StrongPassword123!"
|
||||
suffix = secrets.token_hex(4)
|
||||
|
||||
print("\n1. Register two users")
|
||||
|
||||
users = []
|
||||
|
||||
for name in ["alice_" + suffix, "bob_" + suffix]:
|
||||
status, user = request(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
{
|
||||
"username": name,
|
||||
"display_name": name,
|
||||
"password": password,
|
||||
},
|
||||
)
|
||||
check("register " + name, status, 201)
|
||||
users.append(user)
|
||||
|
||||
|
||||
print("\n2. Login")
|
||||
|
||||
tokens = []
|
||||
|
||||
for user in users:
|
||||
status, result = request(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
basic=user["username"] + ":" + password,
|
||||
)
|
||||
check("login " + user["username"], status, 201)
|
||||
tokens.append(result["token"])
|
||||
|
||||
|
||||
print("\n3. Create chat")
|
||||
|
||||
status, chat = request(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
{
|
||||
"title": "PostgreSQL test chat",
|
||||
"member_ids": [users[0]["id"], users[1]["id"]],
|
||||
},
|
||||
token=tokens[0],
|
||||
)
|
||||
|
||||
check("create chat", status, 201)
|
||||
chat_id = chat["id"]
|
||||
|
||||
print("Chat ID:", chat_id)
|
||||
|
||||
|
||||
print("\n4. Check chat visibility")
|
||||
|
||||
for token in tokens:
|
||||
status, result = request(
|
||||
"GET",
|
||||
"/api/v1/chats/" + chat_id,
|
||||
token=token,
|
||||
)
|
||||
check("get chat", status, 200)
|
||||
|
||||
|
||||
print("\n5. Send messages")
|
||||
|
||||
for index in range(5):
|
||||
status, message = request(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat_id + "/messages",
|
||||
{"text": f"Message number {index + 1}"},
|
||||
token=tokens[index % 2],
|
||||
)
|
||||
check("send message", status, 201)
|
||||
|
||||
|
||||
print("\n6. Read first page")
|
||||
|
||||
status, page1 = request(
|
||||
"GET",
|
||||
"/api/v1/chats/" + chat_id + "/messages?limit=2",
|
||||
token=tokens[0],
|
||||
)
|
||||
|
||||
check("first page", status, 200)
|
||||
|
||||
assert len(page1["items"]) == 2
|
||||
assert page1["next_cursor"] is not None
|
||||
|
||||
|
||||
print("\n7. Read second page")
|
||||
|
||||
cursor = page1["next_cursor"]
|
||||
|
||||
status, page2 = request(
|
||||
"GET",
|
||||
"/api/v1/chats/" + chat_id + "/messages?limit=2&cursor=" + cursor,
|
||||
token=tokens[0],
|
||||
)
|
||||
|
||||
check("second page", status, 200)
|
||||
|
||||
assert len(page2["items"]) == 2
|
||||
|
||||
first_ids = {item["id"] for item in page1["items"]}
|
||||
second_ids = {item["id"] for item in page2["items"]}
|
||||
|
||||
assert not first_ids.intersection(second_ids)
|
||||
|
||||
|
||||
print("\n8. Check chat list")
|
||||
|
||||
status, result = request(
|
||||
"GET",
|
||||
"/api/v1/chats",
|
||||
token=tokens[0],
|
||||
)
|
||||
|
||||
check("list chats", status, 200)
|
||||
|
||||
assert any(item["id"] == chat_id for item in result["items"])
|
||||
|
||||
|
||||
print("\n9. Check PostgreSQL persistence")
|
||||
|
||||
print("Created chat:", chat_id)
|
||||
print("Created messages: 5")
|
||||
|
||||
print("\nALL MESSENGER CHECKS PASSED")
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Миграция с нуля и повторно, в изолированной схеме с ROLLBACK."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from uuid import uuid4
|
||||
|
||||
schema = 'audit_' + uuid4().hex
|
||||
migration = Path('db/migrations/001_initial.sql').read_text()
|
||||
sql = f'BEGIN; CREATE SCHEMA {schema}; SET LOCAL search_path TO {schema};\n'
|
||||
sql += migration + '\n' + migration
|
||||
sql += "\nSELECT count(*) = 5 AS five_tables FROM information_schema.tables WHERE table_schema = '" + schema + "';\nROLLBACK;\n"
|
||||
result = subprocess.run(['docker', 'compose', 'exec', '-T', 'postgres', 'psql',
|
||||
'-v', 'ON_ERROR_STOP=1', '-U', 'messenger', '-d', 'messenger', '-At'],
|
||||
input=sql, text=True, capture_output=True, check=True)
|
||||
assert 't' in result.stdout.splitlines(), result.stdout
|
||||
print('PASS: migration creates five tables from zero and reapplies without errors; rolled back')
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверяет заполнение кеша, fallback, запись и logout при отказе Redis."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, check
|
||||
|
||||
|
||||
def compose(*args, **kwargs):
|
||||
return subprocess.run(['docker', 'compose', *args], check=True, **kwargs)
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, bob, _, chat = c.bootstrap()
|
||||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||||
key = 'session:' + hashlib.sha256(alice['token'].encode('ascii')).hexdigest()
|
||||
# Хеш ключа передаётся через stdin и не выводится.
|
||||
result = compose('exec', '-T', 'redis', 'redis-cli', '-x', 'EXISTS',
|
||||
input=key, text=True, capture_output=True)
|
||||
check(result.stdout.strip() == '1', 'session profile was not cached')
|
||||
result = compose('exec', '-T', 'redis', 'redis-cli', '-x', 'TTL',
|
||||
input=key, text=True, capture_output=True)
|
||||
check(0 < int(result.stdout) <= 3600, 'cache TTL missing')
|
||||
print('PASS: Redis contains session profile with TTL')
|
||||
try:
|
||||
compose('stop', 'redis')
|
||||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||||
message = c.message(alice, chat, 'Redis fallback check')['json']
|
||||
page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages',
|
||||
actor=bob, schema='MessagePage')['json']
|
||||
check(any(m['id'] == message['id'] for m in page['items']), 'fallback lost message')
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
print('PASS: PostgreSQL read/write and logout work without Redis')
|
||||
finally:
|
||||
compose('up', '-d', '--wait', 'redis')
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
c.call('GET', '/api/v1/users/me', actor=bob, schema='User')
|
||||
print('PASS: stale cache cannot revive revoked session; Redis recovered')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Запуск: docker compose exec -T api python < scripts/check_transactions.py.
|
||||
|
||||
Принудительная ошибка после записи первого участника доказывает rollback
|
||||
всего чата. Созданный синтетический пользователь остаётся в базе.
|
||||
"""
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import patch
|
||||
from uuid import uuid4
|
||||
|
||||
from src import chat_repository
|
||||
from src.auth import hash_password
|
||||
from src.database import get_connection, open_database, close_database
|
||||
from src.user_repository import create_user
|
||||
|
||||
|
||||
open_database()
|
||||
try:
|
||||
user = create_user('rollback_' + uuid4().hex[:12], 'Rollback check',
|
||||
hash_password('SyntheticPassword123!'))
|
||||
title = 'rollback-' + uuid4().hex
|
||||
|
||||
class FailingCursor:
|
||||
def __init__(self, cursor):
|
||||
self.cursor = cursor
|
||||
|
||||
def execute(self, *args):
|
||||
return self.cursor.execute(*args)
|
||||
|
||||
def fetchall(self):
|
||||
return self.cursor.fetchall()
|
||||
|
||||
def fetchone(self):
|
||||
return self.cursor.fetchone()
|
||||
|
||||
def executemany(self, query, rows):
|
||||
self.cursor.execute(query, rows[0])
|
||||
raise RuntimeError('injected membership failure')
|
||||
|
||||
class Connection:
|
||||
def __init__(self, conn):
|
||||
self.conn = conn
|
||||
|
||||
def transaction(self):
|
||||
return self.conn.transaction()
|
||||
|
||||
@contextmanager
|
||||
def cursor(self):
|
||||
with self.conn.cursor() as cur:
|
||||
yield FailingCursor(cur)
|
||||
|
||||
@contextmanager
|
||||
def failing_connection():
|
||||
with get_connection() as conn:
|
||||
yield Connection(conn)
|
||||
|
||||
with patch.object(chat_repository, 'get_connection', failing_connection):
|
||||
try:
|
||||
chat_repository.create_chat(user['id'], title, [user['id']])
|
||||
except RuntimeError as exc:
|
||||
assert str(exc) == 'injected membership failure'
|
||||
else:
|
||||
raise AssertionError('injected error was not raised')
|
||||
with get_connection() as conn:
|
||||
assert conn.execute('SELECT count(*) AS n FROM chats WHERE title = %s',
|
||||
(title,)).fetchone()['n'] == 0
|
||||
assert conn.execute('SELECT count(*) AS n FROM chat_members WHERE user_id = %s',
|
||||
(user['id'],)).fetchone()['n'] == 0
|
||||
print('PASS: failure after first member insert rolls back chat and membership')
|
||||
finally:
|
||||
close_database()
|
||||
@@ -0,0 +1,69 @@
|
||||
import base64
|
||||
import json
|
||||
import secrets
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
BASE = "http://" + "127.0.0.1:8081"
|
||||
username = "ttl_" + secrets.token_hex(4)
|
||||
password = "StrongPassword123!"
|
||||
|
||||
|
||||
def call(method, path, data=None, authorization=None):
|
||||
headers = {}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if authorization:
|
||||
headers["Authorization"] = authorization
|
||||
|
||||
req = urllib.request.Request(
|
||||
BASE + path,
|
||||
data=json.dumps(data).encode() if data is not None else None,
|
||||
headers=headers,
|
||||
method=method,
|
||||
)
|
||||
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=15)
|
||||
except urllib.error.HTTPError as exc:
|
||||
response = exc
|
||||
|
||||
with response:
|
||||
raw = response.read()
|
||||
return response.status, json.loads(raw) if raw else None
|
||||
|
||||
|
||||
status, _ = call("POST", "/api/v1/users", {
|
||||
"username": username,
|
||||
"display_name": "TTL Test",
|
||||
"password": password,
|
||||
})
|
||||
assert status == 201, status
|
||||
|
||||
basic = base64.b64encode(
|
||||
(username + ":" + password).encode()
|
||||
).decode()
|
||||
|
||||
status, result = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
authorization="Basic " + basic,
|
||||
)
|
||||
assert status == 201, status
|
||||
|
||||
bearer = "Bearer " + result["token"]
|
||||
|
||||
status, _ = call(
|
||||
"GET", "/api/v1/users/me", authorization=bearer
|
||||
)
|
||||
assert status == 200, status
|
||||
|
||||
time.sleep(3)
|
||||
|
||||
status, _ = call(
|
||||
"GET", "/api/v1/users/me", authorization=bearer
|
||||
)
|
||||
assert status == 401, status
|
||||
|
||||
print("PASS: session expires after TTL")
|
||||
@@ -0,0 +1,36 @@
|
||||
\set ON_ERROR_STOP on
|
||||
\timing on
|
||||
|
||||
SELECT chat_id
|
||||
FROM messages
|
||||
GROUP BY chat_id
|
||||
ORDER BY COUNT(*) DESC
|
||||
LIMIT 1
|
||||
\gset
|
||||
|
||||
\echo 'COMPOSITE INDEX'
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'chat_id'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 50;
|
||||
|
||||
BEGIN;
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_only ON messages (chat_id);
|
||||
|
||||
DROP INDEX IF EXISTS idx_messages_chat_created_id;
|
||||
|
||||
\echo 'SINGLE COLUMN INDEX'
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'chat_id'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 50;
|
||||
|
||||
ROLLBACK;
|
||||
|
||||
\echo 'ORIGINAL INDEX RESTORED'
|
||||
@@ -0,0 +1,15 @@
|
||||
\set ON_ERROR_STOP on
|
||||
SELECT chat_id FROM messages GROUP BY chat_id ORDER BY count(*) DESC LIMIT 1
|
||||
\gset
|
||||
SELECT created_at AS cursor_time, id AS cursor_id FROM messages
|
||||
WHERE chat_id = :'chat_id' ORDER BY created_at, id OFFSET 80000 LIMIT 1
|
||||
\gset
|
||||
BEGIN;
|
||||
SET LOCAL plan_cache_mode = force_generic_plan;
|
||||
PREPARE history(uuid, timestamptz, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id LIMIT $4;
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
EXECUTE history(:'chat_id', :'cursor_time', :'cursor_id', 51);
|
||||
ROLLBACK;
|
||||
@@ -0,0 +1,42 @@
|
||||
\set ON_ERROR_STOP on
|
||||
BEGIN;
|
||||
DO $$
|
||||
BEGIN
|
||||
IF (SELECT count(*) FROM chats c WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id AND cm.user_id = c.created_by
|
||||
)) < 2 THEN
|
||||
RAISE EXCEPTION 'Need at least two chats with creator membership';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
WITH selected AS (
|
||||
SELECT
|
||||
c.id AS chat_id,
|
||||
c.created_by AS sender_id,
|
||||
ROW_NUMBER() OVER (ORDER BY c.created_at, c.id) AS rn
|
||||
FROM chats c
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
AND cm.user_id = c.created_by
|
||||
)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT 2
|
||||
)
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT
|
||||
gen_random_uuid(),
|
||||
s.chat_id,
|
||||
s.sender_id,
|
||||
'Benchmark message ' || g.n,
|
||||
NOW() - INTERVAL '1 day' + g.n * INTERVAL '1 millisecond'
|
||||
FROM selected s
|
||||
CROSS JOIN LATERAL generate_series(
|
||||
1,
|
||||
CASE WHEN s.rn = 1 THEN 90000 ELSE 10000 END
|
||||
) AS g(n);
|
||||
|
||||
COMMIT;
|
||||
|
||||
ANALYZE messages;
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
# Все хранилища сохраняют именованные volumes. API запускается после готовности БД.
|
||||
docker compose restart postgres redis
|
||||
docker compose up -d --wait postgres redis
|
||||
docker compose restart api
|
||||
docker compose up -d --wait api
|
||||
Reference in New Issue
Block a user