Files
Enterprise-Knowledge-Base/internal/middleware/auth_tenantless_test.go
T
wizardchen 87ba3fc2de refactor(auth): unify OIDC provisioning under shared default_tenant_mode policy
Drop the standalone OIDC_AUTH_DEFAULT_TENANT_MODE / oidc_auth.default_tenant_mode
knob so OIDC auto-provisioning and local password registration share the single
auth.default_tenant_mode policy (resolved by the callback handler). Repair
tenantless sessions by falling back to the first active membership on the login
happy path and stale-preference paths, allow tenantless PUT /auth/me/preferences,
clear the tenant snapshot on /auth/me responses without a tenant, and default the
frontend canCreateTenant capability to fail-closed until /auth/me confirms it.
2026-07-13 19:14:49 +08:00

48 lines
1.2 KiB
Go

package middleware
import (
"context"
"net/http"
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
func TestTenantOptionalAPISurface(t *testing.T) {
tests := []struct {
method string
path string
want bool
}{
{http.MethodGet, "/api/v1/auth/me", true},
{http.MethodPut, "/api/v1/auth/me", true},
{http.MethodPut, "/api/v1/auth/me/preferences", true},
{http.MethodPost, "/api/v1/tenants", true},
{http.MethodGet, "/api/v1/me/invitations", true},
{http.MethodPost, "/api/v1/me/invitations/12/accept", true},
{http.MethodGet, "/api/v1/knowledge-bases", false},
{http.MethodGet, "/api/v1/tenants", false},
}
for _, tt := range tests {
if got := isTenantOptionalAPI(tt.path, tt.method); got != tt.want {
t.Errorf("isTenantOptionalAPI(%s %s) = %v, want %v", tt.method, tt.path, got, tt.want)
}
}
}
func TestResolveFirstMembershipTarget(t *testing.T) {
members := newFakeMemberService()
members.seedActive("tenantless-user", 42, types.TenantRoleViewer)
tenants := &fakeTenantService{tenant: &types.Tenant{ID: 42}}
got := resolveFirstMembershipTarget(
context.Background(),
&types.User{ID: "tenantless-user"},
members,
tenants,
)
if got != 42 {
t.Fatalf("resolved tenant = %d, want 42", got)
}
}