caelum0x ecb3242edc fix(card-issuance): surface unknown card IDs as gRPC NotFound, not Unknown
cardsvc.Service.GetCard/AdjustCardLimit wrapped a missing card as a plain
fmt.Errorf(..., airwallex.ErrNotFound), which the gRPC runtime downgrades
to codes.Unknown by default — the gateway's REST facade (services/gateway/
internal/httpapi/cards.go) documented this exact gap as its own TODO: an
unknown card_id surfaced as HTTP 500 instead of 404 for every
GatewayClient caller (TUI, CLI, both SDKs, the REST facade).
grpcserver.Server now wraps cardsvc's not-found error in
status.Error(codes.NotFound, ...) before it leaves card-issuance, so the
existing NotFound->404 mapping downstream works as originally intended.
RED->GREEN with new grpcserver tests; cleared the now-resolved TODOs in
cards.go and updated docs/THREAT-MODEL.md's "Known gaps" list, which
still read webhook rate-limiting/replay-window as open after an earlier
commit had already fixed both. Full go/rust suite green.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-21 16:18:38 +03:00

76 lines
2.8 KiB
Go

package grpcserver
import (
"context"
"testing"
cardissuancepb "github.com/caelum0x/ration/pkg/pb/cardissuance"
ledgerpb "github.com/caelum0x/ration/pkg/pb/ledger"
"github.com/caelum0x/ration/services/card-issuance/internal/airwallex"
"github.com/caelum0x/ration/services/card-issuance/internal/cardsvc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// fakeLedger is a minimal cardsvc.LedgerClient stub — these tests never
// exercise a path that calls it, but cardsvc.New requires one.
type fakeLedger struct{}
func (fakeLedger) GetTenant(ctx context.Context, tenantID string) (*ledgerpb.Tenant, error) {
return &ledgerpb.Tenant{Id: tenantID, Currency: "USD"}, nil
}
func (fakeLedger) CheckReserveFloor(ctx context.Context, tenantID string, proposedDebitMinorUnits int64) (*ledgerpb.CheckReserveFloorResponse, error) {
return &ledgerpb.CheckReserveFloorResponse{Allowed: true}, nil
}
func (fakeLedger) ApplyEntry(ctx context.Context, req *ledgerpb.ApplyEntryRequest) (*ledgerpb.ApplyEntryResponse, error) {
return &ledgerpb.ApplyEntryResponse{Entry: &ledgerpb.LedgerEntry{Id: "entry_test"}}, nil
}
// TestGetCard_NotFound_ReturnsNotFoundStatus guards against the
// precise gap documented in services/gateway/internal/httpapi/cards.go's
// handleGetCard TODO: an unknown card ID must surface as a gRPC
// status.Error(codes.NotFound, ...), not a plain wrapped error that the
// gRPC runtime downgrades to codes.Unknown (and the REST facade in turn
// serves as a 500 instead of a 404).
func TestGetCard_NotFound_ReturnsNotFoundStatus(t *testing.T) {
cards := cardsvc.New(airwallex.NewMock(), fakeLedger{})
srv := New(cards, nil)
_, err := srv.GetCard(context.Background(), &cardissuancepb.GetCardRequest{CardId: "does-not-exist"})
if err == nil {
t.Fatal("GetCard() error = nil, want codes.NotFound")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("GetCard() error = %v, not a gRPC status error", err)
}
if st.Code() != codes.NotFound {
t.Errorf("GetCard() code = %v, want %v", st.Code(), codes.NotFound)
}
}
// TestAdjustCardLimit_NotFound_ReturnsNotFoundStatus is the same
// guarantee for AdjustCardLimit, which hits the identical
// airwallex.ErrNotFound path in cardsvc.Service for an unknown card ID.
func TestAdjustCardLimit_NotFound_ReturnsNotFoundStatus(t *testing.T) {
cards := cardsvc.New(airwallex.NewMock(), fakeLedger{})
srv := New(cards, nil)
_, err := srv.AdjustCardLimit(context.Background(), &cardissuancepb.AdjustCardLimitRequest{
CardId: "does-not-exist",
NewLimitMinorUnits: 100,
})
if err == nil {
t.Fatal("AdjustCardLimit() error = nil, want codes.NotFound")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("AdjustCardLimit() error = %v, not a gRPC status error", err)
}
if st.Code() != codes.NotFound {
t.Errorf("AdjustCardLimit() code = %v, want %v", st.Code(), codes.NotFound)
}
}