FILE / ScuroNeko/Laniakea

tgapi/log_redaction_test.go

Исходный файл и его история в репозитории.
FILE 4d95bd05746a4f16d360080afe904046bb8e38fb
Files
Laniakea/tgapi/log_redaction_test.go
T
ScuroNeko f03a081ed6
Golang lint / lint (pull_request) Successful in 1m20s
Golang lint / lint (push) Successful in 4m8s
(new): rich message support
(fix): runtime reliability
(tests): regression coverage
(doc): v1.1 release notes
2026-08-12 16:34:44 +03:00

40 lines
1.2 KiB
Go

package tgapi
import (
"strings"
"testing"
)
func TestRedactRequestLogRemovesSensitiveValues(t *testing.T) {
const input = `{"secret_token":"webhook-secret","provider_token":"payment-token","nested":{"data":"passport-data","callback_data":"callback-secret"},"chat_id":42}`
got := redactRequestLog([]byte(input))
for _, secret := range []string{"webhook-secret", "payment-token", "passport-data", "callback-secret"} {
if strings.Contains(got, secret) {
t.Errorf("redacted request contains %q: %s", secret, got)
}
}
if !strings.Contains(got, `"chat_id":42`) {
t.Errorf("redacted request lost non-sensitive field: %s", got)
}
}
func TestRedactRequestLogOmitsInvalidJSON(t *testing.T) {
const secret = "not-json-secret"
got := redactRequestLog([]byte(secret))
if strings.Contains(got, secret) {
t.Fatalf("invalid JSON was logged verbatim: %s", got)
}
}
func TestResponseLogSummaryNeverContainsBody(t *testing.T) {
const token = "managed-bot-token"
got := responseLogSummary("getManagedBotToken", len(token))
if strings.Contains(got, token) {
t.Fatalf("response summary contains response body: %s", got)
}
if !strings.Contains(got, "body=omitted") {
t.Fatalf("response summary does not explain omission: %s", got)
}
}