Files
fm_be/internal/shared/pkg/appctx/appctx_test.go
2026-07-16 22:16:45 +07:00

47 lines
976 B
Go

package appctx
import (
"context"
"testing"
"wucher/internal/shared/pkg/uuidv7"
)
func TestWithUserIDAndGetUserID(t *testing.T) {
id, err := uuidv7.ParseString("00000000-0000-0000-0000-000000000001")
if err != nil {
t.Fatal(err)
}
ctx := WithUserID(context.Background(), id)
got := GetUserID(ctx)
if len(got) != len(id) {
t.Fatalf("expected %d bytes, got %d", len(id), len(got))
}
if string(got) != string(id) {
t.Fatalf("expected %x, got %x", id, got)
}
got[0] ^= 0xff
if string(GetUserID(ctx)) == string(got) {
t.Fatal("GetUserID returned a reference to internal data")
}
}
func TestGetUserIDFromLegacyContext(t *testing.T) {
ctx := &legacyContext{Context: context.Background(), userID: []byte("legacy")}
got := GetUserID(ctx)
if string(got) != "legacy" {
t.Fatalf("expected legacy user id, got %s", got)
}
}
type legacyContext struct {
context.Context
userID []byte
}
func (c *legacyContext) UserValue(_ any) any {
return c.userID
}