Skip to content

Commit

Permalink
Merge pull request #1200 from onetechnical/onetechnical/relstable2.0.8
Browse files Browse the repository at this point in the history
Algorand v2.0.8
  • Loading branch information
algojohnlee authored Jun 25, 2020
2 parents 7b6d4d4 + 50b7d87 commit 890f535
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 2 deletions.
2 changes: 1 addition & 1 deletion buildnumber.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7
8
182 changes: 182 additions & 0 deletions daemon/algod/api/server/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright (C) 2019-2020 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package server

import (
"net/http"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/algorand/go-algorand/daemon/algod/api/server/lib"
"github.com/algorand/go-algorand/daemon/algod/api/server/v1/routes"
)

type TestSuite struct {
suite.Suite
calls int
e *echo.Echo
}

func (s *TestSuite) SetupSuite() {
s.e = echo.New()
handler := func(context lib.ReqContext, context2 echo.Context) {
s.calls++
}
// Make a deep copy of the routes array with dummy handlers that log a call.
v1RoutesCopy := make([]lib.Route, len(routes.V1Routes))
for _, route := range routes.V1Routes {
v1RoutesCopy = append(v1RoutesCopy, lib.Route{
Name: route.Name,
Method: route.Method,
Path: route.Path,
HandlerFunc: handler,
})
}
// Registering v1 routes
registerHandlers(s.e, apiV1Tag, v1RoutesCopy, lib.ReqContext{})
}
func (s *TestSuite) SetupTest() {
s.calls = 0
}
func (s *TestSuite) TestBaselineRoute() {
ctx := s.e.NewContext(nil, nil)
s.e.Router().Find(http.MethodGet, "/v0/this/is/no/endpoint", ctx)
assert.Equal(s.T(), echo.ErrNotFound, ctx.Handler()(ctx))
assert.Equal(s.T(), 0, s.calls)
}
func (s *TestSuite) TestAccountPendingTransaction() {
ctx := s.e.NewContext(nil, nil)
s.e.Router().Find(http.MethodGet, "/v1/account/address-param/transactions/pending", ctx)
assert.Equal(s.T(), "/v1/account/:addr/transactions/pending", ctx.Path())
assert.Equal(s.T(), "address-param", ctx.Param("addr"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestWaitAfterBlock() {
ctx := s.e.NewContext(nil, nil)
s.e.Router().Find(http.MethodGet, "/v1/status/wait-for-block-after/123456", ctx)
assert.Equal(s.T(), "/v1/status/wait-for-block-after/:round", ctx.Path())
assert.Equal(s.T(), "123456", ctx.Param("round"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestAccountInformation() {
ctx := s.e.NewContext(nil, nil)
s.e.Router().Find(http.MethodGet, "/v1/account/ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA", ctx)
assert.Equal(s.T(), "/v1/account/:addr", ctx.Path())
assert.Equal(s.T(), "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA", ctx.Param("addr"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestTransactionInformation() {
ctx := s.e.NewContext(nil, nil)
addr := "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA"
txid := "ASPB5E72OT2UWSOCQGD5OPT3W4KV4LZZDL7L5MBCC3EBAIJCDHAA"
s.e.Router().Find(http.MethodGet, "/v1/account/"+addr+"/transaction/"+txid, ctx)
assert.Equal(s.T(), "/v1/account/:addr/transaction/:txid", ctx.Path())
assert.Equal(s.T(), addr, ctx.Param("addr"))
assert.Equal(s.T(), txid, ctx.Param("txid"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestAccountTransaction() {
ctx := s.e.NewContext(nil, nil)
addr := "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA"
s.e.Router().Find(http.MethodGet, "/v1/account/"+addr+"/transactions", ctx)
assert.Equal(s.T(), "/v1/account/:addr/transactions", ctx.Path())
assert.Equal(s.T(), addr, ctx.Param("addr"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestBlock() {
ctx := s.e.NewContext(nil, nil)
s.e.Router().Find(http.MethodGet, "/v1/block/123456", ctx)
assert.Equal(s.T(), "/v1/block/:round", ctx.Path())
assert.Equal(s.T(), "123456", ctx.Param("round"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestPendingTransactionID() {
ctx := s.e.NewContext(nil, nil)
txid := "ASPB5E72OT2UWSOCQGD5OPT3W4KV4LZZDL7L5MBCC3EBAIJCDHAA"
s.e.Router().Find(http.MethodGet, "/v1/transactions/pending/"+txid, ctx)
assert.Equal(s.T(), "/v1/transactions/pending/:txid", ctx.Path())
assert.Equal(s.T(), txid, ctx.Param("txid"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestPendingTransactionInformationByAddress() {
ctx := s.e.NewContext(nil, nil)
addr := "ZBBRQD73JH5KZ7XRED6GALJYJUXOMBBP3X2Z2XFA4LATV3MUJKKMKG7SHA"
s.e.Router().Find(http.MethodGet, "/v1/account/"+addr+"/transactions/pending", ctx)
assert.Equal(s.T(), "/v1/account/:addr/transactions/pending", ctx.Path())
assert.Equal(s.T(), addr, ctx.Param("addr"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestGetAsset() {
ctx := s.e.NewContext(nil, nil)
s.e.Router().Find(http.MethodGet, "/v1/asset/123456", ctx)
assert.Equal(s.T(), "/v1/asset/:index", ctx.Path())
assert.Equal(s.T(), "123456", ctx.Param("index"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func (s *TestSuite) TestGetTransactionByID() {
ctx := s.e.NewContext(nil, nil)
txid := "ASPB5E72OT2UWSOCQGD5OPT3W4KV4LZZDL7L5MBCC3EBAIJCDHAA"
s.e.Router().Find(http.MethodGet, "/v1/transaction/"+txid, ctx)
assert.Equal(s.T(), "/v1/transaction/:txid", ctx.Path())
assert.Equal(s.T(), txid, ctx.Param("txid"))

// Ensure that a handler in the route array was called by checking that the 'calls' variable is incremented.
callsBefore := s.calls
assert.Equal(s.T(), nil, ctx.Handler()(ctx))
assert.Equal(s.T(), callsBefore+1, s.calls)
}
func TestTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}
2 changes: 1 addition & 1 deletion daemon/algod/api/server/v1/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var V1Routes = lib.Routes{
lib.Route{
Name: "pending-transaction-information-by-address",
Method: "GET",
Path: "/account/{addr}/transactions/pending",
Path: "/account/:addr/transactions/pending",
HandlerFunc: handlers.GetPendingTransactionsByAddress,
},

Expand Down

0 comments on commit 890f535

Please sign in to comment.