2021-12-29 07:09:42 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_ntfySending(t *testing.T) {
|
|
|
|
fakeClient := newFakeHttpClient()
|
|
|
|
fakeClient.setHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {}))
|
|
|
|
|
|
|
|
app := &goBlog{
|
|
|
|
cfg: createDefaultTestConfig(t),
|
|
|
|
httpClient: fakeClient.Client,
|
|
|
|
}
|
|
|
|
|
2022-07-16 21:09:43 +02:00
|
|
|
_ = app.initConfig(false)
|
2021-12-29 07:09:42 +01:00
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
t.Run("Default", func(t *testing.T) {
|
|
|
|
app.cfg.Notifications = &configNotifications{
|
|
|
|
Ntfy: &configNtfy{
|
|
|
|
Enabled: true,
|
|
|
|
Topic: "topic",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.sendNotification("Test notification")
|
|
|
|
|
|
|
|
req := fakeClient.req
|
|
|
|
|
|
|
|
require.NotNil(t, req)
|
|
|
|
assert.Equal(t, http.MethodPost, req.Method)
|
|
|
|
assert.Equal(t, "https://ntfy.sh/topic", req.URL.String())
|
|
|
|
|
|
|
|
reqBody, _ := req.GetBody()
|
|
|
|
reqBodyByte, _ := io.ReadAll(reqBody)
|
|
|
|
|
|
|
|
assert.Equal(t, "Test notification", string(reqBodyByte))
|
|
|
|
|
|
|
|
res := fakeClient.res
|
|
|
|
|
|
|
|
require.NotNil(t, res)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Custom server with Basic Auth", func(t *testing.T) {
|
|
|
|
app.cfg.Notifications = &configNotifications{
|
|
|
|
Ntfy: &configNtfy{
|
|
|
|
Enabled: true,
|
|
|
|
Topic: "topic",
|
|
|
|
Server: "https://ntfy.example.com",
|
|
|
|
User: "user",
|
|
|
|
Pass: "pass",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.sendNotification("Test notification")
|
|
|
|
|
|
|
|
req := fakeClient.req
|
|
|
|
|
|
|
|
require.NotNil(t, req)
|
|
|
|
assert.Equal(t, http.MethodPost, req.Method)
|
|
|
|
assert.Equal(t, "https://ntfy.example.com/topic", req.URL.String())
|
2021-12-29 07:09:42 +01:00
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
user, pass, _ := req.BasicAuth()
|
|
|
|
assert.Equal(t, "user", user)
|
|
|
|
assert.Equal(t, "pass", pass)
|
2021-12-29 07:09:42 +01:00
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
reqBody, _ := req.GetBody()
|
|
|
|
reqBodyByte, _ := io.ReadAll(reqBody)
|
2021-12-29 07:09:42 +01:00
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
assert.Equal(t, "Test notification", string(reqBodyByte))
|
2021-12-29 07:09:42 +01:00
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
res := fakeClient.res
|
2021-12-29 07:09:42 +01:00
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
require.NotNil(t, res)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
})
|
2021-12-29 07:09:42 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ntfyConfig(t *testing.T) {
|
|
|
|
var cfg *configNtfy
|
|
|
|
|
|
|
|
assert.False(t, cfg.enabled())
|
|
|
|
|
|
|
|
cfg = &configNtfy{}
|
|
|
|
|
|
|
|
assert.False(t, cfg.enabled())
|
|
|
|
|
|
|
|
cfg.Enabled = true
|
|
|
|
|
|
|
|
assert.False(t, cfg.enabled())
|
|
|
|
|
2022-04-04 13:07:36 +02:00
|
|
|
cfg.Topic = "topic"
|
2021-12-29 07:09:42 +01:00
|
|
|
|
|
|
|
assert.True(t, cfg.enabled())
|
|
|
|
}
|