Skip to content

Commit

Permalink
Add test coverage for "nice status" middleware/interceptor
Browse files Browse the repository at this point in the history
We were missing test coverage for this.
  • Loading branch information
JAORMX committed Feb 7, 2024
1 parent aacbc41 commit 2429168
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
1 change: 0 additions & 1 deletion internal/util/statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func (s *NiceStatus) Error() string {
// SanitizingInterceptor sanitized error statuses which do not conform to NiceStatus, ensuring
// that we don't accidentally leak implementation details over gRPC.
func SanitizingInterceptor() grpc.UnaryServerInterceptor {
// TODO: this has no test coverage!
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ret, err := handler(ctx, req)
if err != nil {
Expand Down
82 changes: 80 additions & 2 deletions internal/util/statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package util_test

import (
"context"
"fmt"
"testing"

"github.com/stacklok/minder/internal/util"

Check failure on line 23 in internal/util/statuses_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/stacklok/minder) (gci)
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"

"github.com/stacklok/minder/internal/util"
"google.golang.org/grpc/status"

Check failure on line 27 in internal/util/statuses_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/stacklok/minder) (gci)
)

func TestNiceStatusCreation(t *testing.T) {
Expand All @@ -37,3 +39,79 @@ func TestNiceStatusCreation(t *testing.T) {
expected := "Code: 0\nName: OK\nDescription: OK\nDetails: OK is returned on success."
require.Equal(t, expected, fmt.Sprint(s))
}

func TestSanitizingInterceptor(t *testing.T) {
t.Parallel()

tests := []struct {
name string
handler func(*testing.T) grpc.UnaryHandler
wantErr bool
errIsNice bool
}{
{
name: "success",
handler: func(t *testing.T) grpc.UnaryHandler {

Check failure on line 54 in internal/util/statuses_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

test helper function should start from t.Helper() (thelper)
return func(ctx context.Context, req interface{}) (interface{}, error) {
return "success", nil
}
},
wantErr: false,
},
{
name: "some error",
handler: func(t *testing.T) grpc.UnaryHandler {

Check failure on line 63 in internal/util/statuses_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

test helper function should start from t.Helper() (thelper)
return func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, status.Error(codes.Internal, "some error")
}
},
wantErr: true,
},
{
name: "nice error",
handler: func(t *testing.T) grpc.UnaryHandler {

Check failure on line 72 in internal/util/statuses_test.go

View workflow job for this annotation

GitHub Actions / lint / Go Lint

test helper function should start from t.Helper() (thelper)
return func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, util.UserVisibleError(codes.Internal, "some error")
}
},
wantErr: true,
errIsNice: true,
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()
handler := tt.handler(t)

i := util.SanitizingInterceptor()
ret, err := i(ctx, nil, nil, handler)
if tt.wantErr {
require.Error(t, err)
require.Nil(t, ret)

if tt.errIsNice {
require.IsType(t, &util.NiceStatus{}, err)
require.Contains(t, err.Error(), "Code: 13\nName: INTERNAL\nDescription: Server error\nDetails: some error")

st := status.Convert(err)
require.Equal(t, codes.Internal, st.Code())
}

return
}

require.NoError(t, err)
require.NotNil(t, ret)

// test nil error
st := status.Convert(err)
nicest := util.FromRpcError(st)
require.Equal(t, codes.OK, nicest.Code)
require.Equal(t, "OK", nicest.Name)
})
}
}

0 comments on commit 2429168

Please sign in to comment.