94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCardSenderUpdateCardLogsOnlySafeContentShape(t *testing.T) {
|
|
ts := newCardTestServer()
|
|
defer ts.Close()
|
|
sender, err := newCardSenderForTest("client-id", "client-secret", "template-id", ts.Client(), ts.URL()+"/token", ts.URL()+"/create")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
previousWriter := log.Writer()
|
|
previousFlags := log.Flags()
|
|
previousPrefix := log.Prefix()
|
|
log.SetOutput(&output)
|
|
log.SetFlags(0)
|
|
log.SetPrefix("")
|
|
t.Cleanup(func() {
|
|
log.SetOutput(previousWriter)
|
|
log.SetFlags(previousFlags)
|
|
log.SetPrefix(previousPrefix)
|
|
})
|
|
|
|
content := "DO_NOT_LOG_BOUNDARY_TEXT\n\n| 型号 | 负载 |\n| --- | --- |\n| CR5 | 5kg |"
|
|
if err := sender.UpdateCard(context.Background(), "track-diagnostic", content, false, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got := output.String()
|
|
if !strings.Contains(got, "DingTalk card update shape:") || !strings.Contains(got, "confirmed_table=true") || !strings.Contains(got, "finalize=false") {
|
|
t.Fatalf("missing safe boundary diagnostic: %q", got)
|
|
}
|
|
if strings.Contains(got, "DO_NOT_LOG_BOUNDARY_TEXT") || strings.Contains(got, "CR5") || strings.Contains(got, "track-diagnostic") {
|
|
t.Fatalf("boundary diagnostic leaked content or card identifier: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFormatCardUpdateDiagnosticReportsStructureWithoutContent(t *testing.T) {
|
|
content := "DO_NOT_LOG_ANSWER_TEXT\n\n| 型号 | 负载 |\n| --- | --- |\n| CR5 | 5kg |"
|
|
|
|
got := formatCardUpdateDiagnostic(content, false, false)
|
|
wantFields := []string{
|
|
"DingTalk card update shape:",
|
|
"finalize=false",
|
|
"error=false",
|
|
fmt.Sprintf("bytes=%d", len(content)),
|
|
"lines=5",
|
|
"pipe_lines=3",
|
|
"leading_pipe_lines=3",
|
|
"delimiter_lines=1",
|
|
"confirmed_table=true",
|
|
fmt.Sprintf("table_start=%d", strings.Index(content, "| 型号")),
|
|
}
|
|
for _, field := range wantFields {
|
|
if !strings.Contains(got, field) {
|
|
t.Fatalf("diagnostic %q missing field %q", got, field)
|
|
}
|
|
}
|
|
if strings.Contains(got, "DO_NOT_LOG_ANSWER_TEXT") || strings.Contains(got, "CR5") {
|
|
t.Fatalf("diagnostic leaked answer content: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFormatCardUpdateDiagnosticDistinguishesFinalPlainText(t *testing.T) {
|
|
got := formatCardUpdateDiagnostic("普通回答", true, false)
|
|
wantFields := []string{
|
|
"finalize=true",
|
|
"error=false",
|
|
"lines=1",
|
|
"pipe_lines=0",
|
|
"leading_pipe_lines=0",
|
|
"delimiter_lines=0",
|
|
"confirmed_table=false",
|
|
"table_start=-1",
|
|
}
|
|
for _, field := range wantFields {
|
|
if !strings.Contains(got, field) {
|
|
t.Fatalf("diagnostic %q missing field %q", got, field)
|
|
}
|
|
}
|
|
if strings.Contains(got, "普通回答") {
|
|
t.Fatalf("diagnostic leaked answer content: %q", got)
|
|
}
|
|
}
|