Files
mxaccessgw/clients/go/mxgateway/errors_auth_test.go
T

34 lines
1.1 KiB
Go

package mxgateway
import (
"errors"
"testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestGatewayErrorClassifiesAuthCodes(t *testing.T) {
unauth := &GatewayError{Op: "invoke", Err: status.Error(codes.Unauthenticated, "bad key")}
denied := &GatewayError{Op: "invoke", Err: status.Error(codes.PermissionDenied, "no")}
other := &GatewayError{Op: "invoke", Err: status.Error(codes.Unavailable, "down")}
if !errors.Is(unauth, ErrUnauthenticated) || errors.Is(unauth, ErrPermissionDenied) {
t.Fatal("Unauthenticated misclassified")
}
if !errors.Is(denied, ErrPermissionDenied) || errors.Is(denied, ErrUnauthenticated) {
t.Fatal("PermissionDenied misclassified")
}
if errors.Is(other, ErrUnauthenticated) || errors.Is(other, ErrPermissionDenied) {
t.Fatal("Unavailable must match neither")
}
var ge *GatewayError
if !errors.As(error(unauth), &ge) {
t.Fatal("errors.As to *GatewayError must still work")
}
wrapped := redactSecrets(unauth, "x")
if !errors.Is(wrapped, ErrUnauthenticated) {
t.Fatal("classification must survive the redaction wrapper")
}
}