42 lines
1.8 KiB
Go
42 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDecodeBannerRules(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
body string
|
|
wantOK bool
|
|
wantText string
|
|
}{
|
|
{"text slide", `{"layout":"text","theme":"lilac","title":"Программы чек-апов"}`, true, ""},
|
|
{"doctor slide", `{"layout":"doctor","title":"Уролог из Москвы","image_id":5,"chips":["26 лет стажа"," "]}`, true, ""},
|
|
{"image slide by picture", `{"layout":"image","image_id":5}`, true, ""},
|
|
{"text slide without title", `{"layout":"text","theme":"lilac"}`, false, "заголовок"},
|
|
{"doctor slide without photo", `{"layout":"doctor","title":"Уролог"}`, false, "фото"},
|
|
{"unknown layout", `{"layout":"video","title":"x"}`, false, "вид"},
|
|
{"unknown theme", `{"layout":"text","theme":"red","title":"x"}`, false, "цвет"},
|
|
{"too many chips", `{"layout":"text","title":"x","chips":["1","2","3","4","5","6","7"]}`, false, "6 плашек"},
|
|
{"javascript link", `{"layout":"text","title":"x","link_url":"javascript:alert(1)"}`, false, "Ссылка"},
|
|
}
|
|
for _, tc := range cases {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", "/api/v1/admin/banners", strings.NewReader(tc.body))
|
|
in, valid := decodeBanner(w, r)
|
|
if valid != tc.wantOK || (tc.wantText != "" && !strings.Contains(w.Body.String(), tc.wantText)) {
|
|
t.Errorf("%s: valid=%v body=%s, want valid=%v containing %q", tc.name, valid, w.Body.String(), tc.wantOK, tc.wantText)
|
|
continue
|
|
}
|
|
if valid && in.Disclaimer == "" {
|
|
t.Errorf("%s: disclaimer must default to the ФЗ «О рекламе» warning", tc.name)
|
|
}
|
|
if tc.name == "doctor slide" && len(in.Chips) != 1 {
|
|
t.Errorf("empty chips must be dropped, got %q", in.Chips)
|
|
}
|
|
}
|
|
}
|