From 4a630135b8f65f5ae88e53bb2a768b15fd746133 Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Fri, 1 May 2020 14:52:15 -0500 Subject: [PATCH] test: move some test helpers over from enterprise (#7754) --- agent/acl_endpoint.go | 2 +- agent/acl_endpoint_test.go | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/agent/acl_endpoint.go b/agent/acl_endpoint.go index 138d07923..839762d92 100644 --- a/agent/acl_endpoint.go +++ b/agent/acl_endpoint.go @@ -1046,7 +1046,7 @@ func (s *HTTPServer) ACLLogin(resp http.ResponseWriter, req *http.Request) (inte } if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.Auth)); err != nil { - return nil, BadRequestError{Reason: fmt.Sprintf("Failed to decode request body:: %v", err)} + return nil, BadRequestError{Reason: fmt.Sprintf("Failed to decode request body: %v", err)} } var out structs.ACLToken diff --git a/agent/acl_endpoint_test.go b/agent/acl_endpoint_test.go index bd59ada9d..31477f100 100644 --- a/agent/acl_endpoint_test.go +++ b/agent/acl_endpoint_test.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/consul/agent/consul/authmethod/testauth" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/testrpc" + "github.com/hashicorp/go-uuid" "github.com/stretchr/testify/require" ) @@ -2031,3 +2032,58 @@ func TestACL_Authorize(t *testing.T) { require.Nil(t, raw) }) } + +type rpcFn func(string, interface{}, interface{}) error + +func upsertTestCustomizedAuthMethod( + rpc rpcFn, masterToken string, datacenter string, + modify func(method *structs.ACLAuthMethod), +) (*structs.ACLAuthMethod, error) { + name, err := uuid.GenerateUUID() + if err != nil { + return nil, err + } + + req := structs.ACLAuthMethodSetRequest{ + Datacenter: datacenter, + AuthMethod: structs.ACLAuthMethod{ + Name: "test-method-" + name, + Type: "testing", + }, + WriteRequest: structs.WriteRequest{Token: masterToken}, + } + + if modify != nil { + modify(&req.AuthMethod) + } + + var out structs.ACLAuthMethod + + err = rpc("ACL.AuthMethodSet", &req, &out) + if err != nil { + return nil, err + } + + return &out, nil +} + +func upsertTestCustomizedBindingRule(rpc rpcFn, masterToken string, datacenter string, modify func(rule *structs.ACLBindingRule)) (*structs.ACLBindingRule, error) { + req := structs.ACLBindingRuleSetRequest{ + Datacenter: datacenter, + BindingRule: structs.ACLBindingRule{}, + WriteRequest: structs.WriteRequest{Token: masterToken}, + } + + if modify != nil { + modify(&req.BindingRule) + } + + var out structs.ACLBindingRule + + err := rpc("ACL.BindingRuleSet", &req, &out) + if err != nil { + return nil, err + } + + return &out, nil +}