Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
111 строки
3.0 KiB
Go
111 строки
3.0 KiB
Go
package notify
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"rocketgit.ru/rsmon/worker/app/models"
|
|
)
|
|
|
|
func TestTelegram_Network_MockBotAPI(t *testing.T) {
|
|
var (
|
|
gotPath atomic.Value
|
|
gotMethod atomic.Value
|
|
gotContentTy atomic.Value
|
|
gotForm atomic.Pointer[url.Values]
|
|
)
|
|
gotPath.Store("")
|
|
gotMethod.Store("")
|
|
gotContentTy.Store("")
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath.Store(r.URL.Path)
|
|
gotMethod.Store(r.Method)
|
|
gotContentTy.Store(r.Header.Get("Content-Type"))
|
|
if err := r.ParseForm(); err == nil {
|
|
form := r.PostForm
|
|
gotForm.Store(&form)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"Test","username":"test_bot"}}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
enabled := true
|
|
cred := &models.NotificationCredential{
|
|
Kind: models.CredentialKindTelegram,
|
|
Name: "test-bot",
|
|
BotName: strPtr("test_bot"),
|
|
APIURL: strPtr(srv.URL),
|
|
Enabled: &enabled,
|
|
SecretEnc: "plain:FAKE_TOKEN_FOR_TEST",
|
|
}
|
|
|
|
if err := Telegram(cred, 123456789, "hello from test"); err != nil {
|
|
t.Fatalf("Telegram returned error: %v", err)
|
|
}
|
|
|
|
path := gotPath.Load().(string)
|
|
method := gotMethod.Load().(string)
|
|
ct := gotContentTy.Load().(string)
|
|
if method != http.MethodPost {
|
|
t.Errorf("method = %s; want POST", method)
|
|
}
|
|
if !strings.HasPrefix(path, "/botFAKE_TOKEN_FOR_TEST/") {
|
|
t.Errorf("unexpected path: %s", path)
|
|
}
|
|
if !strings.HasSuffix(path, "/sendMessage") {
|
|
t.Errorf("expected path to end with /sendMessage, got: %s", path)
|
|
}
|
|
if !strings.Contains(ct, "application/x-www-form-urlencoded") {
|
|
t.Errorf("content-type = %q; want application/x-www-form-urlencoded", ct)
|
|
}
|
|
|
|
formPtr := gotForm.Load()
|
|
if formPtr == nil {
|
|
t.Fatal("form body was not captured")
|
|
}
|
|
form := *formPtr
|
|
if got := form.Get("chat_id"); got != "123456789" {
|
|
t.Errorf("chat_id = %q; want 123456789", got)
|
|
}
|
|
if got := form.Get("text"); got != "hello from test" {
|
|
t.Errorf("text = %q; want %q", got, "hello from test")
|
|
}
|
|
}
|
|
|
|
func TestTelegram_Network_BasicAuthProxy(t *testing.T) {
|
|
var gotAuth atomic.Value
|
|
gotAuth.Store("")
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotAuth.Store(r.Header.Get("Authorization"))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ok":true,"result":{"id":1,"username":"t"}}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
authedURL := strings.Replace(srv.URL, "http://", "http://gleb:rokBelHoho@", 1)
|
|
|
|
enabled := true
|
|
cred := &models.NotificationCredential{
|
|
Kind: models.CredentialKindTelegram,
|
|
Name: "proxy-bot",
|
|
APIURL: strPtr(authedURL),
|
|
Enabled: &enabled,
|
|
SecretEnc: "plain:TOKEN",
|
|
}
|
|
|
|
if err := Telegram(cred, 1, "x"); err != nil {
|
|
t.Fatalf("Telegram returned error: %v", err)
|
|
}
|
|
got := gotAuth.Load().(string)
|
|
if !strings.HasPrefix(got, "Basic ") {
|
|
t.Errorf("expected Basic auth header, got %q", got)
|
|
}
|
|
}
|