Skip to content

Commit db3da20

Browse files
committed
Add integration tests for Calls, CallFlows and Webhooks
1 parent dc243eb commit db3da20

File tree

6 files changed

+176
-25
lines changed

6 files changed

+176
-25
lines changed

voice/call.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ func (call *Call) UnmarshalJSON(data []byte) error {
107107
//
108108
// An error is returned if no such call flow exists or is accessible.
109109
func CallByID(client *messagebird.Client, id string) (*Call, error) {
110-
call := &Call{}
111-
err := client.Request(call, http.MethodGet, apiRoot+"/calls/"+id, nil)
112-
return call, err
110+
var resp struct {
111+
Data []Call `json:"data"`
112+
}
113+
if err := client.Request(&resp, http.MethodGet, apiRoot+"/calls/"+id, nil); err != nil {
114+
return nil, err
115+
}
116+
return &resp.Data[0], nil
113117
}
114118

115119
// Calls returns a Paginator which iterates over all Calls.

voice/call_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,39 @@ func TestInitiateCall(t *testing.T) {
3131
t.Fatal(err)
3232
}
3333
}
34+
35+
func TestCallByID(t *testing.T) {
36+
client, ok := testClient(t)
37+
if !ok {
38+
t.SkipNow()
39+
}
40+
41+
source, destination := "31000000000", "31000000000"
42+
callflow := CallFlow{
43+
Title: "Say test",
44+
Steps: []CallFlowStep{
45+
&CallFlowSayStep{
46+
Voice: "male",
47+
Payload: "You are about to experience a great adventure which reaches from the inner mind to the outer limits",
48+
Language: "en-US",
49+
},
50+
&CallFlowPauseStep{
51+
Length: time.Second,
52+
},
53+
&CallFlowHangupStep{},
54+
},
55+
}
56+
call, err := InitiateCall(client, source, destination, callflow, nil)
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
61+
time.Sleep(time.Second)
62+
fetchedCall, err := CallByID(client, call.ID)
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
if fetchedCall.Source != call.Source {
67+
t.Fatalf("mismatched source: exp %q, got %q", call.Source, fetchedCall.Source)
68+
}
69+
}

