forked from xendit/xendit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_test.go
141 lines (122 loc) · 3.1 KB
/
account_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
package account_test
import (
"context"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/xendit/xendit-go"
"github.com/xendit/xendit-go/account"
"github.com/xendit/xendit-go/utils/validator"
)
func initTesting(apiRequesterMockObj xendit.APIRequester) {
xendit.Opt.SecretKey = "examplesecretkey"
xendit.SetAPIRequester(apiRequesterMockObj)
}
type apiRequesterMock struct {
mock.Mock
}
func (m *apiRequesterMock) Call(ctx context.Context, method string, path string, secretKey string,
header http.Header, params interface{}, result interface{}) *xendit.Error {
m.Called(ctx, method, path, secretKey, header, params, result)
result.(*xendit.Account).ID = "123"
result.(*xendit.Account).Type = xendit.OWNED
result.(*xendit.Account).Email = "customer@customer.com"
return nil
}
func TestCreate(t *testing.T) {
apiRequesterMockObj := new(apiRequesterMock)
initTesting(apiRequesterMockObj)
testCases := []struct {
desc string
data *account.CreateParams
expectedRes *xendit.Account
expectedErr *xendit.Error
}{
{
desc: "should create an account",
data: &account.CreateParams{
Email: "customer@customer.com",
Type: xendit.OWNED,
},
expectedRes: &xendit.Account{
ID: "123",
Email: "customer@customer.com",
Type: xendit.OWNED,
},
expectedErr: nil,
},
{
desc: "should report missing required fields",
data: &account.CreateParams{
Type: xendit.OWNED,
},
expectedRes: nil,
expectedErr: validator.APIValidatorErr(errors.New("Missing required fields: 'Email'")),
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
apiRequesterMockObj.On(
"Call",
context.Background(),
"POST",
xendit.Opt.XenditURL+"/v2/accounts",
xendit.Opt.SecretKey,
http.Header{},
tC.data,
&xendit.Account{},
).Return(nil)
resp, err := account.Create(tC.data)
assert.Equal(t, tC.expectedRes, resp)
assert.Equal(t, tC.expectedErr, err)
})
}
}
func TestGet(t *testing.T) {
apiRequesterMockObj := new(apiRequesterMock)
initTesting(apiRequesterMockObj)
testCases := []struct {
desc string
data *account.GetParams
expectedRes *xendit.Account
expectedErr *xendit.Error
}{
{
desc: "should get an account",
data: &account.GetParams{
ID: "123",
},
expectedRes: &xendit.Account{
ID: "123",
Email: "customer@customer.com",
Type: xendit.OWNED,
},
expectedErr: nil,
},
{
desc: "should report missing required fields",
data: &account.GetParams{},
expectedRes: nil,
expectedErr: validator.APIValidatorErr(errors.New("Missing required fields: 'ID'")),
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
apiRequesterMockObj.On(
"Call",
context.Background(),
"GET",
xendit.Opt.XenditURL+"/v2/accounts/123",
xendit.Opt.SecretKey,
http.Header{},
nil,
&xendit.Account{},
).Return(nil)
resp, err := account.Get(tC.data)
assert.Equal(t, tC.expectedRes, resp)
assert.Equal(t, tC.expectedErr, err)
})
}
}