Files
archie/internal/domain/org/context_test.go
T
sam da731338f1 fix(test): a table case holds the org, not a context
containedctx rejects a struct field of type context.Context, and both new
table tests stored one. Each case now carries what the caller put on the
context -- a builder in the org test, the org id in the client test -- and
builds the context inside the subtest.
2026-09-30 07:24:09 +10:00

55 lines
1.9 KiB
Go

package org
import (
"context"
"testing"
)
// foreignContextKey stands in for another package's key: a value stored under
// it must never be read back as the org.
type foreignContextKey struct{}
func TestOrgFromContext(t *testing.T) {
// build derives each case's context from a fresh background one. The table
// holds no context.Context field on purpose: a stored context is what
// containedctx rejects, and a builder keeps every case readable without one.
tests := []struct {
name string
build func(context.Context) context.Context
want OrgID
}{
{name: "unset is the default org", build: func(ctx context.Context) context.Context { return ctx }, want: DefaultOrgID},
{name: "set org", build: func(ctx context.Context) context.Context { return WithOrg(ctx, "acme") }, want: "acme"},
{
name: "inner org overrides an outer one",
build: func(ctx context.Context) context.Context { return WithOrg(WithOrg(ctx, "outer"), "inner") },
want: "inner",
},
{
name: "nil stored value is the default org",
build: func(ctx context.Context) context.Context { return context.WithValue(ctx, orgContextKey{}, nil) },
want: DefaultOrgID,
},
{
name: "a value of a foreign type is the default org",
build: func(ctx context.Context) context.Context { return context.WithValue(ctx, orgContextKey{}, "acme") },
want: DefaultOrgID,
},
{
name: "a value under a foreign key is the default org",
build: func(ctx context.Context) context.Context {
return context.WithValue(ctx, foreignContextKey{}, OrgID("acme"))
},
want: DefaultOrgID,
},
{name: "empty org is the default org", build: func(ctx context.Context) context.Context { return WithOrg(ctx, "") }, want: DefaultOrgID},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := OrgFromContext(tt.build(context.Background())); got != tt.want {
t.Fatalf("OrgFromContext() = %q, want %q", got, tt.want)
}
})
}
}