1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"net/http" |
6
|
|
|
"testing" |
7
|
|
|
|
8
|
|
|
"github.com/NdoleStudio/lemonsqueezy-go/internal/helpers" |
9
|
|
|
|
10
|
|
|
"github.com/stretchr/testify/assert" |
11
|
|
|
|
12
|
|
|
"github.com/NdoleStudio/lemonsqueezy-go/internal/stubs" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
func TestWebhooksService_Verify(t *testing.T) { |
16
|
|
|
// Arrange |
17
|
|
|
signingSecret := "signing-secret" |
18
|
|
|
client := New(WithSigningSecret(signingSecret)) |
19
|
|
|
|
20
|
|
|
// Act |
21
|
|
|
isValid := client.Webhooks.Verify(context.Background(), "743c0dcfaab3a3d06198183501fb92cc82e548febc80937f3fb236a4932f748f", stubs.WebhookRequestOrderCreated()) |
22
|
|
|
|
23
|
|
|
// Assert |
24
|
|
|
assert.True(t, isValid) |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
func TestWebhooksService_Create(t *testing.T) { |
28
|
|
|
// Setup |
29
|
|
|
t.Parallel() |
30
|
|
|
|
31
|
|
|
// Arrange |
32
|
|
|
server := helpers.MakeTestServer(http.StatusCreated, stubs.WebhookGetResponse()) |
33
|
|
|
client := New(WithBaseURL(server.URL)) |
34
|
|
|
|
35
|
|
|
// Act |
36
|
|
|
webhook, response, err := client.Webhooks.Create(context.Background(), 1, &WebhookCreateParams{ |
37
|
|
|
URL: "https://mysite.com/webhooks/", |
38
|
|
|
Events: []string{"order_created", "subscription_created"}, |
39
|
|
|
Secret: "SIGNING_SECRET", |
40
|
|
|
}) |
41
|
|
|
|
42
|
|
|
// Assert |
43
|
|
|
assert.Nil(t, err) |
44
|
|
|
|
45
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
46
|
|
|
assert.Equal(t, stubs.WebhookGetResponse(), *response.Body) |
47
|
|
|
assert.Equal(t, "1", webhook.Data.ID) |
48
|
|
|
|
49
|
|
|
// Teardown |
50
|
|
|
server.Close() |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
func TestWebhooksService_CreateWithError(t *testing.T) { |
54
|
|
|
// Setup |
55
|
|
|
t.Parallel() |
56
|
|
|
|
57
|
|
|
// Arrange |
58
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
59
|
|
|
client := New(WithBaseURL(server.URL)) |
60
|
|
|
|
61
|
|
|
// Act |
62
|
|
|
_, response, err := client.Webhooks.Create(context.Background(), 1, &WebhookCreateParams{ |
63
|
|
|
URL: "https://mysite.com/webhooks/", |
64
|
|
|
Events: []string{"order_created", "subscription_created"}, |
65
|
|
|
Secret: "SIGNING_SECRET", |
66
|
|
|
}) |
67
|
|
|
|
68
|
|
|
// Assert |
69
|
|
|
assert.NotNil(t, err) |
70
|
|
|
|
71
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
72
|
|
|
|
73
|
|
|
// Teardown |
74
|
|
|
server.Close() |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func TestWebhooksService_Get(t *testing.T) { |
78
|
|
|
// Setup |
79
|
|
|
t.Parallel() |
80
|
|
|
|
81
|
|
|
// Arrange |
82
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.WebhookGetResponse()) |
83
|
|
|
client := New(WithBaseURL(server.URL)) |
84
|
|
|
|
85
|
|
|
// Act |
86
|
|
|
webhook, response, err := client.Webhooks.Get(context.Background(), "1") |
87
|
|
|
|
88
|
|
|
// Assert |
89
|
|
|
assert.Nil(t, err) |
90
|
|
|
|
91
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
92
|
|
|
assert.Equal(t, stubs.WebhookGetResponse(), *response.Body) |
93
|
|
|
assert.Equal(t, "1", webhook.Data.ID) |
94
|
|
|
|
95
|
|
|
// Teardown |
96
|
|
|
server.Close() |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
func TestWebhooksService_GetWithError(t *testing.T) { |
100
|
|
|
// Setup |
101
|
|
|
t.Parallel() |
102
|
|
|
|
103
|
|
|
// Arrange |
104
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
105
|
|
|
client := New(WithBaseURL(server.URL)) |
106
|
|
|
|
107
|
|
|
// Act |
108
|
|
|
_, response, err := client.Webhooks.Get(context.Background(), "1") |
109
|
|
|
|
110
|
|
|
// Assert |
111
|
|
|
assert.NotNil(t, err) |
112
|
|
|
|
113
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
114
|
|
|
|
115
|
|
|
// Teardown |
116
|
|
|
server.Close() |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
func TestWebhooksService_Delete(t *testing.T) { |
120
|
|
|
// Setup |
121
|
|
|
t.Parallel() |
122
|
|
|
|
123
|
|
|
// Arrange |
124
|
|
|
server := helpers.MakeTestServer(http.StatusNoContent, nil) |
125
|
|
|
client := New(WithBaseURL(server.URL)) |
126
|
|
|
|
127
|
|
|
// Act |
128
|
|
|
response, err := client.Webhooks.Delete(context.Background(), "1") |
129
|
|
|
|
130
|
|
|
// Assert |
131
|
|
|
assert.Nil(t, err) |
132
|
|
|
|
133
|
|
|
assert.Equal(t, http.StatusNoContent, response.HTTPResponse.StatusCode) |
134
|
|
|
|
135
|
|
|
// Teardown |
136
|
|
|
server.Close() |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
func TestWebhooksService_DeleteWithError(t *testing.T) { |
140
|
|
|
// Setup |
141
|
|
|
t.Parallel() |
142
|
|
|
|
143
|
|
|
// Arrange |
144
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
145
|
|
|
client := New(WithBaseURL(server.URL)) |
146
|
|
|
|
147
|
|
|
// Act |
148
|
|
|
response, err := client.Webhooks.Delete(context.Background(), "1") |
149
|
|
|
|
150
|
|
|
// Assert |
151
|
|
|
assert.NotNil(t, err) |
152
|
|
|
|
153
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
154
|
|
|
|
155
|
|
|
// Teardown |
156
|
|
|
server.Close() |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
func TestWebhooksService_List(t *testing.T) { |
160
|
|
|
// Setup |
161
|
|
|
t.Parallel() |
162
|
|
|
|
163
|
|
|
// Arrange |
164
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.WebhooksListResponse()) |
165
|
|
|
client := New(WithBaseURL(server.URL)) |
166
|
|
|
|
167
|
|
|
// Act |
168
|
|
|
webhooks, response, err := client.Webhooks.List(context.Background()) |
169
|
|
|
|
170
|
|
|
// Assert |
171
|
|
|
assert.Nil(t, err) |
172
|
|
|
|
173
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
174
|
|
|
assert.Equal(t, stubs.WebhooksListResponse(), *response.Body) |
175
|
|
|
assert.Equal(t, 1, len(webhooks.Data)) |
176
|
|
|
|
177
|
|
|
// Teardown |
178
|
|
|
server.Close() |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
func TestWebhooksService_ListWithError(t *testing.T) { |
182
|
|
|
// Setup |
183
|
|
|
t.Parallel() |
184
|
|
|
|
185
|
|
|
// Arrange |
186
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
187
|
|
|
client := New(WithBaseURL(server.URL)) |
188
|
|
|
|
189
|
|
|
// Act |
190
|
|
|
_, response, err := client.Webhooks.List(context.Background()) |
191
|
|
|
|
192
|
|
|
// Assert |
193
|
|
|
assert.NotNil(t, err) |
194
|
|
|
|
195
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
196
|
|
|
|
197
|
|
|
// Teardown |
198
|
|
|
server.Close() |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
func TestWebhooksService_Update(t *testing.T) { |
202
|
|
|
// Setup |
203
|
|
|
t.Parallel() |
204
|
|
|
|
205
|
|
|
// Arrange |
206
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.WebhookGetResponse()) |
207
|
|
|
client := New(WithBaseURL(server.URL)) |
208
|
|
|
|
209
|
|
|
// Act |
210
|
|
|
webhook, response, err := client.Webhooks.Update(context.Background(), &WebhookUpdateParams{ |
211
|
|
|
ID: "1", |
212
|
|
|
Events: []string{"order_created", "subscription_created"}, |
213
|
|
|
Secret: "SIGNING_SECRET", |
214
|
|
|
}) |
215
|
|
|
|
216
|
|
|
// Assert |
217
|
|
|
assert.Nil(t, err) |
218
|
|
|
|
219
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
220
|
|
|
assert.Equal(t, stubs.WebhookGetResponse(), *response.Body) |
221
|
|
|
assert.Equal(t, "1", webhook.Data.ID) |
222
|
|
|
|
223
|
|
|
// Teardown |
224
|
|
|
server.Close() |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
func TestWebhooksService_UpdateWithError(t *testing.T) { |
228
|
|
|
// Setup |
229
|
|
|
t.Parallel() |
230
|
|
|
|
231
|
|
|
// Arrange |
232
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
233
|
|
|
client := New(WithBaseURL(server.URL)) |
234
|
|
|
|
235
|
|
|
// Act |
236
|
|
|
_, response, err := client.Webhooks.Update(context.Background(), &WebhookUpdateParams{}) |
237
|
|
|
|
238
|
|
|
// Assert |
239
|
|
|
assert.NotNil(t, err) |
240
|
|
|
|
241
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
242
|
|
|
|
243
|
|
|
// Teardown |
244
|
|
|
server.Close() |
245
|
|
|
} |
246
|
|
|
|