Files
ai/web_wire_test.go
zeekay 3674dda05b the filters pass their own tests, and two ceilings were missing
routers/ and every test in it is green. That is the first real check on the filter
work, and running it found two defects and one habit worth naming.

THE HEADER CEILING WAS UNSET, and it is the twin of the body one. fasthttp's
per-connection read buffer is 4 KiB by default and that buffer caps the TOTAL
request headers, so a request whose headers do not fit is refused at the socket
before any handler — with a message naming a buffer size. Every authenticated
request here carries a signed IAM bearer and the gateway's identity set beside it,
which clears 4 KiB together. zip states the ceiling and names the seam in its own
comment; ai set BodyLimit and never set this. Found because a test signed a real
token and the request would not parse.

A FILTER IS MIDDLEWARE, so it is tested through an app. It ends in Continue(), and
Continue means "the next handler" — which a detached context does not have, so
calling one on a TestCtx panics inside fiber's Next. zip's own doc says TestCtx is
for calling ONE handler and a chain belongs to an app. probe.through does that, and
it puts each filter in the position it actually occupies, ahead of a handler.

A FIBER CTX IS POOLED. The harness first captured the Ctx the handler was reached
with and read it after the request; fiber releases its Ctx at the end, so that read
returns a recycled object with nothing of the request on it — and the tenant
attribution tests failed reporting an empty project, which reads exactly like the
filter never ran. Capture the VALUE, never the Ctx.

Two more honest corrections to the tests themselves:

  - A "*" mapping claims every verb, and the live table lists them one at a time
    because the router registers a handler per method. The doc-table check now
    asserts the PATH is served for a "*" row, which is what "*" says.
  - The probe never returns a silent zero on a failed run. It did, and a status of 0
    reads as "the filter answered nothing" — which sent three assertions looking for
    a bug in the filter that was in the harness.

And the tests that had nothing left to test are gone rather than adapted: the
auto-signin and static filter tests went with the filters, and the session
nil-safety test is recast onto the property that survived — no credential resolves
to nobody, and bills nobody, because the filters ask that before any controller.
2026-08-17 09:23:43 -07:00

117 lines
4.4 KiB
Go

// Copyright 2023-2025 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 ai
import (
"io"
"net/http"
"strings"
"testing"
"github.com/zap-proto/zip"
)
// wiredStatus drives one request through the fully wired native router — the
// filter chain (rewrites, auth, tenant, …) plus reflection dispatch — over the
// same /v1/* mount the unified binary uses, and returns the status code.
func wiredStatus(t *testing.T, method, path, body string) int {
t.Helper()
app := zip.New(zip.Config{DisableStartupMessage: true, ReadBufferSize: 32 << 10})
routes(app)
var r io.Reader
if body != "" {
r = strings.NewReader(body)
}
req, _ := http.NewRequest(method, "http://example.com"+path, r)
req.Header.Set("Content-Type", "application/json")
resp, err := app.Fiber().Test(req)
if err != nil {
t.Fatalf("Test %s %s: %v", method, path, err)
}
return resp.StatusCode
}
// TestWiredResourceRouteDispatches proves the generated resource surface is
// live through the fully-wired router: a namespaced REST path reaches its
// handler rather than 404-ing.
func TestWiredResourceRouteDispatches(t *testing.T) {
for _, c := range []struct{ method, path string }{
{"GET", "/v1/ai/account"},
{"GET", "/v1/ai/deployments"},
{"GET", "/v1/ai/stores/acme/thing"},
{"GET", "/v1/ai/stores"},
{"GET", "/v1/ai/version"},
// NB: /v1/ai/signin is deliberately not probed here — its handler makes a
// live OAuth call, so a pass would depend on the network. Its registration
// is covered by the table tests instead.
{"GET", "/v1/ai/signin-sessions"},
{"GET", "/v1/ai/usages/user-names"},
} {
if code := wiredStatus(t, c.method, c.path, "{}"); code == http.StatusNotFound {
t.Errorf("%s %s must dispatch, got 404", c.method, c.path)
}
}
}
// TestOldAddressesAreGone is the "one way" guarantee, stated as a test. Every
// endpoint used to be reachable at up to THREE URLs — the flat compound route,
// the same route under /v1/cloud/*, and (for auth) under /v1/ai/*. Each extra
// address was a place a policy could be applied inconsistently. They must 404.
func TestOldAddressesAreGone(t *testing.T) {
for _, c := range []struct{ method, path string }{
{"GET", "/v1/get-account"},
{"GET", "/v1/get-applications"},
{"GET", "/v1/get-stores"},
{"GET", "/v1/get-providers"},
{"POST", "/v1/add-application"},
{"POST", "/v1/signin"},
{"GET", "/v1/cloud/get-account"},
{"GET", "/v1/cloud/get-providers"},
{"GET", "/v1/ai/get-account"},
// The identity nouns ai borrowed from casibase. /v1/ai/permissions was a
// second address for the IAM service's permissions and has no successor
// here; the other three moved to names that say what ai actually serves.
{"GET", "/v1/ai/permissions"},
{"GET", "/v1/ai/users"},
{"GET", "/v1/ai/applications"},
{"GET", "/v1/ai/sessions"},
} {
// "Gone" means NO HANDLER RUNS. Usually that shows up as a 404 from the
// router. For a path whose name is still in the super-admin policy set
// (the set is keyed by name, and policyKey still derives that name for
// the live REST route), the authz filter denies an unauthenticated caller
// BEFORE routing — 401/403, also without dispatching. Both outcomes are
// the property under test; a 2xx or a 405 would mean a handler was found.
code := wiredStatus(t, c.method, c.path, "{}")
switch code {
case http.StatusNotFound, http.StatusUnauthorized, http.StatusForbidden:
default:
t.Errorf("%s %s must be GONE, got %d — a second address for one resource",
c.method, c.path, code)
}
}
}
// TestWiredGatedResourceAuthReject proves the super-admin gate still fires at the
// NEW address: unauthenticated provider reads are denied before dispatch.
func TestWiredGatedResourceAuthReject(t *testing.T) {
code := wiredStatus(t, "GET", "/v1/ai/providers", "")
if code != http.StatusUnauthorized && code != http.StatusForbidden {
t.Errorf("unauthenticated /v1/ai/providers must be denied (401/403), got %d", code)
}
}