Skip to content

Commit

Permalink
move AssertOutputFields to handlers package
Browse files Browse the repository at this point in the history
  • Loading branch information
bosorawis committed Feb 19, 2025
1 parent 6a0a5cc commit 8079ad7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
31 changes: 7 additions & 24 deletions internal/daemon/controller/handlers/groups/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,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 @@ -1416,7 +1416,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.GetGroup(fullGrantAuthCtx, &pbs.GetGroupRequest{Id: globalGroupWithMember.PublicId})
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -1582,7 +1582,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.CreateGroup(fullGrantAuthCtx, tc.input)
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -1733,7 +1733,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.UpdateGroup(fullGrantAuthCtx, inputFunc(t))
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -1876,7 +1876,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.AddGroupMembers(fullGrantAuthCtx, inputFunc(t))
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -2019,7 +2019,7 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.SetGroupMembers(fullGrantAuthCtx, inputFunc(t))
require.NoError(t, err)
assertOutputFields(t, out.Item, tc.expectOutfields)
handlers.AssertOutputFields(t, out.Item, tc.expectOutfields)
})
}
})
Expand Down Expand Up @@ -2166,30 +2166,13 @@ func TestOutputFields(t *testing.T) {
fullGrantAuthCtx := auth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
out, err := s.RemoveGroupMembers(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 group match the expected fields
// fields that is nil or empty in the result will throw an error if they are listed in expectedFields
// e.g. members when group does not contian any members
func assertOutputFields(t *testing.T, g *pb.Group, expectFields []string) {
msg := g.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())
}
}

func userIDs(users []*iam.User) []string {
result := make([]string, len(users))
for i, u := range users {
Expand Down
30 changes: 30 additions & 0 deletions internal/daemon/controller/handlers/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package handlers

import (
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/reflect/protoreflect"
)

type protoReflector interface {
ProtoReflect() protoreflect.Message
}

// assertOutputFields asserts that the output fields of a group match the expected fields
// fields that is nil or empty in the result will throw an error if they are listed in expectedFields
// e.g. members when group does not contian any members
func AssertOutputFields(t *testing.T, p protoReflector, expectFields []string) {
msg := p.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 8079ad7

Please sign in to comment.