Proposed more streamlined approach to validating schema responses. (#18865)

This commit is contained in:
Nick Cabatoff 2023-01-27 14:34:55 -05:00 committed by GitHub
parent 8788317b8a
commit 9ca78845b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 18 deletions

View file

@ -18,38 +18,36 @@ import (
"github.com/mitchellh/mapstructure"
)
func TestAppRole_LocalSecretIDsRead(t *testing.T) {
var resp *logical.Response
var err error
b, storage := createBackendWithStorage(t)
func (b *backend) requestNoErr(t *testing.T, req *logical.Request) *logical.Response {
t.Helper()
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%v resp:%#v", err, resp)
}
schema.ValidateResponse(t, schema.GetResponseSchema(t, b.Route(req.Path), req.Operation), resp, true)
return resp
}
paths := rolePaths(b)
func TestAppRole_LocalSecretIDsRead(t *testing.T) {
b, storage := createBackendWithStorage(t)
roleData := map[string]interface{}{
"local_secret_ids": true,
"bind_secret_id": true,
}
resp, err = b.HandleRequest(context.Background(), &logical.Request{
b.requestNoErr(t, &logical.Request{
Operation: logical.CreateOperation,
Path: "role/testrole",
Storage: storage,
Data: roleData,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%v resp:%#v", err, resp)
}
schema.ValidateResponse(t, schema.FindResponseSchema(t, paths, 0, logical.CreateOperation), resp, true)
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp := b.requestNoErr(t, &logical.Request{
Operation: logical.ReadOperation,
Storage: storage,
Path: "role/testrole/local-secret-ids",
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%v resp:%#v", err, resp)
}
schema.ValidateResponse(t, schema.FindResponseSchema(t, paths, 2, logical.ReadOperation), resp, true)
if !resp.Data["local_secret_ids"].(bool) {
t.Fatalf("expected local_secret_ids to be returned")

View file

@ -88,11 +88,17 @@ func FindResponseSchema(t *testing.T, paths []*framework.Path, pathIdx int, oper
schemaPath := paths[pathIdx]
schemaOperation, ok := schemaPath.Operations[operation]
return GetResponseSchema(t, schemaPath, operation)
}
func GetResponseSchema(t *testing.T, path *framework.Path, operation logical.Operation) *framework.Response {
t.Helper()
schemaOperation, ok := path.Operations[operation]
if !ok {
t.Fatalf(
"could not find response schema: %s: %q operation does not exist",
schemaPath.Pattern,
path.Pattern,
operation,
)
}
@ -113,7 +119,7 @@ func FindResponseSchema(t *testing.T, paths []*framework.Path, pathIdx int, oper
if len(schemaResponses) == 0 {
t.Fatalf(
"could not find response schema: %s: %q operation: no responses found",
schemaPath.Pattern,
path.Pattern,
operation,
)
}