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>
84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package pocketbase
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
type record struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func fakePocketBase(t *testing.T, authCalls *atomic.Int32) *httptest.Server {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /api/health", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte(`{"code":200}`))
|
|
})
|
|
mux.HandleFunc("POST /api/collections/_superusers/auth-with-password", func(w http.ResponseWriter, r *http.Request) {
|
|
n := authCalls.Add(1)
|
|
json.NewEncoder(w).Encode(map[string]string{"token": "tok" + string(rune('0'+n))})
|
|
})
|
|
mux.HandleFunc("GET /api/collections/workspaces/records", func(w http.ResponseWriter, r *http.Request) {
|
|
// The first token is treated as expired to exercise the retry path.
|
|
if r.Header.Get("Authorization") != "tok2" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte(`{"status":401,"message":"expired"}`))
|
|
return
|
|
}
|
|
if got := r.URL.Query().Get("filter"); got != "slug='acme'" {
|
|
t.Errorf("filter = %q", got)
|
|
}
|
|
w.Write([]byte(`{"page":1,"perPage":30,"totalItems":1,"totalPages":1,"items":[{"id":"w1","name":"Acme"}]}`))
|
|
})
|
|
mux.HandleFunc("GET /api/collections/workspaces/records/missing", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
w.Write([]byte(`{"status":404,"message":"not found"}`))
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func TestHealth(t *testing.T) {
|
|
var calls atomic.Int32
|
|
srv := fakePocketBase(t, &calls)
|
|
if err := New(srv.URL, "a", "b").Health(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls.Load() != 0 {
|
|
t.Error("health check should not authenticate")
|
|
}
|
|
}
|
|
|
|
func TestListReauthenticatesOn401(t *testing.T) {
|
|
var calls atomic.Int32
|
|
srv := fakePocketBase(t, &calls)
|
|
c := New(srv.URL, "a", "b")
|
|
|
|
res, err := List[record](context.Background(), c, "workspaces", ListOptions{Filter: "slug='acme'"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(res.Items) != 1 || res.Items[0].Name != "Acme" {
|
|
t.Errorf("items = %+v", res.Items)
|
|
}
|
|
if calls.Load() != 2 {
|
|
t.Errorf("auth calls = %d, want 2", calls.Load())
|
|
}
|
|
}
|
|
|
|
func TestNotFound(t *testing.T) {
|
|
var calls atomic.Int32
|
|
srv := fakePocketBase(t, &calls)
|
|
_, err := Get[record](context.Background(), New(srv.URL, "a", "b"), "workspaces", "missing")
|
|
if !IsNotFound(err) {
|
|
t.Fatalf("err = %v, want not found", err)
|
|
}
|
|
}
|