|
| 1 | +// Copyright (C) 2019-2020 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +package server |
| 17 | + |
| 18 | +import ( |
| 19 | + "net/http" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/labstack/echo/v4" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/suite" |
| 25 | + |
| 26 | + "github.com/algorand/go-algorand/daemon/algod/api/server/lib" |
| 27 | + "github.com/algorand/go-algorand/daemon/algod/api/server/v1/routes" |
| 28 | +) |
| 29 | + |
| 30 | +type TestSuite struct { |
| 31 | + suite.Suite |
| 32 | + calls int |
| 33 | + e *echo.Echo |
| 34 | +} |
| 35 | + |
| 36 | +func (s *TestSuite) SetupSuite() { |
| 37 | + s.e = echo.New() |
| 38 | + handler := func(context lib.ReqContext, context2 echo.Context) { |
| 39 | + s.calls++ |
| 40 | + } |
| 41 | + // Make a deep copy of the routes array with dummy handlers that log a call. |
| 42 | + v1RoutesCopy := make([]lib.Route, len(routes.V1Routes)) |
| 43 | + for _, route := range routes.V1Routes { |
| 44 | + v1RoutesCopy = append(v1RoutesCopy, lib.Route{ |
| 45 | + Name: route.Name, |
| 46 | + Method: route.Method, |
| 47 | + Path: route.Path, |
| 48 | + HandlerFunc: handler, |
| 49 | + }) |
| 50 | + } |
| 51 | + // Registering v1 routes |
| 52 | + registerHandlers(s.e, apiV1Tag, v1RoutesCopy, lib.ReqContext{}) |
| 53 | +} |
| 54 | +func (s *TestSuite) SetupTest() { |
| 55 | + s.calls = 0 |
| 56 | +} |
| 57 | +func (s *TestSuite) TestBaselineRoute() { |
| 58 | + ctx := s.e.NewContext(nil, nil) |
| 59 | + s.e.Router().Find(http.MethodGet, "/v0/this/is/no/endpoint", ctx) |
| 60 | + assert.Equal(s.T(), echo.ErrNotFound, ctx.Handler()(ctx)) |
| 61 | + assert.Equal(s.T(), 0, s.calls) |
| 62 | +} |
| 63 | +func (s *TestSuite) TestAccountPendingTransaction() { |
| 64 | + ctx := s.e.NewContext(nil, nil) |
| 65 | + s.e.Router().Find(http.MethodGet, "/v1/account/address-param/transactions/pending", ctx) |
| 66 | + assert.Equal(s.T(), "/v1/account/:addr/transactions/pending", ctx.Path()) |
| 67 | + assert.Equal(s.T(), "address-param", ctx.Param("addr")) |
| 68 | + |
| 69 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 70 | + callsBefore := s.calls |
| 71 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 72 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 73 | +} |
| 74 | +func (s *TestSuite) TestWaitAfterBlock() { |
| 75 | + ctx := s.e.NewContext(nil, nil) |
| 76 | + s.e.Router().Find(http.MethodGet, "/v1/status/wait-for-block-after/123456", ctx) |
| 77 | + assert.Equal(s.T(), "/v1/status/wait-for-block-after/:round", ctx.Path()) |
| 78 | + assert.Equal(s.T(), "123456", ctx.Param("round")) |
| 79 | + |
| 80 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 81 | + callsBefore := s.calls |
| 82 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 83 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 84 | +} |
| 85 | +func (s *TestSuite) TestAccountInformation() { |
| 86 | + ctx := s.e.NewContext(nil, nil) |
| 87 | + s.e.Router().Find(http.MethodGet, "/v1/account/ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA", ctx) |
| 88 | + assert.Equal(s.T(), "/v1/account/:addr", ctx.Path()) |
| 89 | + assert.Equal(s.T(), "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA", ctx.Param("addr")) |
| 90 | + |
| 91 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 92 | + callsBefore := s.calls |
| 93 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 94 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 95 | +} |
| 96 | +func (s *TestSuite) TestTransactionInformation() { |
| 97 | + ctx := s.e.NewContext(nil, nil) |
| 98 | + addr := "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA" |
| 99 | + txid := "ASPB5E72OT2UWSOCQGD5OPT3W4KV4LZZDL7L5MBCC3EBAIJCDHAA" |
| 100 | + s.e.Router().Find(http.MethodGet, "/v1/account/"+addr+"/transaction/"+txid, ctx) |
| 101 | + assert.Equal(s.T(), "/v1/account/:addr/transaction/:txid", ctx.Path()) |
| 102 | + assert.Equal(s.T(), addr, ctx.Param("addr")) |
| 103 | + assert.Equal(s.T(), txid, ctx.Param("txid")) |
| 104 | + |
| 105 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 106 | + callsBefore := s.calls |
| 107 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 108 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 109 | +} |
| 110 | +func (s *TestSuite) TestAccountTransaction() { |
| 111 | + ctx := s.e.NewContext(nil, nil) |
| 112 | + addr := "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA" |
| 113 | + s.e.Router().Find(http.MethodGet, "/v1/account/"+addr+"/transactions", ctx) |
| 114 | + assert.Equal(s.T(), "/v1/account/:addr/transactions", ctx.Path()) |
| 115 | + assert.Equal(s.T(), addr, ctx.Param("addr")) |
| 116 | + |
| 117 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 118 | + callsBefore := s.calls |
| 119 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 120 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 121 | +} |
| 122 | +func (s *TestSuite) TestBlock() { |
| 123 | + ctx := s.e.NewContext(nil, nil) |
| 124 | + s.e.Router().Find(http.MethodGet, "/v1/block/123456", ctx) |
| 125 | + assert.Equal(s.T(), "/v1/block/:round", ctx.Path()) |
| 126 | + assert.Equal(s.T(), "123456", ctx.Param("round")) |
| 127 | + |
| 128 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 129 | + callsBefore := s.calls |
| 130 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 131 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 132 | +} |
| 133 | +func (s *TestSuite) TestPendingTransactionID() { |
| 134 | + ctx := s.e.NewContext(nil, nil) |
| 135 | + txid := "ASPB5E72OT2UWSOCQGD5OPT3W4KV4LZZDL7L5MBCC3EBAIJCDHAA" |
| 136 | + s.e.Router().Find(http.MethodGet, "/v1/transactions/pending/"+txid, ctx) |
| 137 | + assert.Equal(s.T(), "/v1/transactions/pending/:txid", ctx.Path()) |
| 138 | + assert.Equal(s.T(), txid, ctx.Param("txid")) |
| 139 | + |
| 140 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 141 | + callsBefore := s.calls |
| 142 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 143 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 144 | +} |
| 145 | +func (s *TestSuite) TestPendingTransactionInformationByAddress() { |
| 146 | + ctx := s.e.NewContext(nil, nil) |
| 147 | + addr := "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA" |
| 148 | + s.e.Router().Find(http.MethodGet, "/v1/account/"+addr+"/transactions/pending", ctx) |
| 149 | + assert.Equal(s.T(), "/v1/account/:addr/transactions/pending", ctx.Path()) |
| 150 | + assert.Equal(s.T(), addr, ctx.Param("addr")) |
| 151 | + |
| 152 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 153 | + callsBefore := s.calls |
| 154 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 155 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 156 | +} |
| 157 | +func (s *TestSuite) TestGetAsset() { |
| 158 | + ctx := s.e.NewContext(nil, nil) |
| 159 | + s.e.Router().Find(http.MethodGet, "/v1/asset/123456", ctx) |
| 160 | + assert.Equal(s.T(), "/v1/asset/:index", ctx.Path()) |
| 161 | + assert.Equal(s.T(), "123456", ctx.Param("index")) |
| 162 | + |
| 163 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 164 | + callsBefore := s.calls |
| 165 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 166 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 167 | +} |
| 168 | +func (s *TestSuite) TestGetTransactionByID() { |
| 169 | + ctx := s.e.NewContext(nil, nil) |
| 170 | + txid := "ASPB5E72OT2UWSOCQGD5OPT3W4KV4LZZDL7L5MBCC3EBAIJCDHAA" |
| 171 | + s.e.Router().Find(http.MethodGet, "/v1/transaction/"+txid, ctx) |
| 172 | + assert.Equal(s.T(), "/v1/transaction/:txid", ctx.Path()) |
| 173 | + assert.Equal(s.T(), txid, ctx.Param("txid")) |
| 174 | + |
| 175 | + // Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented. |
| 176 | + callsBefore := s.calls |
| 177 | + assert.Equal(s.T(), nil, ctx.Handler()(ctx)) |
| 178 | + assert.Equal(s.T(), callsBefore+1, s.calls) |
| 179 | +} |
| 180 | +func TestTestSuite(t *testing.T) { |
| 181 | + suite.Run(t, new(TestSuite)) |
| 182 | +} |
0 commit comments