forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_test.go
88 lines (77 loc) · 2.23 KB
/
card_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
package stripe
import (
"encoding/json"
"testing"
assert "github.com/stretchr/testify/require"
"github.com/stripe/stripe-go/v72/form"
)
func TestCardListParams_AppendTo(t *testing.T) {
// Adds `object` for account (this will hit the external accounts endpoint)
{
params := &CardListParams{Account: String("acct_123")}
body := &form.Values{}
form.AppendTo(body, params)
t.Logf("body = %+v", body)
assert.Equal(t, []string{"card"}, body.Get("object"))
}
// Adds `object` for customer (this will hit the sources endpoint)
{
params := &CardListParams{Customer: String("cus_123")}
body := &form.Values{}
form.AppendTo(body, params)
t.Logf("body = %+v", body)
assert.Equal(t, []string{"card"}, body.Get("object"))
}
}
func TestCard_UnmarshalJSON(t *testing.T) {
// Unmarshals from a JSON string
{
var v Card
err := json.Unmarshal([]byte(`"card_123"`), &v)
assert.NoError(t, err)
assert.Equal(t, "card_123", v.ID)
}
// Unmarshals from a JSON object
{
v := Card{ID: "card_123"}
data, err := json.Marshal(&v)
assert.NoError(t, err)
err = json.Unmarshal(data, &v)
assert.NoError(t, err)
assert.Equal(t, "card_123", v.ID)
}
}
func TestCardParams_AppendToAsCardSourceOrExternalAccount(t *testing.T) {
// We should add more tests for all the various corner cases here ...
// Includes number and object
{
params := &CardParams{Number: String("1234")}
body := &form.Values{}
params.AppendToAsCardSourceOrExternalAccount(body, nil)
t.Logf("body = %+v", body)
assert.Equal(t, []string{"1234"}, body.Get("source[number]"))
assert.Equal(t, []string{"card"}, body.Get("source[object]"))
}
// Includes Params
{
params := &CardParams{
Params: Params{
Metadata: map[string]string{
"foo": "bar",
},
},
}
body := &form.Values{}
params.AppendToAsCardSourceOrExternalAccount(body, nil)
t.Logf("body = %+v", body)
assert.Equal(t, []string{"bar"}, body.Get("metadata[foo]"))
}
// It takes key parts for deeper embedding
{
params := &CardParams{Number: String("1234")}
body := &form.Values{}
params.AppendToAsCardSourceOrExternalAccount(body, []string{"prefix1", "prefix2"})
t.Logf("body = %+v", body)
assert.Equal(t, []string{"1234"}, body.Get("prefix1[prefix2][source][number]"))
}
}