-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbotmetrics_test.go
185 lines (157 loc) · 5.25 KB
/
botmetrics_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
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
package botmetrics
import (
"net/http"
"os"
"testing"
. "github.com/botmetrics/go-botmetrics/Godeps/_workspace/src/github.com/onsi/ginkgo"
. "github.com/botmetrics/go-botmetrics/Godeps/_workspace/src/github.com/onsi/gomega"
"github.com/botmetrics/go-botmetrics/Godeps/_workspace/src/github.com/onsi/gomega/ghttp"
)
func TestContext(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "botmetrics")
}
var _ = Describe("botmetrics", func() {
Describe("NewBotmetricsClient", func() {
Context("with apiKey and botId", func() {
It("should return a client and no error", func() {
c, err := NewBotmetricsClient("api_key", "bot_id")
Expect(c.ApiKey).To(Equal("api_key"))
Expect(c.BotId).To(Equal("bot_id"))
Expect(err).To(BeNil())
})
})
Context("without apiKey and botId", func() {
Context("without environment variables set", func() {
It("should return an error", func() {
c, err := NewBotmetricsClient()
Expect(c).To(BeNil())
Expect(err).To(Equal(ErrClientNotConfigured))
})
})
Context("with environment variables set", func() {
BeforeEach(func() {
os.Setenv("BOTMETRICS_API_KEY", "api_key")
os.Setenv("BOTMETRICS_BOT_ID", "bot_id")
})
AfterEach(func() {
os.Unsetenv("BOTMETRICS_API_KEY")
os.Unsetenv("BOTMETRICS_BOT_ID")
})
It("should return a client and no error", func() {
c, err := NewBotmetricsClient()
Expect(c.ApiKey).To(Equal("api_key"))
Expect(c.BotId).To(Equal("bot_id"))
Expect(err).To(BeNil())
})
})
})
})
Describe("RegisterBot", func() {
var server *ghttp.Server
var bc *BotmetricsClient
var err error
BeforeEach(func() {
server = ghttp.NewServer()
os.Setenv("BOTMETRICS_API_HOST", server.URL())
bc, err = NewBotmetricsClient("api-key", "bot-id")
Expect(err).To(BeNil())
})
AfterEach(func() {
server.Close()
os.Unsetenv("BOTMETRICS_API_HOST")
})
verifyRequestResponse := func(server *ghttp.Server, method, uri, token, createdAt string, responseCode int64) {
var verifier http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
Expect(r.Method).To(Equal(method))
Expect(r.RequestURI).To(Equal(uri))
err := r.ParseForm()
Expect(err).To(BeNil())
Expect(r.Header.Get("Authorization")).To(Equal("api-key"))
Expect(r.PostFormValue("instance[token]")).To(Equal(token))
Expect(r.PostFormValue("instance[created_at]")).To(Equal(createdAt))
Expect(r.PostFormValue("format")).To(Equal("json"))
w.WriteHeader(int(responseCode))
}
server.AppendHandlers(verifier)
}
Context("successful API request", func() {
Context("with token and no createdAt", func() {
It("should return true", func() {
verifyRequestResponse(server, "POST", "/bots/bot-id/instances", "bot-token", "", 201)
status := bc.RegisterBot("bot-token", 0)
Expect(status).To(BeTrue())
})
})
Context("with token and createdAt", func() {
It("should return true", func() {
verifyRequestResponse(server, "POST", "/bots/bot-id/instances", "bot-token", "123456789", 201)
status := bc.RegisterBot("bot-token", 123456789)
Expect(status).To(BeTrue())
})
})
})
Context("unsuccessful API request", func() {
It("should return true", func() {
verifyRequestResponse(server, "POST", "/bots/bot-id/instances", "bot-token", "", 500)
status := bc.RegisterBot("bot-token", 0)
Expect(status).To(BeFalse())
})
})
})
Describe("Track", func() {
var server *ghttp.Server
var bc *BotmetricsClient
var err error
BeforeEach(func() {
server = ghttp.NewServer()
os.Setenv("BOTMETRICS_API_HOST", server.URL())
bc, err = NewBotmetricsClient("api-key", "bot-id")
Expect(err).To(BeNil())
})
AfterEach(func() {
server.Close()
os.Unsetenv("BOTMETRICS_API_HOST")
})
verifyRequestResponse := func(server *ghttp.Server, method, uri, event string, responseCode int64) {
var verifier http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
Expect(r.Method).To(Equal(method))
Expect(r.RequestURI).To(Equal(uri))
err := r.ParseForm()
Expect(err).To(BeNil())
Expect(r.Header.Get("Authorization")).To(Equal("api-key"))
Expect(r.PostFormValue("event")).To(Equal(event))
Expect(r.PostFormValue("format")).To(Equal("json"))
w.WriteHeader(int(responseCode))
}
server.AppendHandlers(verifier)
}
Context("successful API request", func() {
Context("with event", func() {
It("should return true", func() {
event := "{\"name\":\"event-name\", \"timestamp\": \"123456789.0\"}"
verifyRequestResponse(server, "POST", "/bots/bot-id/events", event, 202)
status, err := bc.Track(event)
Expect(status).To(BeTrue())
Expect(err).To(BeNil())
})
})
Context("with invalid JSON", func() {
It("should return true", func() {
status, err := bc.Track("invalid json")
Expect(status).To(BeFalse())
Expect(err).ToNot(BeNil())
})
})
})
Context("unsuccessful API request", func() {
It("should return false", func() {
event := "{\"name\":\"event-name\", \"timestamp\": \"123456789.0\"}"
verifyRequestResponse(server, "POST", "/bots/bot-id/events", event, 500)
status, err := bc.Track(event)
Expect(status).To(BeFalse())
Expect(err).ToNot(BeNil())
})
})
})
})