-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
91 lines (79 loc) · 2.46 KB
/
main_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 main
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/noah-blockchain/explorer-gate/api"
"github.com/noah-blockchain/explorer-gate/core"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/pubsub"
)
var (
router *gin.Engine
gateService *core.NoahGate
testTx = `f8820d018a4d4e540000000000000001a9e88a4d4e5400000000000000941b685a7c1e78726c48f619c497a07ed75fe00483872386f26fc10000808001b845f8431ca05ddcd3ffd2d5b21ffe4686cadbb462bad9facdd7ee0c2db31a7b6da6f06468b3a044df8fc8b4c4190ef352e0f70112527b6b25c4a22a67c9e9365ac7e511ac12f3`
)
type RespData struct {
Commission *string `json:"commission"`
}
type RespError struct {
Code int `json:"code"`
Log string `json:"log"`
Value *int `json:"value"`
Coin *string `json:"coin"`
}
type Resp struct {
Data *RespData `json:"data"`
Error *RespError `json:"error"`
}
func init() {
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetOutput(os.Stdout)
logger.SetReportCaller(true)
contextLogger := logger.WithFields(logrus.Fields{
"version": "0.1.0",
"app": "Noah Gate Test",
})
pubsubServer := pubsub.NewServer()
err := pubsubServer.Start()
if err != nil {
panic(err)
}
gateService = core.New(pubsubServer, contextLogger)
router = api.SetupRouter(gateService, pubsubServer)
}
func TestPushWrongTransaction(t *testing.T) {
var target Resp
w := httptest.NewRecorder()
payload := []byte(`{"transaction":"` + testTx + `"}`)
req, err := http.NewRequest("POST", "/api/v1/transaction/push", bytes.NewBuffer(payload))
assert.NoError(t, err)
router.ServeHTTP(w, req)
err = json.NewDecoder(w.Body).Decode(&target)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.IsType(t, target.Error.Code, int(1))
assert.IsType(t, target.Error.Log, "")
}
func TestEstimateTx(t *testing.T) {
var target Resp
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/estimate/tx-commission?transaction="+testTx, nil)
router.ServeHTTP(w, req)
err := json.NewDecoder(w.Body).Decode(&target)
assert.Equal(t, http.StatusOK, w.Code)
assert.NoError(t, err)
assert.NotNil(t, target.Data)
assert.NotNil(t, target.Data.Commission)
if target.Data.Commission != nil && *target.Data.Commission == "" {
assert.NoError(t, errors.New("empty commission value"))
}
}