rosetta-lbry/services/network_service_test.go

137 lines
3.8 KiB
Go
Raw Normal View History

2020-09-16 21:03:14 +02:00
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package services
import (
"context"
"testing"
"github.com/coinbase/rosetta-bitcoin/bitcoin"
"github.com/coinbase/rosetta-bitcoin/configuration"
mocks "github.com/coinbase/rosetta-bitcoin/mocks/services"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/stretchr/testify/assert"
)
var (
2020-12-02 18:10:37 +01:00
middlewareVersion = "0.0.8"
2020-09-16 21:03:14 +02:00
defaultNetworkOptions = &types.NetworkOptionsResponse{
Version: &types.Version{
2020-10-26 23:48:28 +01:00
RosettaVersion: types.RosettaAPIVersion,
2020-09-21 00:20:34 +02:00
NodeVersion: "0.20.1",
MiddlewareVersion: &middlewareVersion,
2020-09-16 21:03:14 +02:00
},
Allow: &types.Allow{
OperationStatuses: bitcoin.OperationStatuses,
OperationTypes: bitcoin.OperationTypes,
Errors: Errors,
HistoricalBalanceLookup: HistoricalBalanceLookup,
2020-09-16 21:03:14 +02:00
},
}
networkIdentifier = &types.NetworkIdentifier{
Network: bitcoin.MainnetNetwork,
Blockchain: bitcoin.Blockchain,
}
)
func TestNetworkEndpoints_Offline(t *testing.T) {
cfg := &configuration.Configuration{
Mode: configuration.Offline,
Network: networkIdentifier,
}
mockIndexer := &mocks.Indexer{}
mockClient := &mocks.Client{}
servicer := NewNetworkAPIService(cfg, mockClient, mockIndexer)
ctx := context.Background()
networkList, err := servicer.NetworkList(ctx, nil)
assert.Nil(t, err)
assert.Equal(t, []*types.NetworkIdentifier{
networkIdentifier,
}, networkList.NetworkIdentifiers)
networkStatus, err := servicer.NetworkStatus(ctx, nil)
assert.Nil(t, networkStatus)
assert.Equal(t, ErrUnavailableOffline.Code, err.Code)
assert.Equal(t, ErrUnavailableOffline.Message, err.Message)
networkOptions, err := servicer.NetworkOptions(ctx, nil)
assert.Nil(t, err)
assert.Equal(t, defaultNetworkOptions, networkOptions)
mockIndexer.AssertExpectations(t)
mockClient.AssertExpectations(t)
}
func TestNetworkEndpoints_Online(t *testing.T) {
cfg := &configuration.Configuration{
2020-09-28 17:09:07 +02:00
Mode: configuration.Online,
Network: networkIdentifier,
GenesisBlockIdentifier: bitcoin.MainnetGenesisBlockIdentifier,
2020-09-16 21:03:14 +02:00
}
mockIndexer := &mocks.Indexer{}
mockClient := &mocks.Client{}
servicer := NewNetworkAPIService(cfg, mockClient, mockIndexer)
ctx := context.Background()
networkList, err := servicer.NetworkList(ctx, nil)
assert.Nil(t, err)
assert.Equal(t, []*types.NetworkIdentifier{
networkIdentifier,
}, networkList.NetworkIdentifiers)
blockResponse := &types.BlockResponse{
Block: &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Index: 100,
Hash: "block 100",
},
},
}
2020-09-28 17:09:07 +02:00
mockClient.On("GetPeers", ctx).Return([]*types.Peer{
{
PeerID: "77.93.223.9:8333",
},
}, nil)
2020-09-16 21:03:14 +02:00
mockIndexer.On(
"GetBlockLazy",
ctx,
(*types.PartialBlockIdentifier)(nil),
).Return(
blockResponse,
nil,
)
networkStatus, err := servicer.NetworkStatus(ctx, nil)
assert.Nil(t, err)
assert.Equal(t, &types.NetworkStatusResponse{
GenesisBlockIdentifier: bitcoin.MainnetGenesisBlockIdentifier,
CurrentBlockIdentifier: blockResponse.Block.BlockIdentifier,
2020-09-28 17:09:07 +02:00
Peers: []*types.Peer{
{
PeerID: "77.93.223.9:8333",
},
},
2020-09-16 21:03:14 +02:00
}, networkStatus)
networkOptions, err := servicer.NetworkOptions(ctx, nil)
assert.Nil(t, err)
assert.Equal(t, defaultNetworkOptions, networkOptions)
mockIndexer.AssertExpectations(t)
mockClient.AssertExpectations(t)
}