Debian Codegraph @

pkg/services/chat/wecom/wecom_test.go

raw
1 package wecom
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 = "wecom://693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa"
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("WeCom 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 url := testutils.URLMust(fullURL)
37 config := &Config{}
38 pkr := format.NewPropKeyResolver(config)
39 err := config.setURL(&pkr, url)
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 "wecom://693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa?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 key format", func() {
71 err := service.Initialize(testutils.URLMust("wecom://invalid.key"), logger)
72 gomega.Expect(err).
73 To(gomega.MatchError(gomega.ContainSubstring("invalid WeCom webhook key format")))
74 })
75 ginkgo.It("should fail with empty key", func() {
76 err := service.Initialize(testutils.URLMust("wecom://"), logger)
77 gomega.Expect(err).
78 To(gomega.MatchError(gomega.ContainSubstring("WeCom webhook key cannot be empty")))
79 })
80 })
81
82 ginkgo.When("sending a message", func() {
83 ginkgo.When("the message is too large", func() {
84 ginkgo.It("should return large message error", func() {
85 data := make([]string, 410)
86 for i := range data {
87 data[i] = "0123456789"
88 }
89
90 message := strings.Join(data, "")
91 service := Service{Config: &Config{Key: "693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa"}}
92 gomega.Expect(service.Send(message, nil)).To(gomega.MatchError(ErrLargeMessage))
93 })
94 })
95
96 ginkgo.When("an invalid param is passed", func() {
97 ginkgo.It("should fail to send messages", func() {
98 service := Service{Config: &Config{Key: "693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa"}}
99 gomega.Expect(
100 service.Send("test message", &types.Params{"invalid": "value"}),
101 ).To(gomega.MatchError(gomega.ContainSubstring("not a valid config key: invalid")))
102 })
103 })
104
105 ginkgo.Context("sending message by HTTP", func() {
106 ginkgo.BeforeEach(func() {
107 httpmock.ActivateNonDefault(httpClient)
108 })
109 ginkgo.AfterEach(func() {
110 httpmock.DeactivateAndReset()
111 })
112
113 ginkgo.It("should send text message successfully", func() {
114 httpmock.RegisterResponder(
115 http.MethodPost,
116 "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa",
117 httpmock.NewJsonResponderOrPanic(
118 http.StatusOK,
119 map[string]any{"errcode": 0, "errmsg": "ok"},
120 ),
121 )
122
123 err := service.Initialize(testutils.URLMust(fullURL), logger)
124 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
125 err = service.Send("message", nil)
126 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
127 })
128
129 ginkgo.It("should send message with mentions successfully", func() {
130 httpmock.RegisterResponder(
131 http.MethodPost,
132 "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa",
133 httpmock.NewJsonResponderOrPanic(
134 http.StatusOK,
135 map[string]any{"errcode": 0, "errmsg": "ok"},
136 ),
137 )
138
139 err := service.Initialize(testutils.URLMust(fullURL), logger)
140 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
141 err = service.Send("message", &types.Params{"mentioned_list": "@all"})
142 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
143 })
144
145 ginkgo.It("should return error on network failure", func() {
146 httpmock.RegisterResponder(
147 http.MethodPost,
148 "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa",
149 httpmock.NewErrorResponder(errors.New("network error")),
150 )
151
152 err := service.Initialize(testutils.URLMust(fullURL), logger)
153 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
154 err = service.Send("message", nil)
155 gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("network error")))
156 })
157
158 ginkgo.It("should return error on invalid JSON response", func() {
159 httpmock.RegisterResponder(
160 http.MethodPost,
161 "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa",
162 httpmock.NewStringResponder(http.StatusOK, "some response"),
163 )
164
165 err := service.Initialize(testutils.URLMust(fullURL), logger)
166 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
167 err = service.Send("message", nil)
168 gomega.Expect(err).
169 To(gomega.MatchError(gomega.ContainSubstring("invalid character")))
170 })
171
172 ginkgo.It("should return error on non-zero response code", func() {
173 httpmock.RegisterResponder(
174 http.MethodPost,
175 "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa",
176 httpmock.NewJsonResponderOrPanic(
177 http.StatusOK,
178 map[string]any{"errcode": 1, "errmsg": "some error"},
179 ),
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).To(gomega.MatchError(gomega.ContainSubstring("some error")))
186 })
187
188 ginkgo.It("should fail on HTTP 400 status", func() {
189 httpmock.RegisterResponder(
190 http.MethodPost,
191 "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa",
192 httpmock.NewStringResponder(http.StatusBadRequest, "bad request"),
193 )
194
195 err := service.Initialize(testutils.URLMust(fullURL), logger)
196 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
197 err = service.Send("message", nil)
198 gomega.Expect(err).
199 To(gomega.MatchError(gomega.ContainSubstring("unexpected status 400")))
200 })
201 })
202 })
203 })
204
205 func TestWeCom(t *testing.T) {
206 t.Parallel()
207 gomega.RegisterFailHandler(ginkgo.Fail)
208 ginkgo.RunSpecs(t, "Shoutrrr WeCom Suite")
209 }