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

66 lines
2.5 KiB
Go

package megafon
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNewBase(t *testing.T) {
for in, want := range map[string]string{
"vats970345.megapbx.ru": "https://vats970345.megapbx.ru/crmapi/v1",
"https://vats970345.megapbx.ru/": "https://vats970345.megapbx.ru/crmapi/v1",
"https://vats970345.megapbx.ru/crmapi/v1": "https://vats970345.megapbx.ru/crmapi/v1",
"https://vats970345.megapbx.ru/crmapi/v1/": "https://vats970345.megapbx.ru/crmapi/v1",
} {
if got := New(in, "k").Base; got != want {
t.Errorf("New(%q).Base = %q, want %q", in, got, want)
}
}
if New("", "k").Configured() || New("x.ru", "").Configured() {
t.Error("без адреса или ключа клиент не настроен")
}
}
func TestHistory(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-API-KEY") != "secret" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if r.URL.Path != "/crmapi/v1/history/json" || r.URL.Query().Get("start") != "20260920T210000Z" || r.URL.Query().Get("type") != "all" {
t.Errorf("запрос: %s", r.URL.String())
}
_, _ = w.Write([]byte(`[
{"uid":"1755936870","type":"in","status":"success","client":"79894787136","diversion":"79280470055",
"user":"kc","user_name":"КЦ DANA общий","start":"2026-09-23T08:22:00Z","wait":14,"duration":"9","record":"https://x/rec.mp3"},
{"uid":1755936871,"type":"missed","status":"missed","client":"79894787136","via":"+7 (928) 047-00-55","start":"2026-09-23T08:21:00Z","wait":1,"duration":0},
{"type":"in","client":"79000000000"}
]`))
}))
defer srv.Close()
c := New(srv.URL, "secret")
calls, err := c.History(context.Background(), time.Date(2026, 9, 20, 21, 0, 0, 0, time.UTC), time.Date(2026, 9, 23, 21, 0, 0, 0, time.UTC), "all")
if err != nil {
t.Fatal(err)
}
if len(calls) != 2 {
t.Fatalf("звонков %d, ждали 2 (без uid — пропуск)", len(calls))
}
a, b := calls[0], calls[1]
if a.UID != "1755936870" || a.Via != "79280470055" || a.UserName != "КЦ DANA общий" || a.Duration != 9 || a.Wait != 14 || a.RecordURL == "" {
t.Errorf("первый звонок: %+v", a)
}
if b.UID != "1755936871" || b.Type != "missed" || b.Via != "+7 (928) 047-00-55" {
t.Errorf("второй звонок: %+v", b)
}
if _, err := New(srv.URL, "wrong").History(context.Background(), time.Now(), time.Now(), ""); !errors.Is(err, ErrUnauthorized) {
t.Errorf("неверный ключ: %v", err)
}
}