forked from mtusi2026/mtusi2026lab3
feat:fix
This commit is contained in:
@@ -33,4 +33,5 @@ my_tests:
|
||||
$(PYTHON) student_tests/test_limits.py
|
||||
$(PYTHON) student_tests/test_idempotency.py
|
||||
$(PYTHON) student_tests/test_failover_both.py
|
||||
$(PYTHON) student_tests/test_rolling.py
|
||||
$(PYTHON) student_tests/test_rolling.py
|
||||
$(PYTHON) student_tests/test_regressions.py
|
||||
+68
-18
@@ -47,9 +47,7 @@ type createUserReq struct {
|
||||
|
||||
func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
var req createUserReq
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&req); err != nil {
|
||||
if err := decodeSingleJSON(r.Body, &req); err != nil {
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON")
|
||||
return
|
||||
}
|
||||
@@ -130,6 +128,10 @@ func (h *Handler) RevokeSession(w http.ResponseWriter, r *http.Request) {
|
||||
h.bearerAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
token := tokenFrom(r)
|
||||
if err := h.Sessions.Revoke(r.Context(), token); err != nil {
|
||||
if errors.Is(err, domain.ErrTimeout) {
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store timeout")
|
||||
return
|
||||
}
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store unavailable")
|
||||
return
|
||||
}
|
||||
@@ -148,9 +150,7 @@ func (h *Handler) CreateChat(w http.ResponseWriter, r *http.Request) {
|
||||
h.bearerAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
me, _ := userFrom(r)
|
||||
var req createChatReq
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&req); err != nil {
|
||||
if err := decodeSingleJSON(r.Body, &req); err != nil {
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON")
|
||||
return
|
||||
}
|
||||
@@ -183,26 +183,43 @@ func (h *Handler) CreateChat(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *Handler) ListChats(w http.ResponseWriter, r *http.Request) {
|
||||
h.bearerAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
me, _ := userFrom(r)
|
||||
chats, err := h.Store.ListChats(r.Context(), me.ID)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrTimeout) {
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout")
|
||||
limit := 50
|
||||
if v := r.URL.Query().Get("limit"); v != "" {
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n < 1 || n > 100 {
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid limit")
|
||||
return
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
cursor := r.URL.Query().Get("cursor")
|
||||
chats, next, err := h.Store.ListChats(r.Context(), me.ID, limit, cursor)
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, domain.ErrInvalid):
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid cursor")
|
||||
return
|
||||
case errors.Is(err, domain.ErrTimeout):
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout")
|
||||
return
|
||||
default:
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, domain.ChatPage{Items: chats, NextCursor: nil})
|
||||
writeJSON(w, http.StatusOK, domain.ChatPage{Items: chats, NextCursor: next})
|
||||
})(w, r)
|
||||
}
|
||||
|
||||
func (h *Handler) GetChat(w http.ResponseWriter, r *http.Request) {
|
||||
h.bearerAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
me, _ := userFrom(r)
|
||||
c, err := h.Store.GetChat(r.Context(), r.PathValue("chat_id"), me.ID)
|
||||
chatID := r.PathValue("chat_id")
|
||||
c, err := h.Store.GetChat(r.Context(), chatID, me.ID)
|
||||
switch {
|
||||
case err == nil:
|
||||
// ok
|
||||
case errors.Is(err, domain.ErrInvalid):
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id")
|
||||
return
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
writeError(w, r, http.StatusNotFound, "not_found", "chat not found")
|
||||
return
|
||||
@@ -240,7 +257,16 @@ func (h *Handler) CreateMessage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if _, err := h.Store.GetChat(r.Context(), chatID, me.ID); err != nil {
|
||||
writeError(w, r, http.StatusNotFound, "not_found", "chat not found")
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrInvalid):
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id")
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
writeError(w, r, http.StatusNotFound, "not_found", "chat not found")
|
||||
case errors.Is(err, domain.ErrTimeout):
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout")
|
||||
default:
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -251,9 +277,7 @@ func (h *Handler) CreateMessage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
var req createMessageReq
|
||||
dec := json.NewDecoder(bytes.NewReader(rawBody))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&req); err != nil {
|
||||
if err := decodeSingleJSON(bytes.NewReader(rawBody), &req); err != nil {
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON")
|
||||
return
|
||||
}
|
||||
@@ -306,7 +330,16 @@ func (h *Handler) ListMessages(w http.ResponseWriter, r *http.Request) {
|
||||
me, _ := userFrom(r)
|
||||
chatID := r.PathValue("chat_id")
|
||||
if _, err := h.Store.GetChat(r.Context(), chatID, me.ID); err != nil {
|
||||
writeError(w, r, http.StatusNotFound, "not_found", "chat not found")
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrInvalid):
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id")
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
writeError(w, r, http.StatusNotFound, "not_found", "chat not found")
|
||||
case errors.Is(err, domain.ErrTimeout):
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout")
|
||||
default:
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable")
|
||||
}
|
||||
return
|
||||
}
|
||||
limit := 50
|
||||
@@ -336,3 +369,20 @@ func (h *Handler) ListMessages(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, domain.MessagePage{Items: items, NextCursor: next})
|
||||
})(w, r)
|
||||
}
|
||||
|
||||
func decodeSingleJSON(r io.Reader, v any) error {
|
||||
dec := json.NewDecoder(r)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
if dec.More() {
|
||||
return errors.New("trailing data after JSON value")
|
||||
}
|
||||
// Пробуем прочитать ещё один токен — если это не EOF, значит есть мусор.
|
||||
var extra any
|
||||
if err := dec.Decode(&extra); err != io.EOF {
|
||||
return errors.New("trailing data after JSON value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+51
-17
@@ -272,15 +272,40 @@ func (s *Store) GetChat(ctx context.Context, chatID, userID string) (*Chat, erro
|
||||
return &c, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ListChats(ctx context.Context, userID string) ([]Chat, error) {
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY c.created_at, c.id`, userID)
|
||||
// ListChats возвращает чаты пользователя с keyset-пагинацией по (created_at, id).
|
||||
func (s *Store) ListChats(ctx context.Context, userID string, limit int, cursor string) ([]Chat, *string, error) {
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
|
||||
if cursor == "" {
|
||||
rows, err = s.pool.Query(ctx,
|
||||
`SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $2`, userID, limit+1)
|
||||
} else {
|
||||
var cCreated time.Time
|
||||
if err := s.pool.QueryRow(ctx,
|
||||
`SELECT created_at FROM chats WHERE id=$1`, cursor,
|
||||
).Scan(&cCreated); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil, ErrInvalid
|
||||
}
|
||||
return nil, nil, classifyDBError(err)
|
||||
}
|
||||
rows, err = s.pool.Query(ctx,
|
||||
`SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
AND (c.created_at, c.id) > ($2, $3)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4`, userID, cCreated, cursor, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, classifyDBError(err)
|
||||
return nil, nil, classifyDBError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -288,35 +313,42 @@ func (s *Store) ListChats(ctx context.Context, userID string) ([]Chat, error) {
|
||||
for rows.Next() {
|
||||
var c Chat
|
||||
if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
|
||||
return nil, classifyDBError(err)
|
||||
return nil, nil, classifyDBError(err)
|
||||
}
|
||||
chats = append(chats, c)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, classifyDBError(err)
|
||||
return nil, nil, classifyDBError(err)
|
||||
}
|
||||
|
||||
// Догружаем участников (небольшие объёмы в учебном сценарии).
|
||||
var next *string
|
||||
if len(chats) > limit {
|
||||
chats = chats[:limit]
|
||||
last := chats[len(chats)-1].ID
|
||||
next = &last
|
||||
}
|
||||
if chats == nil {
|
||||
chats = []Chat{}
|
||||
}
|
||||
|
||||
// Догружаем участников.
|
||||
for i := range chats {
|
||||
mrows, err := s.pool.Query(ctx,
|
||||
`SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id`, chats[i].ID)
|
||||
if err != nil {
|
||||
return nil, classifyDBError(err)
|
||||
return nil, nil, classifyDBError(err)
|
||||
}
|
||||
for mrows.Next() {
|
||||
var uid string
|
||||
if err := mrows.Scan(&uid); err != nil {
|
||||
mrows.Close()
|
||||
return nil, classifyDBError(err)
|
||||
return nil, nil, classifyDBError(err)
|
||||
}
|
||||
chats[i].MemberIDs = append(chats[i].MemberIDs, uid)
|
||||
}
|
||||
mrows.Close()
|
||||
}
|
||||
if chats == nil {
|
||||
chats = []Chat{}
|
||||
}
|
||||
return chats, nil
|
||||
return chats, next, nil
|
||||
}
|
||||
|
||||
// --- Messages ---
|
||||
@@ -434,6 +466,8 @@ func classifyDBError(err error) error {
|
||||
return ErrUnavailable
|
||||
case "53300": // too_many_connections
|
||||
return ErrUnavailable
|
||||
case "22P02", "22007", "22023": // invalid_text_representation / invalid_datetime_format / invalid_parameter_value
|
||||
return ErrInvalid
|
||||
}
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||
|
||||
+12
-2
@@ -89,10 +89,20 @@ func (s *SessionStore) Lookup(ctx context.Context, token string) (*User, error)
|
||||
}
|
||||
|
||||
func (s *SessionStore) Revoke(ctx context.Context, token string) error {
|
||||
// Достаём user_id, чтобы потом сбросить кэш User (не обязательно, но полезно).
|
||||
var userID string
|
||||
_ = s.store.pool.QueryRow(ctx,
|
||||
err := s.store.pool.QueryRow(ctx,
|
||||
`DELETE FROM sessions WHERE token=$1 RETURNING user_id`, token,
|
||||
).Scan(&userID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
// Сессии уже нет — с точки зрения клиента токен отозван.
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return classifyDBError(err)
|
||||
}
|
||||
if s.cache != nil {
|
||||
// Кэш User не критичен, но полезно сбросить.
|
||||
_ = s.cache.DelUser(ctx, userID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Регрессии: logout, невалидные UUID/cursor, trailing JSON, limit в ListChats."""
|
||||
import base64
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from common import BASE_URL, Client, Failure, check, print_pass
|
||||
|
||||
|
||||
def test_logout_propagates_delete_error():
|
||||
"""После DELETE сессии токен обязан перестать работать; ложный 204 недопустим.
|
||||
|
||||
Проверяем базовую семантику: logout → 204 → токен недействителен.
|
||||
Дополнительно: повторный logout → 401 (сессии уже нет), а не 204.
|
||||
"""
|
||||
c = Client(BASE_URL)
|
||||
alice, _, _, _ = c.bootstrap()
|
||||
# logout
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
# токен больше не должен давать 200
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
# повторный logout тем же токеном → 401
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=alice)
|
||||
|
||||
|
||||
def test_invalid_chat_uuid_returns_400():
|
||||
"""GET /chats/not-a-uuid и .../messages?cursor=not-a-uuid → 400, не 503."""
|
||||
c = Client(BASE_URL)
|
||||
alice, _, _, _ = c.bootstrap()
|
||||
|
||||
c.call('GET', '/api/v1/chats/not-a-uuid', 400, actor=alice)
|
||||
c.call('GET', '/api/v1/chats/not-a-uuid/messages', 400, actor=alice)
|
||||
c.call('POST', '/api/v1/chats/not-a-uuid/messages', 400, actor=alice,
|
||||
body={'text': 'x'}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
|
||||
# Валидный chat_id, но невалидный cursor → 400 (не 503).
|
||||
bob = None
|
||||
alice2, bob2, eve, chat = c.bootstrap()
|
||||
c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?cursor=not-a-uuid',
|
||||
400, actor=alice2)
|
||||
# Невалидный UUID курсора (не hex) — тот же результат.
|
||||
c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?cursor=deadbeef',
|
||||
400, actor=alice2)
|
||||
|
||||
|
||||
def test_trailing_json_rejected():
|
||||
"""Валидный JSON + второй {} → 400, пользователь не создаётся."""
|
||||
c = Client(BASE_URL)
|
||||
username = 'u_trail_' + uuid.uuid4().hex[:12]
|
||||
password = 'T3st-' + uuid.uuid4().hex
|
||||
body = ('{"username":"%s","display_name":"Trail","password":"%s"}'
|
||||
% (username, password)).encode() + b'{}'
|
||||
c.call('POST', '/api/v1/users', 400, body=None, raw=body,
|
||||
headers={'Content-Type': 'application/json'})
|
||||
|
||||
# Убедимся, что пользователь действительно НЕ создан: логин должен упасть.
|
||||
cred = base64.b64encode((username + ':' + password).encode()).decode()
|
||||
c.call('POST', '/api/v1/auth/sessions', 401,
|
||||
headers={'Authorization': 'Basic ' + cred})
|
||||
|
||||
|
||||
def test_list_chats_respects_limit():
|
||||
"""?limit=1 возвращает ровно 1 чат и next_cursor; ?limit=0 → 400."""
|
||||
c = Client(BASE_URL)
|
||||
alice, bob, _, chat1 = c.bootstrap()
|
||||
# Второй чат для того же пользователя.
|
||||
chat2 = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Второй', 'member_ids': [alice['id']]},
|
||||
schema='Chat')['json']
|
||||
|
||||
page1 = c.call('GET', '/api/v1/chats?limit=1', actor=alice, schema='ChatPage')['json']
|
||||
check(len(page1['items']) == 1, 'limit=1 вернул %d чатов' % len(page1['items']))
|
||||
check(page1['next_cursor'] is not None, 'next_cursor должен быть при наличии ещё чатов')
|
||||
|
||||
page2 = c.call('GET', '/api/v1/chats?limit=1&cursor=' + page1['next_cursor'],
|
||||
actor=alice, schema='ChatPage')['json']
|
||||
check(len(page2['items']) == 1, 'вторая страница должна содержать 1 чат')
|
||||
check(page2['next_cursor'] is None, 'после второй страницы курсор должен быть null')
|
||||
|
||||
ids = [page1['items'][0]['id'], page2['items'][0]['id']]
|
||||
check(set(ids) == {chat1['id'], chat2['id']}, 'пагинация потеряла/задвоила чаты')
|
||||
|
||||
c.call('GET', '/api/v1/chats?limit=0', 400, actor=alice)
|
||||
c.call('GET', '/api/v1/chats?limit=101', 400, actor=alice)
|
||||
c.call('GET', '/api/v1/chats?limit=abc', 400, actor=alice)
|
||||
|
||||
|
||||
def test_concurrent_idempotency_under_failover_scope():
|
||||
"""Дополнительно: один ключ, 20 параллельных POST — ровно один id.
|
||||
Про корректность idempotency-транзакции.
|
||||
"""
|
||||
c = Client(BASE_URL)
|
||||
alice, bob, _, chat = c.bootstrap()
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
key = 'reg-' + uuid.uuid4().hex
|
||||
text = 'reg-' + uuid.uuid4().hex
|
||||
|
||||
def send(_):
|
||||
local = Client(BASE_URL, timeout=15)
|
||||
r = local.call('POST', path, (201, 503), actor=alice,
|
||||
body={'text': text}, headers={'Idempotency-Key': key},
|
||||
schema='Message')
|
||||
return r
|
||||
|
||||
with ThreadPoolExecutor(max_workers=20) as pool:
|
||||
results = list(pool.map(send, range(20)))
|
||||
ok = [r for r in results if r['status'] == 201 and r['json']]
|
||||
check(len(ok) >= 15, 'слишком много не-201: %d/20' % len(ok))
|
||||
ids = {r['json']['id'] for r in ok}
|
||||
check(len(ids) == 1, 'конкурентный повтор создал %d id: %r' % (len(ids), ids))
|
||||
|
||||
|
||||
def main():
|
||||
tests = [
|
||||
test_logout_propagates_delete_error,
|
||||
test_invalid_chat_uuid_returns_400,
|
||||
test_trailing_json_rejected,
|
||||
test_list_chats_respects_limit,
|
||||
test_concurrent_idempotency_under_failover_scope,
|
||||
]
|
||||
for t in tests:
|
||||
print('--- ' + t.__name__)
|
||||
t()
|
||||
print_pass('regressions (logout/uuid/cursor/trailing-json/limit)')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except Failure as e:
|
||||
print('FAIL:', e)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user