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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
package lark
import (
"errors"
"log"
"net/http"
"strings"
"testing"
"github.com/jarcoal/httpmock"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/types"
)
func TestLark(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Shoutrrr Lark Suite")
}
var (
service *Service
logger *log.Logger
_ = ginkgo.BeforeSuite(func() {
logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)
})
)
const fullURL = "lark://open.larksuite.com/token?secret=sss"
var _ = ginkgo.Describe("Lark Test", func() {
ginkgo.BeforeEach(func() {
service = &Service{}
})
ginkgo.When("parsing the configuration URL", func() {
ginkgo.It("should be identical after de-/serialization", func() {
url := testutils.URLMust(fullURL)
config := &Config{}
pkr := format.NewPropKeyResolver(config)
err := config.setURL(&pkr, url)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
outputURL := config.GetURL()
ginkgo.GinkgoT().Logf("\n\n%s\n%s\n\n-", outputURL, fullURL)
gomega.Expect(outputURL.String()).To(gomega.Equal(fullURL))
})
})
ginkgo.Context("basic service API methods", func() {
var config *Config
ginkgo.BeforeEach(func() {
config = &Config{}
})
ginkgo.It("should not allow getting invalid query values", func() {
testutils.TestConfigGetInvalidQueryValue(config)
})
ginkgo.It("should not allow setting invalid query values", func() {
testutils.TestConfigSetInvalidQueryValue(
config,
"lark://endpoint/token?secret=sss&foo=bar",
)
})
ginkgo.It("should have the expected number of fields and enums", func() {
testutils.TestConfigGetEnumsCount(config, 0)
testutils.TestConfigGetFieldsCount(config, 3)
})
})
ginkgo.When("initializing the service", func() {
ginkgo.It("should fail with invalid host", func() {
err := service.Initialize(testutils.URLMust("lark://invalid.com/token"), logger)
gomega.Expect(err).To(gomega.MatchError(ErrInvalidHost))
})
ginkgo.It("should fail with no path", func() {
err := service.Initialize(testutils.URLMust("lark://open.larksuite.com"), logger)
gomega.Expect(err).To(gomega.MatchError(ErrNoPath))
})
})
ginkgo.When("sending a message", func() {
ginkgo.When("the message is too large", func() {
ginkgo.It("should return large message error", func() {
data := make([]string, 410)
for i := range data {
data[i] = "0123456789"
}
message := strings.Join(data, "")
service := Service{Config: &Config{Host: larkHost, Path: "token"}}
gomega.Expect(service.Send(message, nil)).To(gomega.MatchError(ErrLargeMessage))
})
})
ginkgo.When("an invalid param is passed", func() {
ginkgo.It("should fail to send messages", func() {
service := Service{Config: &Config{Host: larkHost, Path: "token"}}
gomega.Expect(
service.Send("test message", &types.Params{"invalid": "value"}),
).To(gomega.MatchError(gomega.ContainSubstring("not a valid config key: invalid")))
})
})
ginkgo.Context("sending message by HTTP", func() {
ginkgo.BeforeEach(func() {
httpmock.ActivateNonDefault(httpClient)
})
ginkgo.AfterEach(func() {
httpmock.DeactivateAndReset()
})
ginkgo.It("should send text message successfully", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewJsonResponderOrPanic(
http.StatusOK,
map[string]any{"code": 0, "msg": "success"},
),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send("message", nil)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
ginkgo.It("should send post message with title successfully", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewJsonResponderOrPanic(
http.StatusOK,
map[string]any{"code": 0, "msg": "success"},
),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send("message", &types.Params{"title": "title"})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
ginkgo.It("should send post message with link successfully", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewJsonResponderOrPanic(
http.StatusOK,
map[string]any{"code": 0, "msg": "success"},
),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send(
"message",
&types.Params{"title": "title", "link": "https://example.com"},
)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
ginkgo.It("should return error on network failure", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewErrorResponder(errors.New("network error")),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send("message", nil)
gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("network error")))
})
ginkgo.It("should return error on invalid JSON response", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewStringResponder(http.StatusOK, "some response"),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send("message", nil)
gomega.Expect(err).
To(gomega.MatchError(gomega.ContainSubstring("invalid character")))
})
ginkgo.It("should return error on non-zero response code", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewJsonResponderOrPanic(
http.StatusOK,
map[string]any{"code": 1, "msg": "some error"},
),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send("message", nil)
gomega.Expect(err).To(gomega.MatchError(gomega.ContainSubstring("some error")))
})
ginkgo.It("should fail on HTTP 400 status", func() {
httpmock.RegisterResponder(
http.MethodPost,
"/open-apis/bot/v2/hook/token",
httpmock.NewStringResponder(http.StatusBadRequest, "bad request"),
)
err := service.Initialize(testutils.URLMust(fullURL), logger)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = service.Send("message", nil)
gomega.Expect(err).
To(gomega.MatchError(gomega.ContainSubstring("unexpected status 400")))
})
})
})
})
|