2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-03 16:02:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-06-14 10:32:54 +02:00
|
|
|
"net"
|
2022-05-03 16:02:51 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
|
|
|
|
"github.com/magiconair/properties/assert"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
|
|
|
)
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
var TestPeers = map[string]*server.Peer{
|
2023-02-03 10:33:28 +01:00
|
|
|
"A": {Key: "A", ID: "peer-A-ID", IP: net.ParseIP("100.100.100.100")},
|
|
|
|
"B": {Key: "B", ID: "peer-B-ID", IP: net.ParseIP("200.200.200.200")},
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func initGroupTestData(user *server.User, groups ...*server.Group) *GroupsHandler {
|
|
|
|
return &GroupsHandler{
|
2022-05-03 16:02:51 +02:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2023-01-02 15:11:32 +01:00
|
|
|
SaveGroupFunc: func(accountID, userID string, group *server.Group) error {
|
2022-05-03 16:02:51 +02:00
|
|
|
if !strings.HasPrefix(group.ID, "id-") {
|
|
|
|
group.ID = "id-was-set"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetGroupFunc: func(_, groupID string) (*server.Group, error) {
|
|
|
|
if groupID != "idofthegroup" {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "not found")
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
return &server.Group{
|
|
|
|
ID: "idofthegroup",
|
|
|
|
Name: "Group",
|
|
|
|
}, nil
|
|
|
|
},
|
2022-06-14 10:32:54 +02:00
|
|
|
UpdateGroupFunc: func(_ string, groupID string, operations []server.GroupUpdateOperation) (*server.Group, error) {
|
|
|
|
var group server.Group
|
|
|
|
group.ID = groupID
|
|
|
|
for _, operation := range operations {
|
|
|
|
switch operation.Type {
|
|
|
|
case server.UpdateGroupName:
|
|
|
|
group.Name = operation.Values[0]
|
|
|
|
case server.UpdateGroupPeers, server.InsertPeersToGroup:
|
|
|
|
group.Peers = operation.Values
|
|
|
|
case server.RemovePeersFromGroup:
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("no operation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &group, nil
|
|
|
|
},
|
|
|
|
GetPeerByIPFunc: func(_ string, peerIP string) (*server.Peer, error) {
|
|
|
|
for _, peer := range TestPeers {
|
|
|
|
if peer.IP.String() == peerIP {
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("peer not found")
|
|
|
|
},
|
2022-11-11 20:36:45 +01:00
|
|
|
GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) {
|
2022-05-03 16:02:51 +02:00
|
|
|
return &server.Account{
|
|
|
|
Id: claims.AccountId,
|
|
|
|
Domain: "hotmail.com",
|
2022-06-14 10:32:54 +02:00
|
|
|
Peers: TestPeers,
|
2022-11-05 10:24:50 +01:00
|
|
|
Users: map[string]*server.User{
|
|
|
|
user.Id: user,
|
|
|
|
},
|
2022-06-14 10:32:54 +02:00
|
|
|
Groups: map[string]*server.Group{
|
2022-10-13 18:26:31 +02:00
|
|
|
"id-existed": {ID: "id-existed", Peers: []string{"A", "B"}},
|
2023-02-03 21:47:20 +01:00
|
|
|
"id-all": {ID: "id-all", Name: "All"},
|
|
|
|
},
|
2022-11-11 20:36:45 +01:00
|
|
|
}, user, nil
|
2022-05-03 16:02:51 +02:00
|
|
|
},
|
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2022-05-03 16:02:51 +02:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
|
|
|
UserId: "test_user",
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: "test_id",
|
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetGroup(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "GetGroup OK",
|
|
|
|
expectedBody: true,
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/groups/idofthegroup",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetGroup not found",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/groups/notexists",
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
group := &server.Group{
|
|
|
|
ID: "idofthegroup",
|
|
|
|
Name: "Group",
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
adminUser := server.NewAdminUser("test_user")
|
|
|
|
p := initGroupTestData(adminUser, group)
|
2022-05-03 16:02:51 +02:00
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2023-02-28 15:01:24 +01:00
|
|
|
router.HandleFunc("/api/groups/{id}", p.GetGroup).Methods("GET")
|
2022-05-03 16:02:51 +02:00
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, tc.expectedStatus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := &server.Group{}
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, got.ID, group.ID)
|
|
|
|
assert.Equal(t, got.Name, group.Name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func TestWriteGroup(t *testing.T) {
|
2022-05-03 16:02:51 +02:00
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
2022-06-14 10:32:54 +02:00
|
|
|
expectedGroup *api.Group
|
2022-05-03 16:02:51 +02:00
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
2022-06-14 10:32:54 +02:00
|
|
|
name: "Write Group POST OK",
|
2022-05-03 16:02:51 +02:00
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/groups",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{"Name":"Default POSTed Group"}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
2022-06-14 10:32:54 +02:00
|
|
|
expectedGroup: &api.Group{
|
|
|
|
Id: "id-was-set",
|
2022-05-03 16:02:51 +02:00
|
|
|
Name: "Default POSTed Group",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-14 10:32:54 +02:00
|
|
|
name: "Write Group POST Invalid Name",
|
|
|
|
requestType: http.MethodPost,
|
2022-05-03 16:02:51 +02:00
|
|
|
requestPath: "/api/groups",
|
|
|
|
requestBody: bytes.NewBuffer(
|
2022-06-14 10:32:54 +02:00
|
|
|
[]byte(`{"name":""}`)),
|
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Write Group PUT OK",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/groups/id-existed",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{"Name":"Default POSTed Group"}`)),
|
2022-05-03 16:02:51 +02:00
|
|
|
expectedStatus: http.StatusOK,
|
2022-06-14 10:32:54 +02:00
|
|
|
expectedGroup: &api.Group{
|
|
|
|
Id: "id-existed",
|
2022-05-03 16:02:51 +02:00
|
|
|
Name: "Default POSTed Group",
|
|
|
|
},
|
|
|
|
},
|
2022-06-14 10:32:54 +02:00
|
|
|
{
|
|
|
|
name: "Write Group PUT Invalid Name",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/groups/id-existed",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{"Name":""}`)),
|
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Write Group PUT All Group Name",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/groups/id-all",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{"Name":"super"}`)),
|
2022-11-11 20:36:45 +01:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2022-06-14 10:32:54 +02:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Write Group PATCH Name OK",
|
|
|
|
requestType: http.MethodPatch,
|
|
|
|
requestPath: "/api/groups/id-existed",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`[{"op":"replace","path":"name","value":["Default POSTed Group"]}]`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedGroup: &api.Group{
|
|
|
|
Id: "id-existed",
|
|
|
|
Name: "Default POSTed Group",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Write Group PATCH Invalid Name OP",
|
|
|
|
requestType: http.MethodPatch,
|
|
|
|
requestPath: "/api/groups/id-existed",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`[{"op":"insert","path":"name","value":[""]}]`)),
|
2022-11-11 20:36:45 +01:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2022-06-14 10:32:54 +02:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Write Group PATCH Invalid Name",
|
|
|
|
requestType: http.MethodPatch,
|
|
|
|
requestPath: "/api/groups/id-existed",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`[{"op":"replace","path":"name","value":[]}]`)),
|
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
2023-02-28 15:01:24 +01:00
|
|
|
name: "Write Group PATCH PeersHandler OK",
|
2022-06-14 10:32:54 +02:00
|
|
|
requestType: http.MethodPatch,
|
|
|
|
requestPath: "/api/groups/id-existed",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`[{"op":"replace","path":"peers","value":["100.100.100.100","200.200.200.200"]}]`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedGroup: &api.Group{
|
|
|
|
Id: "id-existed",
|
|
|
|
PeersCount: 2,
|
|
|
|
Peers: []api.PeerMinimum{
|
2023-02-03 10:33:28 +01:00
|
|
|
{Id: "peer-A-ID"},
|
2023-02-03 21:47:20 +01:00
|
|
|
{Id: "peer-B-ID"},
|
|
|
|
},
|
2022-06-14 10:32:54 +02:00
|
|
|
},
|
|
|
|
},
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
adminUser := server.NewAdminUser("test_user")
|
|
|
|
p := initGroupTestData(adminUser)
|
2022-05-03 16:02:51 +02:00
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2023-02-28 15:01:24 +01:00
|
|
|
router.HandleFunc("/api/groups", p.CreateGroup).Methods("POST")
|
|
|
|
router.HandleFunc("/api/groups/{id}", p.UpdateGroup).Methods("PUT")
|
|
|
|
router.HandleFunc("/api/groups/{id}", p.PatchGroup).Methods("PATCH")
|
2022-05-03 16:02:51 +02:00
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v, content: %s",
|
|
|
|
status, tc.expectedStatus, string(content))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
got := &api.Group{}
|
2022-05-03 16:02:51 +02:00
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, got, tc.expectedGroup)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|