-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_test.go
146 lines (112 loc) · 3.94 KB
/
webhook_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
package mobilepay
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"testing"
)
func TestWebhooks_List(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
testdata, err := ioutil.ReadFile("testdata/list_webhooks.json")
if err != nil {
t.Fatal(err)
}
gock.New(TestBaseUrl).
Get("/v1/webhooks").
Reply(200).
JSON(testdata)
client := New("test", "test", config)
ctx := context.TODO()
data, err := client.Webhook.Get(ctx)
assert.Nil(t, err)
assert.Equal(t, 2, len(data.Webhooks))
}
func TestWebhooks_Create(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
testdata, err := ioutil.ReadFile("testdata/create_webhook.json")
if err != nil {
t.Fatal(err)
}
events := []WebhookEvent{PaymentReserved.Name(), PaymentExpired.Name()}
eventsJson, err := json.Marshal(events)
if err != nil {
t.Fatal(err)
}
eventsData := fmt.Sprintf("%v", string(eventsJson))
testdata = bytes.Replace(testdata, []byte("EVENTS"), []byte(eventsData), 1)
testdata = bytes.Replace(testdata, []byte("WEBHOOK_URL"), []byte("https://my-api.com/webhooks"), 1)
gock.New(TestBaseUrl).
Post("/v1/webhooks").
Reply(200).
JSON(testdata)
client := New("test", "test", config)
params := &WebhookCreateParams{
Events: events,
Url: "https://my-api.com/webhooks",
}
ctx := context.TODO()
webhook, err := client.Webhook.Create(ctx, params)
assert.Nil(t, err)
assert.Equal(t, "https://my-api.com/webhooks", webhook.Url)
assert.Equal(t, events, webhook.Events)
}
func TestWebhooks_Get(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
testdata, err := ioutil.ReadFile("testdata/get_webhook.json")
if err != nil {
t.Fatal(err)
}
testdata = bytes.Replace(testdata, []byte("WEBHOOK_ID"), []byte("e4a2e195-74f6-42e1-a172-83291c9d2a41"), 1)
testdata = bytes.Replace(testdata, []byte("WEBHOOK_URL"), []byte("http://localhost/callback"), 1)
testdata = bytes.Replace(testdata, []byte("SIGNATURE_KEY"), []byte("secret"), 1)
gock.New(TestBaseUrl).
Get("/v1/webhooks/e4a2e195-74f6-42e1-a172-83291c9d2a41").
Reply(200).
JSON(testdata)
client := New("test", "test", config)
ctx := context.TODO()
webhook, err := client.Webhook.Find(ctx, "e4a2e195-74f6-42e1-a172-83291c9d2a41")
assert.Nil(t, err)
assert.Equal(t, "e4a2e195-74f6-42e1-a172-83291c9d2a41", webhook.WebhookId)
assert.Equal(t, "http://localhost/callback", webhook.Url)
assert.Equal(t, "secret", webhook.SignatureKey)
}
func TestWebhooks_Update(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
testdata, err := ioutil.ReadFile("testdata/get_webhook.json")
if err != nil {
t.Fatal(err)
}
testdata = bytes.Replace(testdata, []byte("WEBHOOK_ID"), []byte("e4a2e195-74f6-42e1-a172-83291c9d2a41"), 1)
testdata = bytes.Replace(testdata, []byte("WEBHOOK_URL"), []byte("http://localhost/webhook"), 1)
testdata = bytes.Replace(testdata, []byte("SIGNATURE_KEY"), []byte("secret"), 1)
gock.New(TestBaseUrl).
Put("/v1/webhooks/e4a2e195-74f6-42e1-a172-83291c9d2a41").
Reply(200).
JSON(testdata)
client := New("test", "test", config)
ctx := context.TODO()
params := &WebhookUpdateParams{
Events: []WebhookEvent{PaymentReserved.Name()},
Url: "http://localhost/webhook",
}
webhook, err := client.Webhook.Update(ctx, "e4a2e195-74f6-42e1-a172-83291c9d2a41", params)
assert.Nil(t, err)
assert.Equal(t, "e4a2e195-74f6-42e1-a172-83291c9d2a41", webhook.WebhookId)
assert.Equal(t, "http://localhost/webhook", webhook.Url)
assert.Equal(t, "secret", webhook.SignatureKey)
}
func TestWebhooks_Delete(t *testing.T) {
defer gock.Off() // Flush pending mocks after test execution
gock.New(TestBaseUrl).
Delete("/v1/webhooks/e4a2e195-74f6-42e1-a172-83291c9d2a41").
Reply(204)
client := New("test", "test", config)
ctx := context.TODO()
err := client.Webhook.Delete(ctx, "e4a2e195-74f6-42e1-a172-83291c9d2a41")
assert.Nil(t, err)
}