141 lines
4.8 KiB
Go
141 lines
4.8 KiB
Go
package domain
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestPolicySchemaVersionsPreserveLegacyAndRequireExtensions(t *testing.T) {
|
|
t.Run("schema 1 rejects every extension key even when null", func(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
edit func(map[string]any)
|
|
}{
|
|
{name: "attempt", edit: func(document map[string]any) { policySection(t, document, "assessment")["attempt"] = nil }},
|
|
{name: "training", edit: func(document map[string]any) { policySection(t, document, "assessment")["training"] = nil }},
|
|
{name: "support", edit: func(document map[string]any) { document["support"] = nil }},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
encoded := editPolicySchemaDocument(t, []byte(demoProductPolicyV1Document), test.edit)
|
|
if _, err := decodeProductPolicy(encoded); !errors.Is(err, ErrInvalidPolicy) {
|
|
t.Fatalf("decodeProductPolicy(schema 1 with null %s) error = %v, want ErrInvalidPolicy", test.name, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("schema 2 requires at least one extension", func(t *testing.T) {
|
|
encoded := editPolicySchemaDocument(t, []byte(demoProductPolicyV1Document), func(document map[string]any) {
|
|
document["schema_version"] = 2
|
|
})
|
|
if _, err := decodeProductPolicy(encoded); !errors.Is(err, ErrInvalidPolicy) {
|
|
t.Fatalf("decodeProductPolicy(schema 2 without extensions) error = %v, want ErrInvalidPolicy", err)
|
|
}
|
|
})
|
|
|
|
t.Run("unknown schema version is rejected", func(t *testing.T) {
|
|
encoded := editPolicySchemaDocument(t, []byte(demoProductPolicyV1Document), func(document map[string]any) {
|
|
document["schema_version"] = 3
|
|
})
|
|
if _, err := decodeProductPolicy(encoded); !errors.Is(err, ErrInvalidPolicy) {
|
|
t.Fatalf("decodeProductPolicy(schema 3) error = %v, want ErrInvalidPolicy", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPolicySchemaV2RoundTripsEachExtensionIndependently(t *testing.T) {
|
|
base := DemoProductPolicyV1()
|
|
configured := DemoProductPolicyV2()
|
|
tests := []struct {
|
|
name string
|
|
change func(*ProductPolicy)
|
|
check func(*testing.T, ProductPolicy)
|
|
}{
|
|
{
|
|
name: "attempt",
|
|
change: func(policy *ProductPolicy) {
|
|
attempt := *configured.Assessment.Attempt
|
|
policy.Assessment.Attempt = &attempt
|
|
},
|
|
check: func(t *testing.T, policy ProductPolicy) {
|
|
t.Helper()
|
|
if policy.Assessment.Attempt == nil || policy.Assessment.Training != nil || policy.Support != nil {
|
|
t.Fatalf("decoded extensions = attempt:%v training:%v support:%v", policy.Assessment.Attempt != nil, policy.Assessment.Training != nil, policy.Support != nil)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "training",
|
|
change: func(policy *ProductPolicy) {
|
|
training := *configured.Assessment.Training
|
|
policy.Assessment.Training = &training
|
|
},
|
|
check: func(t *testing.T, policy ProductPolicy) {
|
|
t.Helper()
|
|
if policy.Assessment.Attempt != nil || policy.Assessment.Training == nil || policy.Support != nil {
|
|
t.Fatalf("decoded extensions = attempt:%v training:%v support:%v", policy.Assessment.Attempt != nil, policy.Assessment.Training != nil, policy.Support != nil)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "support",
|
|
change: func(policy *ProductPolicy) {
|
|
support := *configured.Support
|
|
policy.Support = &support
|
|
},
|
|
check: func(t *testing.T, policy ProductPolicy) {
|
|
t.Helper()
|
|
if policy.Assessment.Attempt != nil || policy.Assessment.Training != nil || policy.Support == nil {
|
|
t.Fatalf("decoded extensions = attempt:%v training:%v support:%v", policy.Assessment.Attempt != nil, policy.Assessment.Training != nil, policy.Support != nil)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
policy := base.clone()
|
|
test.change(&policy)
|
|
encoded, _, err := encodeProductPolicy(policy)
|
|
if err != nil {
|
|
t.Fatalf("encodeProductPolicy() error = %v", err)
|
|
}
|
|
var envelope struct {
|
|
SchemaVersion int32 `json:"schema_version"`
|
|
}
|
|
if err := json.Unmarshal(encoded, &envelope); err != nil || envelope.SchemaVersion != productPolicySchemaVersionV2 {
|
|
t.Fatalf("encoded schema version = %d (error %v), want 2", envelope.SchemaVersion, err)
|
|
}
|
|
decoded, err := decodeProductPolicy(encoded)
|
|
if err != nil {
|
|
t.Fatalf("decodeProductPolicy() error = %v", err)
|
|
}
|
|
test.check(t, decoded)
|
|
})
|
|
}
|
|
}
|
|
|
|
func editPolicySchemaDocument(t *testing.T, source []byte, edit func(map[string]any)) []byte {
|
|
t.Helper()
|
|
var document map[string]any
|
|
if err := json.Unmarshal(source, &document); err != nil {
|
|
t.Fatalf("unmarshal policy document: %v", err)
|
|
}
|
|
edit(document)
|
|
encoded, err := json.Marshal(document)
|
|
if err != nil {
|
|
t.Fatalf("marshal policy document: %v", err)
|
|
}
|
|
return encoded
|
|
}
|
|
|
|
func policySection(t *testing.T, document map[string]any, name string) map[string]any {
|
|
t.Helper()
|
|
section, ok := document[name].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("policy document %q section is missing or invalid", name)
|
|
}
|
|
return section
|
|
}
|