package router import ( "crypto/sha256" "net/http" "net/http/httptest" "strings" "testing" "time" "gitea.mynodes.xyz/ilker/llmgw/internal/auth" "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) func TestAnOwnedAdapterIsSeenOnlyInItsPartOfTheTree(t *testing.T) { e := newFakeEngine(t, "qwen2.5-14b-instruct") rt, owner := pricedRouter(t, e.srv.URL, "tone") ctx := t.Context() key, _ := rt.store.KeyByID(ctx, "key-1") team, _ := rt.store.CreateTeam(ctx, mustOrgOf(t, rt), "other-team") other, _ := rt.store.CreateProject(ctx, team, "other") otherSecret := store.KeyPrefix + strings.Repeat("b", 43) sum := sha256.Sum256([]byte(otherSecret)) if err := rt.store.CreateKey(ctx, store.Key{ID: "key-2", Hash: sum[:], Prefix: "bbbb", Name: "o", ProjectID: other, CreatedAt: time.Now()}); err != nil { t.Fatal(err) } rt.storeModels([]catalog.Model{{Name: "qwen2.5-14b-instruct", Aliases: []string{"q14"}, Adapters: []catalog.Adapter{{Name: "tone", Source: "/t.gguf", Owner: "project:" + key.ProjectID}}}}) h := rt.Handler() ask := func(secret string) int { req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(`{"model":"tone","messages":[{"role":"user","content":"hi"}]}`)) if secret != "" { req.Header.Set("Authorization", "Bearer "+secret) } rec := httptest.NewRecorder() h.ServeHTTP(rec, req) return rec.Code } if code := ask(owner); code != http.StatusOK { t.Errorf("its own project: %d", code) } if code := ask(otherSecret); code != http.StatusNotFound { t.Errorf("another project: %d", code) } list := func(secret string) string { req := httptest.NewRequest(http.MethodGet, "/v1/models", nil) req.Header.Set("Authorization", "Bearer "+secret) rec := httptest.NewRecorder() h.ServeHTTP(rec, req) return rec.Body.String() } if !strings.Contains(list(owner), `"tone"`) || strings.Contains(list(otherSecret), `"tone"`) { t.Errorf("listed: own %s\nother %s", list(owner), list(otherSecret)) } // No identity, no owned adapter. if rt.adapterHidden(auth.Principal{}, false, "tone") != true { t.Error("an owned adapter was shown to a caller with no identity") } }