Files
EvoBGP/internal/httpapi/peers_discovery_test.go
T
Denozordec 5fbe9c9b56
CI / changes (push) Successful in 6s
CI / commitlint (push) Skipped
CI / openapi (push) Successful in 28s
CI / web (push) Successful in 52s
CI / go (push) Successful in 2m22s
CI / bird2 (push) Successful in 15s
CI / release (push) Successful in 4m36s
feat(network): implement peer discovery features and UI integration
Added functionality for peer discovery, including new API endpoints for listing, approving, and rejecting discovered peers. Updated the network queries and settings to support peer discovery configurations. Enhanced the UI to display discovered peers and integrated related settings in the tenant settings component. Updated OpenAPI documentation to reflect the new endpoints and parameters. This improves the network management capabilities by allowing dynamic peer discovery and management.
2026-07-30 22:31:59 +07:00

114 lines
3.2 KiB
Go

package httpapi
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"evobgp/internal/store"
)
func TestPeerDiscoveryApproveReject(t *testing.T) {
srv, err := New(Options{InsecureDev: true, SeedDemo: true, BundleSeedHex: testBundleSeed})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
tenant, _, _, _, speaker := srv.Store().DemoIDs()
mustSetTestAPIKeys(t, srv, "edkey|"+tenant+"|operator")
h := srv.Handler()
disc, err := srv.Store().UpsertPeerDiscovery(tenant, &store.PeerDiscoveryUpsert{
SpeakerID: speaker,
NeighborID: "203.0.113.10",
Neighbor: "203.0.113.10",
RemoteASN: 65099,
ProtocolName: "evobgp_dyn_0001",
SessionState: "Established",
SeenAt: time.Now().UTC(),
})
if err != nil {
t.Fatal(err)
}
listReq := httptest.NewRequest(http.MethodGet, "/v1/peers/discovered?status=pending", nil)
listReq.Header.Set("Authorization", "Bearer edkey")
listRec := httptest.NewRecorder()
h.ServeHTTP(listRec, listReq)
if listRec.Code != http.StatusOK {
t.Fatalf("list status %d body %s", listRec.Code, listRec.Body.String())
}
approveBody := `{"name":"client-a"}`
approveReq := httptest.NewRequest(http.MethodPost, "/v1/peers/discovered/"+disc.ID+"/approve", strings.NewReader(approveBody))
approveReq.Header.Set("Authorization", "Bearer edkey")
approveReq.Header.Set("Content-Type", "application/json")
approveRec := httptest.NewRecorder()
h.ServeHTTP(approveRec, approveReq)
if approveRec.Code != http.StatusOK {
t.Fatalf("approve status %d body %s", approveRec.Code, approveRec.Body.String())
}
var approveOut struct {
Peer struct {
ID string `json:"id"`
Neighbor string `json:"neighbor"`
Name string `json:"name"`
} `json:"peer"`
}
if err := json.Unmarshal(approveRec.Body.Bytes(), &approveOut); err != nil {
t.Fatal(err)
}
if approveOut.Peer.Neighbor != "203.0.113.10" || approveOut.Peer.Name != "client-a" {
t.Fatalf("unexpected peer: %+v", approveOut.Peer)
}
peers := srv.Store().ListPeers(tenant)
found := false
for _, p := range peers {
if p.ID == approveOut.Peer.ID {
found = true
break
}
}
if !found {
t.Fatal("approved peer not in ListPeers")
}
disc2, err := srv.Store().UpsertPeerDiscovery(tenant, &store.PeerDiscoveryUpsert{
SpeakerID: speaker,
NeighborID: "198.51.100.99",
Neighbor: "198.51.100.99",
RemoteASN: 65100,
ProtocolName: "evobgp_dyn_0002",
SessionState: "Active",
SeenAt: time.Now().UTC(),
})
if err != nil {
t.Fatal(err)
}
rejReq := httptest.NewRequest(http.MethodPost, "/v1/peers/discovered/"+disc2.ID+"/reject", nil)
rejReq.Header.Set("Authorization", "Bearer edkey")
rejRec := httptest.NewRecorder()
h.ServeHTTP(rejRec, rejReq)
if rejRec.Code != http.StatusOK {
t.Fatalf("reject status %d body %s", rejRec.Code, rejRec.Body.String())
}
again, err := srv.Store().UpsertPeerDiscovery(tenant, &store.PeerDiscoveryUpsert{
NeighborID: "198.51.100.99",
Neighbor: "198.51.100.99",
RemoteASN: 65100,
ProtocolName: "evobgp_dyn_0002",
SessionState: "Established",
SeenAt: time.Now().UTC(),
})
if err != nil {
t.Fatal(err)
}
if again.Status != store.PeerDiscoveryRejected {
t.Fatalf("expected rejected, got %s", again.Status)
}
}