Files
ai/controllers/zap_providers-admin_test.go
zeekay fc2d751dc2 admin: ai's operator routes answer under /v1/admin/ai
A host files an app's operator view at /v1/admin/<app> (HIP-0139 §3),
and cloud routes the rest of /v1/admin to its own admin app. Through
api.hanzo.ai, every ai SuperAdmin route at /v1/admin/... answered 404:
providers and their toggle and primary, model-access,
reload-model-config, refresh-model-pricing and usage/backfill-do. Each
now answers under /v1/admin/ai, with the same gate, its normalized
variants included.

Claude-Session: https://claude.ai/code/session_01SCPMGSQsLNF2YtHFL4bwyp
2026-10-09 02:10:58 -07:00

119 lines
4.4 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"context"
"encoding/json"
"testing"
"github.com/hanzoai/ai/object"
)
// gatedProviderMethods are the group's super-admin-gated native methods
// (everything except the public models/providers projection). Each MUST reject an
// anonymous request with a 401 BEFORE touching the DB — the native-path
// re-enforcement of the the router authz filter's superAdminEndpoints gate.
var gatedProviderMethods = []string{
"providers.global.list",
"providers.list",
"providers.get",
"providers.update",
"providers.add",
"providers.delete",
"providers.mcp.refresh",
"admin.providers.list",
"admin.providers.toggle",
"admin.providers.primary",
}
// TestZapProviderAuthRejection asserts every gated handler fails closed with 401
// on an empty Bearer credential, and short-circuits before any DB access (no
// adapter is initialized in this unit test, so a handler that reached the store
// would panic — reaching the 401 assertion proves the gate ran first).
func TestZapProviderAuthRejection(t *testing.T) {
for _, method := range gatedProviderMethods {
h, ok := lookupCloudHandler(method)
if !ok {
t.Fatalf("method %q not registered", method)
}
msg, err := h(context.Background(), "", nil)
if err != nil {
t.Fatalf("%s: unexpected err: %v", method, err)
}
if got := msg.Root().Uint32(object.CloudRespStatus); got != 401 {
t.Errorf("%s: empty auth status = %d, want 401", method, got)
}
}
}
// TestZapProviderBogusTokenRejected asserts an unverifiable Bearer token resolves
// to no principal (fail-secure) and is denied 401 — never granted.
func TestZapProviderBogusTokenRejected(t *testing.T) {
msg, _ := zapGetProvidersHandler(context.Background(), "Bearer not-a-real-token", nil)
if got := msg.Root().Uint32(object.CloudRespStatus); got != 401 {
t.Errorf("bogus token status = %d, want 401", got)
}
}
// TestZapProviderOkEnvelopeParity asserts the success encoding matches the controller
// ResponseOk envelope ({status:"ok",data}) and the paginated two-value form sets
// data2 — the console frontend contract the native path must preserve.
func TestZapProviderOkEnvelopeParity(t *testing.T) {
data := []string{"do-ai", "zen"}
msg, err := zapOk(data)
if err != nil {
t.Fatalf("zapOk: %v", err)
}
if got := msg.Root().Uint32(object.CloudRespStatus); got != 200 {
t.Fatalf("ok status = %d, want 200", got)
}
want, _ := json.Marshal(Response{Status: "ok", Data: data})
if got := msg.Root().Bytes(object.CloudRespBody); string(got) != string(want) {
t.Errorf("ok body = %s, want %s", got, want)
}
msg2, _ := zapOk(data, int64(2))
want2, _ := json.Marshal(Response{Status: "ok", Data: data, Data2: int64(2)})
if got := msg2.Root().Bytes(object.CloudRespBody); string(got) != string(want2) {
t.Errorf("paginated body = %s, want %s", got, want2)
}
}
// TestZapProviderRegistry asserts the group self-registered its full route set
// into both shared registries and that the gateway longest-prefix match keeps the
// multi-segment admin paths from being shadowed.
func TestZapProviderRegistry(t *testing.T) {
for _, method := range append(gatedProviderMethods, "models.providers") {
if _, ok := lookupCloudHandler(method); !ok {
t.Errorf("cloud method %q not registered", method)
}
}
// Gateway longest-prefix: /v1/admin/ai/providers/toggle must resolve to the
// toggle handler, NOT the shorter /v1/admin/ai/providers registration. Prove it
// by driving the resolved handler with an empty credential and asserting the
// gated 401 (both are gated, but an unregistered path would return !ok).
h, ok := lookupGatewayHandler("/v1/admin/ai/providers/toggle")
if !ok {
t.Fatalf("/v1/admin/ai/providers/toggle not registered")
}
msg, _ := h(context.Background(), "", []byte("{}"))
if got := msg.Root().Uint32(object.CloudRespStatus); got != 401 {
t.Errorf("toggle via gateway lookup status = %d, want 401", got)
}
}