-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_test.go
63 lines (47 loc) · 2.05 KB
/
resolve_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
package paystack_test
import (
"bytes"
"context"
_ "embed"
"io"
"net/http"
"testing"
"github.com/brokeyourbike/paystack-api-client-go"
"github.com/sirupsen/logrus"
logrustest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
//go:embed testdata/resolve-success.json
var resolveSuccess []byte
//go:embed testdata/resolve-fail-auth.json
var resolveFailAuth []byte
func TestResolveRequestErr(t *testing.T) {
mockHttpClient := paystack.NewMockHttpClient(t)
client := paystack.NewClient("token", paystack.WithHTTPClient(mockHttpClient))
_, err := client.ResolveBankAccount(nil, "123", "123456789") //lint:ignore SA1012 testing failure
require.Error(t, err)
require.Contains(t, err.Error(), "failed to create request")
}
func TestResolveSuccess(t *testing.T) {
mockHttpClient := paystack.NewMockHttpClient(t)
logger, hook := logrustest.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
client := paystack.NewClient("token", paystack.WithHTTPClient(mockHttpClient), paystack.WithBaseURL("https://a.com"), paystack.WithLogger(logger))
resp := &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(resolveSuccess))}
mockHttpClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return req.Host == "a.com" })).Return(resp, nil).Once()
got, err := client.ResolveBankAccount(context.TODO(), "123", "123456789")
require.NoError(t, err)
assert.True(t, got.Status)
assert.Equal(t, 2, len(hook.Entries))
}
func TestResolveFailedAuth(t *testing.T) {
mockHttpClient := paystack.NewMockHttpClient(t)
client := paystack.NewClient("token", paystack.WithHTTPClient(mockHttpClient))
resp := &http.Response{StatusCode: 401, Body: io.NopCloser(bytes.NewReader(resolveFailAuth))}
mockHttpClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return req.Host == "api.paystack.co" })).Return(resp, nil).Once()
got, err := client.ResolveBankAccount(context.TODO(), "123", "123456789")
require.NoError(t, err)
assert.False(t, got.Status)
}