Skip to content

Commit

Permalink
update output assert to use shared assert function
Browse files Browse the repository at this point in the history
  • Loading branch information
elimt committed Feb 24, 2025
1 parent d7cebeb commit d8c5fe9
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions internal/daemon/controller/handlers/aliases/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package aliases_test
import (
"context"
"fmt"
"slices"
"testing"

"github.com/google/uuid"
Expand All @@ -27,7 +26,6 @@ import (
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/aliases"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
Expand Down Expand Up @@ -762,7 +760,7 @@ func TestOutputFields(t *testing.T) {
})
require.NoError(t, err)
for _, item := range out.Items {
assertOutputFields(t, item, tc.expectOutfields[item.Id])
handlers.AssertOutputFields(t, item, tc.expectOutfields[item.Id])
}
})
}
Expand Down Expand Up @@ -883,7 +881,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.GetAlias(fullGrantAuthCtx, &pbs.GetAliasRequest{Id: globalAlias1.PublicId})
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -1082,7 +1080,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.CreateAlias(fullGrantAuthCtx, tc.input)
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -1263,24 +1261,8 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.UpdateAlias(fullGrantAuthCtx, inputFunc(t))
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
}

// assertOutputFields asserts that the output fields of a proto message match the expected fields
// fields that is nil or empty in the result will throw an error if they are listed in expectedFields
func assertOutputFields(t *testing.T, m proto.Message, expectFields []string) {
msg := m.ProtoReflect()
descriptor := msg.Descriptor()
for i := 0; i < descriptor.Fields().Len(); i++ {
fd := descriptor.Fields().Get(i)
fieldName := string(fd.Name())
if !slices.Contains(expectFields, fieldName) {
require.Falsef(t, msg.Has(fd), "expect field '%s' to be empty but got %+v", fd.Name(), msg.Get(fd).Interface())
continue
}
require.Truef(t, msg.Has(fd), "expect field '%s' to be empty but got %+v", fd.Name(), msg.Get(fd).Interface())
}
}

0 comments on commit d8c5fe9

Please sign in to comment.