forked from mtusi2026/mtusi2026lab2
справление: U+0000 в строках сохраняется (bytea), DataError -> 400
This commit is contained in:
@@ -1,14 +1,3 @@
|
||||
"""Проверки на уровне базы данных. Запуск при запущенном стенде:
|
||||
|
||||
python student-tests/test_db.py
|
||||
|
||||
SQL выполняется в контейнере postgres через `docker compose exec postgres psql`.
|
||||
1. Откат составной операции: чат с несуществующим участником -> 400, и в базе
|
||||
не остаётся ни чата, ни участников, хотя строка чата успела вставиться.
|
||||
2. База сама не даёт записать сообщение от не-участника (внешний ключ).
|
||||
3. Пароль хранится как хеш Argon2id, а не как текст или быстрый хеш.
|
||||
4. Миграции: после перезапуска API ничего не применяется повторно.
|
||||
"""
|
||||
import time
|
||||
import uuid
|
||||
|
||||
@@ -26,14 +15,12 @@ def test_create_chat_rolls_back():
|
||||
title = "rollback-" + uuid.uuid4().hex[:8]
|
||||
inserted_before = int(sql_value("SELECT n_tup_ins FROM pg_stat_user_tables WHERE relname = 'chats'"))
|
||||
|
||||
# Второй участник не существует: INSERT чата пройдёт, INSERT участника упадёт на внешнем ключе.
|
||||
call("POST", "/api/v1/chats", 400, token=alice["token"],
|
||||
body={"title": title, "member_ids": [alice["id"], str(uuid.uuid4())]})
|
||||
|
||||
left = sql_value(f"SELECT count(*) FROM chats WHERE title = '{title}'")
|
||||
orphans = sql_value("SELECT count(*) FROM chats c WHERE NOT EXISTS "
|
||||
"(SELECT 1 FROM chat_members m WHERE m.chat_id = c.id)")
|
||||
# Статистика PostgreSQL считает и отменённые вставки; она обновляется с задержкой до ~1 с.
|
||||
inserted_after = inserted_before
|
||||
deadline = time.monotonic() + 15
|
||||
while inserted_after == inserted_before and time.monotonic() < deadline:
|
||||
@@ -48,7 +35,7 @@ def test_create_chat_rolls_back():
|
||||
|
||||
def test_message_needs_membership_in_db():
|
||||
code, _, error = psql("INSERT INTO messages (id, chat_id, sender_id, text) "
|
||||
f"VALUES ('{uuid.uuid4()}', '{uuid.uuid4()}', '{uuid.uuid4()}', 'direct insert')")
|
||||
f"VALUES ('{uuid.uuid4()}', '{uuid.uuid4()}', '{uuid.uuid4()}', convert_to('direct insert', 'UTF8'))")
|
||||
print(f"direct INSERT of a message from a non-member -> exit code {code}: {error.splitlines()[0] if error else ''}")
|
||||
check(code != 0 and "foreign key" in error, "database accepted a message from a non-member")
|
||||
print("PASS: the database itself rejects a message from a non-member")
|
||||
|
||||
Reference in New Issue
Block a user