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"
|
2022-02-22 18:18:05 +01:00
|
|
|
"encoding/json"
|
|
|
|
"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"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
|
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"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
const testPeerID = "test_peer"
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func initTestMetaData(peers ...*server.Peer) *PeersHandler {
|
|
|
|
return &PeersHandler{
|
2022-02-22 18:18:05 +01:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2023-02-14 10:14:00 +01:00
|
|
|
UpdatePeerFunc: func(accountID, userID string, update *server.Peer) (*server.Peer, error) {
|
|
|
|
p := peers[0].Copy()
|
|
|
|
p.SSHEnabled = update.SSHEnabled
|
|
|
|
p.LoginExpirationEnabled = update.LoginExpirationEnabled
|
|
|
|
p.Name = update.Name
|
|
|
|
return p, nil
|
|
|
|
},
|
2023-02-07 20:11:08 +01:00
|
|
|
GetPeerFunc: func(accountID, peerID, userID string) (*server.Peer, error) {
|
|
|
|
return peers[0], nil
|
|
|
|
},
|
2022-11-05 10:24:50 +01:00
|
|
|
GetPeersFunc: func(accountID, userID string) ([]*server.Peer, error) {
|
|
|
|
return peers, nil
|
|
|
|
},
|
2022-11-11 20:36:45 +01:00
|
|
|
GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) {
|
|
|
|
user := server.NewAdminUser("test_user")
|
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{
|
2023-02-07 20:11:08 +01:00
|
|
|
peers[0].ID: peers[0],
|
2022-11-05 10:24:50 +01:00
|
|
|
},
|
|
|
|
Users: map[string]*server.User{
|
2022-11-11 20:36:45 +01:00
|
|
|
"test_user": user,
|
2022-02-22 18:18:05 +01:00
|
|
|
},
|
2023-02-14 10:14:00 +01:00
|
|
|
Settings: &server.Settings{
|
|
|
|
PeerLoginExpirationEnabled: true,
|
|
|
|
PeerLoginExpiration: time.Hour,
|
|
|
|
},
|
2022-11-11 20:36:45 +01:00
|
|
|
}, user, nil
|
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 {
|
2022-02-23 20:02:02 +01:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
2022-02-22 18:18:05 +01:00
|
|
|
UserId: "test_user",
|
|
|
|
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
|
|
|
|
|
|
|
peer := &server.Peer{
|
|
|
|
ID: testPeerID,
|
|
|
|
Key: "key",
|
|
|
|
SetupKey: "setupkey",
|
|
|
|
IP: net.ParseIP("100.64.0.1"),
|
|
|
|
Status: &server.PeerStatus{},
|
|
|
|
Name: "PeerName",
|
|
|
|
LoginExpirationEnabled: false,
|
|
|
|
Meta: server.PeerSystemMeta{
|
|
|
|
Hostname: "hostname",
|
|
|
|
GoOS: "GoOS",
|
|
|
|
Kernel: "kernel",
|
|
|
|
Core: "core",
|
|
|
|
Platform: "platform",
|
|
|
|
OS: "OS",
|
|
|
|
WtVersion: "development",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedUpdatedPeer := peer.Copy()
|
|
|
|
expectedUpdatedPeer.LoginExpirationEnabled = true
|
|
|
|
expectedUpdatedPeer.SSHEnabled = true
|
|
|
|
expectedUpdatedPeer.Name = "New Name"
|
|
|
|
|
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-02-14 10:14:00 +01:00
|
|
|
expectedPeer *server.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
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetPeer",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/peers/" + testPeerID,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedArray: false,
|
2023-02-14 10:14:00 +01:00
|
|
|
expectedPeer: peer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
|
|
|
|
p := initTestMetaData(peer)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
got = respBody[0]
|
|
|
|
} 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-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)
|
2022-02-22 18:18:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|