voice/callflow.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ func (callflow *CallFlow) UnmarshalJSON(data []byte) error {
105105
//
106106
// An error is returned if no such call flow exists or is accessible.
107107
func CallFlowByID(client *messagebird.Client, id string) (*CallFlow, error) {
108-
callflow := &CallFlow{}
109-
err := client.Request(callflow, http.MethodGet, apiRoot+"/call-flows/"+id, nil)
110-
return callflow, err
108+
var data struct {
109+
Data []CallFlow `json:"data"`
110+
}
111+
if err := client.Request(&data, http.MethodGet, apiRoot+"/call-flows/"+id, nil); err != nil {
112+
return nil, err
113+
}
114+
return &data.Data[0], nil
111115
}
112116

113117
// CallFlows returns a Paginator which iterates over all CallFlows.

voice/callflow_test.go

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package voice
22

33
import (
44
"encoding/json"
5-
"net/http"
65
"reflect"
76
"testing"
87
"time"
@@ -131,25 +130,86 @@ func TestCallFlowJSONUnmarshal(t *testing.T) {
131130
}
132131

133132
func TestCreateCallFlow(t *testing.T) {
134-
mbClient, stop := testRequest(http.StatusOK, []byte(`{
135-
"data": [
136-
{
137-
"id": "the-id",
138-
"title": "the-title",
139-
"createdAt": "2018-01-29T13:46:06Z",
140-
"updatedAt": "2018-01-30T16:00:34Z"
141-
}
142-
]
143-
}`))
144-
defer stop()
145-
newCf := &CallFlow{}
133+
mbClient, ok := testClient(t)
134+
if !ok {
135+
t.SkipNow()
136+
}
137+
138+
newCf := &CallFlow{
139+
Title: "the-title",
140+
Steps: []CallFlowStep{
141+
&CallFlowSayStep{
142+
Payload: "Hello",
143+
Language: "en-US",
144+
Voice: "male",
145+
},
146+
&CallFlowPauseStep{
147+
Length: time.Second,
148+
},
149+
},
150+
}
146151
if err := newCf.Create(mbClient); err != nil {
147152
t.Fatal(err)
148153
}
149-
if newCf.ID != "the-id" {
150-
t.Fatalf("Unexpected ID: %q", newCf.ID)
151-
}
152154
if newCf.Title != "the-title" {
153155
t.Fatalf("Unexpected Title: %q", newCf.Title)
154156
}
157+
if len(newCf.Steps) != 2 {
158+
t.Fatalf("Unexpected Title: %q", newCf.Title)
159+
}
160+
}
161+
162+
func TestCallFlowByID(t *testing.T) {
163+
mbClient, ok := testClient(t)
164+
if !ok {
165+
t.SkipNow()
166+
}
167+
168+
newCf := &CallFlow{
169+
Title: "the-title",
170+
Steps: []CallFlowStep{
171+
&CallFlowHangupStep{},
172+
},
173+
}
174+
if err := newCf.Create(mbClient); err != nil {
175+
t.Fatal(err)
176+
}
177+
178+
fetchedCf, err := CallFlowByID(mbClient, newCf.ID)
179+
if err != nil {
180+
t.Fatal(err)
181+
}
182+
if fetchedCf.ID != newCf.ID {
183+
t.Fatalf("mismatched fetched IDs: exp %q, got %q", newCf.ID, fetchedCf.ID)
184+
}
185+
}
186+
187+
func TestCallFlowList(t *testing.T) {
188+
mbClient, ok := testClient(t)
189+
if !ok {
190+
t.SkipNow()
191+
}
192+
193+
for _, c := range []string{"foo", "bar", "baz"} {
194+
newCf := &CallFlow{
195+
Title: c,
196+
Steps: []CallFlowStep{
197+
&CallFlowHangupStep{},
198+
},
199+
}
200+
if err := newCf.Create(mbClient); err != nil {
201+
t.Fatal(err)
202+
}
203+
}
204+
205+
i := 0
206+
for cf := range CallFlows(mbClient).Stream() {
207+
if err, ok := cf.(error); ok {
208+
t.Fatal(err)
209+
}
210+
i++
211+
}
212+
if i == 0 {
213+
t.Fatal("no callflows were fetched")
214+
}
155215
}

voice/webhook.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ func CreateWebHook(client *messagebird.Client, url, token string) (*Webhook, err
7979
URL: url,
8080
Token: token,
8181
}
82-
wh := &Webhook{}
83-
if err := client.Request(wh, http.MethodPost, apiRoot+"/webhooks/", data); err != nil {
82+
var resp struct {
83+
Data []Webhook `json:"data"`
84+
}
85+
if err := client.Request(&resp, http.MethodPost, apiRoot+"/webhooks/", data); err != nil {
8486
return nil, err
8587
}
86-
return wh, nil
88+
return &resp.Data[0], nil
8789
}
8890

8991
// Update syncs hte local state of a webhook to the MessageBird API.

voice/webhook_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package voice
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCreateWebhook(t *testing.T) {
8+
mbClient, ok := testClient(t)
9+
if !ok {
10+
t.SkipNow()
11+
}
12+
13+
const url = "https://example.com/voice-webhook"
14+
newWh, err := CreateWebHook(mbClient, url, "token")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
if newWh.URL != url {
19+
t.Fatalf("Unexpected URL: %q", newWh.URL)
20+
}
21+
}
22+
23+
func TestWebhookList(t *testing.T) {
24+
mbClient, ok := testClient(t)
25+
if !ok {
26+
t.SkipNow()
27+
}
28+
29+
for _, c := range []string{"foo", "bar", "baz"} {
30+
if _, err := CreateWebHook(mbClient, "https://example/com/"+c, "token"); err != nil {
31+
t.Fatal(err)
32+
}
33+
}
34+
35+
i := 0
36+
for cf := range Webhooks(mbClient).Stream() {
37+
if err, ok := cf.(error); ok {
38+
t.Fatal(err)
39+
}
40+
i++
41+
}
42+
if i == 0 {
43+
t.Fatal("no webhooks were fetched")
44+
}
45+
}

0 commit comments

Comments
 (0)