Перенести пользователей в PostgreSQL и добавить пароли

This commit is contained in:
2026-10-06 14:54:34 +03:00
parent 46c00436a0
commit 618d3919d1
17 changed files with 482 additions and 219 deletions
+14 -5
View File
@@ -1,12 +1,13 @@
// Package app is the composition root: it builds every module with its
// in-memory stores and mounts them on one HTTP engine. Both the server
// binary and the integration tests start from here.
// stores and mounts them on one HTTP engine. Both the server binary and the
// integration tests start from here.
package app
import (
"log/slog"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/auth"
"messenger/internal/chat"
@@ -22,11 +23,19 @@ type App struct {
Health *health.Handler
}
func New(log *slog.Logger, maxBodyBytes int64) *App {
users := user.NewService(userstore.NewInMemUsers())
// Deps are the shared resources the modules are built on; their lifetime is
// owned by the caller.
type Deps struct {
Log *slog.Logger
DB *pgxpool.Pool
MaxBodyBytes int64
}
func New(d Deps) *App {
users := user.NewService(userstore.NewUsers(d.DB))
chats := chat.NewService(chatstore.NewInMemChats(), chatstore.NewInMemMessages(), users)
engine := httpx.NewEngine(log, maxBodyBytes)
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes)
probes := health.New()
probes.Register(engine)
+26 -3
View File
@@ -2,6 +2,7 @@ package app_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@@ -9,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"sort"
"strings"
"sync"
@@ -17,9 +19,26 @@ import (
"messenger/internal/app"
"messenger/internal/chat"
"messenger/internal/platform/pagination"
"messenger/internal/testenv"
"messenger/internal/user"
)
var db *testenv.DB
func TestMain(m *testing.M) {
var err error
db, err = testenv.StartPostgres(context.Background())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
code := m.Run()
db.Close()
os.Exit(code)
}
const testPassword = "correct-horse-battery"
// errorBody mirrors the contract error envelope.
type errorBody struct {
Error struct {
@@ -41,10 +60,12 @@ type tester struct {
a *app.App
}
// newTester assembles the application over an empty database.
func newTester(t *testing.T) *tester {
t.Helper()
db.Reset(t)
log := slog.New(slog.NewTextHandler(io.Discard, nil))
return &tester{t: t, a: app.New(log, 64*1024)}
return &tester{t: t, a: app.New(app.Deps{Log: log, DB: db.Pool, MaxBodyBytes: 64 * 1024})}
}
// do sends one request. A string body is sent verbatim, anything else is
@@ -127,7 +148,8 @@ func decode[T any](t *testing.T, res response) T {
func (ts *tester) user(username, display string) user.Response {
ts.t.Helper()
res := ts.do(http.MethodPost, "/api/v1/users", "", map[string]string{"username": username, "display_name": display}, nil)
body := map[string]string{"username": username, "display_name": display, "password": testPassword}
res := ts.do(http.MethodPost, "/api/v1/users", "", body, nil)
ts.expect(res, http.StatusCreated, "")
return decode[user.Response](ts.t, res)
}
@@ -192,7 +214,8 @@ func TestRegisterUserShapeAndConflicts(t *testing.T) {
t.Fatalf("created_at = %q", alice.CreatedAt)
}
post := func(body any) response { return ts.do(http.MethodPost, "/api/v1/users", "", body, nil) }
ts.expect(post(map[string]string{"username": "alice", "display_name": "Other"}), http.StatusConflict, "conflict")
ts.expect(post(map[string]string{"username": "alice", "display_name": "Other", "password": testPassword}), http.StatusConflict, "conflict")
ts.expect(post(map[string]string{"username": "bob", "display_name": "Bob"}), http.StatusBadRequest, "invalid_request")
ts.expect(post(map[string]string{"username": "Alice", "display_name": "x"}), http.StatusBadRequest, "invalid_request")
ts.expect(post(map[string]string{"username": "bob", "display_name": " "}), http.StatusBadRequest, "invalid_request")
ts.expect(post(map[string]any{"username": "bob", "display_name": "x", "password": "no"}), http.StatusBadRequest, "invalid_request")