-
Notifications
You must be signed in to change notification settings - Fork 61
/
gotify_test.go
165 lines (146 loc) · 5.09 KB
/
gotify_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gotify
import (
"log"
"net/url"
"testing"
"github.com/containrrr/shoutrrr/internal/testutils"
"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestGotify(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Shoutrrr Gotify Suite")
}
var logger *log.Logger
var _ = Describe("the Gotify plugin URL building and token validation functions", func() {
It("should build a valid gotify URL", func() {
config := Config{
Token: "Aaa.bbb.ccc.ddd",
Host: "my.gotify.tld",
}
url, err := buildURL(&config)
Expect(err).To(BeNil())
expectedURL := "https://my.gotify.tld/message?token=Aaa.bbb.ccc.ddd"
Expect(url).To(Equal(expectedURL))
})
When("TLS is disabled", func() {
It("should use http schema", func() {
config := Config{
Token: "Aaa.bbb.ccc.ddd",
Host: "my.gotify.tld",
DisableTLS: true,
}
url, err := buildURL(&config)
Expect(err).To(BeNil())
expectedURL := "http://my.gotify.tld/message?token=Aaa.bbb.ccc.ddd"
Expect(url).To(Equal(expectedURL))
})
})
When("a custom path is provided", func() {
It("should add it to the URL", func() {
config := Config{
Token: "Aaa.bbb.ccc.ddd",
Host: "my.gotify.tld",
Path: "/gotify",
}
url, err := buildURL(&config)
Expect(err).To(BeNil())
expectedURL := "https://my.gotify.tld/gotify/message?token=Aaa.bbb.ccc.ddd"
Expect(url).To(Equal(expectedURL))
})
})
When("provided a valid token", func() {
It("should return true", func() {
token := "Ahwbsdyhwwgarxd"
Expect(isTokenValid(token)).To(BeTrue())
})
})
When("provided a token with an invalid prefix", func() {
It("should return false", func() {
token := "Chwbsdyhwwgarxd"
Expect(isTokenValid(token)).To(BeFalse())
})
})
When("provided a token with an invalid length", func() {
It("should return false", func() {
token := "Chwbsdyhwwga"
Expect(isTokenValid(token)).To(BeFalse())
})
})
Describe("creating the API URL", func() {
When("the token is invalid", func() {
It("should return an error", func() {
config := Config{
Token: "invalid",
}
_, err := buildURL(&config)
Expect(err).To(HaveOccurred())
})
})
})
Describe("creating a config", func() {
When("parsing the configuration URL", func() {
It("should be identical after de-/serialization (with path)", func() {
testURL := "gotify://my.gotify.tld/gotify/Aaa.bbb.ccc.ddd?title=Test+title"
config := &Config{}
Expect(config.SetURL(testutils.URLMust(testURL))).To(Succeed())
Expect(config.GetURL().String()).To(Equal(testURL))
})
It("should be identical after de-/serialization (without path)", func() {
testURL := "gotify://my.gotify.tld/Aaa.bbb.ccc.ddd?disabletls=Yes&priority=1&title=Test+title"
config := &Config{}
Expect(config.SetURL(testutils.URLMust(testURL))).To(Succeed())
Expect(config.GetURL().String()).To(Equal(testURL))
})
It("should allow slash at the end of the token", func() {
url := testutils.URLMust("gotify://my.gotify.tld/Aaa.bbb.ccc.ddd/")
config := &Config{}
Expect(config.SetURL(url)).To(Succeed())
Expect(config.Token).To(Equal("Aaa.bbb.ccc.ddd"))
})
It("should allow slash at the end of the token, with additional path", func() {
url := testutils.URLMust("gotify://my.gotify.tld/path/to/gotify/Aaa.bbb.ccc.ddd/")
config := &Config{}
Expect(config.SetURL(url)).To(Succeed())
Expect(config.Token).To(Equal("Aaa.bbb.ccc.ddd"))
})
It("should not crash on empty token or path slash at the end of the token", func() {
config := &Config{}
Expect(config.SetURL(testutils.URLMust("gotify://my.gotify.tld//"))).To(Succeed())
Expect(config.SetURL(testutils.URLMust("gotify://my.gotify.tld/"))).To(Succeed())
})
})
})
Describe("sending the payload", func() {
var err error
var service Service
AfterEach(func() {
httpmock.DeactivateAndReset()
})
It("should not report an error if the server accepts the payload", func() {
serviceURL, _ := url.Parse("gotify://my.gotify.tld/Aaa.bbb.ccc.ddd")
err = service.Initialize(serviceURL, logger)
httpmock.ActivateNonDefault(service.GetHTTPClient())
Expect(err).NotTo(HaveOccurred())
targetURL := "https://my.gotify.tld/message?token=Aaa.bbb.ccc.ddd"
httpmock.RegisterResponder("POST", targetURL, testutils.JSONRespondMust(200, messageResponse{}))
err = service.Send("Message", nil)
Expect(err).NotTo(HaveOccurred())
})
It("should not panic if an error occurs when sending the payload", func() {
serviceURL, _ := url.Parse("gotify://my.gotify.tld/Aaa.bbb.ccc.ddd")
err = service.Initialize(serviceURL, logger)
httpmock.ActivateNonDefault(service.GetHTTPClient())
Expect(err).NotTo(HaveOccurred())
targetURL := "https://my.gotify.tld/message?token=Aaa.bbb.ccc.ddd"
httpmock.RegisterResponder("POST", targetURL, testutils.JSONRespondMust(401, errorResponse{
Name: "Unauthorized",
Code: 401,
Description: "you need to provide a valid access token or user credentials to access this api",
}))
err = service.Send("Message", nil)
Expect(err).To(HaveOccurred())
})
})
})