-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks_test.go
110 lines (87 loc) · 2.22 KB
/
webhooks_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
package monzo
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestWebhooksRegister(t *testing.T) {
type expected struct {
webhook *WebhookSingle
err error
}
tests := []struct {
inputs map[string]interface{}
expected expected
}{
{
inputs: map[string]interface{}{"account_id": "", "url": ""},
expected: expected{nil, ErrWebhookInvalidAccountID},
},
{
inputs: map[string]interface{}{"account_id": "1", "url": ""},
expected: expected{nil, ErrWebhookInvalidURL},
},
{
inputs: map[string]interface{}{"account_id": "1", "url": "1"},
expected: expected{&WebhookSingle{
Webhook: Webhook{
AccountID: "1",
URL: "1",
ID: "abc",
},
}, nil},
},
}
for _, test := range tests {
c := MockRequest(test.expected.webhook, func(args mock.Arguments) {
req := args.Get(0).(*http.Request)
assert.Equal(t, "/webhooks", req.URL.Path)
params := map[string]interface{}{}
assert.NoError(t, json.NewDecoder(req.Body).Decode(¶ms))
assert.Equal(t, test.inputs, params)
})
test.expected.webhook.setClient(c)
webhook, err := c.Webhooks.Register(test.inputs["account_id"].(string), test.inputs["url"].(string))
assert.Equal(t, test.expected.webhook, webhook)
assert.Equal(t, test.expected.err, err)
}
}
func TestWebhooksList(t *testing.T) {
expected := &WebhookList{
Webhooks: []Webhook{
{
AccountID: "1",
ID: "1_1",
URL: "1_1_1",
},
{
AccountID: "1",
ID: "1_2",
URL: "1_2_1",
},
},
}
c := MockRequest(expected, func(args mock.Arguments) {
req := args.Get(0).(*http.Request)
assert.Equal(t, expected.Webhooks[0].AccountID, req.URL.Query().Get("account_id"))
})
expected.setClient(c)
actual, err := c.Webhooks.List(expected.Webhooks[0].AccountID)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func TestWebhookDelete(t *testing.T) {
w := Webhook{
ID: "a",
}
c := MockRequest(nil, func(args mock.Arguments) {
req := args.Get(0).(*http.Request)
assert.Equal(t, fmt.Sprintf("/webhooks/%s", w.ID), req.URL.Path)
})
w.client = c
err := w.Delete()
assert.NoError(t, err)
}