package authn import ( "context" "testing" "git.sr.ht/~sircmpwn/core-go/auth" "go.bigb.es/sourcehut-dolt/core" ) func TestCallerFromContext_Anonymous(t *testing.T) { if got := CallerFromContext(context.Background()); got != nil { t.Fatalf("expected nil caller for empty context, got %+v", got) } } func TestWithCallerRoundTrip(t *testing.T) { ac := &auth.AuthContext{UserID: 7, Username: "bigbes"} ctx := WithCaller(context.Background(), ac) got := CallerFromContext(ctx) if got != ac { t.Fatalf("expected to retrieve the stored caller, got %+v", got) } } func TestAsCoreCaller_Nil(t *testing.T) { if got := AsCoreCaller(nil); got != nil { t.Fatalf("nil auth context must map to nil (anonymous) caller, got %+v", got) } } func TestAsCoreCaller_Mapping(t *testing.T) { cases := []struct { name string userType string wantType core.UserType wantSuspended bool }{ {"user", auth.USER_TYPE_USER, core.UserTypeUser, false}, {"admin", auth.USER_TYPE_ADMIN, core.UserTypeAdmin, false}, {"pending", auth.USER_TYPE_PENDING, core.UserTypePending, false}, {"suspended", auth.USER_TYPE_SUSPENDED, core.UserTypeSuspended, true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { ac := &auth.AuthContext{UserID: 42, Username: "u", UserType: tc.userType} got := AsCoreCaller(ac) if got == nil { t.Fatal("expected non-nil caller") } if got.UserID != 42 || got.Username != "u" { t.Fatalf("identity not carried through: %+v", got) } if got.UserType != tc.wantType { t.Fatalf("UserType = %q, want %q", got.UserType, tc.wantType) } if got.Suspended != tc.wantSuspended { t.Fatalf("Suspended = %v, want %v", got.Suspended, tc.wantSuspended) } }) } }