pruefwerk-agent-stage logged 'platform token rejected: failed to decode JWT header' in bursts of five provisions and one targets read, each time the registry's tests ran (CI and local). The migrations seed the real provision URLs (0005: pruefwerk-stage), a test config's env 'dev' reads as 'stage', so every postgres test that provisioned a tenant called the live endpoint with the Keycloak mock's non-JWT token and got 401. The postgres harness clears provision_urls after migrating, as the memory store has none; tests that need one set it themselves. Checked: a full local run with postgres left no request in Prüfwerk's traces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4
225 lines
6.0 KiB
Go
225 lines
6.0 KiB
Go
package server_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
migpg "github.com/golang-migrate/migrate/v4/database/postgres"
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/config"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/keycloak"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/licence"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/server"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
"git.breakpilot.com/platform/tenant-registry/migrations"
|
|
)
|
|
|
|
const testWebhookSecret = "test-webhook-secret"
|
|
|
|
// ─── harness ──────────────────────────────────────────────────────────────
|
|
|
|
type testHarness struct {
|
|
t *testing.T
|
|
srv *httptest.Server
|
|
app *server.Server // the wired server (tests set app.Now / app.ERP / app.Cfg fields)
|
|
store store.Store
|
|
tenant *store.Tenant // pre-created acme tenant
|
|
kcMock *keycloak.Mock
|
|
}
|
|
|
|
func (h *testHarness) Close() {
|
|
h.srv.Close()
|
|
h.store.Close()
|
|
}
|
|
|
|
// every test runs against both stores so we know they're equivalent.
|
|
func eachStore(t *testing.T, run func(*testing.T, *testHarness)) {
|
|
t.Run("memory", func(t *testing.T) {
|
|
h := newMemoryHarness(t)
|
|
defer h.Close()
|
|
run(t, h)
|
|
})
|
|
t.Run("postgres", func(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping postgres harness under -short")
|
|
}
|
|
h := newPostgresHarness(t)
|
|
defer h.Close()
|
|
run(t, h)
|
|
})
|
|
}
|
|
|
|
func newMemoryHarness(t *testing.T) *testHarness {
|
|
t.Helper()
|
|
mem := store.NewMemory()
|
|
tenant, _ := mem.GetTenantBySlug(context.Background(), "acme")
|
|
return wireHarness(t, mem, tenant)
|
|
}
|
|
|
|
func newPostgresHarness(t *testing.T) *testHarness {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
|
|
pgc, err := tcpostgres.Run(ctx,
|
|
"postgres:16-alpine",
|
|
tcpostgres.WithDatabase("tenant_registry_test"),
|
|
tcpostgres.WithUsername("test"),
|
|
tcpostgres.WithPassword("test"),
|
|
tcpostgres.BasicWaitStrategies(),
|
|
)
|
|
if err != nil {
|
|
t.Skipf("skipping postgres harness: docker unreachable (%v)", err)
|
|
}
|
|
dsn, err := pgc.ConnectionString(ctx, "sslmode=disable")
|
|
if err != nil {
|
|
_ = pgc.Terminate(context.Background())
|
|
t.Fatalf("dsn: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
c, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
_ = pgc.Terminate(c)
|
|
})
|
|
|
|
// run migrations
|
|
src, err := iofs.New(migrations.FS, ".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db, err := sql.Open("pgx", dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
driver, err := migpg.WithInstance(db, &migpg.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m, err := migrate.NewWithInstance("iofs", src, "postgres", driver)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Up(); err != nil && err.Error() != "no change" {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
// The migrations seed the real product URLs (0005: Prüfwerk's stage
|
|
// provision endpoint), and a test config's env "dev" reads as "stage":
|
|
// without this, every test that provisions a tenant called the live
|
|
// pruefwerk-stage with the mock's non-JWT token (401s in its log). The
|
|
// memory store has no provision URLs either; tests that need one set it.
|
|
if _, err := db.ExecContext(ctx, `UPDATE products SET provision_urls = '{}'`); err != nil {
|
|
t.Fatalf("clear provision URLs: %v", err)
|
|
}
|
|
_, _ = m.Close()
|
|
_ = db.Close()
|
|
|
|
pg, err := store.NewPostgres(ctx, dsn)
|
|
if err != nil {
|
|
t.Fatalf("new postgres: %v", err)
|
|
}
|
|
|
|
// seed an acme tenant so the per-endpoint tests can reuse the slug.
|
|
tenant, err := pg.CreateTenant(ctx, store.TenantCreate{
|
|
Slug: "acme", Name: "Acme Inc.", Plan: "professional",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed acme: %v", err)
|
|
}
|
|
return wireHarness(t, pg, tenant)
|
|
}
|
|
|
|
func wireHarness(t *testing.T, s store.Store, seed *store.Tenant) *testHarness {
|
|
t.Helper()
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
mock := keycloak.NewMock()
|
|
signer, err := licence.NewSigner("", "tenant-registry-test")
|
|
if err != nil {
|
|
t.Fatalf("licence signer: %v", err)
|
|
}
|
|
app := &server.Server{
|
|
Cfg: &config.Config{Env: "dev", ErpWebhookSecret: testWebhookSecret, LicenceTTLDays: 30},
|
|
Log: logger,
|
|
Store: s,
|
|
Keycloak: mock,
|
|
Licences: signer,
|
|
}
|
|
handler := server.NewRouter(app)
|
|
return &testHarness{
|
|
t: t,
|
|
app: app,
|
|
srv: httptest.NewServer(handler),
|
|
store: s,
|
|
tenant: seed,
|
|
kcMock: mock,
|
|
}
|
|
}
|
|
|
|
func (h *testHarness) do(method, path string, body any) (*http.Response, []byte) {
|
|
h.t.Helper()
|
|
var reader io.Reader
|
|
if body != nil {
|
|
buf, _ := json.Marshal(body)
|
|
reader = bytes.NewReader(buf)
|
|
}
|
|
req, err := http.NewRequest(method, h.srv.URL+path, reader)
|
|
if err != nil {
|
|
h.t.Fatal(err)
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
h.t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
return resp, raw
|
|
}
|
|
|
|
// doRaw sends body verbatim (malformed JSON tests).
|
|
func (h *testHarness) doRaw(method, path, body string) (*http.Response, []byte) {
|
|
h.t.Helper()
|
|
req, err := http.NewRequest(method, h.srv.URL+path, bytes.NewReader([]byte(body)))
|
|
if err != nil {
|
|
h.t.Fatal(err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
h.t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
return resp, raw
|
|
}
|
|
|
|
var errAny = fmt.Errorf("boom")
|
|
|
|
func decode[T any](t *testing.T, raw []byte) T {
|
|
t.Helper()
|
|
var v T
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
t.Fatalf("decode: %v; raw=%s", err, raw)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// silence unused-import linter warnings if a test is removed temporarily.
|
|
var _ = fmt.Sprintf
|
|
var _ = os.Stderr
|