-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_square_catalog_item_test.go
268 lines (226 loc) · 6.59 KB
/
resource_square_catalog_item_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/Houndie/square-go"
"github.com/Houndie/square-go/catalog"
"github.com/Houndie/square-go/objects"
"github.com/Houndie/square-go/options"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func providerBlock(token string) string {
return fmt.Sprintf(`
provider "square" {
access_token = "%s"
environment = "sandbox"
}
`, token)
}
const catalogItemBlock = `resource "square_catalog_item" "test_item" {
name = "my-item"
variation {
name = "variation1"
pricing_type = "FIXED_PRICING"
amount = 500
}
variation {
name = "variation2"
pricing_type = "VARIABLE_PRICING"
}
}
`
func TestCatalogItem(t *testing.T) {
t.Parallel()
token := os.Getenv("TEST_TOKEN")
if token == "" {
t.Log("Test skipped as TEST_TOKEN not set")
t.Skip()
}
resource.Test(t, resource.TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
"square": func() (*schema.Provider, error) { return Provider(), nil }, //nolint:unparam
},
Steps: []resource.TestStep{
{
Config: providerBlock(token) + catalogItemBlock,
Check: resource.ComposeTestCheckFunc(
checkCatalogItem("square_catalog_item.test_item", &objects.CatalogObject{
Type: &objects.CatalogItem{
Name: "my-item",
Variations: []*objects.CatalogObject{
{
Type: &objects.CatalogItemVariation{
Name: "variation1",
PricingType: objects.CatalogPricingTypeFixed,
PriceMoney: &objects.Money{
Amount: 500,
},
},
},
{
Type: &objects.CatalogItemVariation{
Name: "variation2",
PricingType: objects.CatalogPricingTypeVariable,
},
},
},
},
}),
checkCatalogItemRemote("square_catalog_item.test_item", token),
),
},
{
Config: providerBlock(token),
Check: resource.ComposeTestCheckFunc(
checkCatalogItemDoesntExist("square_catalog_item.test_item"),
checkCatalogItemDoesntExistRemote("my-item", token),
),
},
},
})
}
func checkCatalogItem(resourceName string, expected *objects.CatalogObject) resource.TestCheckFunc {
return func(s *terraform.State) error {
// retrieve the resource by name from state
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
if rs.Primary.ID == "" {
return fmt.Errorf("Widget ID is not set")
}
return compareCatalogItemToResource(rs.Primary, expected)
}
}
func compareCatalogItemToResource(d *terraform.InstanceState, s *objects.CatalogObject) error {
if s.ID != "" && d.ID != s.ID {
return fmt.Errorf("unexpected id")
}
if strings.HasPrefix(d.ID, "#") {
return fmt.Errorf("no id assigned from server")
}
item, ok := s.Type.(*objects.CatalogItem)
if !ok {
return fmt.Errorf("provided catalog object is not an item")
}
if d.Attributes["name"] != item.Name {
return fmt.Errorf("unexpected name")
}
for _, variation := range item.Variations {
v, ok := variation.Type.(*objects.CatalogItemVariation)
if !ok {
return fmt.Errorf("provided catalog object is not a variation")
}
i := 0
found := false
for ; i < len(item.Variations); i++ {
if d.Attributes[fmt.Sprintf("variation.%d.name", i)] == v.Name {
found = true
break
}
}
if !found {
return fmt.Errorf("unable to find terraform variation matching square variation name")
}
vID := d.Attributes[fmt.Sprintf("variation.%d.id", i)]
if strings.HasPrefix(vID, "#") {
return fmt.Errorf("no variation id assigned from the server")
}
if vID == d.ID {
return fmt.Errorf("variation id matches item id")
}
if variation.ID != "" && vID != variation.ID {
return fmt.Errorf("unexpected variation id")
}
switch d.Attributes[fmt.Sprintf("variation.%d.pricing_type", i)] {
case "FIXED_PRICING":
if v.PricingType != objects.CatalogPricingTypeFixed {
return fmt.Errorf("unexpected pricing type")
}
case "VARIABLE_PRICING":
if v.PricingType != objects.CatalogPricingTypeVariable {
return fmt.Errorf("unexpected pricing type")
}
default:
return fmt.Errorf("unexpected pricing type")
}
if v.PricingType == objects.CatalogPricingTypeFixed {
amount, err := strconv.Atoi(d.Attributes[fmt.Sprintf("variation.%d.amount", i)])
if err != nil {
return fmt.Errorf("terraform amount is not a string")
}
if amount != v.PriceMoney.Amount {
return fmt.Errorf("unexpected amount")
}
}
}
return nil
}
func checkCatalogItemDoesntExist(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// retrieve the resource by name from state
_, ok := s.RootModule().Resources[resourceName]
if ok {
return fmt.Errorf("Found: %s", resourceName)
}
return nil
}
}
//nolint:dupl
func checkCatalogItemDoesntExistRemote(itemName, apiKey string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client, err := square.NewClient(apiKey, objects.Sandbox, options.WithHTTPClient(&http.Client{
Timeout: 10 * time.Second,
}))
if err != nil {
return fmt.Errorf("error creating square client: %w", err)
}
res, err := client.Catalog.List(context.Background(), &catalog.ListRequest{
Types: []objects.CatalogObjectEnumType{objects.CatalogObjectEnumTypeItem},
})
if err != nil {
return fmt.Errorf("error listing all catalog items: %w", err)
}
for res.Objects.Next() {
v := res.Objects.Value()
item, ok := v.Object.Type.(*objects.CatalogItem)
if !ok {
return fmt.Errorf("object is not a catalog item")
}
if item.Name == itemName {
return fmt.Errorf("found catalog item that should be deleted")
}
}
return nil
}
}
func checkCatalogItemRemote(resourceName, apiKey string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// retrieve the resource by name from state
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
client, err := square.NewClient(apiKey, objects.Sandbox, options.WithHTTPClient(&http.Client{
Timeout: 10 * time.Second,
}))
if err != nil {
return fmt.Errorf("error creating square client: %w", err)
}
res, err := client.Catalog.RetrieveObject(context.Background(), &catalog.RetrieveObjectRequest{
ObjectID: rs.Primary.ID,
})
if err != nil {
return fmt.Errorf("error retrieving remote object: %w", err)
}
return compareCatalogItemToResource(rs.Primary, res.Object)
}
}