forked from netbirdio/netbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check links of groups before delete it (netbirdio#1010)
* Check links of groups before delete it * Add delete group handler test * Rename dns error msg * Add delete group test * Remove rule check The policy cover this scenario * Fix test * Check disabled management grps * Change error message * Add new activity for group delete event
- Loading branch information
Showing
9 changed files
with
368 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
nbdns "github.com/netbirdio/netbird/dns" | ||
"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", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
err = am.DeleteGroup(account.Id, "", testCase.groupID) | ||
if err == nil { | ||
t.Errorf("delete %s group successfully", testCase.groupID) | ||
return | ||
} | ||
|
||
gErr, ok := err.(*GroupLinkError) | ||
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{ | ||
"grp-for-route", | ||
"Group for route", | ||
GroupIssuedAPI, | ||
make([]string, 0), | ||
} | ||
|
||
groupForNameServerGroups := &Group{ | ||
"grp-for-name-server-grp", | ||
"Group for name server groups", | ||
GroupIssuedAPI, | ||
make([]string, 0), | ||
} | ||
|
||
groupForPolicies := &Group{ | ||
"grp-for-policies", | ||
"Group for policies", | ||
GroupIssuedAPI, | ||
make([]string, 0), | ||
} | ||
|
||
groupForSetupKeys := &Group{ | ||
"grp-for-keys", | ||
"Group for setup keys", | ||
GroupIssuedAPI, | ||
make([]string, 0), | ||
} | ||
|
||
groupForUsers := &Group{ | ||
"grp-for-users", | ||
"Group for users", | ||
GroupIssuedAPI, | ||
make([]string, 0), | ||
} | ||
|
||
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) | ||
|
||
return am.Store.GetAccount(account.Id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.