Files

33 lines
1.0 KiB
Go

package main
import "testing"
func TestGateFromResponse(t *testing.T) {
cases := []struct {
name, body, want string
}{
{"block", `{"id":"x","gate":{"decision":"block","reason":"r"}}`, "block"},
{"warn", `{"gate":{"decision":"warn"}}`, "warn"},
{"pass", `{"gate":{"decision":"pass"}}`, "pass"},
{"an older platform answers without a gate", `{"id":"x"}`, "pass"},
{"an unknown decision is not invented into a rule", `{"gate":{"decision":"stop"}}`, "pass"},
{"unreadable", `not json`, "pass"},
}
for _, c := range cases {
if got := gateFromResponse([]byte(c.body)).Decision; got != c.want {
t.Errorf("%s: got %q, want %q", c.name, got, c.want)
}
}
}
func TestOnlyBlockStopsTheScan(t *testing.T) {
if err := (platformGate{Decision: "block", Reason: "gate failed (uat: block)"}).err(); err == nil {
t.Fatal("block must fail the scan")
}
for _, d := range []string{"pass", "warn", ""} {
if err := (platformGate{Decision: d}).err(); err != nil {
t.Errorf("%q must not fail the scan: %v", d, err)
}
}
}