Перенести пользователей в PostgreSQL и добавить пароли
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user