Forward the lab 4 foundation and migrate the lab 5 pipeline to SQLAlchemy
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from uuid import uuid4
|
||||
import time
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy import select
|
||||
|
||||
from conftest import actor, keyed, make_chat, register
|
||||
from conftest import actor, keyed, make_chat, register, sql_connection
|
||||
from messenger.messaging.infrastructure.postgres import ChatRow
|
||||
|
||||
|
||||
async def test_outsider_gets_404_on_chat(client: AsyncClient) -> None:
|
||||
@@ -82,3 +85,36 @@ async def test_403_is_never_used(client: AsyncClient) -> None:
|
||||
]
|
||||
for path in paths:
|
||||
assert (await client.get(path, headers=actor(eve))).status_code != 403
|
||||
|
||||
|
||||
async def test_request_shape_is_checked_before_access(client: AsyncClient) -> None:
|
||||
alice = await register(client, "alice")
|
||||
eve = await register(client, "eve")
|
||||
chat = await make_chat(client, alice)
|
||||
for chat_id in (chat["id"], uuid4()):
|
||||
path = f"/api/v1/chats/{chat_id}/messages"
|
||||
unkeyed = await client.post(path, json={"text": "чужое"}, headers=actor(eve))
|
||||
extra = await client.post(path, json={"text": "x", "a": 1}, headers=keyed(eve))
|
||||
assert unkeyed.status_code == extra.status_code == 400
|
||||
assert unkeyed.json()["error"]["code"] == extra.json()["error"]["code"] == "invalid_request"
|
||||
blank = await client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages", json={"text": " "}, headers=keyed(eve)
|
||||
)
|
||||
assert blank.status_code == 404
|
||||
|
||||
|
||||
async def test_outsider_does_not_wait_for_members_lock(client: AsyncClient) -> None:
|
||||
alice = await register(client, "alice")
|
||||
eve = await register(client, "eve")
|
||||
chat = await make_chat(client, alice)
|
||||
async with sql_connection() as holder:
|
||||
await holder.execute(
|
||||
select(ChatRow.id).where(ChatRow.id == UUID(chat["id"])).with_for_update()
|
||||
)
|
||||
started = time.perf_counter()
|
||||
response = await client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages", json={"text": "чужое"}, headers=keyed(eve)
|
||||
)
|
||||
elapsed = time.perf_counter() - started
|
||||
assert response.status_code == 404
|
||||
assert elapsed < 0.5
|
||||
|
||||
Reference in New Issue
Block a user