package bouncer import ( "context" "testing" ) // TestDispatchedSmoke covers the reader of the in-process dispatch marker // (phase 19.7 D-17): only a marker with a principal that lists the exact guard // name is honoured. func TestDispatchedSmoke(t *testing.T) { if _, ok := Dispatched(context.Background(), "api_token"); ok { t.Fatal("plain context reports a dispatch") } if _, ok := Dispatched(nil, "api_token"); ok { t.Fatal("nil context reports a dispatch") } noPrincipal := WithDispatch(context.Background(), &Dispatch{Guards: []string{"api_token"}}) if _, ok := Dispatched(noPrincipal, "api_token"); ok { t.Fatal("marker without a principal reports a dispatch") } marker := &Dispatch{Guards: []string{"api_token"}, Principal: &Principal{ID: 42}, Credential: "cred"} ctx := WithDispatch(context.Background(), marker) if _, ok := Dispatched(ctx, "backend"); ok { t.Fatal("unlisted guard reports a dispatch") } if _, ok := Dispatched(ctx, ""); ok { t.Fatal("empty guard name reports a dispatch") } if _, ok := Dispatched(ctx, "api_token*"); ok { t.Fatal("wildcard guard name reports a dispatch") } got, ok := Dispatched(ctx, "api_token") if !ok || got != marker || got.Principal.ID != 42 || got.Credential != "cred" { t.Fatalf("Dispatched = %+v, %v", got, ok) } if ctx := WithDispatch(nil, marker); ctx == nil { t.Fatal("WithDispatch(nil) returned nil") } }