1
|
|
|
package client |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"net/http" |
6
|
|
|
"testing" |
7
|
|
|
"time" |
8
|
|
|
|
9
|
|
|
"github.com/NdoleStudio/lemonsqueezy-go/internal/helpers" |
10
|
|
|
"github.com/NdoleStudio/lemonsqueezy-go/internal/stubs" |
11
|
|
|
"github.com/stretchr/testify/assert" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
func TestSubscriptionsService_Get(t *testing.T) { |
15
|
|
|
// Setup |
16
|
|
|
t.Parallel() |
17
|
|
|
|
18
|
|
|
// Arrange |
19
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.SubscriptionGetResponse()) |
20
|
|
|
client := New(WithBaseURL(server.URL)) |
21
|
|
|
|
22
|
|
|
// Act |
23
|
|
|
subscription, response, err := client.Subscriptions.Get(context.Background(), "1") |
24
|
|
|
|
25
|
|
|
// Assert |
26
|
|
|
assert.Nil(t, err) |
27
|
|
|
|
28
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
29
|
|
|
assert.Equal(t, stubs.SubscriptionGetResponse(), *response.Body) |
30
|
|
|
|
31
|
|
|
assert.Equal(t, &ApiResponseSubscription{ |
32
|
|
|
Jsonapi: ApiResponseJSONAPI{ |
33
|
|
|
Version: "1.0", |
34
|
|
|
}, |
35
|
|
|
Links: ApiResponseLink{ |
36
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1", |
37
|
|
|
}, |
38
|
|
|
Data: ApiResponseData[Subscription, ApiResponseRelationshipsSubscription]{ |
39
|
|
|
Type: "subscriptions", |
40
|
|
|
ID: "1", |
41
|
|
|
Attributes: Subscription{ |
42
|
|
|
StoreID: 1, |
43
|
|
|
OrderID: 1, |
44
|
|
|
OrderItemID: 1, |
45
|
|
|
ProductID: 1, |
46
|
|
|
VariantID: 1, |
47
|
|
|
ProductName: "Example Product", |
48
|
|
|
VariantName: "Example Variant", |
49
|
|
|
UserName: "Darlene Daugherty", |
50
|
|
|
UserEmail: "[email protected]", |
51
|
|
|
Status: "active", |
52
|
|
|
StatusFormatted: "Active", |
53
|
|
|
Pause: nil, |
54
|
|
|
Cancelled: false, |
55
|
|
|
TrialEndsAt: nil, |
56
|
|
|
BillingAnchor: 12, |
57
|
|
|
Urls: SubscriptionURLs{ |
58
|
|
|
UpdatePaymentMethod: "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413", |
59
|
|
|
}, |
60
|
|
|
RenewsAt: time.Date(2022, time.November, 12, 0, 0, 0, 0, time.UTC), |
61
|
|
|
EndsAt: nil, |
62
|
|
|
CreatedAt: time.Date(2021, time.August, 11, 13, 47, 27, 0, time.UTC), |
63
|
|
|
UpdatedAt: time.Date(2021, time.August, 11, 13, 54, 19, 0, time.UTC), |
64
|
|
|
TestMode: false, |
65
|
|
|
}, |
66
|
|
|
Relationships: ApiResponseRelationshipsSubscription{ |
67
|
|
|
Store: ApiResponseLinks{ |
68
|
|
|
Links: ApiResponseLink{ |
69
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/store", |
70
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/store", |
71
|
|
|
}, |
72
|
|
|
}, |
73
|
|
|
Order: ApiResponseLinks{ |
74
|
|
|
Links: ApiResponseLink{ |
75
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/order", |
76
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/order", |
77
|
|
|
}, |
78
|
|
|
}, |
79
|
|
|
OrderItem: ApiResponseLinks{ |
80
|
|
|
Links: ApiResponseLink{ |
81
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/order-item", |
82
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/order-item", |
83
|
|
|
}, |
84
|
|
|
}, |
85
|
|
|
Product: ApiResponseLinks{ |
86
|
|
|
Links: ApiResponseLink{ |
87
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/product", |
88
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/product", |
89
|
|
|
}, |
90
|
|
|
}, |
91
|
|
|
Variant: ApiResponseLinks{ |
92
|
|
|
Links: ApiResponseLink{ |
93
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/variant", |
94
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/variant", |
95
|
|
|
}, |
96
|
|
|
}, |
97
|
|
|
}, |
98
|
|
|
Links: ApiResponseLink{ |
99
|
|
|
Related: "", |
100
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1", |
101
|
|
|
}, |
102
|
|
|
}, |
103
|
|
|
}, subscription) |
104
|
|
|
|
105
|
|
|
// Teardown |
106
|
|
|
server.Close() |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
func TestSubscriptionsService_GetWithError(t *testing.T) { |
110
|
|
|
// Setup |
111
|
|
|
t.Parallel() |
112
|
|
|
|
113
|
|
|
// Arrange |
114
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
115
|
|
|
client := New(WithBaseURL(server.URL)) |
116
|
|
|
|
117
|
|
|
// Act |
118
|
|
|
_, response, err := client.Subscriptions.Get(context.Background(), "1") |
119
|
|
|
|
120
|
|
|
// Assert |
121
|
|
|
assert.NotNil(t, err) |
122
|
|
|
|
123
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
124
|
|
|
|
125
|
|
|
// Teardown |
126
|
|
|
server.Close() |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
func TestSubscriptionsService_Cancel(t *testing.T) { |
130
|
|
|
// Setup |
131
|
|
|
t.Parallel() |
132
|
|
|
|
133
|
|
|
// Arrange |
134
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.SubscriptionCancelResponse()) |
135
|
|
|
client := New(WithBaseURL(server.URL)) |
136
|
|
|
|
137
|
|
|
// Act |
138
|
|
|
subscription, response, err := client.Subscriptions.Cancel(context.Background(), "1") |
139
|
|
|
|
140
|
|
|
// Assert |
141
|
|
|
assert.Nil(t, err) |
142
|
|
|
|
143
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
144
|
|
|
assert.Equal(t, stubs.SubscriptionCancelResponse(), *response.Body) |
145
|
|
|
|
146
|
|
|
assert.Equal(t, &ApiResponseSubscription{ |
147
|
|
|
Jsonapi: ApiResponseJSONAPI{ |
148
|
|
|
Version: "1.0", |
149
|
|
|
}, |
150
|
|
|
Links: ApiResponseLink{ |
151
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1", |
152
|
|
|
}, |
153
|
|
|
Data: ApiResponseData[Subscription, ApiResponseRelationshipsSubscription]{ |
154
|
|
|
Type: "subscriptions", |
155
|
|
|
ID: "1", |
156
|
|
|
Attributes: Subscription{ |
157
|
|
|
StoreID: 1, |
158
|
|
|
OrderID: 1, |
159
|
|
|
OrderItemID: 1, |
160
|
|
|
ProductID: 1, |
161
|
|
|
VariantID: 1, |
162
|
|
|
ProductName: "Example Product", |
163
|
|
|
VariantName: "Example Variant", |
164
|
|
|
UserName: "Darlene Daugherty", |
165
|
|
|
UserEmail: "[email protected]", |
166
|
|
|
Status: "cancelled", |
167
|
|
|
StatusFormatted: "Cancelled", |
168
|
|
|
Pause: nil, |
169
|
|
|
Cancelled: true, |
170
|
|
|
TrialEndsAt: nil, |
171
|
|
|
BillingAnchor: 12, |
172
|
|
|
Urls: SubscriptionURLs{ |
173
|
|
|
UpdatePaymentMethod: "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413", |
174
|
|
|
}, |
175
|
|
|
RenewsAt: time.Date(2022, time.November, 12, 0, 0, 0, 0, time.UTC), |
176
|
|
|
EndsAt: (func() *time.Time { |
177
|
|
|
val := time.Date(2022, time.November, 12, 0, 0, 0, 0, time.UTC) |
178
|
|
|
return &val |
179
|
|
|
})(), |
180
|
|
|
CreatedAt: time.Date(2021, time.August, 11, 13, 47, 27, 0, time.UTC), |
181
|
|
|
UpdatedAt: time.Date(2021, time.August, 11, 13, 54, 19, 0, time.UTC), |
182
|
|
|
TestMode: false, |
183
|
|
|
}, |
184
|
|
|
Relationships: ApiResponseRelationshipsSubscription{ |
185
|
|
|
Store: ApiResponseLinks{ |
186
|
|
|
Links: ApiResponseLink{ |
187
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/store", |
188
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/store", |
189
|
|
|
}, |
190
|
|
|
}, |
191
|
|
|
Order: ApiResponseLinks{ |
192
|
|
|
Links: ApiResponseLink{ |
193
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/order", |
194
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/order", |
195
|
|
|
}, |
196
|
|
|
}, |
197
|
|
|
OrderItem: ApiResponseLinks{ |
198
|
|
|
Links: ApiResponseLink{ |
199
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/order-item", |
200
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/order-item", |
201
|
|
|
}, |
202
|
|
|
}, |
203
|
|
|
Product: ApiResponseLinks{ |
204
|
|
|
Links: ApiResponseLink{ |
205
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/product", |
206
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/product", |
207
|
|
|
}, |
208
|
|
|
}, |
209
|
|
|
Variant: ApiResponseLinks{ |
210
|
|
|
Links: ApiResponseLink{ |
211
|
|
|
Related: "https://api.lemonsqueezy.com/v1/subscriptions/1/variant", |
212
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1/relationships/variant", |
213
|
|
|
}, |
214
|
|
|
}, |
215
|
|
|
}, |
216
|
|
|
Links: ApiResponseLink{ |
217
|
|
|
Related: "", |
218
|
|
|
Self: "https://api.lemonsqueezy.com/v1/subscriptions/1", |
219
|
|
|
}, |
220
|
|
|
}, |
221
|
|
|
}, subscription) |
222
|
|
|
|
223
|
|
|
// Teardown |
224
|
|
|
server.Close() |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
func TestSubscriptionsService_CancelWithError(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.Subscriptions.Cancel(context.Background(), "1") |
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
|
|
|
|
247
|
|
|
func TestSubscriptionsService_Update(t *testing.T) { |
248
|
|
|
// Setup |
249
|
|
|
t.Parallel() |
250
|
|
|
|
251
|
|
|
// Arrange |
252
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.SubscriptionUpdateResponse()) |
253
|
|
|
client := New(WithBaseURL(server.URL)) |
254
|
|
|
|
255
|
|
|
// Act |
256
|
|
|
subscription, response, err := client.Subscriptions.Update(context.Background(), &SubscriptionUpdateParams{ |
257
|
|
|
Type: "subscriptions", |
258
|
|
|
ID: "1", |
259
|
|
|
Attributes: SubscriptionUpdateParamsAttributes{ |
260
|
|
|
ProductID: 9, |
261
|
|
|
VariantID: 11, |
262
|
|
|
BillingAnchor: 29, |
263
|
|
|
}, |
264
|
|
|
}) |
265
|
|
|
|
266
|
|
|
// Assert |
267
|
|
|
assert.Nil(t, err) |
268
|
|
|
|
269
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
270
|
|
|
assert.Equal(t, stubs.SubscriptionUpdateResponse(), *response.Body) |
271
|
|
|
assert.Equal(t, "1", subscription.ID) |
272
|
|
|
|
273
|
|
|
// Teardown |
274
|
|
|
server.Close() |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
func TestSubscriptionsService_UpdateWithError(t *testing.T) { |
278
|
|
|
// Setup |
279
|
|
|
t.Parallel() |
280
|
|
|
|
281
|
|
|
// Arrange |
282
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
283
|
|
|
client := New(WithBaseURL(server.URL)) |
284
|
|
|
|
285
|
|
|
// Act |
286
|
|
|
_, response, err := client.Subscriptions.Update(context.Background(), &SubscriptionUpdateParams{}) |
287
|
|
|
|
288
|
|
|
// Assert |
289
|
|
|
assert.NotNil(t, err) |
290
|
|
|
|
291
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
292
|
|
|
|
293
|
|
|
// Teardown |
294
|
|
|
server.Close() |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
func TestSubscriptionsService_List(t *testing.T) { |
298
|
|
|
// Setup |
299
|
|
|
t.Parallel() |
300
|
|
|
|
301
|
|
|
// Arrange |
302
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.SubscriptionsListResponse()) |
303
|
|
|
client := New(WithBaseURL(server.URL)) |
304
|
|
|
|
305
|
|
|
// Act |
306
|
|
|
subscriptions, response, err := client.Subscriptions.List(context.Background()) |
307
|
|
|
|
308
|
|
|
// Assert |
309
|
|
|
assert.Nil(t, err) |
310
|
|
|
|
311
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
312
|
|
|
assert.Equal(t, stubs.SubscriptionsListResponse(), *response.Body) |
313
|
|
|
assert.Equal(t, 2, len(subscriptions.Data)) |
314
|
|
|
assert.Equal(t, "2", subscriptions.Data[1].ID) |
315
|
|
|
|
316
|
|
|
// Teardown |
317
|
|
|
server.Close() |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
func TestSubscriptionsService_ListWithError(t *testing.T) { |
321
|
|
|
// Setup |
322
|
|
|
t.Parallel() |
323
|
|
|
|
324
|
|
|
// Arrange |
325
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
326
|
|
|
client := New(WithBaseURL(server.URL)) |
327
|
|
|
|
328
|
|
|
// Act |
329
|
|
|
_, response, err := client.Subscriptions.List(context.Background()) |
330
|
|
|
|
331
|
|
|
// Assert |
332
|
|
|
assert.NotNil(t, err) |
333
|
|
|
|
334
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
335
|
|
|
|
336
|
|
|
// Teardown |
337
|
|
|
server.Close() |
338
|
|
|
} |
339
|
|
|
|