Skip to content

Commit

Permalink
Reintroduce missing code for rest endpoint wiring (#2080)
Browse files Browse the repository at this point in the history
* Reintroduce missing code for rest endpoint wiring

* Fix gofumpt

* e2e test to check if endpoints are available

* Ran linter/formater

* Refactor and add comprehensive tests for missing routes (#2094)

* Fixed var -> const definition

---------

Co-authored-by: lg <lauren@interchain.io>
Co-authored-by: lg <8335464+glnro@users.noreply.github.com>
(cherry picked from commit 690f167)
  • Loading branch information
mmulji-ic authored and mergify[bot] committed Jan 30, 2023
1 parent 4b1e218 commit ee78122
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/api"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
Expand Down Expand Up @@ -319,12 +321,16 @@ func (app *GaiaApp) SimulationManager() *module.SimulationManager {
// API server.
func (app *GaiaApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
clientCtx := apiSvr.ClientCtx
rpc.RegisterRoutes(clientCtx, apiSvr.Router)
// Register legacy tx routes.
authrest.RegisterTxRoutes(clientCtx, apiSvr.Router)
// Register new tx routes from grpc-gateway.
authtx.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register new tendermint queries routes from grpc-gateway.
tmservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// Register legacy and grpc-gateway routes for all modules.
ModuleBasics.RegisterRESTRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// register swagger API from root so that other applications can override easily
Expand Down
89 changes: 89 additions & 0 deletions tests/e2e/e2e_rest_regression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package e2e

import (
"fmt"
"net/http"
)

/*
RestRegression tests the continuity of critical endpoints that node operators, block explorers, and ecosystem participants depend on.
Test Node REST Endpoints:
1. http://host:1317/validatorsets/latest
2. http://host:1317/validatorsets/{height}
3. http://host:1317/blocks/latest
4. http://host:1317/blocks/{height}
5. http://host:1317/syncing
6. http://host:1317/node_info
7. http://host:1317/txs
Test Module REST Endpoints
1. Bank total
2. Auth params
3. Distribution for Community Pool
4. Evidence
5. Gov proposals
6. Mint params
7. Slashing params
8. Staking params
*/

const (
valSetLatestPath = "/validatorsets/latest"
valSetHeightPath = "/validatorsets/1"
blocksLatestPath = "/blocks/latest"
blocksHeightPath = "/blocks/1"
syncingPath = "/syncing"
nodeInfoPath = "/node_info"
transactionsPath = "/txs"
bankTotalModuleQueryPath = "/bank/total"
authParamsModuleQueryPath = "/auth/params"
distributionCommPoolModuleQueryPath = "/distribution/community_pool"
evidenceModuleQueryPath = "/evidence"
govPropsModuleQueryPath = "/gov/proposals"
mintingParamsModuleQueryPath = "/minting/parameters"
slashingParamsModuleQueryPath = "/slashing/parameters"
stakingParamsModuleQueryPath = "/staking/parameters"
missingPath = "/missing_endpoint"
)

func (s *IntegrationTestSuite) testRestInterfaces() {
s.Run("test rest interfaces", func() {
var (
valIdx = 0
c = s.chainA
endpointURL = fmt.Sprintf("http://%s", s.valResources[c.id][valIdx].GetHostPort("1317/tcp"))
testEndpoints = []struct {
Path string
ExpectedStatus int
}{
// Client Endpoints
{nodeInfoPath, 200},
{syncingPath, 200},
{valSetLatestPath, 200},
{valSetHeightPath, 200},
{blocksLatestPath, 200},
{blocksHeightPath, 200},
{transactionsPath, 200},
// Module Endpoints
{bankTotalModuleQueryPath, 200},
{authParamsModuleQueryPath, 200},
{distributionCommPoolModuleQueryPath, 200},
{evidenceModuleQueryPath, 200},
{govPropsModuleQueryPath, 200},
{mintingParamsModuleQueryPath, 200},
{slashingParamsModuleQueryPath, 200},
{stakingParamsModuleQueryPath, 200},
{missingPath, 501},
}
)

for _, endpoint := range testEndpoints {
resp, err := http.Get(fmt.Sprintf("%s%s", endpointURL, endpoint.Path))
s.NoError(err, fmt.Sprintf("failed to get endpoint: %s%s", endpointURL, endpoint.Path))

_, err = readJSON(resp)
s.NoError(err, fmt.Sprintf("failed to read body of endpoint: %s%s", endpointURL, endpoint.Path))

s.EqualValues(resp.StatusCode, endpoint.ExpectedStatus)
}
})
}
8 changes: 8 additions & 0 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ var (
runSlashingTest = true
runStakingAndDistributionTest = true
runVestingTest = true
runRestInterfacesTest = true
)

func (s *IntegrationTestSuite) TestRestInterfaces() {
if !runRestInterfacesTest {
s.T().Skip()
}
s.testRestInterfaces()
}

func (s *IntegrationTestSuite) TestBank() {
if !runBankTest {
s.T().Skip()
Expand Down
18 changes: 18 additions & 0 deletions tests/e2e/http_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -20,3 +21,20 @@ func httpGet(endpoint string) ([]byte, error) {

return body, nil
}

func readJSON(resp *http.Response) (map[string]interface{}, error) {
defer resp.Body.Close()

body, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return nil, fmt.Errorf("failed to read Body")
}

var data map[string]interface{}
err := json.Unmarshal(body, &data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response body")
}

return data, nil
}

0 comments on commit ee78122

Please sign in to comment.