47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
"""Ограничения на уровне БД: приложение — не единственный защитник данных."""
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from helpers import create_chat, create_user
|
|
|
|
|
|
async def test_duplicate_username_is_rejected_by_database(client, db_engine):
|
|
await create_user(client, username="unique_name")
|
|
|
|
with pytest.raises(IntegrityError):
|
|
async with db_engine.begin() as connection:
|
|
await connection.execute(
|
|
text(
|
|
"INSERT INTO users (id, username, display_name, password_hash) "
|
|
"VALUES (gen_random_uuid(), 'unique_name', 'Other', 'hash')"
|
|
)
|
|
)
|
|
|
|
|
|
async def test_message_requires_existing_chat(client, db_engine):
|
|
alice = await create_user(client)
|
|
|
|
with pytest.raises(IntegrityError):
|
|
async with db_engine.begin() as connection:
|
|
await connection.execute(
|
|
text(
|
|
"INSERT INTO messages (id, chat_id, sender_id, text) "
|
|
"VALUES (gen_random_uuid(), gen_random_uuid(), :sender, 'x')"
|
|
),
|
|
{"sender": alice["id"]},
|
|
)
|
|
|
|
|
|
async def test_duplicate_membership_is_rejected_by_database(client, db_engine):
|
|
alice = await create_user(client)
|
|
chat = await create_chat(client, alice)
|
|
|
|
with pytest.raises(IntegrityError):
|
|
async with db_engine.begin() as connection:
|
|
await connection.execute(
|
|
text("INSERT INTO chat_members (chat_id, user_id) VALUES (:c, :u)"),
|
|
{"c": chat["id"], "u": alice["id"]},
|
|
)
|