dana-medical-site/internal/api/admin_documents_test.go
2026-10-14 13:33:13 +00:00

66 lines
2.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"net/http/httptest"
"strings"
"testing"
)
func TestDecodeDocumentRules(t *testing.T) {
cases := []struct {
name string
body string
wantCode int
wantText string
}{
{"license with scan", `{"kind":"license","title":"Лицензия","preview_id":5,"issued_at":"2025-12-17","published":true}`, 200, ""},
{"supervisor without files", `{"kind":"supervisor","title":"Минздрав","body":"Телефон: +7","published":true}`, 200, ""},
{"hidden draft without files", `{"kind":"award","title":"Премия","published":false}`, 200, ""},
{"unknown kind", `{"kind":"passport","title":"x"}`, 422, "вид документа"},
{"no title", `{"kind":"license","title":" "}`, 422, "название"},
{"published license without files", `{"kind":"license","title":"Лицензия","published":true}`, 422, "загрузите скан"},
{"bad date", `{"kind":"license","title":"Лицензия","issued_at":"17.12.2025"}`, 422, "ГГГГ-ММ-ДД"},
}
for _, tc := range cases {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(tc.body))
_, valid := decodeDocument(w, r)
code := 200
if !valid {
code = w.Code
}
if code != tc.wantCode || (tc.wantText != "" && !strings.Contains(w.Body.String(), tc.wantText)) {
t.Errorf("%s: got %d %s, want %d containing %q", tc.name, code, w.Body.String(), tc.wantCode, tc.wantText)
}
}
}
func TestDecodeFAQRules(t *testing.T) {
cases := []struct {
name string
body string
wantCode int
wantText string
}{
{"published answer", `{"question":"Где парковка?","answer":"Во дворе\r\nбесплатно","published":true}`, 200, ""},
{"draft without answer", `{"question":"Черновик","published":false}`, 200, ""},
{"no question", `{"question":"","answer":"x"}`, 422, "вопрос"},
{"published without answer", `{"question":"Где парковка?","published":true}`, 422, "напишите ответ"},
}
for _, tc := range cases {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(tc.body))
in, valid := decodeFAQ(w, r)
code := 200
if !valid {
code = w.Code
}
if code != tc.wantCode || (tc.wantText != "" && !strings.Contains(w.Body.String(), tc.wantText)) {
t.Errorf("%s: got %d %s, want %d containing %q", tc.name, code, w.Body.String(), tc.wantCode, tc.wantText)
}
if valid && strings.Contains(in.Answer, "\r") {
t.Errorf("%s: answer keeps CR", tc.name)
}
}
}