Files
ai/controllers/model_access_test.go
zeekayandHanzo Dev 6eb509001d refactor(iam): ai off Casdoor iam-v1 → clean iam over OIDC/JWKS (internal/iam shim)
Drop the last old-stack coupling: ai no longer links the retired Casdoor SDK
module github.com/hanzoai/iam-v1 (which dragged beego/xorm/ldap/aliyun/aws into
ai's build for server code ai never ran). Replace it with an ai-internal
package internal/iam (package iam) that talks to Hanzo IAM (hanzo.id) over
standard OIDC — JWKS token verification against {endpoint}/v1/iam/.well-known/
jwks — and the IAM server's JSON REST API (/v1/iam/get-user, get-organization,
get-application, permissions, resources, send-email, oauth token exchange).

- internal/iam mirrors the IAM wire models. User/Permission/Resource keep the
  FULL field set with json tags: object/redact.go reflects over every User
  field by json tag as a fail-secure secret-redaction control, so a slim User
  would silently weaken it and break redact_test. Read-only response types
  (Organization/Application/Provider/Cert) are projections of the fields ai reads.
- All 79 importing files swap github.com/hanzoai/iam-v1 →
  github.com/hanzoai/ai/internal/iam (package name unchanged, so call sites are
  untouched). iam-v1 require dropped; go mod tidy.
- No Casdoor call was stubbed: SendEmail/UploadResource/GetResources/
  DeleteResourceWithTag were already clean /v1/iam/ REST calls, reproduced
  faithfully (zero behavior change), not faked.

Verify: CGO_ENABLED=0 go build ./... clean; go vet ./... clean; redaction
security tests pass (TestRedactUserSecrets/AllowlistFailSecure/Claims/NilSafe).

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-21 14:43:37 -07:00

100 lines
3.5 KiB
Go

// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
package controllers
import (
"strings"
"testing"
iam "github.com/hanzoai/ai/internal/iam"
)
// A family SKU that advertises access:"waitlist" is parsed as gated.
func TestZenModelGatedParse(t *testing.T) {
gated := zenWireModel{ID: "enso", Access: "waitlist"}.model()
if !gated.gated() {
t.Fatal("enso (access=waitlist) should parse as gated")
}
open := zenWireModel{ID: "zen5"}.model()
if open.gated() {
t.Fatal("zen5 (no access) should not be gated")
}
}
// familyAccessAllowed is the enforcement decision: a non-gated model is always
// allowed; a gated model with no grant (no DB / no row) is denied — closed by default.
func TestFamilyAccessAllowedDecision(t *testing.T) {
fam := &modelFamily{name: "enso", prefix: "enso"}
fam.byID = map[string]zenModel{
"enso": {ID: "enso", Access: "waitlist"},
"open": {ID: "open"},
}
c := &ApiController{}
// Non-gated model → allowed regardless of caller.
if !c.familyAccessAllowed(fam, "open", "acme", nil) {
t.Fatal("non-gated model must be allowed")
}
// Model unknown to discovery → treated as non-gated (allowed).
if !c.familyAccessAllowed(fam, "mystery", "acme", nil) {
t.Fatal("model unknown to discovery must be allowed (not gated)")
}
// Gated model, no grant row (no DB in test) → denied.
if c.familyAccessAllowed(fam, "enso", "acme", &iam.User{Owner: "acme", Name: "bob"}) {
t.Fatal("gated model without a grant must be denied")
}
}
// FamilyModelGated keys off discovery: a gated SKU is recognized as access-gated.
// Enso is PAID — gating controls ACCESS (a grant unlocks the SKU), never billing;
// there is no comp path, so a granted caller still pays the normal balance gate.
func TestFamilyModelGatedDecision(t *testing.T) {
// Seed the enso family's discovered catalog with a gated SKU.
saved := ensoFam.byID
ensoFam.byID = map[string]zenModel{"enso": {ID: "enso", Access: "waitlist"}}
defer func() { ensoFam.byID = saved }()
if !FamilyModelGated("enso") {
t.Fatal("enso should be recognized as gated")
}
if FamilyModelGated("zen5") {
t.Fatal("zen5 is not gated")
}
}
// The gated denial message tells the caller exactly how to request access.
func TestGatedAccessMessage(t *testing.T) {
msg := gatedAccessMessage("enso")
if !strings.Contains(msg, "limited preview") || !strings.Contains(msg, "POST /v1/models/enso/access") {
t.Fatalf("gated message missing the request-access instruction: %q", msg)
}
}
// mergeModels stamps a gated SKU with the default "waitlist" standing so /v1/models
// advertises the gating to everyone (the caller's real status is overlaid later).
func TestMergeModelsGatedAnnotation(t *testing.T) {
fam := &modelFamily{name: "enso", prefix: "enso"}
fam.byID = map[string]zenModel{
"enso": {ID: "enso", Access: "waitlist", Base: zenTier{}},
}
fam.ids = []string{"enso"}
fam.loaded = true // enabled() is false (no urlKey) so snapshot() uses this data directly
out := fam.mergeModels(nil)
if len(out) != 1 {
t.Fatalf("mergeModels returned %d models, want 1", len(out))
}
if out[0].Access == nil || out[0].Access.State != "waitlist" {
t.Fatalf("gated SKU access = %+v, want state=waitlist", out[0].Access)
}
}
// annotateModelAccess is a no-op without an authenticated caller (nothing to overlay).
func TestAnnotateModelAccessNilUser(t *testing.T) {
models := []modelInfo{{ID: "enso", Access: &modelAccessInfo{State: "waitlist"}}}
annotateModelAccess(models, nil)
if models[0].Access.State != "waitlist" {
t.Fatal("annotate with nil user must not change the default standing")
}
}