forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicationcharge_test.go
144 lines (122 loc) · 4.16 KB
/
applicationcharge_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
package goshopify
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/shopspring/decimal"
)
// applicationChargeTests tests if the fields are properly parsed.
func applicationChargeTests(t *testing.T, charge ApplicationCharge) {
var nilTest *bool
cases := []struct {
field string
expected interface{}
actual interface{}
}{
{"Id", uint64(1017262355), charge.Id},
{"Name", "Super Duper Expensive action", charge.Name},
{"APIClientId", uint64(755357713), charge.APIClientId},
{"Price", decimal.NewFromFloat(100.00).String(), charge.Price.String()},
{"Status", "pending", charge.Status},
{"ReturnURL", "http://super-duper.shopifyapps.com/", charge.ReturnURL},
{"Test", nilTest, charge.Test},
{"CreatedAt", "2018-07-05T13:11:28-04:00", charge.CreatedAt.Format(time.RFC3339)},
{"UpdatedAt", "2018-07-05T13:11:28-04:00", charge.UpdatedAt.Format(time.RFC3339)},
{
"DecoratedReturnURL",
"http://super-duper.shopifyapps.com/?charge_id=1017262355",
charge.DecoratedReturnURL,
},
{
"ConfirmationURL",
fmt.Sprintf("https://apple.myshopify.com/%s/charges/1017262355/confirm_application_charge?signature=BAhpBBMxojw%%3D--1139a82a3433b1a6771786e03f02300440e11883", client.pathPrefix),
charge.ConfirmationURL,
},
}
for _, c := range cases {
if c.expected != c.actual {
t.Errorf("ApplicationCharge.%s returned %v, expected %v", c.field, c.actual, c.expected)
}
}
}
func TestApplicationChargeServiceOp_Create(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
fmt.Sprintf("https://fooshop.myshopify.com/%s/application_charges.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("applicationcharge.json")),
)
p := decimal.NewFromFloat(100.00)
charge := ApplicationCharge{
Name: "Super Duper Expensive action",
Price: &p,
ReturnURL: "http://super-duper.shopifyapps.com",
}
returnedCharge, err := client.ApplicationCharge.Create(context.Background(), charge)
if err != nil {
t.Errorf("ApplicationCharge.Create returned an error: %v", err)
}
applicationChargeTests(t, *returnedCharge)
}
func TestApplicationChargeServiceOp_Get(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/application_charges/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"application_charge": {"id":1}}`),
)
charge, err := client.ApplicationCharge.Get(context.Background(), 1, nil)
if err != nil {
t.Errorf("ApplicationCharge.Get returned an error: %v", err)
}
expected := &ApplicationCharge{Id: 1}
if !reflect.DeepEqual(charge, expected) {
t.Errorf("ApplicationCharge.Get returned %+v, expected %+v", charge, expected)
}
}
func TestApplicationChargeServiceOp_List(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/application_charges.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"application_charges": [{"id":1},{"id":2}]}`),
)
charges, err := client.ApplicationCharge.List(context.Background(), nil)
if err != nil {
t.Errorf("ApplicationCharge.List returned an error: %v", err)
}
expected := []ApplicationCharge{{Id: 1}, {Id: 2}}
if !reflect.DeepEqual(charges, expected) {
t.Errorf("ApplicationCharge.List returned %+v, expected %+v", charges, expected)
}
}
func TestApplicationChargeServiceOp_Activate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
fmt.Sprintf("https://fooshop.myshopify.com/%s/application_charges/455696195/activate.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"application_charge":{"id":455696195,"status":"active"}}`,
),
)
charge := ApplicationCharge{
Id: 455696195,
Status: "accepted",
}
returnedCharge, err := client.ApplicationCharge.Activate(context.Background(), charge)
if err != nil {
t.Errorf("ApplicationCharge.Activate returned an error: %v", err)
}
expected := &ApplicationCharge{Id: 455696195, Status: "active"}
if !reflect.DeepEqual(returnedCharge, expected) {
t.Errorf("ApplicationCharge.Activate returned %+v, expected %+v", charge, expected)
}
}