Files
tenant-registry/internal/server/onprem_test.go
T
Sharang Parnerkar a4fb318773 feat(onprem): APP_ENV=onprem for a customer's own node
The registry on a customer's node must never reach a Breakpilot host and
must not run the cloud's commercial machinery.

- APP_ENV=onprem: no ERP (nothing queued, webhook off), no trial (new
  workspaces start active, the ladder never escalates by time), and the
  trial-offer, trial-terms, catalog/trial request and admin trial routes
  answer 404 not_available.
- PRODUCTS, PRODUCT_LAUNCH_URLS, PRODUCT_PROVISION_URLS: one store
  decorator applies the product list and URLs to every catalog read
  (catalog, activation, provisioning, entitlement checks). On-prem has no
  seeded URLs, so provisioning goes only to the local products. Checked at
  start ({id} in provision URLs, absolute http(s)).
- On-prem start fails when the values leave something to a cloud or dev
  default (auth off, no authz rules, ephemeral licence key, portal, invite,
  sender, LLM gateway) or set an ERP or contract documents.
- SMTP_TLS=starttls|tls|none: a plain relay inside the customer's network;
  no AUTH without encryption.
- Migration 0032: the Werkpilot text no longer names our LLM host.

Cloud behaviour is unchanged without the new variables (tested).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2f6fa5f3e8)
2026-11-20 17:08:42 +01:00

193 lines
6.3 KiB
Go

