forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 12
/
client_token_integration_test.go
139 lines (125 loc) · 3.62 KB
/
client_token_integration_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
// +build integration
package braintree
import (
"context"
"testing"
"github.com/BoltApp/braintree-go/testhelpers"
)
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestClientToken(t *testing.T) {
t.Parallel()
ctx := context.Background()
g := testGateway.ClientToken()
token, err := g.Generate(ctx)
if err != nil {
t.Fatalf("failed to generate client token: %s", err)
}
if len(token) == 0 {
t.Fatalf("empty client token!")
}
}
func TestClientTokenWithCustomer(t *testing.T) {
t.Parallel()
ctx := context.Background()
customerRequest := &CustomerRequest{FirstName: "Lionel"}
customer, err := testGateway.Customer().Create(ctx, customerRequest)
if err != nil {
t.Error(err)
}
customerId := customer.Id
token, err := testGateway.ClientToken().GenerateWithCustomer(ctx, customerId)
if err != nil {
t.Error(err)
} else if len(token) == 0 {
t.Fatalf("Received empty client token")
}
}
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestClientTokenGateway_GenerateWithRequest(t *testing.T) {
// Getting customer from the API
customer, err := testGateway.Customer().Create(context.Background(), &CustomerRequest{FirstName: "Lionel"})
if err != nil {
t.Error(err)
}
tests := []struct {
name string
req *ClientTokenRequest
wantErr bool
}{
{
name: "empty request struct",
req: nil,
},
{
name: "request with provided version",
req: &ClientTokenRequest{Version: 2},
},
{
name: "request with non existent customerID",
req: &ClientTokenRequest{CustomerID: "///////@@@@@@@"},
wantErr: true,
},
{
name: "request with customer id",
req: &ClientTokenRequest{CustomerID: customer.Id},
},
{
name: "request with merchant id",
req: &ClientTokenRequest{MerchantAccountID: testMerchantAccountId},
},
{
name: "request with customer id and merchant id",
req: &ClientTokenRequest{CustomerID: customer.Id, MerchantAccountID: testMerchantAccountId},
},
{
name: "request with customer id and merchant id and options verify card not set",
req: &ClientTokenRequest{
CustomerID: customer.Id,
MerchantAccountID: testMerchantAccountId,
Options: &ClientTokenRequestOptions{
FailOnDuplicatePaymentMethod: true,
MakeDefault: true,
},
},
},
{
name: "request with customer id and merchant id and options verify card true",
req: &ClientTokenRequest{
CustomerID: customer.Id,
MerchantAccountID: testMerchantAccountId,
Options: &ClientTokenRequestOptions{
FailOnDuplicatePaymentMethod: true,
MakeDefault: true,
VerifyCard: testhelpers.BoolPtr(true),
},
},
},
{
name: "request with customer id and merchant id and options verify card false",
req: &ClientTokenRequest{
CustomerID: customer.Id,
MerchantAccountID: testMerchantAccountId,
Options: &ClientTokenRequestOptions{
FailOnDuplicatePaymentMethod: true,
MakeDefault: true,
VerifyCard: testhelpers.BoolPtr(false),
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
g := testGateway.ClientToken()
token, err := g.GenerateWithRequest(context.TODO(), tt.req)
if (err != nil) != tt.wantErr {
t.Errorf("ClientTokenGateway.Generate() error = %v, wantErr = %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(token) == 0 {
t.Errorf("ClientTokenGateway.Generate() returned empty client token!")
}
})
}
}