Forward the lab 4 foundation and migrate the lab 5 pipeline to SQLAlchemy
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from hashlib import sha256
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from conftest import (
|
||||
PASSWORD,
|
||||
@@ -16,6 +19,7 @@ from conftest import (
|
||||
running_app,
|
||||
scalar,
|
||||
)
|
||||
from messenger.identity.infrastructure.postgres import SessionRow, UserRow
|
||||
|
||||
BASIC = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER = 'Bearer realm="messenger"'
|
||||
@@ -52,7 +56,7 @@ async def test_registration_response_hides_password(client: AsyncClient) -> None
|
||||
|
||||
async def test_password_is_stored_as_argon2id(client: AsyncClient) -> None:
|
||||
alice = await register(client, "alice")
|
||||
stored = await scalar("SELECT password_hash FROM users WHERE id = $1::uuid", alice["id"])
|
||||
stored = await scalar(select(UserRow.password_hash).where(UserRow.id == UUID(alice["id"])))
|
||||
assert stored.startswith("$argon2id$v=19$m=19456,t=2,p=1$")
|
||||
assert PASSWORD not in stored
|
||||
|
||||
@@ -60,10 +64,9 @@ async def test_password_is_stored_as_argon2id(client: AsyncClient) -> None:
|
||||
async def test_session_stores_only_token_digest(client: AsyncClient) -> None:
|
||||
alice = await register(client, "alice")
|
||||
token = actor(alice)["Authorization"].split()[1]
|
||||
sql = "SELECT count(*) FROM sessions WHERE token_hash = sha256(convert_to($1, 'UTF8'))"
|
||||
assert await scalar(sql, token) == 1
|
||||
raw = "SELECT count(*) FROM sessions WHERE encode(token_hash, 'escape') = $1"
|
||||
assert await scalar(raw, token) == 0
|
||||
rows = select(func.count()).select_from(SessionRow)
|
||||
assert await scalar(rows.where(SessionRow.token_hash == sha256(token.encode()).digest())) == 1
|
||||
assert await scalar(rows.where(SessionRow.token_hash == token.encode())) == 0
|
||||
|
||||
|
||||
async def test_wrong_password_and_unknown_user_look_the_same(client: AsyncClient) -> None:
|
||||
@@ -154,8 +157,8 @@ async def test_expired_sessions_are_purged_on_next_login() -> None:
|
||||
await login(client, body["username"])
|
||||
await asyncio.sleep(1.2)
|
||||
await login(client, body["username"])
|
||||
sql = "SELECT count(*) FROM sessions WHERE user_id = $1::uuid"
|
||||
assert await scalar(sql, user["id"]) == 1
|
||||
sql = select(func.count()).select_from(SessionRow).where(SessionRow.user_id == UUID(user["id"]))
|
||||
assert await scalar(sql) == 1
|
||||
|
||||
|
||||
async def test_chat_routes_require_bearer(client: AsyncClient) -> None:
|
||||
|
||||
Reference in New Issue
Block a user