package server_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"sync"
"testing"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/config"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
// onPremHarness turns a harness into an on-prem registry: Prüfwerk and
// Werkmeister ship (Werkmeister's row is disabled in the seed), with local URLs.
func onPremHarness(h *testHarness, provision map[string]string) {
h.app.Cfg = &config.Config{Env: config.EnvOnPrem, LicenceTTLDays: 30}
h.app.Store = store.WithProductOverrides(h.store, h.app.Cfg.CatalogEnv(),
[]string{"pruefwerk", "werkmeister"},
map[string]string{"pruefwerk": "https://pruefwerk.onprem.test", "werkmeister": "https://werkmeister.onprem.test"},
provision)
}
func TestOnPrem_catalogListsOnlyShippedProductsWithLocalURLs(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
onPremHarness(h, nil)
resp, body := h.do("GET", "/v1/catalog", nil)
if resp.StatusCode != 200 {
t.Fatalf("status = %d", resp.StatusCode)
}
out := decode[struct {
Items []store.CatalogEntry `json:"items"`
}](t, body)
got := map[string]string{}
for _, it := range out.Items {
got[it.Key] = it.LaunchURL
}
want := map[string]string{"pruefwerk": "https://pruefwerk.onprem.test", "werkmeister": "https://werkmeister.onprem.test"}
if len(got) != len(want) {
t.Fatalf("catalog = %v, want %v", got, want)
}
for k, v := range want {
if got[k] != v {
t.Errorf("%s launch URL = %q, want %q", k, got[k], v)
}
}
if strings.Contains(string(body), "breakpilot.com") {
t.Errorf("on-prem catalog names a Breakpilot host: %s", body)
}
})
}
func TestOnPrem_cloudOnlyRoutesAnswer404(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
onPremHarness(h, nil)
for _, c := range []struct{ method, path string }{
{"POST", "/v1/catalog/request"},
{"POST", "/v1/catalog/trial-request"},
{"POST", "/v1/trial-offers"},
{"POST", "/v1/trial-offers/verify"},
{"GET", "/v1/trial-offers/x"},
{"POST", "/v1/trial-offers/x/resend"},
{"GET", "/v1/legal/trial-terms"},
{"POST", "/v1/internal/erp/webhook"},
{"POST", "/v1/admin/tenants/" + h.tenant.ID + "/trial"},
{"GET", "/v1/tenants/" + h.tenant.ID + "/contracts/trial/terms.pdf"},
} {
resp, body := h.do(c.method, c.path, map[string]any{"tenant_id": h.tenant.ID, "product": "pruefwerk"})
if resp.StatusCode != http.StatusNotFound || !strings.Contains(string(body), "not_available") {
t.Errorf("%s %s = %d %s, want 404 not_available", c.method, c.path, resp.StatusCode, body)
}
}
})
}
func TestOnPrem_newWorkspaceIsActiveAndQueuesNothingForTheERP(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
onPremHarness(h, nil)
ctx := context.Background()
before, err := h.store.PendingOutbox(ctx)
if err != nil {
t.Fatal(err)
}
resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "werk", "name": "Werk GmbH"})
if resp.StatusCode != http.StatusCreated {
t.Fatalf("create = %d %s", resp.StatusCode, body)
}
out := decode[struct {
Tenant *store.Tenant `json:"tenant"`
}](t, body)
if out.Tenant.Status != store.StatusActive {
t.Errorf("status = %q, want active", out.Tenant.Status)
}
after, err := h.store.PendingOutbox(ctx)
if err != nil {
t.Fatal(err)
}
if after != before {
t.Errorf("outbox grew from %d to %d on-prem", before, after)
}
})
}
func TestOnPrem_nothingEscalatesByTime(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
ctx := context.Background()
trial := newTrial(t, h)
onPremHarness(h, nil)
// A year later the cloud would have restricted it.
h.app.Now = func() time.Time { return time.Now().AddDate(1, 0, 0) }
rep := h.app.Reconcile(ctx)
if len(rep.Escalated) != 0 {
t.Errorf("escalated on-prem: %v", rep.Escalated)
}
got, err := h.store.GetTenant(ctx, trial.ID)
if err != nil {
t.Fatal(err)
}
if got.Status != store.StatusTrial {
t.Errorf("status trial → %q", got.Status)
}
})
}
func TestOnPrem_activationProvisionsAtTheLocalURL(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
var mu sync.Mutex
var paths []string
local := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
mu.Lock()
paths = append(paths, r.Method+" "+r.URL.Path)
mu.Unlock()
w.WriteHeader(http.StatusOK)
}))
defer local.Close()
onPremHarness(h, map[string]string{"pruefwerk": local.URL + "/api/v1/platform/tenants/{id}/provision"})
resp, body := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/activate", map[string]any{
"products": []map[string]any{{"key": "pruefwerk"}},
})
if resp.StatusCode != http.StatusOK {
t.Fatalf("activate = %d %s", resp.StatusCode, body)
}
mu.Lock()
defer mu.Unlock()
want := "POST /api/v1/platform/tenants/" + h.tenant.ID + "/provision"
if len(paths) != 1 || paths[0] != want {
t.Errorf("provision calls = %v, want [%s]", paths, want)
}
})
}
func TestOnPrem_unshippedProductCannotBeActivated(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
onPremHarness(h, nil)
resp, body := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/activate", map[string]any{
"products": []map[string]any{{"key": "firmwerk"}},
})
if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), "unknown_product") {
t.Errorf("activate firmwerk = %d %s, want 400 unknown_product", resp.StatusCode, body)
}
})
}
// The same year-old trial does escalate in the cloud (the control for the test above).
func TestCloud_trialStillEscalatesByTime(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
trial := newTrial(t, h)
h.app.Now = func() time.Time { return time.Now().AddDate(1, 0, 0) }
rep := h.app.Reconcile(context.Background())
if !slices.Contains(rep.Escalated, trial.Slug+"→"+store.StatusRestricted) {
t.Errorf("escalated = %v, want %s restricted", rep.Escalated, trial.Slug)
}
})
}
func newTrial(t *testing.T, h *testHarness) *store.Tenant {
t.Helper()
tr, err := h.store.CreateTenant(context.Background(), store.TenantCreate{Slug: "trial-ws", Name: "Trial Workspace"})
if err != nil {
t.Fatal(err)
}
if tr.Status != store.StatusTrial || tr.TrialEndsAt == nil {
t.Fatalf("new tenant is %q (trial end %v), want a trial", tr.Status, tr.TrialEndsAt)
}
return tr
}