52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.118im.com/im_team/im_workbench/internal/store"
|
|
)
|
|
|
|
func TestValidateMailboxSubmissionRejectsTooLongContent(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/workbench/executive-mailbox/submissions", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
if validateMailboxSubmission(rec, req, "建议", strings.Repeat("a", maxMailboxContentLength+1)) {
|
|
t.Fatal("validateMailboxSubmission returned true for too-long content")
|
|
}
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body apiError
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal response: %v", err)
|
|
}
|
|
if body.Reason != reasonValidationFailed {
|
|
t.Fatalf("reason=%q body=%s", body.Reason, rec.Body.String())
|
|
}
|
|
if body.Details["field"] != "content" {
|
|
t.Fatalf("details=%v", body.Details)
|
|
}
|
|
}
|
|
|
|
func TestWriteStoreErrorMapsPolicyToValidationFailed(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/workbench/executive-mailbox/submissions", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
writeStoreError(rec, req, store.ErrPolicy)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body apiError
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal response: %v", err)
|
|
}
|
|
if body.Reason != reasonValidationFailed {
|
|
t.Fatalf("reason=%q body=%s", body.Reason, rec.Body.String())
|
|
}
|
|
}
|