From 40054e2b0a4c05466f155cdf8adb0adc89cb11aa Mon Sep 17 00:00:00 2001 From: Alejandro Baez Date: Sun, 20 Oct 2019 14:00:15 -0400 Subject: [PATCH] add test for policy name endpoint --- agent/acl_endpoint_test.go | 11 +++++++++ agent/consul/acl_endpoint_test.go | 39 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/agent/acl_endpoint_test.go b/agent/acl_endpoint_test.go index f29ec136ac93..739fa689b4f7 100644 --- a/agent/acl_endpoint_test.go +++ b/agent/acl_endpoint_test.go @@ -374,6 +374,17 @@ func TestACL_HTTP(t *testing.T) { require.True(t, ok) require.Equal(t, policyMap[idMap["policy-read-all-nodes"]], policy) }) + + t.Run("Read Name", func(t *testing.T) { + policyName := "policy-read-all-nodes" + req, _ := http.NewRequest("GET", "/v1/acl/policy/name/"+policyName+"?token=root", nil) + resp := httptest.NewRecorder() + raw, err := a.srv.ACLPolicyCRUD(resp, req) + require.NoError(t, err) + policy, ok := raw.(*structs.ACLPolicy) + require.True(t, ok) + require.Equal(t, policyMap[idMap[policyName]], policy) + }) }) t.Run("Role", func(t *testing.T) { diff --git a/agent/consul/acl_endpoint_test.go b/agent/consul/acl_endpoint_test.go index 07dc6540167d..8d8f79a0d625 100644 --- a/agent/consul/acl_endpoint_test.go +++ b/agent/consul/acl_endpoint_test.go @@ -2240,6 +2240,45 @@ func TestACLEndpoint_PolicyRead(t *testing.T) { } } +func TestACLEndpoint_PolicyReadByName(t *testing.T) { + t.Parallel() + dir1, s1 := testServerWithConfig(t, func(c *Config) { + c.ACLDatacenter = "dc1" + c.ACLsEnabled = true + c.ACLMasterToken = "root" + }) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + codec := rpcClient(t, s1) + defer codec.Close() + + testrpc.WaitForLeader(t, s1.RPC, "dc1") + + policy, err := upsertTestPolicy(codec, "root", "dc1") + if err != nil { + t.Fatalf("err: %v", err) + } + + acl := ACL{srv: s1} + + req := structs.ACLPolicyGetRequest{ + Datacenter: "dc1", + PolicyName: policy.Name, + QueryOptions: structs.QueryOptions{Token: "root"}, + } + + resp := structs.ACLPolicyResponse{} + + err = acl.PolicyRead(&req, &resp) + if err != nil { + t.Fatalf("err: %v", err) + } + + if !reflect.DeepEqual(resp.Policy, policy) { + t.Fatalf("tokens are not equal: %v != %v", resp.Policy, policy) + } +} + func TestACLEndpoint_PolicyBatchRead(t *testing.T) { t.Parallel()