ЛР2: PostgreSQL + Valkey, сессии Basic/Bearer, миграции, compose, тесты, отчёт

This commit is contained in:
2026-10-15 19:17:22 +03:00
parent 65003b6119
commit 6808ed8c98
44 changed files with 2370 additions and 19 deletions
+16
View File
@@ -0,0 +1,16 @@
Container mtusi2026lab2-valkey-1 Stopping
Container mtusi2026lab2-valkey-1 Stopped
Container mtusi2026lab2-valkey-1 Starting
Container mtusi2026lab2-valkey-1 Started
Container mtusi2026lab2-postgres-1 Stopping
Container mtusi2026lab2-postgres-1 Stopped
Container mtusi2026lab2-postgres-1 Starting
Container mtusi2026lab2-postgres-1 Started
baseline: /users/me = 200
stopping valkey ...
valkey down: /health/ready = 200, /users/me = 200 (cache bypassed)
valkey restored: /users/me = 200
stopping postgres ...
postgres down: /health/ready = 503, /users/me = 503, anonymous = 503
postgres restored: /users/me = 200 (session survived)
PASS: graceful degradation and recovery without trusting the cache/anonymous user
+3
View File
@@ -0,0 +1,3 @@
# Expiry with a short SESSION_TTL_SECONDS=2 on a throwaway stack
$ sh scripts/check-expiry.sh
PASS: session expired after ~2s (before=200, after=401)
+47
View File
@@ -0,0 +1,47 @@
# Grade 4: keyset pagination uses the composite index on >=10k messages
# dataset
$ docker compose exec -T postgres psql -U messenger -d messenger -c select count(*) as messages, pg_size_pretty(pg_total_relation_size('messages')) as size from messages;
messages | size
----------+---------
10343 | 2568 kB
(1 row)
# first page (cursor before history)
$ docker compose exec -T postgres psql -U messenger -d messenger -c EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d' AND (created_at, id) > ('1970-01-01T00:00:00Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid) ORDER BY created_at, id LIMIT 50;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.29..9.97 rows=50 width=81) (actual time=0.015..0.071 rows=50 loops=1)
Buffers: shared hit=52
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.29..645.61 rows=3333 width=81) (actual time=0.015..0.067 rows=50 loops=1)
Index Cond: ((chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d'::uuid) AND (ROW(created_at, id) > ROW('1970-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Buffers: shared hit=52
Planning:
Buffers: shared hit=106
Planning Time: 0.248 ms
Execution Time: 0.098 ms
(9 rows)
# deep page (cursor at offset 5000) - cost stays flat
$ docker compose exec -T postgres psql -U messenger -d messenger -c EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d' AND (created_at, id) > (SELECT created_at, id FROM messages WHERE chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d' ORDER BY created_at, id OFFSET 5000 LIMIT 1) ORDER BY created_at, id LIMIT 50;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=186.95..209.67 rows=50 width=81) (actual time=0.620..0.621 rows=0 loops=1)
Buffers: shared hit=35
InitPlan 1 (returns $0,$1)
-> Limit (cost=186.61..186.67 rows=1 width=24) (actual time=0.607..0.608 rows=0 loops=1)
Buffers: shared hit=35
-> Index Only Scan using messages_chat_created_id_idx on messages messages_1 (cost=0.29..186.61 rows=3333 width=24) (actual time=0.024..0.511 rows=3333 loops=1)
Index Cond: (chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d'::uuid)
Heap Fetches: 0
Buffers: shared hit=35
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.29..504.99 rows=1111 width=81) (actual time=0.619..0.619 rows=0 loops=1)
Index Cond: ((chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d'::uuid) AND (ROW(created_at, id) > ROW($0, $1)))
Buffers: shared hit=35
Planning:
Buffers: shared hit=106
Planning Time: 0.384 ms
Execution Time: 0.659 ms
(16 rows)
+11
View File
@@ -0,0 +1,11 @@
# Migrations are idempotent: re-running adds nothing
$ docker compose restart api # entrypoint runs migrations again
$ docker compose exec -T api python -m src.migrate
INFO:messenger.migrate:migrations up to date
$ docker compose exec -T postgres psql -U messenger -d messenger -c select version, applied_at from schema_migrations order by version;
version | applied_at
--------------+-------------------------------
001_init.sql | 2026-10-15 15:58:41.848316+00
(1 row)
+18
View File
@@ -0,0 +1,18 @@
# Persistence: restart API + all state stores, keep volumes (make test-persistence)
## containers before
mtusi2026lab2-api-1 Up About a minute (healthy)
mtusi2026lab2-postgres-1 Up About a minute (healthy)
mtusi2026lab2-valkey-1 Up About a minute (healthy)
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
## containers after
mtusi2026lab2-api-1 Up 1 second (health: starting)
mtusi2026lab2-postgres-1 Up 1 second (health: starting)
mtusi2026lab2-valkey-1 Up 1 second (health: starting)
## volumes (still present, not removed)
mtusi2026lab2_pgdata
mtusi2026lab2_valkeydata
+3
View File
@@ -0,0 +1,3 @@
# Public acceptance scenario
$ python tests/smoke.py --base-url http://localhost:8080
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
+39
View File
@@ -0,0 +1,39 @@
# Student black-box tests (grade 4), one command
$ C:\IT-labs\VNP\mtusi2026lab2\.venv\Scripts\python.exe -m pytest student-tests -v
============================= test session starts =============================
platform win32 -- Python 3.11.9, pytest-8.3.4, pluggy-1.7.0 -- C:\IT-labs\VNP\mtusi2026lab2\.venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\IT-labs\VNP\mtusi2026lab2
plugins: anyio-4.15.1
collecting ... collected 28 items
student-tests/test_auth.py::test_registration_requires_password PASSED [ 3%]
student-tests/test_auth.py::test_short_password_is_400 PASSED [ 7%]
student-tests/test_auth.py::test_extra_field_is_400 PASSED [ 10%]
student-tests/test_auth.py::test_malformed_json_is_400 PASSED [ 14%]
student-tests/test_auth.py::test_duplicate_username_is_409 PASSED [ 17%]
student-tests/test_auth.py::test_registration_never_returns_password PASSED [ 21%]
student-tests/test_auth.py::test_wrong_password_is_401_basic PASSED [ 25%]
student-tests/test_auth.py::test_unknown_user_is_401_basic PASSED [ 28%]
student-tests/test_auth.py::test_basic_on_business_endpoint_is_401_bearer PASSED [ 32%]
student-tests/test_auth.py::test_missing_auth_is_401_bearer PASSED [ 35%]
student-tests/test_auth.py::test_unknown_bearer_is_401 PASSED [ 39%]
student-tests/test_auth.py::test_x_user_id_is_ignored PASSED [ 42%]
student-tests/test_auth.py::test_logout_revokes_session PASSED [ 46%]
student-tests/test_concurrency.py::test_100_concurrent_messages_are_not_lost PASSED [ 50%]
student-tests/test_negative.py::test_foreign_chat_is_404 PASSED [ 53%]
student-tests/test_negative.py::test_empty_message_is_400 PASSED [ 57%]
student-tests/test_negative.py::test_oversized_message_is_400 PASSED [ 60%]
student-tests/test_negative.py::test_limit_out_of_range_is_400 PASSED [ 64%]
student-tests/test_negative.py::test_sender_id_in_body_is_rejected PASSED [ 67%]
student-tests/test_negative.py::test_chat_without_caller_is_400 PASSED [ 71%]
student-tests/test_negative.py::test_chat_with_unknown_member_is_400 PASSED [ 75%]
student-tests/test_negative.py::test_chat_with_duplicate_members_is_400 PASSED [ 78%]
student-tests/test_pagination.py::test_full_traversal_without_duplicates PASSED [ 82%]
student-tests/test_pagination.py::test_cursor_from_other_chat_is_400 PASSED [ 85%]
student-tests/test_pagination.py::test_garbage_cursor_is_400 PASSED [ 89%]
student-tests/test_pagination.py::test_well_formed_cursor_with_wrong_types_is_400 PASSED [ 92%]
student-tests/test_race.py::test_concurrent_duplicate_username_single_winner PASSED [ 96%]
student-tests/test_race.py::test_failed_chat_creation_rolls_back PASSED [100%]
============================= 28 passed in 5.10s ==============================