feat: enforce signature-scoped drainage authorization before SMS submission

This commit is contained in:
hectorzhao
2026-09-10 13:29:04 +08:00
parent 5bcdbb2a03
commit 0c3f820cc9
35 changed files with 2769 additions and 791 deletions
@@ -0,0 +1,43 @@
package upstream
import (
"cmpp-platform/gateway/internal/queue"
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestDrainageGuardFailClosed(t *testing.T) {
for _, body := range []string{`{"allowed":false,"reason":"未报备"}`, `{}`, `not json`} {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(body)) }))
m := &Manager{DrainageGuardEnabled: true, EventAPIBaseURL: server.URL}
if err := m.authorizeDrainage(context.Background(), queue.SubmitCommand{SubmitID: "s", Content: "原文"}); err == nil {
t.Fatalf("unexpected authorization: %s", body)
}
server.Close()
}
}
func TestDrainageGuardRequiresFreshResponse(t *testing.T) {
allowed := true
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/gateway/events/authorize-drainage" {
t.Error(r.URL.Path)
}
if allowed {
w.Write([]byte(`{"allowed":true}`))
} else {
w.WriteHeader(503)
}
}))
defer server.Close()
m := &Manager{DrainageGuardEnabled: true, EventAPIBaseURL: server.URL}
cmd := queue.SubmitCommand{SubmitID: "s", Content: "unchanged"}
if err := m.authorizeDrainage(context.Background(), cmd); err != nil {
t.Fatal(err)
}
allowed = false
if err := m.authorizeDrainage(context.Background(), cmd); err == nil {
t.Fatal("reused stale permission")
}
}