Files
Pulse/internal/api/websocket_isolation_test.go
rcourtman 9072b8eaa8 feat: enhance API router with multi-tenant authorization
Router & Middleware:
- Add auth context middleware for user/token extraction
- Add tenant middleware with authorization checking
- Refactor middleware chain ordering for proper isolation
- Add router helpers for common patterns

Authentication & SSO:
- Enhance auth with tenant-aware context
- Update OIDC, SAML, and SSO handlers for multi-tenant
- Add RBAC handler improvements
- Add security enhancements

New Test Coverage:
- API foundation tests
- Auth and authorization tests
- Router state and general tests
- SSO handler CRUD tests
- WebSocket isolation tests
- Resource handler tests
2026-01-24 22:42:23 +00:00

63 lines
1.8 KiB
Go

package api
import (
"context"
"testing"
"github.com/rcourtman/pulse-go-rewrite/internal/license"
"github.com/stretchr/testify/assert"
)
// WSMockLicenseService for checking feature flags in tests
type WSMockLicenseService struct {
Features map[string]bool
}
func (m *WSMockLicenseService) HasFeature(feature string) bool {
return m.Features[feature]
}
func (m *WSMockLicenseService) Service(ctx context.Context) *license.Service {
// Return empty service (no license) by default
return license.NewService()
}
// WebSocketMockLicenseProvider to return our mock service
type WebSocketMockLicenseProvider struct {
service *WSMockLicenseService
}
func (p *WebSocketMockLicenseProvider) Service(ctx context.Context) *license.Service {
return license.NewService()
}
func TestWebSocketIsolation_Permanent(t *testing.T) {
// Reset global state
defer SetMultiTenantEnabled(false)
checker := NewMultiTenantChecker()
// Case 1: Default Org always allowed
result := checker.CheckMultiTenant(context.Background(), "default")
assert.True(t, result.Allowed)
assert.True(t, result.FeatureEnabled)
assert.True(t, result.Licensed)
// Case 2: Multi-Tenant Disabled (Flag=False)
SetMultiTenantEnabled(false)
result = checker.CheckMultiTenant(context.Background(), "tenant-1")
assert.False(t, result.Allowed)
assert.False(t, result.FeatureEnabled, "Feature should be disabled")
assert.Contains(t, result.Reason, "not enabled")
// Case 3: Flag=True, License=False
SetMultiTenantEnabled(true)
// Default license provider returns no license, which is what we want to test (Block unlicensed)
result = checker.CheckMultiTenant(context.Background(), "tenant-1")
assert.False(t, result.Allowed)
assert.True(t, result.FeatureEnabled)
assert.False(t, result.Licensed, "Should be unlicensed by default")
assert.Contains(t, result.Reason, "Enterprise license")
}