veza/veza-backend-api/tests/contract/contract_test_helpers.go
senke 72d40990c5
Some checks failed
Backend API CI / test-unit (push) Failing after 0s
Backend API CI / test-integration (push) Failing after 0s
Frontend CI / test (push) Failing after 0s
Storybook Audit / Build & audit Storybook (push) Failing after 0s
feat(v0.923): API contract tests, OpenAPI generation, CI type sync check
2026-02-27 20:23:10 +01:00

33 lines
926 B
Go

package contract
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
// ValidateAPIResponseEnvelope checks that the response has success, data/error.
func ValidateAPIResponseEnvelope(t *testing.T, body []byte, expectSuccess bool) map[string]interface{} {
t.Helper()
var resp map[string]interface{}
require.NoError(t, json.Unmarshal(body, &resp))
success, ok := resp["success"].(bool)
require.True(t, ok, "response must have success field")
require.Equal(t, expectSuccess, success)
if expectSuccess {
require.Contains(t, resp, "data")
require.Nil(t, resp["error"])
} else {
require.Contains(t, resp, "error")
}
return resp
}
// RequireDataKeys checks that data has the required top-level keys.
func RequireDataKeys(t *testing.T, data map[string]interface{}, keys ...string) {
t.Helper()
for _, k := range keys {
require.Contains(t, data, k, "data must contain key %q", k)
}
}