mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Ninety-one errors, all residue of the same mechanical pass: recorders read after the response moved onto the context, a probe renamed mid-conversion so its old name resolved to a struct and read as a type conversion, harnesses returning a recorder they no longer had, and signatures that had already become pure functions (bearerToken of two strings, publicAddr of two, relayZenStream of its destination) still being handed a request. The tests that survive say what they always meant, and a few say it better: bearerToken's is a truth table now because the function is one, and the reward and contribution suites present a credential instead of seeding a store. Three tests are gone rather than converted, each pinning something that no longer exists: two host-resolver tests superseded by the stamped header, a session-path test that had become the bearer test beside it, and a nil-session table that was the table above it with four rows missing. go mod tidy runs for the first time on this branch — nothing imports the deleted router any more — so the build no longer needs -mod=mod.
98 lines
4.0 KiB
Go
98 lines
4.0 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 controllers
|
|
|
|
import (
|
|
"testing"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
)
|
|
|
|
// TestResolveAccountAction pins the get-account identity policy: a resolved
|
|
// subject (real, or self-healed from a verified IAM credential) ALWAYS gets its
|
|
// canonical identity — never an anonymous u-<hash> record — and an unresolved
|
|
// identity on a protected surface fails LOUD instead of fabricating a guest.
|
|
func TestResolveAccountAction(t *testing.T) {
|
|
realUser := &iam.User{Owner: "hanzo", Name: "z", IsAdmin: true}
|
|
cases := []struct {
|
|
name string
|
|
sessionUser *iam.User
|
|
anonymousAllowed bool
|
|
want accountAction
|
|
}{
|
|
{"real subject, public surface -> canonical identity", realUser, true, serveIdentity},
|
|
{"real subject, protected surface -> canonical identity", realUser, false, serveIdentity},
|
|
{"no identity, public/preview surface -> anonymous", nil, true, serveAnonymous},
|
|
{"no identity, protected surface -> fail loud", nil, false, failUnauthenticated},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := resolveAccountAction(tc.sessionUser, tc.anonymousAllowed); got != tc.want {
|
|
t.Errorf("%s: resolveAccountAction = %d, want %d", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestSessionNeedsSelfHeal pins the cookie-path fix: get-account must attempt
|
|
// credential self-heal not only when the session is empty, but ALSO when the
|
|
// session already holds an anonymous guest — the case where an earlier
|
|
// anonymousSignin bound a u-<hash> record that would otherwise shadow a real
|
|
// signed-in subject (the u-19669097 admin-403 bug). A resolved real subject
|
|
// needs no self-heal.
|
|
func TestSessionNeedsSelfHeal(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
sessionUser *iam.User
|
|
want bool
|
|
}{
|
|
{"no session -> self-heal", nil, true},
|
|
{"anonymous guest by type -> self-heal", &iam.User{Type: "anonymous-user", Name: "someone"}, true},
|
|
{"anonymous guest by u-<hash> name -> self-heal", &iam.User{Name: "u-19669097"}, true},
|
|
{"real subject -> no self-heal", &iam.User{Owner: "hanzo", Name: "z", IsAdmin: true}, false},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := sessionNeedsSelfHeal(tc.sessionUser); got != tc.want {
|
|
t.Errorf("%s: sessionNeedsSelfHeal = %v, want %v", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBearerTokenCookieFallback is the credential source, as its own truth table:
|
|
// the Authorization header wins when both are present, the hanzo_iam_token cookie is
|
|
// honoured when it is not (the browser cookie case, which is what self-heal reads),
|
|
// and nothing yields nothing.
|
|
//
|
|
// It reads as a table because the function IS one — two values in, one out. It used
|
|
// to build a request and set a header and a cookie on it to say the same thing.
|
|
func TestBearerTokenCookieFallback(t *testing.T) {
|
|
const headerTok = "hdr.jwt.sig"
|
|
const cookieTok = "cookie.jwt.sig"
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
authorization string
|
|
cookie string
|
|
want string
|
|
}{
|
|
{"header wins over a cookie", "Bearer " + headerTok, cookieTok, headerTok},
|
|
{"cookie alone still carries one", "", cookieTok, cookieTok},
|
|
{"neither is no credential", "", "", ""},
|
|
{"a header that is not a Bearer names nothing", "Basic " + headerTok, "", ""},
|
|
} {
|
|
if got := bearerToken(tc.authorization, tc.cookie); got != tc.want {
|
|
t.Errorf("%s: bearerToken(%q, %q) = %q, want %q", tc.name, tc.authorization, tc.cookie, got, tc.want)
|
|
}
|
|
}
|
|
}
|