Add AppRole response schema validation tests (#18636)
This PR modifies every test in `builtin/credentials/approle/path_role_test.go` with new validation checks to ensure that approle/path_role successful responses align with the declared response schema. It also introduces a test helper in `sdk/helper/testhelpers`: ```go func FindResponseSchema(t *testing.T, ...) ``` This test helper will be useful for all plugins that require similar response schema validation in tests. ### Background This PR is part of the ongoing work to add structured responses in Vault OpenAPI (VLT-234)
This commit is contained in:
parent
9696600e59
commit
6ae09f3074
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,3 @@
|
||||||
|
```release-note:improvement
|
||||||
|
sdk: Adding FindResponseSchema test helper to assist with response schema validation in tests
|
||||||
|
```
|
|
@ -3,6 +3,7 @@ package schema
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/sdk/framework"
|
"github.com/hashicorp/vault/sdk/framework"
|
||||||
|
@ -76,3 +77,45 @@ func validateResponseDataImpl(schema *framework.Response, data map[string]interf
|
||||||
|
|
||||||
return fd.Validate()
|
return fd.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindResponseSchema is a test helper to extract the response schema from a given framework path / operation
|
||||||
|
func FindResponseSchema(t *testing.T, paths []*framework.Path, pathIdx int, operation logical.Operation) *framework.Response {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if pathIdx >= len(paths) {
|
||||||
|
t.Fatalf("path index %d is out of range", pathIdx)
|
||||||
|
}
|
||||||
|
|
||||||
|
schemaPath := paths[pathIdx]
|
||||||
|
|
||||||
|
schemaOperation, ok := schemaPath.Operations[operation]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf(
|
||||||
|
"could not find response schema: %s: %q operation does not exist",
|
||||||
|
schemaPath.Pattern,
|
||||||
|
operation,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var schemaResponses []framework.Response
|
||||||
|
|
||||||
|
for _, status := range []int{
|
||||||
|
http.StatusOK,
|
||||||
|
http.StatusNoContent,
|
||||||
|
} {
|
||||||
|
schemaResponses, ok = schemaOperation.Properties().Responses[status]
|
||||||
|
if ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(schemaResponses) == 0 {
|
||||||
|
t.Fatalf(
|
||||||
|
"could not find response schema: %s: %q operation: no responses found",
|
||||||
|
schemaPath.Pattern,
|
||||||
|
operation,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &schemaResponses[0]
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue