ffe4d335aa
Introduces current-workspace context helpers plus a WorkspaceScope middleware that validates X-Workspace-ID against workspace memberships, defaulting to the actor's primary workspace. Prepares monitoring paths to resolve quota/access-state per request workspace instead of per tenant. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestWithActorAndFromCtx(t *testing.T) {
|
|
actor := Actor{UserID: 1, TenantID: 10, PrimaryWorkspaceID: 100, Role: "editor"}
|
|
ctx := WithActor(context.Background(), actor)
|
|
|
|
got, ok := ActorFromCtx(ctx)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, actor, got)
|
|
}
|
|
|
|
func TestActorFromCtxMissing(t *testing.T) {
|
|
_, ok := ActorFromCtx(context.Background())
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestMustActorPanics(t *testing.T) {
|
|
assert.Panics(t, func() {
|
|
MustActor(context.Background())
|
|
})
|
|
}
|
|
|
|
func TestMustActorReturns(t *testing.T) {
|
|
actor := Actor{UserID: 5, TenantID: 20, PrimaryWorkspaceID: 200, Role: "tenant_admin"}
|
|
ctx := WithActor(context.Background(), actor)
|
|
|
|
got := MustActor(ctx)
|
|
assert.Equal(t, actor, got)
|
|
}
|
|
|
|
func TestActorCarriesPrimaryWorkspaceID(t *testing.T) {
|
|
ctx := WithActor(context.Background(), Actor{
|
|
UserID: 10,
|
|
TenantID: 20,
|
|
PrimaryWorkspaceID: 30,
|
|
Role: "member",
|
|
})
|
|
|
|
actor, ok := ActorFromCtx(ctx)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, int64(30), actor.PrimaryWorkspaceID)
|
|
}
|
|
|
|
func TestCurrentWorkspaceIDFallsBackToPrimaryWorkspace(t *testing.T) {
|
|
ctx := WithActor(context.Background(), Actor{
|
|
UserID: 10,
|
|
TenantID: 20,
|
|
PrimaryWorkspaceID: 30,
|
|
Role: "member",
|
|
})
|
|
|
|
assert.Equal(t, int64(30), CurrentWorkspaceID(ctx))
|
|
_, ok := CurrentWorkspaceIDFromCtx(ctx)
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestCurrentWorkspaceIDUsesScopedWorkspace(t *testing.T) {
|
|
ctx := WithActor(context.Background(), Actor{
|
|
UserID: 10,
|
|
TenantID: 20,
|
|
PrimaryWorkspaceID: 30,
|
|
Role: "member",
|
|
})
|
|
ctx = WithCurrentWorkspaceID(ctx, 40)
|
|
|
|
got, ok := CurrentWorkspaceIDFromCtx(ctx)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, int64(40), got)
|
|
assert.Equal(t, int64(40), CurrentWorkspaceID(ctx))
|
|
}
|