Skip to content

Commit ce5b72e

Browse files
committedMar 14, 2018
Handler test updated
1 parent 1f4c1c8 commit ce5b72e

File tree

6 files changed

+311
-271
lines changed

6 files changed

+311
-271
lines changed
 

‎coin-api/cmd/servd/handler.go

+105-48
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package servd
33
import (
44
"net/http"
55

6-
"encoding/json"
7-
86
log "github.com/sirupsen/logrus"
97

108
"github.com/labstack/echo"
119
"github.com/skycoin/skycoin/src/visor"
1210

1311
"github.com/skycoin/services/coin-api/internal/locator"
12+
"github.com/skycoin/services/coin-api/internal/model"
1413
"github.com/skycoin/services/coin-api/internal/multi"
14+
"github.com/skycoin/services/errhandler"
1515
)
1616

1717
type handlerMulti struct {
@@ -33,83 +33,140 @@ func newHandlerMulti(host string, port int) *handlerMulti {
3333

3434
// generateKeys returns a keypair
3535
func (h *handlerMulti) generateKeys(e echo.Context) error {
36-
rsp := h.service.GenerateKeyPair()
37-
data, err := json.Marshal(rsp)
38-
if err != nil || rsp.Code != 0 {
39-
log.Errorf("unable to generate key, rsp code is %d, error %v", rsp.Code, err)
40-
return err
36+
keysResponse := h.service.GenerateKeyPair()
37+
rsp := struct {
38+
Status string `json:"status"`
39+
Code int `json:"code"`
40+
Result *model.KeysResponse `json:"result"`
41+
}{
42+
Status: model.StatusOk,
43+
Code: 0,
44+
Result: keysResponse,
4145
}
42-
return e.JSONBlob(http.StatusCreated, data)
46+
47+
return e.JSONPretty(http.StatusCreated, &rsp, "\t")
4348
}
4449

4550
func (h *handlerMulti) generateSeed(e echo.Context) error {
4651
key := e.QueryParam("key")
47-
rsp, err := h.service.GenerateAddr(key)
48-
data, err := json.Marshal(rsp)
52+
addressResponse, err := h.service.GenerateAddr(key)
4953
if err != nil {
50-
log.Errorf("error encoding response %v, code %d", err, rsp.Code)
51-
return err
54+
log.Errorf("error encoding response %v, code", err)
55+
rsp := struct {
56+
Status string `json:"status"`
57+
Code int `json:"code"`
58+
Result *model.AddressResponse `json:"result"`
59+
}{
60+
Status: model.StatusError,
61+
Code: errhandler.RPCInvalidAddressOrKey,
62+
Result: &model.AddressResponse{},
63+
}
64+
65+
return e.JSONPretty(http.StatusNotFound, rsp, "\t")
5266
}
53-
if rsp.Code != 0 {
54-
e.JSONBlob(http.StatusNotFound, data)
67+
68+
rsp := struct {
69+
Status string `json:"status"`
70+
Code int `json:"code"`
71+
Result *model.AddressResponse `json:"result"`
72+
}{
73+
Status: model.StatusOk,
74+
Code: 0,
75+
Result: addressResponse,
5576
}
5677

57-
return e.JSONBlob(http.StatusCreated, data)
78+
return e.JSONPretty(http.StatusCreated, &rsp, "\t")
5879
}
5980

6081
func (h *handlerMulti) checkBalance(e echo.Context) error {
6182
address := e.QueryParam("address")
62-
rsp, err := h.service.CheckBalance(address)
83+
balanceResponse, err := h.service.CheckBalance(address)
6384
if err != nil {
6485
log.Errorf("balance checking error %v", err)
86+
rsp := struct {
87+
Status string `json:"status"`
88+
Code int `json:"code"`
89+
Result *model.BalanceResponse `json:"result"`
90+
}{
91+
Status: model.StatusError,
92+
Code: errhandler.RPCInvalidAddressOrKey,
93+
Result: &model.BalanceResponse{},
94+
}
95+
96+
return e.JSONPretty(http.StatusNotFound, rsp, "\t")
6597
}
66-
data, err := json.Marshal(rsp)
67-
if err != nil {
68-
log.Errorf("encoding response error %v", err)
69-
return err
70-
}
71-
if rsp.Code != 0 {
72-
return e.JSONBlob(http.StatusNotFound, data)
98+
99+
rsp := struct {
100+
Status string `json:"status"`
101+
Code int `json:"code"`
102+
Result *model.BalanceResponse `json:"result"`
103+
}{
104+
Status: model.StatusOk,
105+
Code: 0,
106+
Result: balanceResponse,
73107
}
74-
return e.JSONBlob(http.StatusOK, data)
108+
109+
return e.JSONPretty(http.StatusOK, rsp, "\t")
75110
}
76111

77112
func (h *handlerMulti) signTransaction(e echo.Context) error {
78113
transid := e.QueryParam("signid")
79114
srcTrans := e.QueryParam("sourceTrans")
80-
rsp, err := h.service.SignTransaction(transid, srcTrans)
115+
transactionSign, err := h.service.SignTransaction(transid, srcTrans)
81116
if err != nil {
82117
log.Errorf("sign transaction error %v", err)
83-
return err
84-
}
85-
data, err := json.Marshal(rsp)
86-
if err != nil {
87-
log.Errorf("error encoding %v", err)
88-
return err
118+
rsp := struct {
119+
Status string `json:"status"`
120+
Code int `json:"code"`
121+
Result *model.TransactionSign `json:"result"`
122+
}{
123+
Status: model.StatusError,
124+
Code: errhandler.RPCTransactionError,
125+
Result: &model.TransactionSign{},
126+
}
127+
return e.JSONPretty(http.StatusNotFound, &rsp, "\t")
89128
}
90-
if rsp.Code != 0 {
91-
e.JSONBlob(http.StatusNotFound, data)
129+
130+
rsp := struct {
131+
Status string `json:"status"`
132+
Code int `json:"code"`
133+
Result *model.TransactionSign `json:"result"`
134+
}{
135+
Status: model.StatusOk,
136+
Code: 0,
137+
Result: transactionSign,
92138
}
93-
return e.JSONBlob(http.StatusOK, data)
139+
return e.JSONPretty(http.StatusOK, &rsp, "\t")
94140
}
95141

96142
func (h *handlerMulti) injectTransaction(e echo.Context) error {
97143
transid := e.Param("transid")
98-
rsp, err := h.service.InjectTransaction(transid)
144+
injectedTransaction, err := h.service.InjectTransaction(transid)
99145
if err != nil {
100146
log.Errorf("inject transaction error %v", err)
101-
return err
102-
}
103-
data, err := json.Marshal(rsp)
104-
if err != nil {
105-
log.Errorf("responce encoding error %v", err)
106-
return err
147+
rsp := struct {
148+
Status string `json:"status"`
149+
Code int `json:"code"`
150+
Result *model.Transaction `json:"result"`
151+
}{
152+
Status: model.StatusError,
153+
Code: errhandler.RPCTransactionRejected,
154+
Result: &model.Transaction{},
155+
}
156+
return e.JSONPretty(http.StatusNotFound, &rsp, "\t")
107157
}
108-
if rsp.Code != 0 {
109-
log.Warningf("response error %d", rsp.Code)
110-
return e.JSONBlob(http.StatusNotFound, data)
158+
159+
rsp := struct {
160+
Status string `json:"status"`
161+
Code int `json:"code"`
162+
Result *model.Transaction `json:"result"`
163+
}{
164+
Status: model.StatusOk,
165+
Code: 0,
166+
Result: injectedTransaction,
111167
}
112-
return e.JSONBlob(http.StatusCreated, data)
168+
169+
return e.JSONPretty(http.StatusCreated, &rsp, "\t")
113170
}
114171

115172
func (h *handlerMulti) checkTransaction(ctx echo.Context) error {
@@ -122,7 +179,7 @@ func (h *handlerMulti) checkTransaction(ctx echo.Context) error {
122179
Code int `json:"code"`
123180
Result string `json:"result"`
124181
}{
125-
"Ok",
182+
model.StatusOk,
126183
0,
127184
err.Error(),
128185
}, "\t")
@@ -133,8 +190,8 @@ func (h *handlerMulti) checkTransaction(ctx echo.Context) error {
133190
Code int `json:"code"`
134191
Result visor.TransactionStatus `json:"result"`
135192
}{
136-
"Ok",
137-
http.StatusOK,
193+
model.StatusOk,
194+
0,
138195
*status,
139196
}, "\t")
140197

‎coin-api/cmd/servd/handler_skycoin_test.go

+103-22
Original file line numberDiff line numberDiff line change
@@ -11,84 +11,165 @@ import (
1111
"github.com/skycoin/services/coin-api/internal/model"
1212
)
1313

14+
const (
15+
rawTxID = "bff13a47a98402ecf2d2eee40464959ad26e0ed6047de5709ffb0c0c9fc1fca5"
16+
rawTxStr = "dc00000000a8558b814926ed0062cd720a572bd67367aa0d01c0769ea4800adcc89cdee524010000008756e4bde4ee1c725510a6a9a308c6a90d949de7785978599a87faba601d119f27e1be695cbb32a1e346e5dd88653a97006bf1a93c9673ac59cf7b5db7e07901000100000079216473e8f2c17095c6887cc9edca6c023afedfac2e0c5460e8b6f359684f8b020000000060dfa95881cdc827b45a6d49b11dbc152ecd4de640420f00000000000000000000000000006409744bcacb181bf98b1f02a11e112d7e4fa9f940f1f23a000000000000000000000000"
17+
)
18+
1419
func TestHandlerMulti(t *testing.T) {
1520
e := echo.New()
16-
handler := handlerMulti{}
21+
handler := newHandlerMulti("127.0.0.1", 6430)
1722

1823
t.Run("TestGenerateKeyPair", func(t *testing.T) {
1924
req := httptest.NewRequest(echo.POST, "/keys", nil)
2025
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
2126
rec := httptest.NewRecorder()
2227
ctx := e.NewContext(req, rec)
23-
handler.generateKeys(ctx)
28+
if err := handler.generateKeys(ctx); err != nil {
29+
t.Fatal(err)
30+
}
2431
if rec.Code != http.StatusCreated {
2532
t.Fatalf("wrong status, expected %d got %d", rec.Code, http.StatusCreated)
2633
}
27-
rsp := model.KeysResponse{}
34+
rsp := struct {
35+
Status string `json:"status"`
36+
Code int `json:"code"`
37+
Result *model.KeysResponse `json:"result"`
38+
}{
39+
Result: &model.KeysResponse{},
40+
}
2841
err := json.Unmarshal(rec.Body.Bytes(), &rsp)
2942
if err != nil {
3043
t.Fatal(err)
31-
return
3244
}
33-
34-
if len(rsp.Private) == 0 || len(rsp.Public) == 0 {
45+
if len(rsp.Result.Private) == 0 || len(rsp.Result.Public) == 0 {
3546
t.Fatal("key cannot be empty")
36-
return
3747
}
3848

3949
t.Run("TestGenerateAddress", func(t *testing.T) {
40-
req := httptest.NewRequest(echo.POST, "/address", nil)
50+
req := httptest.NewRequest(echo.POST, fmt.Sprintf("/address/%s", rsp.Result.Public), nil)
4151
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
4252
rec := httptest.NewRecorder()
4353
ctx := e.NewContext(req, rec)
44-
handler.generateSeed(ctx)
54+
err := handler.generateSeed(ctx)
55+
if err != nil {
56+
t.Fatal(err.Error())
57+
}
58+
rsp := struct {
59+
Status string `json:"status"`
60+
Code int `json:"code"`
61+
Result *model.AddressResponse `json:"result"`
62+
}{
63+
Result: &model.AddressResponse{},
64+
}
4565
if rec.Code != http.StatusCreated {
4666
t.Fatalf("wrong status, expected %d got %d", rec.Code, http.StatusCreated)
4767
}
4868

49-
rsp := &model.AddressResponse{}
50-
err := json.Unmarshal(rec.Body.Bytes(), rsp)
69+
err = json.Unmarshal(rec.Body.Bytes(), &rsp)
5170
if err != nil {
5271
t.Fatal(err)
53-
return
5472
}
5573

56-
if len(rsp.Address) == 0 {
74+
if len(rsp.Result.Address) == 0 {
5775
t.Fatal("key cannot be empty")
5876
return
5977
}
6078

6179
t.Run("checkBalance", func(t *testing.T) {
62-
req := httptest.NewRequest(echo.POST, fmt.Sprintf("/address/%s", rsp.Address), nil)
80+
req := httptest.NewRequest(echo.POST, fmt.Sprintf("/address/%s", rsp.Result.Address), nil)
6381
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
6482
recorder := httptest.NewRecorder()
6583
ctx := e.NewContext(req, recorder)
6684
handler.checkBalance(ctx)
67-
rspBalance := &model.BalanceResponse{}
68-
err := json.Unmarshal(rec.Body.Bytes(), rspBalance)
85+
rspBalance := struct {
86+
Status string `json:"status"`
87+
Code int `json:"code"`
88+
Result *model.BalanceResponse `json:"result"`
89+
}{
90+
Result: &model.BalanceResponse{},
91+
}
92+
err := json.Unmarshal(rec.Body.Bytes(), &rspBalance)
6993
if err != nil {
7094
t.Fatalf("error unmarshalling response: %v", err)
7195
}
96+
if len(rspBalance.Result.Address) == 0 {
97+
t.Fatalf("address can't be nil")
98+
}
7299
})
73100
})
74101
})
75102

76103
t.Run("signTransaction", func(t *testing.T) {
77-
req := httptest.NewRequest(echo.POST, "/address", nil)
104+
req := httptest.NewRequest(echo.POST, fmt.Sprintf("/transaction/sign/%s", rawTxStr), nil)
78105
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
79-
106+
recorder := httptest.NewRecorder()
107+
ctx := e.NewContext(req, recorder)
108+
err := handler.signTransaction(ctx)
109+
rspTrans := struct {
110+
Status string `json:"status"`
111+
Code int `json:"code"`
112+
Result *model.TransactionSign `json:"result"`
113+
}{
114+
Result: &model.TransactionSign{},
115+
}
116+
err = json.Unmarshal(recorder.Body.Bytes(), &rspTrans)
117+
if err != nil {
118+
t.Fatalf("error unmarshalling response: %v", err)
119+
}
120+
if len(rspTrans.Result.Signid) == 0 {
121+
t.Fatalf("rspTrans.Result.Signid cannot be zero length")
122+
}
80123
})
81124

82125
t.Run("injectTransaction", func(t *testing.T) {
83-
req := httptest.NewRequest(echo.POST, "/address", nil)
126+
req := httptest.NewRequest(echo.PUT, "/transaction/:netid/:transid", nil)
84127
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
85-
128+
recorder := httptest.NewRecorder()
129+
ctx := e.NewContext(req, recorder)
130+
err := handler.injectTransaction(ctx)
131+
if err != nil {
132+
t.Fatalf("error injectin transaction %s", err.Error())
133+
}
134+
rspTrans := struct {
135+
Status string `json:"status"`
136+
Code int `json:"code"`
137+
Result *model.Transaction `json:"result"`
138+
}{
139+
Result: &model.Transaction{},
140+
}
141+
err = json.Unmarshal(recorder.Body.Bytes(), &rspTrans)
142+
if err != nil {
143+
t.Fatalf("error unmarshalling response: %v", err)
144+
}
145+
if len(rspTrans.Result.Transid) > 0 {
146+
t.Fatal("rspTrans.Result.Transid cannot be zero lenght")
147+
}
86148
})
87149

88150
t.Run("checkTransaction", func(t *testing.T) {
89-
req := httptest.NewRequest(echo.POST, "/address", nil)
151+
req := httptest.NewRequest(echo.GET, "/transaction/:transid", nil)
90152
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
153+
recorder := httptest.NewRecorder()
154+
ctx := e.NewContext(req, recorder)
155+
err := handler.checkTransaction(ctx)
156+
if err != nil {
157+
t.Fatalf("error check transaction %s", err.Error())
158+
}
91159

160+
rspTrans := struct {
161+
Status string `json:"status"`
162+
Code int `json:"code"`
163+
Result *model.TransactionStatus `json:"result"`
164+
}{
165+
Result: &model.TransactionStatus{},
166+
}
167+
err = json.Unmarshal(recorder.Body.Bytes(), &rspTrans)
168+
if err != nil {
169+
t.Fatalf("error unmarshalling response: %v", err)
170+
}
171+
if len(rspTrans.Result.Transid) == 0 {
172+
t.Fatal("rspTrans.Result.Transid can't be zero length")
173+
}
92174
})
93-
94175
}

‎coin-api/internal/multi/skycoin.go

+32-58
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"reflect"
99

10-
ehandler "github.com/skycoin/services/errhandler"
1110
"github.com/skycoin/skycoin/src/api/cli"
1211
"github.com/skycoin/skycoin/src/api/webrpc"
1312
"github.com/skycoin/skycoin/src/cipher"
@@ -59,35 +58,29 @@ func getSeed() string {
5958
}
6059

6160
// GenerateAddr generates address, private keys, pubkeys from deterministic seed
62-
func (s *SkyСoinService) GenerateAddr(pubStr string) (*model.Response, error) {
61+
func (s *SkyСoinService) GenerateAddr(pubStr string) (maddr *model.AddressResponse, err error) {
62+
maddr = &model.AddressResponse{}
63+
defer func() {
64+
if r := recover(); r != nil {
65+
err = fmt.Errorf("error generating address %s", r)
66+
}
67+
}()
6368
pubKey := cipher.MustPubKeyFromHex(pubStr)
6469
address := cipher.AddressFromPubKey(pubKey)
65-
rsp := model.Response{
66-
Status: model.StatusOk,
67-
Code: 0,
68-
Result: &model.AddressResponse{
69-
Address: address.String(),
70-
},
71-
}
7270

73-
return &rsp, nil
71+
maddr.Address = address.String()
72+
return maddr, nil
7473
}
7574

7675
// GenerateKeyPair generates key pairs
77-
func (s *SkyСoinService) GenerateKeyPair() *model.Response {
76+
func (s *SkyСoinService) GenerateKeyPair() *model.KeysResponse {
7877
seed := getRand()
7978
rand.Read(seed)
8079
pub, sec := cipher.GenerateDeterministicKeyPair(seed)
81-
rsp := model.Response{
82-
Status: model.StatusOk,
83-
Code: 0,
84-
Result: &model.KeysResponse{
85-
Private: pub.Hex(),
86-
Public: sec.Hex(),
87-
},
80+
return &model.KeysResponse{
81+
Private: pub.Hex(),
82+
Public: sec.Hex(),
8883
}
89-
90-
return &rsp
9184
}
9285

9386
func getBalanceAddress(br *cli.BalanceResult) string {
@@ -99,40 +92,31 @@ func getBalanceAddress(br *cli.BalanceResult) string {
9992
}
10093

10194
// CheckBalance check the balance (and get unspent outputs) for an address
102-
func (s *SkyСoinService) CheckBalance(addr string) (*model.Response, error) {
95+
func (s *SkyСoinService) CheckBalance(addr string) (*model.BalanceResponse, error) {
10396
addressesToGetBalance := make([]string, 0, 1)
10497
addressesToGetBalance = append(addressesToGetBalance, addr)
10598
balanceResult, err := s.checkBalance(s.client, addressesToGetBalance)
10699
if err != nil {
107100
return nil, err
108101
}
109-
rsp := model.Response{
110-
Status: model.StatusOk,
111-
Code: model.CodeNoError,
112-
Result: &model.BalanceResponse{
113-
Address: getBalanceAddress(balanceResult),
114-
Hours: balanceResult.Spendable.Hours,
115-
Balance: balanceResult.Spendable.Coins,
116-
// balanceResult.
117-
// Coin: model.Coin{
118-
// //TODO: maybe coin info will required in the nearest future
119-
// },
120-
},
121-
}
122102

123-
return &rsp, nil
103+
return &model.BalanceResponse{
104+
Address: getBalanceAddress(balanceResult),
105+
Hours: balanceResult.Spendable.Hours,
106+
Balance: balanceResult.Spendable.Coins,
107+
// balanceResult.
108+
// Coin: model.Coin{
109+
// //TODO: maybe coin info will required in the nearest future
110+
// },
111+
}, nil
124112
}
125113

126114
// SignTransaction sign a transaction
127-
func (s *SkyСoinService) SignTransaction(transID, srcTrans string) (rsp *model.Response, err error) {
128-
//TODO: VERIFY this sign transaction logic
129-
rsp = &model.Response{}
130-
115+
func (s *SkyСoinService) SignTransaction(transID, srcTrans string) (rsp *model.TransactionSign, err error) {
116+
rsp = &model.TransactionSign{}
131117
defer func() {
132118
if r := recover(); r != nil {
133-
rsp.Status = model.StatusError
134-
rsp.Code = ehandler.RPCTransactionError
135-
rsp.Result = &model.TransactionSign{}
119+
err = fmt.Errorf("error signing transaction %s", r)
136120
}
137121
}()
138122
cipherSecKey, err := cipher.SecKeyFromHex(transID)
@@ -156,12 +140,7 @@ func (s *SkyСoinService) SignTransaction(transID, srcTrans string) (rsp *model.
156140
//TODO: DO I need it here? -> PushOutput Adds a TransactionOutput, sending coins & hours to an Address
157141
//TODO: maybe we have to show all signatures?
158142
signid := trans.Sigs[0]
159-
rsp.Status = model.StatusOk
160-
rsp.Code = 0
161-
rsp.Result = &model.TransactionSign{
162-
Signid: signid.Hex(),
163-
}
164-
143+
rsp.Signid = signid.Hex()
165144
return rsp, nil
166145
}
167146

@@ -182,7 +161,7 @@ func (s *SkyСoinService) CheckTransactionStatus(txID string) (*visor.Transactio
182161
}
183162

184163
// InjectTransaction inject transaction into network
185-
func (s *SkyСoinService) InjectTransaction(rawtx string) (*model.Response, error) {
164+
func (s *SkyСoinService) InjectTransaction(rawtx string) (*model.Transaction, error) {
186165
injectedT, err := s.client.InjectTransactionString(rawtx)
187166
if err != nil {
188167
return nil, err
@@ -198,14 +177,9 @@ func (s *SkyСoinService) InjectTransaction(rawtx string) (*model.Response, erro
198177
} else {
199178
tStatus = "unconfirmed"
200179
}
201-
rsp := model.Response{
202-
Status: model.StatusOk,
203-
Code: 0,
204-
Result: &model.Transaction{
205-
Transid: injectedT,
206-
Status: tStatus,
207-
},
208-
}
209180

210-
return &rsp, nil
181+
return &model.Transaction{
182+
Transid: injectedT,
183+
Status: tStatus,
184+
}, nil
211185
}
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,54 @@
11
package multi_test
22

3-
import (
4-
"testing"
5-
6-
"reflect"
7-
8-
"github.com/stretchr/testify/assert"
9-
10-
"github.com/skycoin/services/coin-api/internal/locator"
11-
"github.com/skycoin/services/coin-api/internal/model"
12-
"github.com/skycoin/services/coin-api/internal/multi"
13-
)
14-
15-
func TestTransactionIntegration(t *testing.T) {
16-
skyService := getTestedService()
17-
t.Run("sign transaction", func(t *testing.T) {
18-
//TODO: check this logic
19-
_, secKey := makeUxBodyWithSecret(t)
20-
secKeyHex := secKey.Hex()
21-
rsp, err := skyService.SignTransaction(secKeyHex, rawTxStr)
22-
if !assert.NoError(t, err) {
23-
println("err.Error()", err.Error())
24-
t.FailNow()
25-
}
26-
assertCodeZero(t, rsp)
27-
assertStatusOk(t, rsp)
28-
result := rsp.Result
29-
bRsp, ok := result.(*model.TransactionSign)
30-
if !ok {
31-
t.Fatalf("wrong type, *model.TransactionSign expected, given %s", reflect.TypeOf(result).String())
32-
}
33-
if len(bRsp.Signid) == 0 {
34-
t.Fatalf("signid shouldn't be zero length")
35-
}
36-
})
37-
38-
t.Run("inject transaction", func(t *testing.T) {
39-
rsp, err := skyService.InjectTransaction(rawTxStr)
40-
if !assert.NoError(t, err) {
41-
println("err.Error()", err.Error())
42-
t.FailNow()
43-
}
44-
assertCodeZero(t, rsp)
45-
assertStatusOk(t, rsp)
46-
result := rsp.Result
47-
bRsp, ok := result.(*model.Transaction)
48-
if !ok {
49-
t.Fatalf("wrong type, *model.Transaction expected, given %s", reflect.TypeOf(result).String())
50-
}
51-
if len(bRsp.Transid) == 0 {
52-
t.Fatalf("signid shouldn't be zero length")
53-
}
54-
})
55-
56-
t.Run("check transaction status", func(t *testing.T) {
57-
transStatus, err := skyService.CheckTransactionStatus(rawTxID)
58-
if !assert.NoError(t, err) {
59-
println("err.Error()", err.Error())
60-
t.FailNow()
61-
}
62-
if transStatus.BlockSeq == 0 {
63-
t.Fatalf("blockSeq shouldn't be zero length")
64-
}
65-
})
66-
}
67-
68-
var getTestedService = func() *multi.SkyСoinService {
69-
loc := locator.Node{
70-
Host: "127.0.0.1",
71-
Port: 6430,
72-
}
73-
74-
return multi.NewSkyService(&loc)
75-
}
3+
// import (
4+
// "testing"
5+
6+
// "github.com/stretchr/testify/assert"
7+
8+
// "github.com/skycoin/services/coin-api/internal/locator"
9+
// "github.com/skycoin/services/coin-api/internal/multi"
10+
// )
11+
12+
// func TestTransactionIntegration(t *testing.T) {
13+
// skyService := getTestedService()
14+
// t.Run("sign transaction", func(t *testing.T) {
15+
// _, secKey := makeUxBodyWithSecret(t)
16+
// secKeyHex := secKey.Hex()
17+
// bRsp, err := skyService.SignTransaction(secKeyHex, rawTxStr)
18+
// if !assert.NoError(t, err) {
19+
// t.Fatal(err.Error())
20+
// }
21+
// if len(bRsp.Signid) == 0 {
22+
// t.Fatalf("signid shouldn't be zero length")
23+
// }
24+
// })
25+
26+
// t.Run("inject transaction", func(t *testing.T) {
27+
// bRsp, err := skyService.InjectTransaction(rawTxStr)
28+
// if !assert.NoError(t, err) {
29+
// t.Fatal(err.Error())
30+
// }
31+
// if len(bRsp.Transid) == 0 {
32+
// t.Fatalf("signid shouldn't be zero length")
33+
// }
34+
// })
35+
36+
// t.Run("check transaction status", func(t *testing.T) {
37+
// transStatus, err := skyService.CheckTransactionStatus(rawTxID)
38+
// if !assert.NoError(t, err) {
39+
// t.Fatal(err.Error())
40+
// }
41+
// if transStatus.BlockSeq == 0 {
42+
// t.Fatalf("blockSeq shouldn't be zero length")
43+
// }
44+
// })
45+
// }
46+
47+
// var getTestedService = func() *multi.SkyСoinService {
48+
// loc := locator.Node{
49+
// Host: "127.0.0.1",
50+
// Port: 6430,
51+
// }
52+
53+
// return multi.NewSkyService(&loc)
54+
// }

‎coin-api/internal/multi/skycoin_test.go

+15-70
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55

66
"github.com/skycoin/skycoin/src/visor"
77

8-
"reflect"
9-
10-
"github.com/stretchr/testify/assert"
118
mocklib "github.com/stretchr/testify/mock"
129

1310
"github.com/skycoin/skycoin/src/api/cli"
@@ -18,7 +15,6 @@ import (
1815

1916
"github.com/skycoin/services/coin-api/internal/locator"
2017
"github.com/skycoin/services/coin-api/internal/mock"
21-
"github.com/skycoin/services/coin-api/internal/model"
2218
"github.com/skycoin/services/coin-api/internal/multi"
2319
)
2420

@@ -39,28 +35,15 @@ func TestGenerateKeyPair(t *testing.T) {
3935
Port: 6420,
4036
}
4137
skyService := multi.NewSkyService(&loc)
42-
rsp := skyService.GenerateKeyPair()
43-
assertCodeZero(t, rsp)
44-
assertStatusOk(t, rsp)
45-
result := rsp.Result
46-
keysResponse, ok := result.(*model.KeysResponse)
47-
if !ok {
48-
t.Fatalf("wrong type, result.(*model.KeysResponse) expected, given %s", reflect.TypeOf(result).String())
49-
}
38+
keysResponse := skyService.GenerateKeyPair()
5039
if len(keysResponse.Private) == 0 || len(keysResponse.Public) == 0 {
5140
t.Fatalf("keysResponse.Private or keysResponse.Public should not be zero length")
5241
}
5342

5443
t.Run("TestGenerateAddress", func(t *testing.T) {
55-
rsp, err := skyService.GenerateAddr(keysResponse.Private)
56-
assert.NoError(t, err)
57-
assertCodeZero(t, rsp)
58-
assertStatusOk(t, rsp)
59-
result := rsp.Result
60-
rspAdd, ok := result.(*model.AddressResponse)
61-
62-
if !ok {
63-
t.Fatalf("wrong type, result.(*model.AddressResponse) expected, given %s", reflect.TypeOf(result).String())
44+
rspAdd, err := skyService.GenerateAddr(keysResponse.Private)
45+
if err != nil {
46+
t.Fatal(err.Error())
6447
}
6548
if len(rspAdd.Address) == 0 {
6649
t.Fatalf("address cannot be zero lenght")
@@ -69,17 +52,9 @@ func TestGenerateKeyPair(t *testing.T) {
6952
t.Run("check balance", func(t *testing.T) {
7053
address := rspAdd.Address
7154
skyServiceIsolated := getTestedMockedService()
72-
rsp, err := skyServiceIsolated.CheckBalance(address)
73-
if !assert.NoError(t, err) {
74-
t.Fatal()
75-
}
76-
77-
assertCodeZero(t, rsp)
78-
assertStatusOk(t, rsp)
79-
result := rsp.Result
80-
bRsp, ok := result.(*model.BalanceResponse)
81-
if !ok {
82-
t.Fatalf("wrong type, *model.BalanceResponse expected, given %s", reflect.TypeOf(result).String())
55+
bRsp, err := skyServiceIsolated.CheckBalance(address)
56+
if err != nil {
57+
t.Fatal(err.Error())
8358
}
8459
if bRsp.Balance != "23" || bRsp.Hours != "3" {
8560
t.Fatalf("wrong balance")
@@ -91,20 +66,11 @@ func TestGenerateKeyPair(t *testing.T) {
9166
func TestTransaction(t *testing.T) {
9267
skyService := getTestedMockedService()
9368
t.Run("sign transaction", func(t *testing.T) {
94-
//TODO: check this logic
9569
_, secKey := makeUxBodyWithSecret(t)
9670
secKeyHex := secKey.Hex()
97-
rsp, err := skyService.SignTransaction(secKeyHex, rawTxStr)
98-
if !assert.NoError(t, err) {
99-
println("err.Error()", err.Error())
100-
t.FailNow()
101-
}
102-
assertCodeZero(t, rsp)
103-
assertStatusOk(t, rsp)
104-
result := rsp.Result
105-
bRsp, ok := result.(*model.TransactionSign)
106-
if !ok {
107-
t.Fatalf("wrong type, *model.TransactionSign expected, given %s", reflect.TypeOf(result).String())
71+
bRsp, err := skyService.SignTransaction(secKeyHex, rawTxStr)
72+
if err != nil {
73+
t.Fatal(err.Error())
10874
}
10975
if len(bRsp.Signid) == 0 {
11076
t.Fatalf("signid shouldn't be zero length")
@@ -136,17 +102,9 @@ func TestTransaction(t *testing.T) {
136102
return true
137103
})).Return(rawTxID, nil)
138104

139-
rsp, err := skyService.InjectTransaction(rawTxStr)
140-
if !assert.NoError(t, err) {
141-
println("err.Error()", err.Error())
142-
t.FailNow()
143-
}
144-
assertCodeZero(t, rsp)
145-
assertStatusOk(t, rsp)
146-
result := rsp.Result
147-
bRsp, ok := result.(*model.Transaction)
148-
if !ok {
149-
t.Fatalf("wrong type, *model.Transaction expected, given %s", reflect.TypeOf(result).String())
105+
bRsp, err := skyService.InjectTransaction(rawTxStr)
106+
if err != nil {
107+
t.Fatal(err.Error())
150108
}
151109
if len(bRsp.Transid) == 0 {
152110
t.Fatalf("signid shouldn't be zero length")
@@ -171,9 +129,8 @@ func TestTransaction(t *testing.T) {
171129
}, nil)
172130

173131
transStatus, err := skyService.CheckTransactionStatus(rawTxID)
174-
if !assert.NoError(t, err) {
175-
println("err.Error()", err.Error())
176-
t.FailNow()
132+
if err != nil {
133+
t.Fatal(err.Error())
177134
}
178135
if transStatus.BlockSeq == 0 {
179136
t.Fatalf("blockSeq shouldn't be zero length")
@@ -220,15 +177,3 @@ func makeUxBodyWithSecret(t *testing.T) (coin.UxBody, cipher.SecKey) {
220177
Hours: 100,
221178
}, s
222179
}
223-
224-
func assertCodeZero(t *testing.T, rsp *model.Response) {
225-
if rsp.Code != 0 {
226-
t.Fatalf("the code must be 0, %d given", rsp.Code)
227-
}
228-
}
229-
230-
func assertStatusOk(t *testing.T, rsp *model.Response) {
231-
if rsp.Status != "ok" {
232-
t.Fatalf("status must be ok %s given", rsp.Status)
233-
}
234-
}

‎errhandler/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## SkyCoin error handler
2+
3+
Just import this package and use constants from it like error codes for various blockchain errors.
4+
Existing error codes and names mostly similar to bitcoin ones, but feel free to add your own error codes.

0 commit comments

Comments
 (0)
Please sign in to comment.