Debian Codegraph @

pkg/services/chat/lark/lark_test.go

raw
1 package lark
2
3 import (
4 "errors"
5 "log"
6 "net/http"
7 "strings"
8 "testing"
9
10 "github.com/jarcoal/httpmock"
11 "github.com/onsi/ginkgo/v2"
12 "github.com/onsi/gomega"
13
14 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
15 "github.com/nicholas-fedor/shoutrrr/pkg/format"
16 "github.com/nicholas-fedor/shoutrrr/pkg/types"
17 )
18
19 const fullURL = "lark://open.larksuite.com/token?secret=sss"
20
21 var (
22 service *Service
23 logger *log.Logger
24 _ = ginkgo.BeforeSuite(func() {
25 logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)
26 })
27 )
28
29 var _ = ginkgo.Describe("Lark Test", func() {
30 ginkgo.BeforeEach(func() {
31 service = &Service{}
32 })
33
34 ginkgo.When("parsing the configuration URL", func() {
35 ginkgo.It("should be identical after de-/serialization", func() {
36 urlStr := testutils.URLMust(fullURL)
37 config := &Config{}
38 pkr := format.NewPropKeyResolver(config)
39 err := config.setURL(&pkr, urlStr)
40 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
41
42 outputURL := config.GetURL()
43 ginkgo.GinkgoT().Logf("\n\n%s\n%s\n\n-", outputURL, fullURL)
44 gomega.Expect(outputURL.String()).To(gomega.Equal(fullURL))
45 })
46 })
47
48 ginkgo.Context("basic service API methods", func() {
49 var config *Config
50
51 ginkgo.BeforeEach(func() {
52 config = &Config{}
53 })
54 ginkgo.It("should not allow getting invalid query values", func() {
55 testutils.TestConfigGetInvalidQueryValue(config)
56 })
57 ginkgo.It("should not allow setting invalid query values", func() {
58 testutils.TestConfigSetInvalidQueryValue(
59 config,
60 "lark://endpoint/token?secret=sss&foo=bar",
61 )
62 })
63 ginkgo.It("should have the expected number of fields and enums", func() {
64 testutils.TestConfigGetEnumsCount(config, 0)
65 testutils.TestConfigGetFieldsCount(config, 3)
66 })
67 })
68
69 ginkgo.When("initializing the service", func() {
70 ginkgo.It("should fail with invalid host", func() {
71 err := service.Initialize(testutils.URLMust("lark://invalid.com/token"), logger)
72 gomega.Expect(err).To(gomega.MatchError(ErrInvalidHost))
73 })
74 ginkgo.It("should fail with no path", func() {
75 err := service.Initialize(testutils.URLMust("lark://open.larksuite.com"), logger)
76 gomega.Expect(err).To(gomega.MatchError(ErrNoPath))
77 })
78 })
79
80 ginkgo.When("sending a message", func() {
81 ginkgo.When("the message is too large", func() {
82 ginkgo.It("should return large message error", func() {
83 data := make([]string, 410)
84 for i := range data {
85 data[i] = "0123456789"
86 }
87
88 message := strings.Join(data, "")
89 service := Service{Config: &Config{Host: larkHost, Path: "token"}}
90 gomega.Expect(service.Send(message, nil)).To(gomega.MatchError(ErrLargeMessage))
91 })
92 })
93
94 ginkgo.When("an invalid param is passed", func() {
95 ginkgo.It("should fail to send messages", func() {
96 service := Service{Config: &Config{Host: larkHost, Path: "token"}}
97 gomega.Expect(
98 service.Send("test message", &types.Params{"invalid": "value"}),
99 ).To(gomega.MatchError(gomega.ContainSubstring("not a valid config key: invalid")))
100 })
101 })
102
103 ginkgo.Context("sending message by HTTP", func() {
104 ginkgo.BeforeEach(func() {
105 httpmock.ActivateNonDefault(httpClient)
106 })
107 ginkgo.AfterEach(func() {
108 httpmock.DeactivateAndReset()
109 })
110
111 ginkgo.It("should send text message successfully", func() {
112 httpmock.RegisterResponder(
113 http.MethodPost,
114 "/open-apis/bot/v2/hook/token",
115 httpmock.NewJsonResponderOrPanic(
116 http.StatusOK,
117 map[string]any{"code": 0, "msg": "success"},
118 ),
119 )
120
121 err := service.Initialize(testutils.URLMust(fullURL), logger)
122 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
123 err = service.Send("message", nil)
124 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
125 })
126
127 ginkgo.It("should send post message with title successfully", func() {
128 httpmock.RegisterResponder(
129 http.MethodPost,
130 "/open-apis/bot/v2/hook/token",
131 httpmock.NewJsonResponderOrPanic(
132 http.StatusOK,
133 map[string]any{"code": 0, "msg": "success"},
134 ),
135 )
136
137 err := service.Initialize(testutils.URLMust(fullURL), logger)
138 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
139 err = service.Send("message", &types.Params{"title": "title"})
140 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
141 })
142
143 ginkgo.It("should send post message with link successfully", func() {
144 httpmock.RegisterResponder(
145 http.MethodPost,
146 "/open-apis/bot/v2/hook/token",
147 httpmock.NewJsonResponderOrPanic(
148 http.StatusOK,
149 map[string]any{"code": 0, "msg": "success"},
150 ),
151 )
152
153 err := service.Initialize(testutils.URLMust(fullURL), logger)
154 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
155 err = service.Send(
156 "message",
157 &types.Params{"title": "title", "link": "https://example.com"},
158 )
159 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
160 })
161
162 ginkgo.It("should return error on network failure", func() {
163 httpmock.RegisterResponder(
164 http.MethodPost,
165 "/open-apis/bot/v2/hook/token",
166 httpmock.NewErrorResponder(errors.New("network error")),
167 )
168
169 err := service.Initialize(testutils.URLMust(fullURL), logger)
170 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
171 err = service.Send("message", nil)
172 gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("network error")))
173 })
174
175 ginkgo.It("should return error on invalid JSON response", func() {
176 httpmock.RegisterResponder(
177 http.MethodPost,
178 "/open-apis/bot/v2/hook/token",
179 httpmock.NewStringResponder(http.StatusOK, "some response"),
180 )
181
182 err := service.Initialize(testutils.URLMust(fullURL), logger)
183 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
184 err = service.Send("message", nil)
185 gomega.Expect(err).
186 To(gomega.MatchError(gomega.ContainSubstring("invalid character")))
187 })
188
189 ginkgo.It("should return error on non-zero response code", func() {
190 httpmock.RegisterResponder(
191 http.MethodPost,
192 "/open-apis/bot/v2/hook/token",
193 httpmock.NewJsonResponderOrPanic(
194 http.StatusOK,
195 map[string]any{"code": 1, "msg": "some error"},
196 ),
197 )
198
199 err := service.Initialize(testutils.URLMust(fullURL), logger)
200 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
201 err = service.Send("message", nil)
202 gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("some error")))
203 })
204
205 ginkgo.It("should fail on HTTP 400 status", func() {
206 httpmock.RegisterResponder(
207 http.MethodPost,
208 "/open-apis/bot/v2/hook/token",
209 httpmock.NewStringResponder(http.StatusBadRequest, "bad request"),
210 )
211
212 err := service.Initialize(testutils.URLMust(fullURL), logger)
213 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
214 err = service.Send("message", nil)
215 gomega.Expect(err).
216 To(gomega.MatchError(gomega.ContainSubstring("unexpected status 400")))
217 })
218 })
219 })
220 })
221
222 func TestLark(t *testing.T) {
223 t.Parallel()
224
225 gomega.RegisterFailHandler(ginkgo.Fail)
226 ginkgo.RunSpecs(t, "Shoutrrr Lark Suite")
227 }