-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
72 lines (56 loc) · 1.73 KB
/
handler_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/andygrunwald/go-jira"
"github.com/stretchr/testify/assert"
chat "google.golang.org/api/chat/v1"
)
func TestServer(t *testing.T) {
BotName = "@ABCBOT"
t.Run("Registered Routes", func(t *testing.T) {
mjs := MockJiraService{}
mcs := MockChatService{}
routeNames := []string{
"data",
"healthCheck",
}
for _, name := range routeNames {
route := getRouter(mjs, mcs).Get(name)
assert.NotNilf(t, route, "No router found with name %q", name)
}
})
t.Run("Data receive route", func(t *testing.T) {
called1 := 0
called2 := 0
mjs := MockJiraService{&called1}
mcs := MockChatService{&called2}
server := httptest.NewServer(http.HandlerFunc(getDataHandler(mjs, mcs)))
payload := fmt.Sprintf(`{"type": "MESSAGE", "message": { "text": "%s xxx-1234" }}`, BotName)
_, err := http.Post(server.URL, "application/json", bytes.NewBufferString(payload))
assert.NoError(t, err, "Error posting JSON")
assert.Equal(t, 1, *mjs.called, "Jira not called")
assert.Equal(t, 1, *mcs.called, "Chat not called")
})
t.Run("Health Check Route", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(healthCheck))
resp, err := http.Get(server.URL)
assert.NoError(t, err, "Error getting healthcheck route")
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, `{}`, string(body), "Bad message returned")
})
}
type MockJiraService struct{ called *int }
func (mjs MockJiraService) GetTicketData(string) (*jira.Issue, error) {
*mjs.called++
return nil, nil
}
type MockChatService struct{ called *int }
func (mcs MockChatService) CreateIssueCard(*jira.Issue) (*chat.Message, error) {
*mcs.called++
return nil, nil
}