Files
framework/module_installed_test.go
zeekayandHanzo Dev 9be6fecb65 framework: extract the DocType engine as a standalone module
Split from hanzoai/cloud clients/framework, preserving history. The engine
no longer depends on hanzoai/cloud at all.

Everything the engine took from its host is now an injected seam:

- cloud.Service[state] + the package-global 'mounted' become an explicit
  *Engine value, so a process can run more than one and nothing is
  reachable through a global.
- cek.Open becomes Config.OpenDB. Storage policy is the host's: cloud
  injects its encrypted-at-rest opener, a standalone app gets plain
  SQLite. The engine never decides encryption.
- principal.Org becomes the Caller value. The engine needs the validated
  tenant, not the ability to go looking for one, so it takes a value
  rather than a resolver and cannot be handed a request to inspect.
- cloud.Handle HTTP handlers become Engine operations that enforce
  permissions themselves, so a host cannot forget the gate and does not
  reimplement it. framework.go -> ops.go.
- zip HTTP status mapping becomes Classify + Code, so the engine keeps
  the knowledge of what a failure means and hosts cannot drift.

Operations return Doc (document + its schema) so Wire, the Password
redaction choke point, can never be handed a mismatched schema.

Adds ops_test.go: permission enforcement moved out of the HTTP handlers,
where it was only reachable through a router, so it is proven here at the
level every host shares.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 23:18:57 -07:00

41 lines
1.4 KiB
Go

package framework
import (
"context"
"github.com/hanzoai/doctype"
"testing"
)
// TestModuleInstalled_OrgScoped proves the module-granularity observe read
// (bound as guide's ModuleInstalled growth seam) is strictly org-scoped and
// honest-degrading: it reports a module present ONLY for the org that installed it,
// an unknown module reads false, and the boolean never exposes another org's data.
func TestModuleInstalled_OrgScoped(t *testing.T) {
s, done := mountForIngest(t)
defer done()
ctx := context.Background()
doctype.ResetModules()
t.Cleanup(doctype.ResetModules)
RegisterModule("shop", []DocType{{Name: "Widget", Fields: []DocField{{Fieldname: "sku", Fieldtype: FieldData}}}})
// orgA installs the module's doctype; orgB installs nothing.
if _, err := s.store.CreateDocType(ctx, "orgA", DocType{
Name: "Widget", Module: "shop",
Fields: []DocField{{Fieldname: "sku", Fieldtype: FieldData}},
Perms: []DocPerm{{Role: RoleSystemManager, Read: true, Write: true, Create: true, Delete: true}},
}); err != nil {
t.Fatalf("install: %v", err)
}
if !s.ModuleInstalled(ctx, "orgA", "shop") {
t.Fatal("orgA installed shop → ModuleInstalled must be true")
}
if s.ModuleInstalled(ctx, "orgB", "shop") {
t.Fatal("cross-tenant leak: orgB must NOT observe orgA's installed module")
}
if s.ModuleInstalled(ctx, "orgA", "not-a-module") {
t.Fatal("an unregistered module must read false")
}
}