68 lines
2.6 KiB
Go
68 lines
2.6 KiB
Go
package domain
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestLegacyPolicyDocumentRestoresWithoutAttemptSettings(t *testing.T) {
|
|
policy, err := decodeProductPolicy([]byte(demoProductPolicyV1Document))
|
|
if err != nil {
|
|
t.Fatalf("decodeProductPolicy(V1) error = %v", err)
|
|
}
|
|
if policy.Assessment.Attempt != nil {
|
|
t.Fatal("legacy V1 policy acquired implicit attempt settings")
|
|
}
|
|
encoded, _, err := encodeProductPolicy(policy)
|
|
if err != nil {
|
|
t.Fatalf("encodeProductPolicy(V1) error = %v", err)
|
|
}
|
|
if !bytes.Equal(encoded, []byte(demoProductPolicyV1Document)) {
|
|
t.Fatalf("legacy V1 canonical document changed:\n%s\nwant:\n%s", encoded, demoProductPolicyV1Document)
|
|
}
|
|
}
|
|
|
|
func TestDemoProductPolicyV2PinsAttemptSettings(t *testing.T) {
|
|
policy := DemoProductPolicyV2()
|
|
if err := policy.Validate(); err != nil {
|
|
t.Fatalf("DemoProductPolicyV2().Validate() error = %v", err)
|
|
}
|
|
got := policy.Assessment.Attempt
|
|
if got == nil {
|
|
t.Fatal("DemoProductPolicyV2() has no attempt settings")
|
|
}
|
|
if got.Lifetime != 72 || got.ChainDeadline != 72 || got.MaxSessions != 2 ||
|
|
got.IdleTimeoutSeconds != 1800 || got.RealMode != AssessmentRealModeDemoBasic ||
|
|
got.BasicPassMark != 8000 {
|
|
t.Fatalf("DemoProductPolicyV2().Assessment.Attempt = %+v, want explicitly configured values", got)
|
|
}
|
|
if training := policy.Assessment.Training; training == nil || training.DemoExpressDurationSeconds != 240 || training.DemoExpressItemCount != 3 {
|
|
t.Fatalf("DemoProductPolicyV2().Assessment.Training = %+v, want explicit duration and item count", training)
|
|
}
|
|
|
|
draft, err := NewPolicyVersionDraft(NewPolicyVersionDraftInput{
|
|
Key: ProductPolicyKey, Kind: PolicyKindDemo, Author: DemoPolicySeedAuthor, Policy: policy,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewPolicyVersionDraft(V2) error = %v", err)
|
|
}
|
|
decoded, err := decodeProductPolicy(draft.Document())
|
|
if err != nil {
|
|
t.Fatalf("decodeProductPolicy(V2) error = %v", err)
|
|
}
|
|
if decoded.Assessment.Attempt == nil || *decoded.Assessment.Attempt != *got ||
|
|
decoded.Assessment.Training == nil || *decoded.Assessment.Training != *policy.Assessment.Training {
|
|
t.Fatalf("decoded attempt policy = %+v, want %+v", decoded.Assessment.Attempt, got)
|
|
}
|
|
|
|
returnedPolicy := draft.Policy()
|
|
returnedPolicy.Assessment.Attempt.IdleTimeoutSeconds++
|
|
if draft.Policy().Assessment.Attempt.IdleTimeoutSeconds != got.IdleTimeoutSeconds {
|
|
t.Fatal("changing returned attempt settings changed the policy draft")
|
|
}
|
|
returnedPolicy.Assessment.Training.DemoExpressDurationSeconds++
|
|
if draft.Policy().Assessment.Training.DemoExpressDurationSeconds != policy.Assessment.Training.DemoExpressDurationSeconds {
|
|
t.Fatal("changing returned training settings changed the policy draft")
|
|
}
|
|
}
|