This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
applicationcharge_test.go
143 lines (121 loc) · 3.91 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
package goshopify
import (
"reflect"
"testing"
"time"
"github.com/shopspring/decimal"
"gopkg.in/jarcoal/httpmock.v1"
)
// 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", 1017262355, charge.ID},
{"Name", "Super Duper Expensive action", charge.Name},
{"APIClientID", 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",
"https://apple.myshopify.com/admin/charges/1017262355/confirm_application_charge?sign" +
"ature=BAhpBBMxojw%3D--1139a82a3433b1a6771786e03f02300440e11883",
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",
"https://fooshop.myshopify.com/admin/application_charges.json",
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(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",
"https://fooshop.myshopify.com/admin/application_charges/1.json",
httpmock.NewStringResponder(200, `{"application_charge": {"id":1}}`),
)
charge, err := client.ApplicationCharge.Get(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",
"https://fooshop.myshopify.com/admin/application_charges.json",
httpmock.NewStringResponder(200, `{"application_charges": [{"id":1},{"id":2}]}`),
)
charges, err := client.ApplicationCharge.List(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",
"https://fooshop.myshopify.com/admin/application_charges/455696195/activate.json",
httpmock.NewStringResponder(
200,
`{"application_charge":{"id":455696195,"status":"active"}}`,
),
)
charge := ApplicationCharge{
ID: 455696195,
Status: "accepted",
}
returnedCharge, err := client.ApplicationCharge.Activate(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)
}
}