56 lines
2.2 KiB
Go
56 lines
2.2 KiB
Go
package api
|
||
|
||
import (
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func TestPartnerURL(t *testing.T) {
|
||
for in, want := range map[string]string{
|
||
"": "",
|
||
"glaz-almaz05.ru": "https://glaz-almaz05.ru",
|
||
" https://ochevidno.ru/ ": "https://ochevidno.ru/",
|
||
"http://abilitycenter.ru": "http://abilitycenter.ru",
|
||
} {
|
||
if got, ok := partnerURL(in); !ok || got != want {
|
||
t.Errorf("partnerURL(%q) = %q, %v; want %q", in, got, ok, want)
|
||
}
|
||
}
|
||
for _, in := range []string{"javascript:alert(1)", "https://localhost", "ftp://files.example.ru", "https://user:pass@example.ru"} {
|
||
if got, ok := partnerURL(in); ok {
|
||
t.Errorf("partnerURL(%q) accepted as %q", in, got)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestDecodeReviewRules(t *testing.T) {
|
||
cases := []struct {
|
||
name, body, wantText string
|
||
wantCode int
|
||
}{
|
||
{"text review", `{"author_name":"Мадина","text":"Спасибо","status":"published","rating":5}`, "", 200},
|
||
{"video review link is normalised", `{"author_name":"Пациент","video_url":"https://youtu.be/V1w8DhkWARc","status":"published"}`, "", 200},
|
||
{"foreign video host", `{"author_name":"Пациент","video_url":"javascript:alert(1)","status":"published"}`, "YouTube или Rutube", 422},
|
||
{"empty review", `{"author_name":"Пациент","status":"pending"}`, "текст или ссылку", 422},
|
||
{"no author", `{"author_name":" ","text":"Спасибо","status":"pending"}`, "имя автора", 422},
|
||
{"bad status", `{"author_name":"А","text":"Б","status":"spam"}`, "статус", 422},
|
||
{"bad rating", `{"author_name":"А","text":"Б","status":"pending","rating":7}`, "от 1 до 5", 422},
|
||
}
|
||
for _, tc := range cases {
|
||
w := httptest.NewRecorder()
|
||
r := httptest.NewRequest("POST", "/", strings.NewReader(tc.body))
|
||
in, valid := decodeReview(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 tc.name == "video review link is normalised" && in.VideoURL != "https://www.youtube.com/embed/V1w8DhkWARc" {
|
||
t.Errorf("video url = %q", in.VideoURL)
|
||
}
|
||
}
|
||
}
|