package api import ( "bytes" "encoding/json" "strings" "testing" "git.golem15.com/golem15/sm-faq-plugin/models" "git.golem15.com/golem15/summercms/modules/lagoon" ) // TestFaqSerialize pins the D-17 response object without a database: the // description and every answer pass through the sanitizer on output, clean // HTML byte for byte; a null description stays null; questions are plain // text (numbers formatted, null empty); content is never null; and the // slug pattern the handler accepts. func TestFaqSerialize(t *testing.T) { desc := `

About.

` faq := models.FAQ{Name: "Shipping", Slug: "shipping", Description: &desc, Content: lagoon.Jsonable[models.Content]{Valid: true, Data: models.Content{ {"question": "How long?", "answer": "

Two days & more.

"}, {"question": 42.0, "answer": `

Here

`}, {"question": nil, "answer": nil}, }}} out := serialize(faq, "en") var buf bytes.Buffer enc := json.NewEncoder(&buf) enc.SetEscapeHTML(false) if err := enc.Encode(out); err != nil { t.Fatal(err) } raw := strings.TrimSuffix(buf.String(), "\n") want := `{"name":"Shipping","slug":"shipping","locale":"en","description":"

About.

","content":[` + `{"question":"How long?","answer":"

Two days & more.

"},` + `{"question":"42","answer":"

Here

"},` + `{"question":"","answer":""}]}` if raw != want { t.Fatalf("serialized:\n%s\nwant:\n%s", raw, want) } if strings.Contains(raw, "onerror") || strings.Contains(raw, "onclick") || strings.Contains(raw, "