2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-02-22 18:18:05 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2022-02-22 18:18:05 +01:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
|
2022-02-22 18:18:05 +01:00
|
|
|
"github.com/magiconair/properties/assert"
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
2022-02-22 18:18:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func initTestMetaData(peer ...*server.Peer) *Peers {
|
|
|
|
return &Peers{
|
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2022-10-13 18:26:31 +02:00
|
|
|
GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, error) {
|
2022-02-22 18:18:05 +01:00
|
|
|
return &server.Account{
|
2022-03-01 15:22:18 +01:00
|
|
|
Id: claims.AccountId,
|
2022-02-22 18:18:05 +01:00
|
|
|
Domain: "hotmail.com",
|
|
|
|
Peers: map[string]*server.Peer{
|
|
|
|
"test_peer": peer[0],
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
authAudience: "",
|
2022-02-23 20:02:02 +01:00
|
|
|
jwtExtractor: jwtclaims.ClaimsExtractor{
|
|
|
|
ExtractClaimsFromRequestContext: func(r *http.Request, authAudiance string) jwtclaims.AuthorizationClaims {
|
|
|
|
return jwtclaims.AuthorizationClaims{
|
2022-02-22 18:18:05 +01:00
|
|
|
UserId: "test_user",
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: "test_id",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the GetPeers endpoint reachable in the route /api/peers
|
|
|
|
// Use the metadata generated by initTestMetaData() to check for values
|
|
|
|
func TestGetPeers(t *testing.T) {
|
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
|
|
|
|
}{
|
2022-05-03 16:02:51 +02:00
|
|
|
{
|
|
|
|
name: "GetPeersMetaData",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/peers/",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
2022-02-22 18:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
peer := &server.Peer{
|
|
|
|
Key: "key",
|
|
|
|
SetupKey: "setupkey",
|
|
|
|
IP: net.ParseIP("100.64.0.1"),
|
|
|
|
Status: &server.PeerStatus{},
|
|
|
|
Name: "PeerName",
|
|
|
|
Meta: server.PeerSystemMeta{
|
|
|
|
Hostname: "hostname",
|
|
|
|
GoOS: "GoOS",
|
|
|
|
Kernel: "kernel",
|
|
|
|
Core: "core",
|
|
|
|
Platform: "platform",
|
|
|
|
OS: "OS",
|
|
|
|
WtVersion: "development",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p := initTestMetaData(peer)
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
|
|
|
|
|
|
|
p.GetPeers(rr, req)
|
|
|
|
|
|
|
|
res := rr.Result()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
respBody := []*api.Peer{}
|
2022-02-22 18:18:05 +01:00
|
|
|
err = json.Unmarshal(content, &respBody)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := respBody[0]
|
|
|
|
assert.Equal(t, got.Name, peer.Name)
|
|
|
|
assert.Equal(t, got.Version, peer.Meta.WtVersion)
|
2022-06-14 10:32:54 +02:00
|
|
|
assert.Equal(t, got.Ip, peer.IP.String())
|
|
|
|
assert.Equal(t, got.Os, "OS core")
|
2022-02-22 18:18:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|