-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathwebex_test.go
154 lines (132 loc) · 4.28 KB
/
webex_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
package webex_test
import (
"fmt"
"log"
"github.com/containrrr/shoutrrr/internal/testutils"
. "github.com/containrrr/shoutrrr/pkg/services/webex"
"github.com/containrrr/shoutrrr/pkg/types"
"github.com/jarcoal/httpmock"
"net/url"
"os"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestWebex(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Shoutrrr Webex Suite")
}
var (
service *Service
envWebexURL *url.URL
logger *log.Logger
_ = BeforeSuite(func() {
service = &Service{}
envWebexURL, _ = url.Parse(os.Getenv("SHOUTRRR_WEBEX_URL"))
logger = log.New(GinkgoWriter, "Test", log.LstdFlags)
})
)
var _ = Describe("the webex service", func() {
When("running integration tests", func() {
It("should work without errors", func() {
if envWebexURL.String() == "" {
return
}
serviceURL, _ := url.Parse(envWebexURL.String())
err := service.Initialize(serviceURL, testutils.TestLogger())
Expect(err).NotTo(HaveOccurred())
err = service.Send(
"this is an integration test",
nil,
)
Expect(err).NotTo(HaveOccurred())
})
})
Describe("the service", func() {
It("should implement Service interface", func() {
var impl types.Service = service
Expect(impl).ToNot(BeNil())
})
})
Describe("creating a config", func() {
When("given an url and a message", func() {
It("should return an error if no arguments where supplied", func() {
serviceURL, _ := url.Parse("webex://")
err := service.Initialize(serviceURL, nil)
Expect(err).To(HaveOccurred())
})
It("should not return an error if exactly two arguments are given", func() {
serviceURL, _ := url.Parse("webex://dummyToken@dummyRoom")
err := service.Initialize(serviceURL, nil)
Expect(err).NotTo(HaveOccurred())
})
It("should return an error if more than two arguments are given", func() {
serviceURL, _ := url.Parse("webex://dummyToken@dummyRoom/illegal-argument")
err := service.Initialize(serviceURL, nil)
Expect(err).To(HaveOccurred())
})
})
When("parsing the configuration URL", func() {
It("should be identical after de-/serialization", func() {
testURL := "webex://token@room"
url, err := url.Parse(testURL)
Expect(err).NotTo(HaveOccurred(), "parsing")
config := &Config{}
err = config.SetURL(url)
Expect(err).NotTo(HaveOccurred(), "verifying")
outputURL := config.GetURL()
Expect(outputURL.String()).To(Equal(testURL))
})
})
})
Describe("sending the payload", func() {
var dummyConfig = Config{
RoomID: "1",
BotToken: "dummyToken",
}
var service Service
BeforeEach(func() {
httpmock.Activate()
service = Service{}
if err := service.Initialize(dummyConfig.GetURL(), logger); err != nil {
panic(fmt.Errorf("service initialization failed: %w", err))
}
})
AfterEach(func() {
httpmock.DeactivateAndReset()
})
It("should not report an error if the server accepts the payload", func() {
setupResponder(&dummyConfig, 200, "")
Expect(service.Send("Message", nil)).To(Succeed())
})
It("should report an error if the server response is not OK", func() {
setupResponder(&dummyConfig, 400, "")
Expect(service.Initialize(dummyConfig.GetURL(), logger)).To(Succeed())
Expect(service.Send("Message", nil)).NotTo(Succeed())
})
It("should report an error if the message is empty", func() {
setupResponder(&dummyConfig, 400, "")
Expect(service.Initialize(dummyConfig.GetURL(), logger)).To(Succeed())
Expect(service.Send("", nil)).NotTo(Succeed())
})
})
Describe("doing request", func() {
dummyConfig := &Config{
BotToken: "dummyToken",
}
It("should add authorization header", func() {
request, err := BuildRequestFromPayloadAndConfig("", dummyConfig)
Expect(err).To(BeNil())
Expect(request.Header.Get("Authorization")).To(Equal("Bearer dummyToken"))
})
// webex API rejects messages which do not define Content-Type
It("should add content type header", func() {
request, err := BuildRequestFromPayloadAndConfig("", dummyConfig)
Expect(err).To(BeNil())
Expect(request.Header.Get("Content-Type")).To(Equal("application/json"))
})
})
})
func setupResponder(config *Config, code int, body string) {
httpmock.RegisterResponder("POST", MessagesEndpoint, httpmock.NewStringResponder(code, body))
}