refactor: testing from the beginning. a journey in 150k lines of tests to none

This commit is contained in:
sam committed 2026-10-03 21:16:25 +10:00
1 parent ba37e37e53
commit ff424b70ac
651 files changed
-146443

No files matched your search

-54
View File
@@ -1,54 +0,0 @@
package webhookguard_test
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
func sign(secret string, body []byte) string {
mac := hmac.New(sha256.New, []byte(secret))
mac.Write(body)
return hex.EncodeToString(mac.Sum(nil))
}
func TestVerifyHMAC(t *testing.T) {
const secret = "shared-secret"
body := []byte(`{"issue":{"number":42}}`)
validSig := sign(secret, body)
tests := []struct {
name string
body []byte
signature string
secret string
want bool
}{
{"valid signature", body, validSig, secret, true},
{"valid signature with sha256= prefix", body, "sha256=" + validSig, secret, true},
{"uppercase hex signature", body, strings.ToUpper(validSig), secret, true},
{"uppercase hex signature with sha256= prefix", body, "sha256=" + strings.ToUpper(validSig), secret, true},
{"wrong secret", body, validSig, "different-secret", false},
{"uppercase hex signature for a different secret", body, strings.ToUpper(sign("different-secret", body)), secret, false},
{"tampered body", []byte(`{"issue":{"number":99}}`), validSig, secret, false},
{"empty signature", body, "", secret, false},
{"empty signature against empty secret", body, "", "", false},
{"sha256= with no digest", body, "sha256=", secret, false},
{"garbage signature", body, "not-a-hex-signature", secret, false},
{"odd-length hex", body, validSig[:len(validSig)-1], secret, false},
{"truncated digest", body, validSig[:len(validSig)/2], secret, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := webhookguard.VerifyHMAC(test.body, test.signature, test.secret)
if got != test.want {
t.Errorf("VerifyHMAC() = %v, want %v", got, test.want)
}
})
}
}
-70
View File
@@ -1,70 +0,0 @@
package webhookguard_test
import (
"testing"
"time"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// fakeClock lets tests advance time deterministically instead of sleeping.
type fakeClock struct{ now time.Time }
func (c *fakeClock) Now() time.Time { return c.now }
func (c *fakeClock) advance(d time.Duration) { c.now = c.now.Add(d) }
func TestRateLimiterAllowsUpToBurstThenDenies(t *testing.T) {
clock := &fakeClock{now: time.Unix(0, 0)}
limiter := webhookguard.NewRateLimiter(1, 3, clock.Now) // 1/s refill, burst 3
for i := range 3 {
if !limiter.Allow("source-a") {
t.Fatalf("request %d: Allow() = false, want true (within burst)", i)
}
}
if limiter.Allow("source-a") {
t.Fatal("Allow() = true, want false (burst exhausted)")
}
}
func TestRateLimiterRefillsOverTime(t *testing.T) {
clock := &fakeClock{now: time.Unix(0, 0)}
limiter := webhookguard.NewRateLimiter(1, 1, clock.Now) // 1/s refill, burst 1
if !limiter.Allow("source-a") {
t.Fatal("first request denied, want allowed")
}
if limiter.Allow("source-a") {
t.Fatal("second immediate request allowed, want denied")
}
clock.advance(time.Second)
if !limiter.Allow("source-a") {
t.Fatal("request after refill window denied, want allowed")
}
}
func TestRateLimiterSourcesAreIndependent(t *testing.T) {
clock := &fakeClock{now: time.Unix(0, 0)}
limiter := webhookguard.NewRateLimiter(1, 1, clock.Now)
if !limiter.Allow("source-a") {
t.Fatal("source-a first request denied, want allowed")
}
if limiter.Allow("source-a") {
t.Fatal("source-a burst exhausted request allowed, want denied")
}
if !limiter.Allow("source-b") {
t.Fatal("source-b denied by source-a's exhausted budget, want independent")
}
}
func TestRateLimiterUnknownSourceStartsWithFullBurst(t *testing.T) {
clock := &fakeClock{now: time.Unix(0, 0)}
limiter := webhookguard.NewRateLimiter(1, 5, clock.Now)
for i := range 5 {
if !limiter.Allow("first-time-source") {
t.Fatalf("request %d for a never-seen source denied, want allowed within burst", i)
}
}
}
-197
View File
@@ -1,197 +0,0 @@
package webhookguard_test
import (
"encoding/json"
"testing"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// TestRedactPayloadPreservesNonSensitiveValues is the regression test for
// the defect a naive port of gateway/stream.go's redaction would introduce:
// that function replaces every string value regardless of key, which is
// correct for a bounded chat-transcript summary but would make a captured
// webhook payload useless for schema-by-example mapping (t2db.3) -- an
// operator cannot map a field they cannot see. Only values under a
// sensitive-looking key may be redacted; everything else must survive
// byte-for-byte.
func TestRedactPayloadPreservesNonSensitiveValues(t *testing.T) {
input := []byte(`{"issue":{"number":42,"title":"Fix the thing","open":true}}`)
got, err := webhookguard.RedactPayload(input)
if err != nil {
t.Fatalf("RedactPayload() error = %v", err)
}
var decoded map[string]any
if err := json.Unmarshal(got, &decoded); err != nil {
t.Fatalf("redacted output is not valid JSON: %v", err)
}
issue, ok := decoded["issue"].(map[string]any)
if !ok {
t.Fatalf("decoded[%q] = %#v, want map[string]any", "issue", decoded["issue"])
}
if issue["title"] != "Fix the thing" {
t.Errorf("title = %v, want unredacted %q", issue["title"], "Fix the thing")
}
if issue["number"] != float64(42) {
t.Errorf("number = %v, want unredacted 42", issue["number"])
}
if issue["open"] != true {
t.Errorf("open = %v, want unredacted true", issue["open"])
}
}
func TestRedactPayloadRedactsSensitiveKeys(t *testing.T) {
tests := []struct {
name string
key string
}{
{"exact match", "token"},
{"api_key", "api_key"},
{"apikey no separator", "apikey"},
{"hyphenated", "api-key"},
{"spaced", "API Key"},
{"mixed case", "Authorization"},
{"substring match", "access_token"},
{"password", "password"},
{"passwd abbreviation", "passwd"},
{"private key", "private_key"},
{"cookie", "cookie"},
{"credential", "credential"},
{"access_token", "access_token"},
{"client_secret", "client_secret"},
{"signing_key", "signing_key"},
{"webhook_secret", "webhook_secret"},
{"x-api-key", "x-api-key"},
{"x_auth_token", "x_auth_token"},
{"otp", "otp"},
{"refresh_token", "refresh_token"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
input, err := json.Marshal(map[string]string{test.key: "super-secret-value"})
if err != nil {
t.Fatal(err)
}
got, err := webhookguard.RedactPayload(input)
if err != nil {
t.Fatalf("RedactPayload() error = %v", err)
}
if string(got) == string(input) {
t.Fatalf("payload unchanged, want %q redacted", test.key)
}
var decoded map[string]any
if err := json.Unmarshal(got, &decoded); err != nil {
t.Fatalf("redacted output is not valid JSON: %v", err)
}
if decoded[test.key] == "super-secret-value" {
t.Errorf("value under key %q survived redaction", test.key)
}
})
}
}
func TestRedactPayloadRedactsNestedObjectsWholesale(t *testing.T) {
input := []byte(`{"credentials":{"host":"db.internal","user":"admin"}}`)
got, err := webhookguard.RedactPayload(input)
if err != nil {
t.Fatalf("RedactPayload() error = %v", err)
}
var decoded map[string]any
if err := json.Unmarshal(got, &decoded); err != nil {
t.Fatalf("redacted output is not valid JSON: %v", err)
}
if _, stillObject := decoded["credentials"].(map[string]any); stillObject {
t.Errorf("credentials still an object, want wholesale redaction: %v", decoded["credentials"])
}
}
func TestRedactPayloadHandlesArraysAndScalarsAtTopLevel(t *testing.T) {
tests := []struct {
name string
input string
}{
{"array", `[{"title":"a"},{"title":"b"}]`},
{"bare string", `"just a string"`},
{"bare number", `42`},
{"null", `null`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if _, err := webhookguard.RedactPayload([]byte(test.input)); err != nil {
t.Errorf("RedactPayload(%s) error = %v, want no error", test.input, err)
}
})
}
}
func TestRedactPayloadRejectsInvalidJSON(t *testing.T) {
if _, err := webhookguard.RedactPayload([]byte("{not valid json")); err == nil {
t.Error("RedactPayload() succeeded on invalid JSON, want error")
}
}
// TestRedactPayloadRedactsSecretValueShapes pins the value-shape heuristic:
// a compact JWT or a PEM private-key block is redacted even when it sits under
// a key the name heuristic would never flag -- the exact "sender names a secret
// field something the heuristic misses" gap docs/prds/webhook-intake-
// security.md point 5 documents as best-effort.
func TestRedactPayloadRedactsSecretValueShapes(t *testing.T) {
tests := []struct {
name string
value any
}{
{
"compact JWT",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
},
{
"PEM private key",
"-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA7...\n-----END RSA PRIVATE KEY-----",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Under an innocuous key, not a secret-named one.
input, err := json.Marshal(map[string]any{"payload": test.value})
if err != nil {
t.Fatal(err)
}
got, err := webhookguard.RedactPayload(input)
if err != nil {
t.Fatalf("RedactPayload() error = %v", err)
}
var decoded map[string]any
if err := json.Unmarshal(got, &decoded); err != nil {
t.Fatalf("redacted output is not valid JSON: %v", err)
}
if decoded["payload"] == test.value {
t.Errorf("secret-shaped value under 'payload' survived redaction")
}
if decoded["payload"] != "[redacted]" {
t.Errorf("decoded payload = %#v, want %q", decoded["payload"], "[redacted]")
}
})
}
}
// TestRedactPayloadKeepsOrdinaryStrings verifies the value-shape heuristic
// does not over-redact ordinary content (the field-mapping use case depends on
// leaving non-secret values intact).
func TestRedactPayloadKeepsOrdinaryStrings(t *testing.T) {
input := []byte(`{"description":"Fix the build pipeline","buildId":"build-123"}`)
got, err := webhookguard.RedactPayload(input)
if err != nil {
t.Fatalf("RedactPayload() error = %v", err)
}
var decoded map[string]any
if err := json.Unmarshal(got, &decoded); err != nil {
t.Fatalf("redacted output is not valid JSON: %v", err)
}
if decoded["description"] != "Fix the build pipeline" || decoded["buildId"] != "build-123" {
t.Errorf("ordinary strings were redacted: %#v", decoded)
}
}