Phase 0 foundation for Docwell, an open-source knowledge base: - Database: PocketBase v0.40.6 with initial schema migrations; users is the built-in auth collection, plus a hook for token checks and env-driven app URL/SMTP settings - Server: Go API (health, version, auth endpoints forwarded to PocketBase, admin user list) with an embedded Vue admin panel - Web-App: Go static server and Vue/Tailwind app with Tiptap editor, sign-in, sign-up, password reset and email verification (EN/PL) - Desktop-App: Wails v3 host reusing the web app - Phone-App: Flutter app with sign-in - Shared packages (editor, ui, api-client), OpenAPI spec, Docker compose (separate services and all-in-one), GitHub Actions CI - Docs: feature inventory and architecture/build plan Licensed under AGPL-3.0-or-later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
3.9 KiB
Go
129 lines
3.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"docwell/server/internal/auth"
|
|
)
|
|
|
|
// fakeAuth implements the AuthService calls these tests reach; the embedded
|
|
// nil interface panics on anything else.
|
|
type fakeAuth struct {
|
|
AuthService
|
|
}
|
|
|
|
var (
|
|
alice = auth.User{ID: "u1", Email: "alice@docwell.test", Name: "Alice", Status: "active"}
|
|
admin = auth.User{ID: "u2", Email: "root@docwell.test", Name: "Root", Status: "active", IsAdmin: true}
|
|
)
|
|
|
|
func (fakeAuth) Verify(_ context.Context, token string) (auth.User, error) {
|
|
switch token {
|
|
case "alice":
|
|
return alice, nil
|
|
case "admin":
|
|
return admin, nil
|
|
case "disabled":
|
|
return auth.User{}, auth.ErrAccountDisabled
|
|
}
|
|
return auth.User{}, auth.ErrUnauthorized
|
|
}
|
|
|
|
func (fakeAuth) Login(_ context.Context, email, password, mfaID string) (auth.Session, error) {
|
|
switch {
|
|
case email == "mfa@docwell.test" && mfaID == "":
|
|
return auth.Session{}, &auth.MFARequiredError{MFAID: "m1"}
|
|
case email == alice.Email && password == "password123":
|
|
return auth.Session{Token: "alice", User: alice}, nil
|
|
}
|
|
return auth.Session{}, auth.ErrInvalidCredentials
|
|
}
|
|
|
|
func (fakeAuth) Signup(_ context.Context, in auth.SignupInput) (auth.Session, error) {
|
|
return auth.Session{}, &auth.ValidationError{Fields: map[string]string{"password": "too short"}}
|
|
}
|
|
|
|
func (fakeAuth) ListUsers(context.Context, auth.ListUsersInput) (auth.UserList, error) {
|
|
return auth.UserList{Items: []auth.User{alice, admin}, TotalItems: 2}, nil
|
|
}
|
|
|
|
func authRouter() http.Handler {
|
|
return NewRouter(Deps{
|
|
Version: "test",
|
|
DB: fakeDB{},
|
|
Auth: fakeAuth{},
|
|
Log: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
})
|
|
}
|
|
|
|
func send(h http.Handler, method, path, token, body string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
func TestLoginResponses(t *testing.T) {
|
|
h := authRouter()
|
|
cases := []struct {
|
|
body string
|
|
wantStatus int
|
|
wantBody string
|
|
}{
|
|
{`{"email":"alice@docwell.test","password":"password123"}`, 200, `"token":"alice"`},
|
|
{`{"email":"alice@docwell.test","password":"nope"}`, 401, `"invalid_credentials"`},
|
|
{`{"email":"mfa@docwell.test","password":"x"}`, 401, `"mfaId":"m1"`},
|
|
{`not json`, 400, `"invalid_json"`},
|
|
}
|
|
for _, c := range cases {
|
|
rec := send(h, "POST", "/api/v1/auth/login", "", c.body)
|
|
if rec.Code != c.wantStatus || !strings.Contains(rec.Body.String(), c.wantBody) {
|
|
t.Errorf("login %s = %d %s", c.body, rec.Code, rec.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSignupValidationError(t *testing.T) {
|
|
rec := send(authRouter(), "POST", "/api/v1/auth/signup", "", `{"email":"a@b.c","password":"x","name":"A"}`)
|
|
if rec.Code != 400 || !strings.Contains(rec.Body.String(), `"fields":{"password":"too short"}`) {
|
|
t.Errorf("signup = %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
|
|
func TestMeRequiresValidToken(t *testing.T) {
|
|
h := authRouter()
|
|
if rec := send(h, "GET", "/api/v1/auth/me", "", ""); rec.Code != 401 {
|
|
t.Errorf("no token = %d", rec.Code)
|
|
}
|
|
if rec := send(h, "GET", "/api/v1/auth/me", "disabled", ""); rec.Code != 403 {
|
|
t.Errorf("disabled = %d", rec.Code)
|
|
}
|
|
rec := send(h, "GET", "/api/v1/auth/me", "alice", "")
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"email":"alice@docwell.test"`) {
|
|
t.Errorf("me = %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
|
|
func TestAdminUsersRequiresAdmin(t *testing.T) {
|
|
h := authRouter()
|
|
if rec := send(h, "GET", "/api/v1/admin/users", "", ""); rec.Code != 401 {
|
|
t.Errorf("anonymous = %d", rec.Code)
|
|
}
|
|
if rec := send(h, "GET", "/api/v1/admin/users", "alice", ""); rec.Code != 403 {
|
|
t.Errorf("non-admin = %d", rec.Code)
|
|
}
|
|
rec := send(h, "GET", "/api/v1/admin/users?perPage=5000", "admin", "")
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"totalItems":2`) {
|
|
t.Errorf("admin = %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|