2021-06-14 19:34:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-06-19 06:37:16 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-06-14 19:34:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_configTelegram_enabled(t *testing.T) {
|
|
|
|
if (&configTelegram{}).enabled() == true {
|
|
|
|
t.Error("Telegram shouldn't be enabled")
|
2021-06-14 20:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tg *configTelegram
|
|
|
|
if tg.enabled() == true {
|
|
|
|
t.Error("Telegram shouldn't be enabled")
|
2021-06-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (&configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
}).enabled() == true {
|
|
|
|
t.Error("Telegram shouldn't be enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
ChatID: "abc",
|
|
|
|
}).enabled() == true {
|
|
|
|
t.Error("Telegram shouldn't be enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
BotToken: "abc",
|
|
|
|
}).enabled() == true {
|
|
|
|
t.Error("Telegram shouldn't be enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
BotToken: "abc",
|
|
|
|
ChatID: "abc",
|
|
|
|
}).enabled() != true {
|
|
|
|
t.Error("Telegram should be enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_configTelegram_generateHTML(t *testing.T) {
|
|
|
|
tg := &configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
ChatID: "abc",
|
|
|
|
BotToken: "abc",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Without Instant View
|
|
|
|
|
|
|
|
expected := "Title\n\n<a href=\"https://example.com/s/1\">https://example.com/s/1</a>"
|
|
|
|
if got := tg.generateHTML("Title", "https://example.com/test", "https://example.com/s/1"); got != expected {
|
|
|
|
t.Errorf("Wrong result, got: %v", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With Instant View
|
|
|
|
|
|
|
|
tg.InstantViewHash = "abc"
|
|
|
|
expected = "Title\n\n<a href=\"https://t.me/iv?rhash=abc&url=https%3A%2F%2Fexample.com%2Ftest\">https://example.com/s/1</a>"
|
|
|
|
if got := tg.generateHTML("Title", "https://example.com/test", "https://example.com/s/1"); got != expected {
|
|
|
|
t.Errorf("Wrong result, got: %v", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_configTelegram_send(t *testing.T) {
|
2021-06-19 06:37:16 +00:00
|
|
|
fakeClient := getFakeHTTPClient()
|
2021-06-14 19:34:29 +00:00
|
|
|
|
|
|
|
tg := &configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
ChatID: "chatid",
|
|
|
|
BotToken: "bottoken",
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
app := &goBlog{
|
|
|
|
httpClient: fakeClient,
|
2021-06-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 13:18:02 +00:00
|
|
|
fakeClient.setFakeResponse(200, "")
|
2021-06-19 06:37:16 +00:00
|
|
|
|
|
|
|
err := app.send(tg, "Message", "HTML")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, fakeClient.req)
|
|
|
|
assert.Equal(t, http.MethodPost, fakeClient.req.Method)
|
|
|
|
assert.Equal(t, "https://api.telegram.org/botbottoken/sendMessage?chat_id=chatid&parse_mode=HTML&text=Message", fakeClient.req.URL.String())
|
2021-06-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_goBlog_initTelegram(t *testing.T) {
|
|
|
|
app := &goBlog{
|
|
|
|
pPostHooks: []postHookFunc{},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.initTelegram()
|
|
|
|
|
|
|
|
if len(app.pPostHooks) != 1 {
|
|
|
|
t.Error("Hook not registered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_telegram(t *testing.T) {
|
|
|
|
t.Run("Send post to Telegram", func(t *testing.T) {
|
2021-06-19 06:37:16 +00:00
|
|
|
fakeClient := getFakeHTTPClient()
|
2021-06-14 19:34:29 +00:00
|
|
|
|
2021-06-20 13:18:02 +00:00
|
|
|
fakeClient.setFakeResponse(200, "")
|
2021-06-14 19:34:29 +00:00
|
|
|
|
|
|
|
app := &goBlog{
|
|
|
|
pPostHooks: []postHookFunc{},
|
|
|
|
cfg: &config{
|
|
|
|
Server: &configServer{
|
|
|
|
PublicAddress: "https://example.com",
|
|
|
|
},
|
|
|
|
Blogs: map[string]*configBlog{
|
|
|
|
"en": {
|
|
|
|
Telegram: &configTelegram{
|
|
|
|
Enabled: true,
|
|
|
|
ChatID: "chatid",
|
|
|
|
BotToken: "bottoken",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-19 06:37:16 +00:00
|
|
|
httpClient: fakeClient,
|
2021-06-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
app.setInMemoryDatabase()
|
|
|
|
|
|
|
|
app.initTelegram()
|
|
|
|
|
|
|
|
p := &post{
|
|
|
|
Path: "/test",
|
|
|
|
Parameters: map[string][]string{
|
|
|
|
"title": {"Title"},
|
|
|
|
},
|
|
|
|
Published: time.Now().String(),
|
|
|
|
Section: "test",
|
|
|
|
Blog: "en",
|
|
|
|
Status: statusPublished,
|
|
|
|
}
|
|
|
|
|
|
|
|
app.pPostHooks[0](p)
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"https://api.telegram.org/botbottoken/sendMessage?chat_id=chatid&parse_mode=HTML&text=Title%0A%0A%3Ca+href%3D%22https%3A%2F%2Fexample.com%2Fs%2F1%22%3Ehttps%3A%2F%2Fexample.com%2Fs%2F1%3C%2Fa%3E",
|
|
|
|
fakeClient.req.URL.String(),
|
|
|
|
)
|
2021-06-14 19:34:29 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Telegram disabled", func(t *testing.T) {
|
2021-06-19 06:37:16 +00:00
|
|
|
fakeClient := getFakeHTTPClient()
|
2021-06-14 19:34:29 +00:00
|
|
|
|
2021-06-20 13:18:02 +00:00
|
|
|
fakeClient.setFakeResponse(200, "")
|
2021-06-14 19:34:29 +00:00
|
|
|
|
|
|
|
app := &goBlog{
|
|
|
|
pPostHooks: []postHookFunc{},
|
|
|
|
cfg: &config{
|
|
|
|
Server: &configServer{
|
|
|
|
PublicAddress: "https://example.com",
|
|
|
|
},
|
|
|
|
Blogs: map[string]*configBlog{
|
|
|
|
"en": {},
|
|
|
|
},
|
|
|
|
},
|
2021-06-19 06:37:16 +00:00
|
|
|
httpClient: fakeClient,
|
2021-06-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
app.setInMemoryDatabase()
|
|
|
|
|
|
|
|
app.initTelegram()
|
|
|
|
|
|
|
|
p := &post{
|
|
|
|
Path: "/test",
|
|
|
|
Parameters: map[string][]string{
|
|
|
|
"title": {"Title"},
|
|
|
|
},
|
|
|
|
Published: time.Now().String(),
|
|
|
|
Section: "test",
|
|
|
|
Blog: "en",
|
|
|
|
Status: statusPublished,
|
|
|
|
}
|
|
|
|
|
|
|
|
app.pPostHooks[0](p)
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
assert.Nil(t, fakeClient.req)
|
2021-06-14 19:34:29 +00:00
|
|
|
})
|
|
|
|
}
|