-
Notifications
You must be signed in to change notification settings - Fork 0
/
merchants_test.go
94 lines (83 loc) · 3.2 KB
/
merchants_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
package dusupay
import (
"context"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"io/ioutil"
"net/http"
"testing"
)
type MerchantsResourceTestSuite struct {
suite.Suite
cfg *Config
ctx context.Context
testable *MerchantsResource
}
func (suite *MerchantsResourceTestSuite) SetupTest() {
cfg := BuildStubConfig()
transport := BuildStubHttpTransport()
suite.cfg = cfg
suite.ctx = context.Background()
suite.testable = &MerchantsResource{NewResourceAbstract(transport, cfg)}
httpmock.Activate()
}
func (suite *MerchantsResourceTestSuite) TearDownTest() {
httpmock.DeactivateAndReset()
}
func (suite *MerchantsResourceTestSuite) TestGetBalancesSuccess() {
body, _ := LoadStubResponseData("stubs/merchants/balance/success.json")
httpmock.RegisterResponder(http.MethodGet, suite.cfg.Uri+"/v1/merchants/balance", httpmock.NewBytesResponder(http.StatusOK, body))
result, resp, err := suite.testable.GetBalances(suite.ctx)
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), resp)
assert.NotEmpty(suite.T(), result)
//result
assert.True(suite.T(), result.IsSuccess())
assert.Equal(suite.T(), http.StatusOK, result.Code)
assert.Equal(suite.T(), "success", result.Status)
assert.Equal(suite.T(), "Request completed successfully.", result.Message)
assert.Equal(suite.T(), "UGX", (*result.Data)[0].Currency)
assert.Equal(suite.T(), 5475.816, (*result.Data)[0].Balance)
assert.Equal(suite.T(), "USD", (*result.Data)[1].Currency)
assert.Equal(suite.T(), float64(12), (*result.Data)[1].Balance)
//response
defer resp.Body.Close()
bodyRsp, _ := ioutil.ReadAll(resp.Body)
assert.Equal(suite.T(), body, bodyRsp)
}
func (suite *MerchantsResourceTestSuite) TestGetBalancesJsonError() {
body, _ := LoadStubResponseData("stubs/errors/401.json")
httpmock.RegisterResponder(http.MethodGet, suite.cfg.Uri+"/v1/merchants/balance", httpmock.NewBytesResponder(http.StatusOK, body))
result, resp, err := suite.testable.GetBalances(suite.ctx)
assert.Error(suite.T(), err)
assert.NotEmpty(suite.T(), resp)
assert.NotEmpty(suite.T(), result)
//result
assert.False(suite.T(), result.IsSuccess())
assert.Equal(suite.T(), http.StatusUnauthorized, result.Code)
assert.Equal(suite.T(), "error", result.Status)
assert.Equal(suite.T(), "Unauthorized API access. Unknown Merchant", result.Message)
assert.Empty(suite.T(), result.Data)
//response
defer resp.Body.Close()
bodyRsp, _ := ioutil.ReadAll(resp.Body)
assert.Equal(suite.T(), body, bodyRsp)
//error
assert.Equal(suite.T(), "Unauthorized API access. Unknown Merchant", err.Error())
}
func (suite *MerchantsResourceTestSuite) TestGetBalancesNonJsonError() {
body, _ := LoadStubResponseData("stubs/errors/500.html")
httpmock.RegisterResponder(http.MethodGet, suite.cfg.Uri+"/v1/merchants/balance", httpmock.NewBytesResponder(http.StatusOK, body))
result, resp, err := suite.testable.GetBalances(suite.ctx)
assert.Error(suite.T(), err)
assert.NotEmpty(suite.T(), resp)
assert.Empty(suite.T(), result)
//response
defer resp.Body.Close()
bodyRsp, _ := ioutil.ReadAll(resp.Body)
assert.Equal(suite.T(), body, bodyRsp)
}
func TestMerchantsResourceTestSuite(t *testing.T) {
suite.Run(t, new(MerchantsResourceTestSuite))
}