2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-02-22 18:18:05 +01:00
|
|
|
|
|
|
|
import (
|
2023-02-14 10:14:00 +01:00
|
|
|
"bytes"
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2022-02-22 18:18:05 +01:00
|
|
|
"encoding/json"
|
2024-09-20 11:36:58 +02:00
|
|
|
"fmt"
|
2022-02-22 18:18:05 +01:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2023-02-14 10:14:00 +01:00
|
|
|
"time"
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/gorilla/mux"
|
2024-09-22 22:44:10 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2024-09-20 11:36:58 +02:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2024-09-22 22:44:10 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2023-11-28 15:15:51 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-09-20 11:36:58 +02:00
|
|
|
"golang.org/x/exp/maps"
|
2023-02-03 21:47:20 +01:00
|
|
|
|
2024-09-20 11:36:58 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
2022-02-22 18:18:05 +01:00
|
|
|
)
|
|
|
|
|
2024-09-20 11:36:58 +02:00
|
|
|
type ctxKey string
|
|
|
|
|
|
|
|
const (
|
|
|
|
testPeerID = "test_peer"
|
|
|
|
noUpdateChannelTestPeerID = "no-update-channel"
|
|
|
|
|
|
|
|
adminUser = "admin_user"
|
|
|
|
regularUser = "regular_user"
|
|
|
|
serviceUser = "service_user"
|
|
|
|
userIDKey ctxKey = "user_id"
|
|
|
|
)
|
2023-02-07 20:11:08 +01:00
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
func initTestMetaData(peers ...*nbpeer.Peer) *PeersHandler {
|
2023-02-28 15:01:24 +01:00
|
|
|
return &PeersHandler{
|
2022-02-22 18:18:05 +01:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2024-07-03 11:33:02 +02:00
|
|
|
UpdatePeerFunc: func(_ context.Context, accountID, userID string, update *nbpeer.Peer) (*nbpeer.Peer, error) {
|
2023-11-28 15:15:51 +01:00
|
|
|
var p *nbpeer.Peer
|
2023-10-11 18:11:45 +02:00
|
|
|
for _, peer := range peers {
|
|
|
|
if update.ID == peer.ID {
|
|
|
|
p = peer.Copy()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 10:14:00 +01:00
|
|
|
p.SSHEnabled = update.SSHEnabled
|
|
|
|
p.LoginExpirationEnabled = update.LoginExpirationEnabled
|
|
|
|
p.Name = update.Name
|
|
|
|
return p, nil
|
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
GetPeerFunc: func(_ context.Context, accountID, peerID, userID string) (*nbpeer.Peer, error) {
|
2023-11-28 15:15:51 +01:00
|
|
|
var p *nbpeer.Peer
|
2023-10-11 18:11:45 +02:00
|
|
|
for _, peer := range peers {
|
|
|
|
if peerID == peer.ID {
|
|
|
|
p = peer.Copy()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p, nil
|
2023-02-07 20:11:08 +01:00
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
GetPeersFunc: func(_ context.Context, accountID, userID string) ([]*nbpeer.Peer, error) {
|
2022-11-05 10:24:50 +01:00
|
|
|
return peers, nil
|
|
|
|
},
|
2024-03-18 12:25:01 +01:00
|
|
|
GetDNSDomainFunc: func() string {
|
|
|
|
return "netbird.selfhosted"
|
|
|
|
},
|
2024-09-22 22:44:10 +02:00
|
|
|
GetAccountIDFromTokenFunc: func(_ context.Context, claims jwtclaims.AuthorizationClaims) (string, string, error) {
|
|
|
|
return claims.AccountId, claims.UserId, nil
|
|
|
|
},
|
|
|
|
GetAccountByIDFunc: func(ctx context.Context, accountID string, userID string) (*server.Account, error) {
|
2024-09-20 11:36:58 +02:00
|
|
|
peersMap := make(map[string]*nbpeer.Peer)
|
|
|
|
for _, peer := range peers {
|
|
|
|
peersMap[peer.ID] = peer.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := &server.Policy{
|
|
|
|
ID: "policy",
|
2024-09-22 22:44:10 +02:00
|
|
|
AccountID: accountID,
|
2024-09-20 11:36:58 +02:00
|
|
|
Name: "policy",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*server.PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "rule",
|
|
|
|
Name: "rule",
|
|
|
|
Enabled: true,
|
|
|
|
Action: "accept",
|
|
|
|
Destinations: []string{"group1"},
|
|
|
|
Sources: []string{"group1"},
|
|
|
|
Bidirectional: true,
|
|
|
|
Protocol: "all",
|
|
|
|
Ports: []string{"80"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
srvUser := server.NewRegularUser(serviceUser)
|
|
|
|
srvUser.IsServiceUser = true
|
|
|
|
|
|
|
|
account := &server.Account{
|
2024-09-22 22:44:10 +02:00
|
|
|
Id: accountID,
|
2022-02-22 18:18:05 +01:00
|
|
|
Domain: "hotmail.com",
|
2024-09-20 11:36:58 +02:00
|
|
|
Peers: peersMap,
|
2022-11-05 10:24:50 +01:00
|
|
|
Users: map[string]*server.User{
|
2024-09-20 11:36:58 +02:00
|
|
|
adminUser: server.NewAdminUser(adminUser),
|
|
|
|
regularUser: server.NewRegularUser(regularUser),
|
|
|
|
serviceUser: srvUser,
|
|
|
|
},
|
|
|
|
Groups: map[string]*nbgroup.Group{
|
|
|
|
"group1": {
|
|
|
|
ID: "group1",
|
2024-09-22 22:44:10 +02:00
|
|
|
AccountID: accountID,
|
2024-09-20 11:36:58 +02:00
|
|
|
Name: "group1",
|
|
|
|
Issued: "api",
|
|
|
|
Peers: maps.Keys(peersMap),
|
|
|
|
},
|
2022-02-22 18:18:05 +01:00
|
|
|
},
|
2023-02-14 10:14:00 +01:00
|
|
|
Settings: &server.Settings{
|
|
|
|
PeerLoginExpirationEnabled: true,
|
|
|
|
PeerLoginExpiration: time.Hour,
|
|
|
|
},
|
2024-09-20 11:36:58 +02:00
|
|
|
Policies: []*server.Policy{policy},
|
2023-11-07 14:38:36 +01:00
|
|
|
Network: &server.Network{
|
|
|
|
Identifier: "ciclqisab2ss43jdn8q0",
|
|
|
|
Net: net.IPNet{
|
|
|
|
IP: net.ParseIP("100.67.0.0"),
|
|
|
|
Mask: net.IPv4Mask(255, 255, 0, 0),
|
|
|
|
},
|
|
|
|
Serial: 51,
|
|
|
|
},
|
2024-09-20 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 22:44:10 +02:00
|
|
|
return account, nil
|
2022-02-22 18:18:05 +01:00
|
|
|
},
|
2023-12-05 14:17:56 +01:00
|
|
|
HasConnectedChannelFunc: func(peerID string) bool {
|
2023-10-11 18:11:45 +02:00
|
|
|
statuses := make(map[string]struct{})
|
|
|
|
for _, peer := range peers {
|
|
|
|
if peer.ID == noUpdateChannelTestPeerID {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
statuses[peer.ID] = struct{}{}
|
|
|
|
}
|
2023-12-05 14:17:56 +01:00
|
|
|
_, ok := statuses[peerID]
|
|
|
|
return ok
|
2023-10-11 18:11:45 +02:00
|
|
|
},
|
2022-02-22 18:18:05 +01:00
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2024-09-20 11:36:58 +02:00
|
|
|
userID := r.Context().Value(userIDKey).(string)
|
2022-02-23 20:02:02 +01:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
2024-09-20 11:36:58 +02:00
|
|
|
UserId: userID,
|
2022-02-22 18:18:05 +01:00
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: "test_id",
|
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2022-02-22 18:18:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// Tests the GetAllPeers endpoint reachable in the route /api/peers
|
2022-02-22 18:18:05 +01:00
|
|
|
// Use the metadata generated by initTestMetaData() to check for values
|
|
|
|
func TestGetPeers(t *testing.T) {
|
2023-02-14 10:14:00 +01:00
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
peer := &nbpeer.Peer{
|
2023-02-14 10:14:00 +01:00
|
|
|
ID: testPeerID,
|
|
|
|
Key: "key",
|
|
|
|
SetupKey: "setupkey",
|
|
|
|
IP: net.ParseIP("100.64.0.1"),
|
2023-11-28 15:15:51 +01:00
|
|
|
Status: &nbpeer.PeerStatus{Connected: true},
|
2023-02-14 10:14:00 +01:00
|
|
|
Name: "PeerName",
|
|
|
|
LoginExpirationEnabled: false,
|
2023-11-28 15:15:51 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
2024-05-06 14:49:03 +02:00
|
|
|
Hostname: "hostname",
|
|
|
|
GoOS: "GoOS",
|
|
|
|
Kernel: "kernel",
|
|
|
|
Core: "core",
|
|
|
|
Platform: "platform",
|
|
|
|
OS: "OS",
|
|
|
|
WtVersion: "development",
|
|
|
|
SystemSerialNumber: "C02XJ0J0JGH7",
|
2023-02-14 10:14:00 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:45 +02:00
|
|
|
peer1 := peer.Copy()
|
|
|
|
peer1.ID = noUpdateChannelTestPeerID
|
|
|
|
|
2023-02-14 10:14:00 +01:00
|
|
|
expectedUpdatedPeer := peer.Copy()
|
|
|
|
expectedUpdatedPeer.LoginExpirationEnabled = true
|
|
|
|
expectedUpdatedPeer.SSHEnabled = true
|
|
|
|
expectedUpdatedPeer.Name = "New Name"
|
|
|
|
|
2023-10-11 18:11:45 +02:00
|
|
|
expectedPeer1 := peer1.Copy()
|
|
|
|
expectedPeer1.Status.Connected = false
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
tt := []struct {
|
2022-02-22 18:18:05 +01:00
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
2023-02-07 20:11:08 +01:00
|
|
|
expectedArray bool
|
2023-11-28 15:15:51 +01:00
|
|
|
expectedPeer *nbpeer.Peer
|
2022-02-22 18:18:05 +01:00
|
|
|
}{
|
2022-05-03 16:02:51 +02:00
|
|
|
{
|
|
|
|
name: "GetPeersMetaData",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/peers/",
|
|
|
|
expectedStatus: http.StatusOK,
|
2023-02-07 20:11:08 +01:00
|
|
|
expectedArray: true,
|
2023-02-14 10:14:00 +01:00
|
|
|
expectedPeer: peer,
|
2023-02-07 20:11:08 +01:00
|
|
|
},
|
|
|
|
{
|
2023-10-11 18:11:45 +02:00
|
|
|
name: "GetPeer with update channel",
|
2023-02-07 20:11:08 +01:00
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/peers/" + testPeerID,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedArray: false,
|
2023-02-14 10:14:00 +01:00
|
|
|
expectedPeer: peer,
|
|
|
|
},
|
2023-10-11 18:11:45 +02:00
|
|
|
{
|
|
|
|
name: "GetPeer with no update channel",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/peers/" + peer1.ID,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedArray: false,
|
|
|
|
expectedPeer: expectedPeer1,
|
|
|
|
},
|
2023-02-14 10:14:00 +01:00
|
|
|
{
|
|
|
|
name: "PutPeer",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/peers/" + testPeerID,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedArray: false,
|
|
|
|
requestBody: bytes.NewBufferString("{\"login_expiration_enabled\":true,\"name\":\"New Name\",\"ssh_enabled\":true}"),
|
|
|
|
expectedPeer: expectedUpdatedPeer,
|
2022-05-03 16:02:51 +02:00
|
|
|
},
|
2022-02-22 18:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
2023-10-11 18:11:45 +02:00
|
|
|
p := initTestMetaData(peer, peer1)
|
2022-02-22 18:18:05 +01:00
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-02-07 20:11:08 +01:00
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
2022-02-22 18:18:05 +01:00
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
2024-09-20 11:36:58 +02:00
|
|
|
ctx := context.WithValue(context.Background(), userIDKey, "admin_user")
|
|
|
|
req = req.WithContext(ctx)
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
router := mux.NewRouter()
|
2023-02-28 15:01:24 +01:00
|
|
|
router.HandleFunc("/api/peers/", p.GetAllPeers).Methods("GET")
|
2023-05-03 00:15:25 +02:00
|
|
|
router.HandleFunc("/api/peers/{peerId}", p.HandlePeer).Methods("GET")
|
|
|
|
router.HandleFunc("/api/peers/{peerId}", p.HandlePeer).Methods("PUT")
|
2023-02-07 20:11:08 +01:00
|
|
|
router.ServeHTTP(recorder, req)
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
res := recorder.Result()
|
2022-02-22 18:18:05 +01:00
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if status := rr.Code; status != tc.expectedStatus {
|
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
var got *api.Peer
|
|
|
|
if tc.expectedArray {
|
|
|
|
respBody := []*api.Peer{}
|
|
|
|
err = json.Unmarshal(content, &respBody)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:45 +02:00
|
|
|
// hardcode this check for now as we only have two peers in this suite
|
|
|
|
assert.Equal(t, len(respBody), 2)
|
|
|
|
|
2024-09-22 22:44:10 +02:00
|
|
|
for _, peer := range respBody {
|
|
|
|
if peer.Id == testPeerID {
|
|
|
|
got = peer
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, peer.Connected, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
} else {
|
|
|
|
got = &api.Peer{}
|
|
|
|
err = json.Unmarshal(content, got)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
2022-02-22 18:18:05 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Log(got)
|
2023-10-11 18:11:45 +02:00
|
|
|
|
2023-02-14 10:14:00 +01:00
|
|
|
assert.Equal(t, got.Name, tc.expectedPeer.Name)
|
|
|
|
assert.Equal(t, got.Version, tc.expectedPeer.Meta.WtVersion)
|
|
|
|
assert.Equal(t, got.Ip, tc.expectedPeer.IP.String())
|
2022-06-14 10:32:54 +02:00
|
|
|
assert.Equal(t, got.Os, "OS core")
|
2023-02-14 10:14:00 +01:00
|
|
|
assert.Equal(t, got.LoginExpirationEnabled, tc.expectedPeer.LoginExpirationEnabled)
|
|
|
|
assert.Equal(t, got.SshEnabled, tc.expectedPeer.SSHEnabled)
|
2023-10-11 18:11:45 +02:00
|
|
|
assert.Equal(t, got.Connected, tc.expectedPeer.Status.Connected)
|
2024-05-06 14:49:03 +02:00
|
|
|
assert.Equal(t, got.SerialNumber, tc.expectedPeer.Meta.SystemSerialNumber)
|
2022-02-22 18:18:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-20 11:36:58 +02:00
|
|
|
|
|
|
|
func TestGetAccessiblePeers(t *testing.T) {
|
|
|
|
peer1 := &nbpeer.Peer{
|
|
|
|
ID: "peer1",
|
|
|
|
Key: "key1",
|
|
|
|
IP: net.ParseIP("100.64.0.1"),
|
|
|
|
Status: &nbpeer.PeerStatus{Connected: true},
|
|
|
|
Name: "peer1",
|
|
|
|
LoginExpirationEnabled: false,
|
|
|
|
UserID: regularUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
peer2 := &nbpeer.Peer{
|
|
|
|
ID: "peer2",
|
|
|
|
Key: "key2",
|
|
|
|
IP: net.ParseIP("100.64.0.2"),
|
|
|
|
Status: &nbpeer.PeerStatus{Connected: true},
|
|
|
|
Name: "peer2",
|
|
|
|
LoginExpirationEnabled: false,
|
|
|
|
UserID: adminUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
peer3 := &nbpeer.Peer{
|
|
|
|
ID: "peer3",
|
|
|
|
Key: "key3",
|
|
|
|
IP: net.ParseIP("100.64.0.3"),
|
|
|
|
Status: &nbpeer.PeerStatus{Connected: true},
|
|
|
|
Name: "peer3",
|
|
|
|
LoginExpirationEnabled: false,
|
|
|
|
UserID: regularUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
p := initTestMetaData(peer1, peer2, peer3)
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
peerID string
|
|
|
|
callerUserID string
|
|
|
|
expectedStatus int
|
|
|
|
expectedPeers []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "non admin user can access owned peer",
|
|
|
|
peerID: "peer1",
|
|
|
|
callerUserID: regularUser,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedPeers: []string{"peer2", "peer3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "non admin user can't access unowned peer",
|
|
|
|
peerID: "peer2",
|
|
|
|
callerUserID: regularUser,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedPeers: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "admin user can access owned peer",
|
|
|
|
peerID: "peer2",
|
|
|
|
callerUserID: adminUser,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedPeers: []string{"peer1", "peer3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "admin user can access unowned peer",
|
|
|
|
peerID: "peer3",
|
|
|
|
callerUserID: adminUser,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedPeers: []string{"peer1", "peer2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service user can access unowned peer",
|
|
|
|
peerID: "peer3",
|
|
|
|
callerUserID: serviceUser,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedPeers: []string{"peer1", "peer2"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/peers/%s/accessible-peers", tc.peerID), nil)
|
|
|
|
ctx := context.WithValue(context.Background(), userIDKey, tc.callerUserID)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/peers/{peerId}/accessible-peers", p.GetAccessiblePeers).Methods("GET")
|
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
if res.StatusCode != tc.expectedStatus {
|
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v", res.StatusCode, tc.expectedStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read response body: %v", err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var accessiblePeers []api.AccessiblePeer
|
|
|
|
err = json.Unmarshal(body, &accessiblePeers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerIDs := make([]string, len(accessiblePeers))
|
|
|
|
for i, peer := range accessiblePeers {
|
|
|
|
peerIDs[i] = peer.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, peerIDs, tc.expectedPeers)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|