38 lines
1.5 KiB
Go
38 lines
1.5 KiB
Go
package redaction
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRedactTextSensitiveValues(t *testing.T) {
|
|
r := New()
|
|
in := "phone +77771234567 iin 123456789012 card 4400123412341234 email test@example.com код 123456 sk-secretvalue Bearer abcdef password=qwerty postgres://u:pass@127.0.0.1/db"
|
|
out := r.RedactText(in)
|
|
for _, raw := range []string{"+77771234567", "123456789012", "4400123412341234", "test@example.com", "123456 sk-secretvalue", "Bearer abcdef", "password=qwerty", ":pass@"} {
|
|
if strings.Contains(out.Text, raw) {
|
|
t.Fatalf("raw sensitive value still present: %s in %s", raw, out.Text)
|
|
}
|
|
}
|
|
for _, want := range []string{"+777***4567", "1234****9012", "4400********1234", "t***@example.com", "код CODE", "sk-***MASKED***"} {
|
|
if !strings.Contains(out.Text, want) {
|
|
t.Fatalf("missing masked value %q in %s", want, out.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRedactDoesNotOverRedactOrdinaryNumbers(t *testing.T) {
|
|
out := New().RedactText("3 рабочих дня, код услуги 5104")
|
|
if !strings.Contains(out.Text, "3 рабочих дня") || !strings.Contains(out.Text, "5104") {
|
|
t.Fatalf("over-redacted ordinary text: %s", out.Text)
|
|
}
|
|
}
|
|
|
|
func TestRedactJSONRecursive(t *testing.T) {
|
|
out := New().RedactJSON(map[string]any{"a": map[string]any{"phone": "+77771234567"}, "b": []any{"test@example.com"}})
|
|
s := strings.Join([]string{out["a"].(map[string]any)["phone"].(string), out["b"].([]any)[0].(string)}, " ")
|
|
if strings.Contains(s, "+77771234567") || strings.Contains(s, "test@example.com") {
|
|
t.Fatalf("recursive redaction failed: %v", out)
|
|
}
|
|
}
|