test(backend): pin the invitation acceptance and terminal state contract
This commit is contained in:
1 parent
d2c6f1375a
commit
f5b07e8f4d
1 file changed
+148
@@ -0,0 +1,148 @@
|
||||
package httpadapter
|
||||
|
||||
import (
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ogen-go/ogen"
|
||||
)
|
||||
|
||||
// The acceptance must name the accepted terms version and an explicit C4
|
||||
// field choice: a stale version is refused with a status the client can act
|
||||
// on, and no schema default may choose contacts for the candidate.
|
||||
func TestInvitationAcceptanceRequiresExplicitTermsAndFields(t *testing.T) {
|
||||
spec := parsePublicContract(t)
|
||||
|
||||
accept := operationByID(t, spec, "AcceptMyInvitation")
|
||||
for _, status := range []string{"409", "412"} {
|
||||
if _, ok := accept.Responses[status]; !ok {
|
||||
t.Errorf("AcceptMyInvitation does not declare %s", status)
|
||||
}
|
||||
}
|
||||
if accept.RequestBody == nil || !accept.RequestBody.Required {
|
||||
t.Fatal("AcceptMyInvitation must require a body")
|
||||
}
|
||||
media, ok := accept.RequestBody.Content["application/json"]
|
||||
if !ok || media.Schema == nil {
|
||||
t.Fatal("AcceptMyInvitation has no JSON body schema")
|
||||
}
|
||||
|
||||
acceptance := resolveSchema(t, spec, media.Schema)
|
||||
for _, field := range []string{"expected_version", "terms_version", "consent_c4"} {
|
||||
if !slices.Contains(acceptance.Required, field) {
|
||||
t.Errorf("the acceptance does not require %s", field)
|
||||
}
|
||||
}
|
||||
consent := resolveSchema(t, spec, schemaProperty(t, acceptance, "consent_c4"))
|
||||
if !slices.Contains(consent.Required, "fields") {
|
||||
t.Error("the C4 consent does not require a field choice")
|
||||
}
|
||||
fields := resolveSchema(t, spec, schemaProperty(t, consent, "fields"))
|
||||
if fields.MinItems == nil || *fields.MinItems < 1 {
|
||||
t.Error("the C4 field choice may be empty")
|
||||
}
|
||||
if len(fields.Default) != 0 {
|
||||
t.Errorf("the C4 field choice has a default %s", fields.Default)
|
||||
}
|
||||
}
|
||||
|
||||
// Terminal states and the reasons of an expiry are distinct enum values, so a
|
||||
// client never has to infer them from free text.
|
||||
func TestInvitationTerminalStatesAndExpiryReasonsAreTyped(t *testing.T) {
|
||||
spec := parsePublicContract(t)
|
||||
|
||||
states := enumValues(t, resolveSchema(t, spec, &ogen.Schema{Ref: "#/components/schemas/InvitationState"}))
|
||||
for _, state := range []string{"declined", "expired", "withdrawn", "cancelled_by_candidate", "no_deal"} {
|
||||
if !slices.Contains(states, state) {
|
||||
t.Errorf("InvitationState has no %s", state)
|
||||
}
|
||||
}
|
||||
reasons := enumValues(t, resolveSchema(t, spec, &ogen.Schema{Ref: "#/components/schemas/InvitationExpiryReason"}))
|
||||
if !slices.Equal(reasons, []string{"invitation_timeout", "negotiation_timeout"}) {
|
||||
t.Errorf("InvitationExpiryReason = %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
func parsePublicContract(t *testing.T) *ogen.Spec {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(publicContractBundle)
|
||||
if err != nil {
|
||||
t.Fatalf("read the public contract bundle: %v", err)
|
||||
}
|
||||
spec, err := ogen.Parse(data)
|
||||
if err != nil {
|
||||
t.Fatalf("parse the public contract bundle: %v", err)
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
func operationByID(t *testing.T, spec *ogen.Spec, id string) *ogen.Operation {
|
||||
t.Helper()
|
||||
for _, item := range spec.Paths {
|
||||
for _, operation := range []*ogen.Operation{item.Get, item.Post, item.Put, item.Patch, item.Delete} {
|
||||
if operation != nil && operation.OperationID == id {
|
||||
return operation
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Fatalf("the contract has no operation %s", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveSchema follows local component references and returns the single
|
||||
// non-null branch of a nullable oneOf.
|
||||
func resolveSchema(t *testing.T, spec *ogen.Spec, schema *ogen.Schema) *ogen.Schema {
|
||||
t.Helper()
|
||||
for range 8 {
|
||||
switch {
|
||||
case schema == nil:
|
||||
t.Fatal("missing schema")
|
||||
case schema.Ref != "":
|
||||
name, ok := strings.CutPrefix(schema.Ref, "#/components/schemas/")
|
||||
if !ok {
|
||||
t.Fatalf("unexpected reference %s", schema.Ref)
|
||||
}
|
||||
schema = spec.Components.Schemas[name]
|
||||
case len(schema.OneOf) > 0:
|
||||
var branch *ogen.Schema
|
||||
for _, candidate := range schema.OneOf {
|
||||
if candidate.Type != "null" {
|
||||
if branch != nil {
|
||||
return schema
|
||||
}
|
||||
branch = candidate
|
||||
}
|
||||
}
|
||||
schema = branch
|
||||
default:
|
||||
return schema
|
||||
}
|
||||
}
|
||||
t.Fatal("schema references do not resolve")
|
||||
return nil
|
||||
}
|
||||
|
||||
func schemaProperty(t *testing.T, schema *ogen.Schema, name string) *ogen.Schema {
|
||||
t.Helper()
|
||||
for _, property := range schema.Properties {
|
||||
if property.Name == name {
|
||||
return property.Schema
|
||||
}
|
||||
}
|
||||
t.Fatalf("the schema has no property %s", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func enumValues(t *testing.T, schema *ogen.Schema) []string {
|
||||
t.Helper()
|
||||
values := make([]string, 0, len(schema.Enum))
|
||||
for _, raw := range schema.Enum {
|
||||
values = append(values, strings.Trim(string(raw), `"`))
|
||||
}
|
||||
if len(values) == 0 {
|
||||
t.Fatal("the schema has no enum values")
|
||||
}
|
||||
return values
|
||||
}
|
||||
Reference in new issue
Block a user