-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathntfy_test.go
104 lines (73 loc) · 2.15 KB
/
ntfy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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,
}
_ = app.initConfig(false)
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 and Email", func(t *testing.T) {
app.cfg.Notifications = &configNotifications{
Ntfy: &configNtfy{
Enabled: true,
Topic: "topic",
Server: "https://ntfy.example.com",
User: "user",
Pass: "pass",
Email: "test@example.com",
},
}
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())
assert.Equal(t, "test@example.com", req.Header.Get("X-Email"))
user, pass, _ := req.BasicAuth()
assert.Equal(t, "user", user)
assert.Equal(t, "pass", pass)
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)
})
}
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())
cfg.Topic = "topic"
assert.True(t, cfg.enabled())
}