forked from mtusi2026/mtusi2026lab2
реализован уровень 3
This commit is contained in:
@@ -2,6 +2,7 @@ package student_tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -18,25 +19,67 @@ import (
|
||||
"mtusi2026lab2/internal/transport/http/middleware"
|
||||
)
|
||||
|
||||
func requireLegacyAuth(
|
||||
memoryStore *store.MemoryStore,
|
||||
next http.HandlerFunc,
|
||||
) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
userID := r.Header.Get("X-User-Id")
|
||||
|
||||
user, err := memoryStore.GetUserByID(userID)
|
||||
if err != nil {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(
|
||||
r.Context(),
|
||||
middleware.UserContextKey,
|
||||
user,
|
||||
)
|
||||
|
||||
next(w, r.WithContext(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
// изолированный серв для тестов
|
||||
func setupTestServer() (*httptest.Server, *store.MemoryStore) {
|
||||
st := store.NewMemoryStore()
|
||||
h := handlers.NewHandler(st)
|
||||
memoryStore := store.NewMemoryStore()
|
||||
repository := store.NewMemoryRepository(memoryStore)
|
||||
handler := handlers.NewHandler(repository)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("POST /api/v1/users", h.CreateUser)
|
||||
mux.HandleFunc("GET /api/v1/users/me", middleware.RequireAuth(st, h.GetMe))
|
||||
mux.HandleFunc("POST /api/v1/chats", middleware.RequireAuth(st, h.CreateChat))
|
||||
mux.HandleFunc("GET /api/v1/chats", middleware.RequireAuth(st, h.ListChats))
|
||||
mux.HandleFunc("GET /api/v1/chats/{chat_id}", middleware.RequireAuth(st, h.GetChat))
|
||||
mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", middleware.RequireAuth(st, h.CreateMessage))
|
||||
mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", middleware.RequireAuth(st, h.GetMessages))
|
||||
mux.HandleFunc("POST /api/v1/users", handler.CreateUser)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/users/me",
|
||||
requireLegacyAuth(memoryStore, handler.GetMe),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"POST /api/v1/chats",
|
||||
requireLegacyAuth(memoryStore, handler.CreateChat),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/chats",
|
||||
requireLegacyAuth(memoryStore, handler.ListChats),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/chats/{chat_id}",
|
||||
requireLegacyAuth(memoryStore, handler.GetChat),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"POST /api/v1/chats/{chat_id}/messages",
|
||||
requireLegacyAuth(memoryStore, handler.CreateMessage),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/chats/{chat_id}/messages",
|
||||
requireLegacyAuth(memoryStore, handler.GetMessages),
|
||||
)
|
||||
|
||||
var root http.Handler = mux
|
||||
root = middleware.Recovery(root)
|
||||
root = middleware.RequestID(root)
|
||||
|
||||
return httptest.NewServer(root), st
|
||||
return httptest.NewServer(root), memoryStore
|
||||
}
|
||||
|
||||
// 1 тест на дубликат username -> 409 Conflict
|
||||
@@ -44,7 +87,10 @@ func TestDuplicateUsername(t *testing.T) {
|
||||
ts, _ := setupTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
body := `{"username":"bob_the_builder","display_name":"Боб"}`
|
||||
body := `{
|
||||
"username":"bob_the_builder",
|
||||
"display_name":"Боб",
|
||||
"password":"Test-password-123"}`
|
||||
|
||||
// Первый раз — 201 Created
|
||||
res1, _ := http.Post(ts.URL+"/api/v1/users", "application/json", bytes.NewBufferString(body))
|
||||
|
||||
Reference in New Issue
Block a user