go-waku/waku/v2/rpc/admin_test.go

70 lines
1.7 KiB
Go
Raw Normal View History

2021-11-10 13:36:51 +00:00
package rpc
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"net/http"
"testing"
2021-11-17 16:19:42 +00:00
"time"
2021-11-10 13:36:51 +00:00
"github.com/multiformats/go-multiaddr"
"github.com/status-im/go-waku/tests"
"github.com/status-im/go-waku/waku/v2/node"
2021-11-17 16:19:42 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/relay"
2021-11-10 13:36:51 +00:00
"github.com/stretchr/testify/require"
)
func makeAdminService(t *testing.T) *AdminService {
options := node.WithWakuRelay()
n, err := node.New(context.Background(), options)
require.NoError(t, err)
err = n.Start()
require.NoError(t, err)
return &AdminService{n, tests.Logger()}
2021-11-10 13:36:51 +00:00
}
func TestV1Peers(t *testing.T) {
port, err := tests.FindFreePort(t, "", 5)
require.NoError(t, err)
host, err := tests.MakeHost(context.Background(), port, rand.Reader)
require.NoError(t, err)
relay, err := relay.NewWakuRelay(context.Background(), host, nil, 0, tests.Logger())
2021-11-17 16:19:42 +00:00
require.NoError(t, err)
defer relay.Stop()
2021-11-10 13:36:51 +00:00
var reply PeersReply
request, err := http.NewRequest(http.MethodPost, "url", bytes.NewReader([]byte("")))
require.NoError(t, err)
a := makeAdminService(t)
err = a.GetV1Peers(request, &GetPeersArgs{}, &reply)
require.NoError(t, err)
require.Len(t, reply.Peers, 0)
var reply2 SuccessReply
hostInfo, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", host.ID().Pretty()))
require.NoError(t, err)
var addr multiaddr.Multiaddr
for _, a := range host.Addrs() {
addr = a.Encapsulate(hostInfo)
break
}
err = a.PostV1Peers(request, &PeersArgs{Peers: []string{addr.String()}}, &reply2)
require.NoError(t, err)
require.True(t, reply2.Success)
2021-11-17 16:19:42 +00:00
time.Sleep(2 * time.Second)
2021-11-10 13:36:51 +00:00
err = a.GetV1Peers(request, &GetPeersArgs{}, &reply)
require.NoError(t, err)
require.Len(t, reply.Peers, 2)
}