-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
91 lines (81 loc) · 2.26 KB
/
client_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
package paypal
import (
"context"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/adobaai/paypal/ptesting"
)
func NewTestClient() *Client {
return NewClient(
"https://api-m.sandbox.paypal.com",
os.Getenv("UT_PAYPAL_ID"),
os.Getenv("UT_PAYPAL_SECRET"),
)
}
func TestAuth(t *testing.T) {
ctx := context.Background()
c := NewTestClient()
token := ptesting.R(c.Auth(ctx)).NoError(t).V()
assert.NotZero(t, token.Scope)
assert.NotZero(t, token.AccessToken)
assert.Equal(t, token.TokenType, "Bearer")
assert.NotZero(t, token.AppID)
assert.NotZero(t, token.ExpiresIn)
assert.NotZero(t, token.Nonce)
}
func TestError(t *testing.T) {
ctx := context.Background()
t.Run("Auth", func(t *testing.T) {
c := NewClient("https://api-m.sandbox.paypal.com", "1234", "5678")
ptesting.R(c.Auth(ctx)).EqualError(t, "invalid_client: Client Authentication failed")
})
t.Run("CreateOrder", func(t *testing.T) {
c := NewTestClient()
var e *Error
order := &Order{
Intent: OICapture,
}
ptesting.R(c.CreateOrder(ctx, &CreateOrderReq{Order: order})).ErrorAs(t, &e)
assert.NotZero(t, e.DebugID)
e.DebugID = ""
assert.Equal(t, &Error{
StatusCode: http.StatusBadRequest,
Name: "INVALID_REQUEST",
Message: "Request is not well-formed, syntactically incorrect, or violates schema.",
Details: []*ErrorDetail{
{
Field: "/purchase_units",
Location: "body",
Issue: "MISSING_REQUIRED_PARAMETER",
Description: "A required field / parameter is missing.",
},
},
Links: []*Link{
{
HRef: "https://developer.paypal.com/docs/api/orders/v2/#error-MISSING_REQUIRED_PARAMETER",
Rel: "information_link",
Method: "",
},
},
}, e)
})
}
type Hello struct {
Name string
}
type HelloResp struct {
JSON *Hello
}
func TestJSON(t *testing.T) {
c := &http.Client{}
ctx := context.Background()
name := "Фёдор Миха́йлович Достое́вский"
url := "https://httpbin.org/anything"
hreq := ptesting.R(NewJSONRequest(ctx, http.MethodPost, url, Hello{Name: name})).NoError(t).V()
hres := ptesting.R(c.Do(hreq)).NoError(t).V()
ptesting.R(RespJSON[HelloResp](hres)).NoError(t).Do(func(t *testing.T, it *HelloResp) {
assert.Equal(t, name, it.JSON.Name)
})
}