2023-07-14 20:45:40 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-11-07 15:02:51 +01:00
|
|
|
"errors"
|
2023-07-14 20:45:40 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
2023-11-07 15:02:51 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2023-07-14 20:45:40 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
groupAdminUserID = "testingAdminUser"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultAccountManager_DeleteGroup(t *testing.T) {
|
|
|
|
am, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestGroupAccount(am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
groupID string
|
|
|
|
expectedReason string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"route",
|
|
|
|
"grp-for-route",
|
|
|
|
"route",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name server groups",
|
|
|
|
"grp-for-name-server-grp",
|
|
|
|
"name server groups",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"policy",
|
|
|
|
"grp-for-policies",
|
|
|
|
"policy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"setup keys",
|
|
|
|
"grp-for-keys",
|
|
|
|
"setup key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"users",
|
|
|
|
"grp-for-users",
|
|
|
|
"user",
|
|
|
|
},
|
2023-11-01 11:04:17 +01:00
|
|
|
{
|
|
|
|
"integration",
|
|
|
|
"grp-for-integration",
|
2023-11-07 15:02:51 +01:00
|
|
|
"only admins service user can delete integration group",
|
2023-11-01 11:04:17 +01:00
|
|
|
},
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
2023-11-07 15:02:51 +01:00
|
|
|
err = am.DeleteGroup(account.Id, groupAdminUserID, testCase.groupID)
|
2023-07-14 20:45:40 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("delete %s group successfully", testCase.groupID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:02:51 +01:00
|
|
|
var sErr *status.Error
|
|
|
|
if errors.As(err, &sErr) {
|
|
|
|
if sErr.Message != testCase.expectedReason {
|
|
|
|
t.Errorf("invalid error case: %s, expected: %s", sErr.Message, testCase.expectedReason)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var gErr *GroupLinkError
|
|
|
|
ok := errors.As(err, &gErr)
|
2023-07-14 20:45:40 +02:00
|
|
|
if !ok {
|
|
|
|
t.Error("invalid error type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if gErr.Resource != testCase.expectedReason {
|
|
|
|
t.Errorf("invalid error case: %s, expected: %s", gErr.Resource, testCase.expectedReason)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestGroupAccount(am *DefaultAccountManager) (*Account, error) {
|
|
|
|
accountID := "testingAcc"
|
|
|
|
domain := "example.com"
|
|
|
|
|
|
|
|
groupForRoute := &Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-route",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for route",
|
|
|
|
Issued: GroupIssuedAPI,
|
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
groupForNameServerGroups := &Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-name-server-grp",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for name server groups",
|
|
|
|
Issued: GroupIssuedAPI,
|
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
groupForPolicies := &Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-policies",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for policies",
|
|
|
|
Issued: GroupIssuedAPI,
|
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
groupForSetupKeys := &Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-keys",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for setup keys",
|
|
|
|
Issued: GroupIssuedAPI,
|
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
groupForUsers := &Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-users",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for users",
|
|
|
|
Issued: GroupIssuedAPI,
|
|
|
|
Peers: make([]string, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
groupForIntegration := &Group{
|
|
|
|
ID: "grp-for-integration",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for users",
|
|
|
|
Issued: GroupIssuedIntegration,
|
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
routeResource := &route.Route{
|
|
|
|
ID: "example route",
|
|
|
|
Groups: []string{groupForRoute.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
nameServerGroup := &nbdns.NameServerGroup{
|
|
|
|
ID: "example name server group",
|
|
|
|
Groups: []string{groupForNameServerGroups.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := &Policy{
|
|
|
|
ID: "example policy",
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "example policy rule",
|
|
|
|
Destinations: []string{groupForPolicies.ID},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := &SetupKey{
|
|
|
|
Id: "example setup key",
|
|
|
|
AutoGroups: []string{groupForSetupKeys.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
user := &User{
|
|
|
|
Id: "example user",
|
|
|
|
AutoGroups: []string{groupForUsers.ID},
|
|
|
|
}
|
|
|
|
account := newAccountWithId(accountID, groupAdminUserID, domain)
|
|
|
|
account.Routes[routeResource.ID] = routeResource
|
|
|
|
account.NameServerGroups[nameServerGroup.ID] = nameServerGroup
|
|
|
|
account.Policies = append(account.Policies, policy)
|
|
|
|
account.SetupKeys[setupKey.Id] = setupKey
|
|
|
|
account.Users[user.Id] = user
|
|
|
|
|
|
|
|
err := am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = am.SaveGroup(accountID, groupAdminUserID, groupForRoute)
|
|
|
|
_ = am.SaveGroup(accountID, groupAdminUserID, groupForNameServerGroups)
|
|
|
|
_ = am.SaveGroup(accountID, groupAdminUserID, groupForPolicies)
|
|
|
|
_ = am.SaveGroup(accountID, groupAdminUserID, groupForSetupKeys)
|
|
|
|
_ = am.SaveGroup(accountID, groupAdminUserID, groupForUsers)
|
2023-11-01 11:04:17 +01:00
|
|
|
_ = am.SaveGroup(accountID, groupAdminUserID, groupForIntegration)
|
2023-07-14 20:45:40 +02:00
|
|
|
|
|
|
|
return am.Store.GetAccount(account.Id)
|
|
|
|
}
|