Files
base/core/system_alert_test.go
zeekay e7df8c856e base: complete pocketbase→base rename; Base = pure IAM client
Rename / namespace:
- Finish github.com/pocketbase/* → github.com/hanzoai/base (+ /dbx) sweep
  across all Go sources; restore the renamed root launcher (Base, base.go).
- Drop the upstream-only surfaces ours doesn't ship (admin ui/, the
  password/OTP/oauth2/impersonate auth files, extensions) re-added by the
  upstream merge.
- Decomplect the on-disk layout to one base_/ namespace: data/, migrations/,
  hooks/, types.d.ts are siblings under base_/ (default data dir
  base_/data). Drop the hz_ prefix everywhere; collapse migratecmd's
  identical JS/Go branches to one path.
- Remove the accidentally-committed hz_data/ dev dir (db + iam.key) and
  gitignore the runtime namespace dirs.

Deps / build:
- kmsclient now imported from the light github.com/hanzoai/kms/sdk/go module
  (the root kms module zip exceeds Go's download limit); cloud/pkg/base
  Mount removed (lives in the fused daemon).
- replicate v0.4.0 → v0.8.0; metric.Counter.Get() (no prometheus dto);
  network/metrics.go import fix; auth gitlab/github tidy.

Tests / behavior:
- Wire registerNotifyWatcherHooks; debounce notify files past the kqueue
  rescan window so cross-instance settings/collections reload fires.
- migratecmd: exclude createdAt/updatedAt (not created/updated); regenerate
  Go+JS automigration goldens from canonical output.
- Convert the two stale archive red-probes to assert the landed defenses
  (backlog cap bounds memory; forged-segment signature is rejected).

IAM-native (Base = pure IAM client):
- Rip the ~1k-line home-grown embedded IAM (iam_embedded*.go) and the
  iam-user CLI. Base no longer reinvents identity as _iam_users/_superusers
  rows. IAM_ENDPOINT (hanzo.id, or an in-process iam.Embed in the fused
  daemon) is required; Base validates IAM JWTs via JWKS only — superuser is
  the IAM admin claim, resolved to an ephemeral unpersisted record.
- Extract the IDV reverse-proxy (/v1/idv/*) into idv_proxy.go — it was
  mis-colocated in the embedded onboarding file and is independent of IAM.
- Bind the SQL console (/v1/sql) and test it through real IAM JWTs against a
  JWKS stub (no hardcoded record tokens, no persisted rows).
2026-06-10 21:29:14 -07:00

124 lines
2.7 KiB
Go

package core
import (
"os"
"strconv"
"strings"
"testing"
)
func TestSendSystemAlert(t *testing.T) {
t.Parallel()
testDataDir, err := os.MkdirTemp("", "sendSystemAlert_pb_data")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testDataDir)
testApp := NewBaseApp(BaseAppConfig{
DataDir: testDataDir,
})
defer testApp.ResetBootstrapState()
if err := testApp.Bootstrap(); err != nil {
t.Fatal(err)
}
if err := createTestSuperusers(testApp, 3); err != nil {
t.Fatal(err)
}
superuser, err := testApp.FindAuthRecordByEmail(CollectionNameSuperusers, "[email protected]")
if err != nil {
t.Fatal(err)
}
var sendCalls int
testApp.OnMailerSend().BindFunc(func(e *MailerEvent) error {
sendCalls++
if !strings.Contains(e.Message.Subject, "test_subject") {
t.Fatalf("Missing %q in Message.Subject:\n%s", "test_subject", e.Message.Subject)
}
if !strings.Contains(e.Message.HTML, "test_details") {
t.Fatalf("Missing %q in Message.HTML:\n%s", "test_details", e.Message.HTML)
}
if len(e.Message.To) != 1 || e.Message.To[0].Address != "[email protected]" {
t.Fatalf("Expected To address %q, got %v", "[email protected]", e.Message.To)
}
return nil
})
sendSystemAlert(testApp, superuser, "test_subject", "test_details")
if sendCalls != 1 {
t.Fatalf("Expected 1 mail send call, got %d", sendCalls)
}
}
func TestSendSystemAlertToAllSuperusers(t *testing.T) {
t.Parallel()
testDataDir, err := os.MkdirTemp("", "sendSystemAlertToAllSuperusers_pb_data")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testDataDir)
testApp := NewBaseApp(BaseAppConfig{
DataDir: testDataDir,
})
defer testApp.ResetBootstrapState()
if err := testApp.Bootstrap(); err != nil {
t.Fatal(err)
}
if err := createTestSuperusers(testApp, 3); err != nil {
t.Fatal(err)
}
var sendCalls int
testApp.OnMailerSend().BindFunc(func(e *MailerEvent) error {
sendCalls++
if !strings.Contains(e.Message.Subject, "test_subject") {
t.Fatalf("Missing %q in Message.Subject:\n%s", "test_subject", e.Message.Subject)
}
if !strings.Contains(e.Message.HTML, "test_details") {
t.Fatalf("Missing %q in Message.HTML:\n%s", "test_details", e.Message.HTML)
}
return nil
})
sendSystemAlertToAllSuperusers(testApp, "test_subject", "test_details")
if sendCalls != 3 {
t.Fatalf("Expected 3 mail send calls, got %d", sendCalls)
}
}
func createTestSuperusers(app App, total int) error {
superusersCollection, err := app.FindCollectionByNameOrId(CollectionNameSuperusers)
if err != nil {
return err
}
for i := range total {
superuser := NewRecord(superusersCollection)
superuser.SetEmail("test" + strconv.Itoa(i+1) + "@example.com")
if err := app.Save(superuser); err != nil {
return err
}
}
return nil
}