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

71 lines
3.2 KiB
Go
Raw 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"
)
const validLegal = `"legal":{"org_name":"ООО «Дана Медикал»","org_short":"ООО «Дана Медикал»","inn":"0500020358","ogrn":"1240500017702","address":"Махачкала","license":"Л041-01172-05/03997730"}`
func TestDecodeSettings(t *testing.T) {
cases := []struct {
name, body, wantText string
wantCode int
}{
{"valid", `{"email":"danaclinic@yandex.ru","max_url":"https://max.ru/dana",` + validLegal + `}`, "", 200},
{"no max link", `{"email":"",` + validLegal + `}`, "", 200},
{"bad email", `{"email":"danaclinic",` + validLegal + `}`, "почты", 422},
{"max over http", `{"max_url":"javascript:alert(1)",` + validLegal + `}`, "MAX", 422},
{"short inn", `{` + strings.Replace(validLegal, "0500020358", "05000", 1) + `}`, "ИНН", 422},
{"bad ogrn", `{` + strings.Replace(validLegal, "1240500017702", "12405000177", 1) + `}`, "ОГРН", 422},
{"no license", `{` + strings.Replace(validLegal, "Л041-01172-05/03997730", " ", 1) + `}`, "лицензию", 422},
}
for _, tc := range cases {
w := httptest.NewRecorder()
r := httptest.NewRequest("PUT", "/", strings.NewReader(tc.body))
values, valid := decodeSettings(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 && len(values) != 3 {
t.Errorf("%s: unexpected keys %v", tc.name, values)
}
}
}
func TestDecodeBranch(t *testing.T) {
cases := []struct {
name, body, wantText string
wantCode int
}{
{"valid", `{"name":"Ляхова, 22","address":"Махачкала","phones":[" +7 928 962-00-94 ",""],"lat":42.97,"lon":47.49,"published":true}`, "", 200},
{"hidden without phone", `{"name":"Новый","address":"Махачкала","published":false}`, "", 200},
{"published without phone", `{"name":"Новый","address":"Махачкала","published":true}`, "телефон", 422},
{"short phone", `{"name":"Новый","address":"Махачкала","phones":["962-00"]}`, "целиком", 422},
{"one coordinate", `{"name":"Новый","address":"Махачкала","lat":42.9}`, "обе координаты", 422},
{"coordinates out of range", `{"name":"Новый","address":"Махачкала","lat":142.9,"lon":47.5}`, "диапазона", 422},
{"no address", `{"name":"Новый","address":" "}`, "адрес", 422},
}
for _, tc := range cases {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(tc.body))
in, valid := decodeBranch(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 == "valid" && (len(in.Phones) != 1 || in.Phones[0] != "+7 928 962-00-94" || in.Slug != "lyakhova-22") {
t.Errorf("valid: got %+v", in)
}
}
}