1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"net/http" |
6
|
|
|
"testing" |
7
|
|
|
"time" |
8
|
|
|
|
9
|
|
|
"github.com/stretchr/testify/assert" |
10
|
|
|
|
11
|
|
|
"github.com/NdoleStudio/lemonsqueezy-go/internal/helpers" |
12
|
|
|
"github.com/NdoleStudio/lemonsqueezy-go/internal/stubs" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
func TestCheckoutService_Create(t *testing.T) { |
16
|
|
|
// Setup |
17
|
|
|
t.Parallel() |
18
|
|
|
|
19
|
|
|
// Arrange |
20
|
|
|
server := helpers.MakeTestServer(http.StatusCreated, stubs.CheckoutGetResponse()) |
21
|
|
|
client := New(WithBaseURL(server.URL)) |
22
|
|
|
|
23
|
|
|
// Act |
24
|
|
|
expireAt := time.Now().UTC().Format(time.RFC3339) |
25
|
|
|
customPrice := 5000 |
26
|
|
|
checkout, response, err := client.Checkouts.Create(context.Background(), 1, 1, &CheckoutCreateAttributes{ |
27
|
|
|
CustomPrice: &customPrice, |
28
|
|
|
ProductOptions: CheckoutCreateProductOptions{ |
29
|
|
|
EnabledVariants: []int{1}, |
30
|
|
|
}, |
31
|
|
|
CheckoutOptions: CheckoutCreateOptions{ |
32
|
|
|
ButtonColor: "2DD272", |
33
|
|
|
}, |
34
|
|
|
CheckoutData: CheckoutCreateData{ |
35
|
|
|
Custom: map[string]any{"user_id": "123"}, |
36
|
|
|
}, |
37
|
|
|
ExpiresAt: &expireAt, |
38
|
|
|
}) |
39
|
|
|
|
40
|
|
|
// Assert |
41
|
|
|
assert.Nil(t, err) |
42
|
|
|
|
43
|
|
|
assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode) |
44
|
|
|
assert.Equal(t, stubs.CheckoutGetResponse(), *response.Body) |
45
|
|
|
assert.Equal(t, "5e8b546c-c561-4a2c-a586-40c18bb2a195", checkout.Data.ID) |
46
|
|
|
|
47
|
|
|
// Teardown |
48
|
|
|
server.Close() |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func TestCheckoutService_CreateWithError(t *testing.T) { |
52
|
|
|
// Setup |
53
|
|
|
t.Parallel() |
54
|
|
|
|
55
|
|
|
// Arrange |
56
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
57
|
|
|
client := New(WithBaseURL(server.URL)) |
58
|
|
|
|
59
|
|
|
// Act |
60
|
|
|
_, response, err := client.Checkouts.Create(context.Background(), 1, 1, &CheckoutCreateAttributes{}) |
61
|
|
|
|
62
|
|
|
// Assert |
63
|
|
|
assert.NotNil(t, err) |
64
|
|
|
|
65
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
66
|
|
|
|
67
|
|
|
// Teardown |
68
|
|
|
server.Close() |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func TestCheckoutService_Get(t *testing.T) { |
72
|
|
|
// Setup |
73
|
|
|
t.Parallel() |
74
|
|
|
|
75
|
|
|
// Arrange |
76
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.CheckoutGetResponse()) |
77
|
|
|
client := New(WithBaseURL(server.URL)) |
78
|
|
|
|
79
|
|
|
// Act |
80
|
|
|
checkout, response, err := client.Checkouts.Get(context.Background(), "1") |
81
|
|
|
|
82
|
|
|
// Assert |
83
|
|
|
assert.Nil(t, err) |
84
|
|
|
|
85
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
86
|
|
|
assert.Equal(t, stubs.CheckoutGetResponse(), *response.Body) |
87
|
|
|
assert.Equal(t, "5e8b546c-c561-4a2c-a586-40c18bb2a195", checkout.Data.ID) |
88
|
|
|
|
89
|
|
|
// Teardown |
90
|
|
|
server.Close() |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
func TestCheckoutService_GetWithError(t *testing.T) { |
94
|
|
|
// Setup |
95
|
|
|
t.Parallel() |
96
|
|
|
|
97
|
|
|
// Arrange |
98
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
99
|
|
|
client := New(WithBaseURL(server.URL)) |
100
|
|
|
|
101
|
|
|
// Act |
102
|
|
|
_, response, err := client.Checkouts.Get(context.Background(), "1") |
103
|
|
|
|
104
|
|
|
// Assert |
105
|
|
|
assert.NotNil(t, err) |
106
|
|
|
|
107
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
108
|
|
|
|
109
|
|
|
// Teardown |
110
|
|
|
server.Close() |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
func TestCheckoutService_List(t *testing.T) { |
114
|
|
|
// Setup |
115
|
|
|
t.Parallel() |
116
|
|
|
|
117
|
|
|
// Arrange |
118
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.CheckoutListResponse()) |
119
|
|
|
client := New(WithBaseURL(server.URL)) |
120
|
|
|
|
121
|
|
|
// Act |
122
|
|
|
checkouts, response, err := client.Checkouts.List(context.Background()) |
123
|
|
|
|
124
|
|
|
// Assert |
125
|
|
|
assert.Nil(t, err) |
126
|
|
|
|
127
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
128
|
|
|
assert.Equal(t, stubs.CheckoutListResponse(), *response.Body) |
129
|
|
|
assert.Equal(t, 1, len(checkouts.Data)) |
130
|
|
|
|
131
|
|
|
// Teardown |
132
|
|
|
server.Close() |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
func TestCheckoutService_ListWithError(t *testing.T) { |
136
|
|
|
// Setup |
137
|
|
|
t.Parallel() |
138
|
|
|
|
139
|
|
|
// Arrange |
140
|
|
|
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) |
141
|
|
|
client := New(WithBaseURL(server.URL)) |
142
|
|
|
|
143
|
|
|
// Act |
144
|
|
|
_, response, err := client.Checkouts.List(context.Background()) |
145
|
|
|
|
146
|
|
|
// Assert |
147
|
|
|
assert.NotNil(t, err) |
148
|
|
|
|
149
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
150
|
|
|
|
151
|
|
|
// Teardown |
152
|
|
|
server.Close() |
153
|
|
|
} |
154
|
|
|
|