refact(action): Make ActionSet behave as a set instead of a list
This switches the implementation of an ActionSet from a slice to a map to ensure the elements in an ActionSet are unique. This allows for other packages to perform set operations such as a union or difference without needing to account for duplicates.
This commit is contained in:
52 files changed
+1401
-274
No files matched your search
@@ -64,14 +64,15 @@ const (
|
||||
var (
|
||||
// IdActions contains the set of actions that can be performed on
|
||||
// individual resources
|
||||
// This will be set in init functions in files for each subtype in this package.
|
||||
IdActions = make(map[globals.Subtype]action.ActionSet)
|
||||
|
||||
// CollectionActions contains the set of actions that can be performed on
|
||||
// this collection
|
||||
CollectionActions = action.ActionSet{
|
||||
CollectionActions = action.NewActionSet(
|
||||
action.Create,
|
||||
action.List,
|
||||
}
|
||||
)
|
||||
|
||||
collectionTypeMap = map[resource.Type]action.ActionSet{
|
||||
resource.Account: accounts.CollectionActions,
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/auth/ldap"
|
||||
"github.com/hashicorp/boundary/internal/auth/oidc"
|
||||
@@ -269,7 +270,18 @@ func TestGet(t *testing.T) {
|
||||
if oidcAttrs := got.Item.GetOidcAuthMethodsAttributes(); oidcAttrs != nil {
|
||||
assert.NotEqual("secret", oidcAttrs.ClientSecretHmac)
|
||||
}
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform(), protocmp.IgnoreFields(&pb.OidcAuthMethodAttributes{}, "client_secret_hmac")), "GetAuthMethod(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
assert.Empty(cmp.Diff(
|
||||
got,
|
||||
tc.res,
|
||||
protocmp.Transform(),
|
||||
protocmp.IgnoreFields(&pb.OidcAuthMethodAttributes{}, "client_secret_hmac"),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
), "GetAuthMethod(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -489,10 +501,19 @@ func TestList(t *testing.T) {
|
||||
}
|
||||
|
||||
slices.SortFunc(got.Items, sorterFn)
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform(),
|
||||
assert.Empty(cmp.Diff(
|
||||
got,
|
||||
tc.res,
|
||||
protocmp.Transform(),
|
||||
protocmp.IgnoreFields(&pb.OidcAuthMethodAttributes{}, "client_secret_hmac"),
|
||||
protocmp.IgnoreFields(&pb.LdapAuthMethodAttributes{}, "bind_password_hmac", "client_certificate_key_hmac")),
|
||||
"ListAuthMethods() for scope %q got response %q, wanted %q", tc.req.GetScopeId(), got, tc.res)
|
||||
protocmp.IgnoreFields(&pb.LdapAuthMethodAttributes{}, "bind_password_hmac", "client_certificate_key_hmac"),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
), "ListAuthMethods() for scope %q got response %q, wanted %q", tc.req.GetScopeId(), got, tc.res)
|
||||
|
||||
// Now check with anonymous user
|
||||
got, gErr = s.ListAuthMethods(requestauth.DisabledAuthTestContext(iamRepoFn, tc.req.GetScopeId(), requestauth.WithUserId(globals.AnonymousUserId)), tc.req)
|
||||
@@ -1460,7 +1481,15 @@ func TestCreate(t *testing.T) {
|
||||
if tc.res == nil {
|
||||
require.Nil(got)
|
||||
}
|
||||
cmpOptions := []cmp.Option{protocmp.Transform()}
|
||||
cmpOptions := []cmp.Option{
|
||||
protocmp.Transform(),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
}
|
||||
if got != nil {
|
||||
assert.Contains(got.GetUri(), tc.res.Uri)
|
||||
assert.True(strings.HasPrefix(got.GetItem().GetId(), tc.idPrefix))
|
||||
@@ -1486,7 +1515,6 @@ func TestCreate(t *testing.T) {
|
||||
assert.True(matches, "%q doesn't match %q", gVal, exp)
|
||||
cmpOptions = append(
|
||||
cmpOptions,
|
||||
protocmp.SortRepeatedFields(&pb.OidcAuthMethodAttributes{}, "account_claim_maps"),
|
||||
protocmp.IgnoreFields(&pb.OidcAuthMethodAttributes{}, "client_secret_hmac", "callback_url"),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -36,13 +36,13 @@ func init() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
IdActions[ldap.Subtype] = action.ActionSet{
|
||||
IdActions[ldap.Subtype] = action.NewActionSet(
|
||||
action.NoOp,
|
||||
action.Read,
|
||||
action.Update,
|
||||
action.Delete,
|
||||
action.Authenticate,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/auth/ldap"
|
||||
"github.com/hashicorp/boundary/internal/auth/oidc"
|
||||
@@ -850,7 +851,15 @@ func Test_UpdateLdap(t *testing.T) {
|
||||
if tc.res == nil {
|
||||
require.Nil(got)
|
||||
}
|
||||
cmpOptions := []cmp.Option{protocmp.Transform()}
|
||||
cmpOptions := []cmp.Option{
|
||||
protocmp.Transform(),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
}
|
||||
if got != nil {
|
||||
assert.NotNilf(tc.res, "Expected UpdateAuthMethod response to be nil, but was %v", got)
|
||||
gotUpdateTime := got.GetItem().GetUpdatedTime().AsTime()
|
||||
|
||||
@@ -57,14 +57,14 @@ func init() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
IdActions[oidc.Subtype] = action.ActionSet{
|
||||
IdActions[oidc.Subtype] = action.NewActionSet(
|
||||
action.NoOp,
|
||||
action.Read,
|
||||
action.Update,
|
||||
action.Delete,
|
||||
action.ChangeState,
|
||||
action.Authenticate,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
type oidcState uint
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
authpb "github.com/hashicorp/boundary/internal/gen/controller/auth"
|
||||
|
||||
@@ -1076,7 +1077,15 @@ func TestUpdate_OIDC(t *testing.T) {
|
||||
if tc.res == nil {
|
||||
require.Nil(got)
|
||||
}
|
||||
cmpOptions := []cmp.Option{protocmp.Transform()}
|
||||
cmpOptions := []cmp.Option{
|
||||
protocmp.Transform(),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
}
|
||||
if got != nil {
|
||||
assert.NotNilf(tc.res, "Expected UpdateAuthMethod response to be nil, but was %v", got)
|
||||
gotUpdateTime := got.GetItem().GetUpdatedTime().AsTime()
|
||||
@@ -1279,11 +1288,32 @@ func TestUpdate_OIDCDryRun(t *testing.T) {
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, cmp.Diff(dryUpdated.GetItem(), tc.want, protocmp.Transform(), protocmp.SortRepeatedFields(dryUpdated)))
|
||||
assert.Empty(t, cmp.Diff(
|
||||
dryUpdated.GetItem(),
|
||||
tc.want,
|
||||
protocmp.Transform(),
|
||||
protocmp.SortRepeatedFields(dryUpdated),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
))
|
||||
|
||||
got, err := tested.GetAuthMethod(auth.DisabledAuthTestContext(iamRepoFn, o.GetPublicId()), &pbs.GetAuthMethodRequest{Id: tc.id})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, cmp.Diff(got.GetItem(), wireAuthMethod, protocmp.Transform()))
|
||||
assert.Empty(t, cmp.Diff(
|
||||
got.GetItem(),
|
||||
wireAuthMethod,
|
||||
protocmp.Transform(),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1543,7 +1573,18 @@ func TestChangeState_OIDC(t *testing.T) {
|
||||
got.Item.UpdatedTime, tc.res.Item.UpdatedTime = nil, nil
|
||||
|
||||
assert.Empty(
|
||||
cmp.Diff(got, tc.res, protocmp.Transform(), protocmp.IgnoreFields(&pb.OidcAuthMethodAttributes{}, "client_secret_hmac", "callback_url")),
|
||||
cmp.Diff(
|
||||
got,
|
||||
tc.res,
|
||||
protocmp.Transform(),
|
||||
protocmp.IgnoreFields(&pb.OidcAuthMethodAttributes{}, "client_secret_hmac", "callback_url"),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
),
|
||||
"ChangeState() got response %q, wanted %q", got, tc.res,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -36,13 +36,13 @@ func init() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
IdActions[password.Subtype] = action.ActionSet{
|
||||
IdActions[password.Subtype] = action.NewActionSet(
|
||||
action.NoOp,
|
||||
action.Read,
|
||||
action.Update,
|
||||
action.Delete,
|
||||
action.Authenticate,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// createPwInRepo creates a password auth method in a repo and returns the result.
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/auth/ldap"
|
||||
"github.com/hashicorp/boundary/internal/auth/oidc"
|
||||
@@ -458,7 +459,15 @@ func TestUpdate_Password(t *testing.T) {
|
||||
require.Nil(got)
|
||||
}
|
||||
|
||||
cmpOptions := []cmp.Option{protocmp.Transform()}
|
||||
cmpOptions := []cmp.Option{
|
||||
protocmp.Transform(),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
|
||||
return a.String() < b.String()
|
||||
}),
|
||||
}
|
||||
if got != nil {
|
||||
assert.NotNilf(tc.res, "Expected UpdateAuthMethod response to be nil, but was %v", got)
|
||||
|
||||
|
||||
Reference in new issue
Block a user