OSDN Git Service

modify bytomcli reset-key-password response
[bytom/bytom-spv.git] / api / api_test.go
1 package api
2
3 import (
4         "context"
5         "encoding/json"
6         "math"
7         "net/http/httptest"
8         "os"
9         "testing"
10
11         dbm "github.com/tendermint/tmlibs/db"
12
13         "github.com/bytom/accesstoken"
14         "github.com/bytom/blockchain/rpc"
15         "github.com/bytom/blockchain/txbuilder"
16         "github.com/bytom/testutil"
17 )
18
19 func TestAPIHandler(t *testing.T) {
20         a := &API{}
21         response := &Response{}
22
23         // init httptest server
24         a.buildHandler()
25         server := httptest.NewServer(a.handler)
26         defer server.Close()
27
28         // create accessTokens
29         testDB := dbm.NewDB("testdb", "leveldb", "temp")
30         defer os.RemoveAll("temp")
31         a.accessTokens = accesstoken.NewStore(testDB)
32
33         client := &rpc.Client{
34                 BaseURL:     server.URL,
35                 AccessToken: "test-user:test-secret",
36         }
37
38         cases := []struct {
39                 path     string
40                 request  interface{}
41                 respWant *Response
42         }{
43                 {
44                         path: "/create-key",
45                         request: struct {
46                                 Alias    string `json:"alias"`
47                                 Password string `json:"password"`
48                         }{Alias: "alice", Password: "123456"},
49                         respWant: &Response{
50                                 Status: "fail",
51                                 Msg:    "wallet not found, please check that the wallet is open",
52                         },
53                 },
54                 {
55                         path:    "/error",
56                         request: nil,
57                         respWant: &Response{
58                                 Status: "fail",
59                                 Msg:    "wallet not found, please check that the wallet is open",
60                         },
61                 },
62                 {
63                         path:    "/",
64                         request: nil,
65                         respWant: &Response{
66                                 Status: "",
67                                 Msg:    "",
68                         },
69                 },
70                 {
71                         path: "/create-access-token",
72                         request: struct {
73                                 ID   string `json:"id"`
74                                 Type string `json:"type"`
75                         }{ID: "test-access-id", Type: "test-access-type"},
76                         respWant: &Response{
77                                 Status: "success",
78                                 Msg:    "",
79                                 Data:   map[string]interface{}{"id": "test-access-id", "type": "test-access-type", "token": "test-access-id:440d87ae0d625a7fcf076275b18372e09a0899e37ec86398879388de90cb0c67"},
80                         },
81                 },
82                 {
83                         path:    "/gas-rate",
84                         request: nil,
85                         respWant: &Response{
86                                 Status: "success",
87                                 Msg:    "",
88                                 Data:   map[string]interface{}{"gasRate": 1000},
89                         },
90                 },
91         }
92
93         for _, c := range cases {
94                 response = &Response{}
95                 client.Call(context.Background(), c.path, c.request, &response)
96
97                 if !testutil.DeepEqual(response.Status, c.respWant.Status) {
98                         t.Errorf(`got=%#v; want=%#v`, response.Status, c.respWant.Status)
99                 }
100         }
101 }
102
103 func TestEstimateTxGas(t *testing.T) {
104         tmplStr := `{"allow_additional_actions":false,"raw_transaction":"070100010161015ffe8a1209937a6a8b22e8c01f056fd5f1730734ba8964d6b79de4a639032cecddffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d59901000116001485eb6eee8023332da85df60157dc9b16cc553fb2010002013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80afa08b4f011600142b4fd033bc76b4ddf5cb00f625362c4bc7b10efa00013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8090dfc04a011600146eea1ce6cfa5b718ae8094376be9bc1a87c9c82700","signing_instructions":[{"position":0,"witness_components":[{"keys":[{"derivation_path":["010100000000000000","0100000000000000"],"xpub":"cb4e5932d808ee060df9552963d87f60edac42360b11d4ad89558ef2acea4d4aaf4818f2ebf5a599382b8dfce0a0c798c7e44ec2667b3a1d34c61ba57609de55"}],"quorum":1,"signatures":null,"type":"raw_tx_signature"},{"type":"data","value":"1c9b5c1db7f4afe31fd1b7e0495a8bb042a271d8d7924d4fc1ff7cf1bff15813"}]}]}`
105         template := txbuilder.Template{}
106         err := json.Unmarshal([]byte(tmplStr), &template)
107         if err != nil {
108                 t.Fatal(err)
109         }
110
111         estimateResult, err := EstimateTxGas(template)
112         if err != nil {
113                 t.Fatal(err)
114         }
115
116         baseRate := float64(100000)
117         totalNeu := float64(estimateResult.StorageNeu+estimateResult.VMNeu) / baseRate
118         roundingNeu := math.Ceil(totalNeu)
119         estimateNeu := int64(roundingNeu) * int64(baseRate)
120
121         if estimateResult.TotalNeu != estimateNeu {
122                 t.Errorf(`got=%#v; want=%#v`, estimateResult.TotalNeu, estimateNeu)
123         }
124 }