fix sanity tests when no non-owner roles exist (#3105)
A prior patch to sanity testing allowed an empty permissionInfo struct to get added to the slice instead of being dropped, if permissions only contained the "owner" value. This cascaded to a failed test. Also includes some other minor cleanup. --- #### Does this PR need a docs update or release note? - [x] ⛔ No #### Type of change - [x] 🐛 Bugfix - [x] 🤖 Supportability/Tests #### Issue(s) * #3081 #### Test Plan - [x] 💚 E2E
This commit is contained in:
parent
51b3f25311
commit
d393bb09a7
@ -23,11 +23,23 @@ import (
|
|||||||
"github.com/alcionai/corso/src/pkg/logger"
|
"github.com/alcionai/corso/src/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// types, consts, etc
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
type permissionInfo struct {
|
type permissionInfo struct {
|
||||||
entityID string
|
entityID string
|
||||||
roles []string
|
roles []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
owner = "owner"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// main
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, log := logger.Seed(context.Background(), "info", logger.GetLogFile(""))
|
ctx, log := logger.Seed(context.Background(), "info", logger.GetLogFile(""))
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -71,6 +83,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// exchange
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
// checkEmailRestoration verifies that the emails count in restored folder is equivalent to
|
// checkEmailRestoration verifies that the emails count in restored folder is equivalent to
|
||||||
// emails in actual m365 account
|
// emails in actual m365 account
|
||||||
func checkEmailRestoration(
|
func checkEmailRestoration(
|
||||||
@ -104,7 +120,7 @@ func checkEmailRestoration(
|
|||||||
|
|
||||||
if itemName == dataFolder || itemName == baseBackupFolder {
|
if itemName == dataFolder || itemName == baseBackupFolder {
|
||||||
// otherwise, recursively aggregate all child folders.
|
// otherwise, recursively aggregate all child folders.
|
||||||
getAllSubFolder(ctx, client, testUser, v, itemName, dataFolder, itemCount)
|
getAllMailSubFolders(ctx, client, testUser, v, itemName, dataFolder, itemCount)
|
||||||
|
|
||||||
itemCount[itemName] = ptr.Val(v.GetTotalItemCount())
|
itemCount[itemName] = ptr.Val(v.GetTotalItemCount())
|
||||||
}
|
}
|
||||||
@ -170,7 +186,7 @@ func verifyEmailData(ctx context.Context, restoreMessageCount, messageCount map[
|
|||||||
|
|
||||||
// getAllSubFolder will recursively check for all subfolders and get the corresponding
|
// getAllSubFolder will recursively check for all subfolders and get the corresponding
|
||||||
// email count.
|
// email count.
|
||||||
func getAllSubFolder(
|
func getAllMailSubFolders(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
client *msgraphsdk.GraphServiceClient,
|
client *msgraphsdk.GraphServiceClient,
|
||||||
testUser string,
|
testUser string,
|
||||||
@ -214,7 +230,7 @@ func getAllSubFolder(
|
|||||||
if childFolderCount > 0 {
|
if childFolderCount > 0 {
|
||||||
parentFolder := fullFolderName
|
parentFolder := fullFolderName
|
||||||
|
|
||||||
getAllSubFolder(ctx, client, testUser, child, parentFolder, dataFolder, messageCount)
|
getAllMailSubFolders(ctx, client, testUser, child, parentFolder, dataFolder, messageCount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -271,6 +287,10 @@ func checkAllSubFolder(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// oneDrive
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
func checkOnedriveRestoration(
|
func checkOnedriveRestoration(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
client *msgraphsdk.GraphServiceClient,
|
client *msgraphsdk.GraphServiceClient,
|
||||||
@ -347,21 +367,21 @@ func checkOnedriveRestoration(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
permissionIn(ctx, client, driveID, itemID, itemName, folderPermission)
|
folderPermission[itemName] = permissionIn(ctx, client, driveID, itemID)
|
||||||
getOneDriveChildFolder(ctx, client, driveID, itemID, itemName, fileSizes, folderPermission, startTime)
|
getOneDriveChildFolder(ctx, client, driveID, itemID, itemName, fileSizes, folderPermission, startTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
getRestoreData(ctx, client, *drive.GetId(), restoreFolderID, restoreFile, restoreFolderPermission, startTime)
|
getRestoredDrive(ctx, client, *drive.GetId(), restoreFolderID, restoreFile, restoreFolderPermission, startTime)
|
||||||
|
|
||||||
for folderName, permissions := range folderPermission {
|
for folderName, permissions := range folderPermission {
|
||||||
logger.Ctx(ctx).Info("checking for folder: ", folderName, "\n")
|
logger.Ctx(ctx).Info("checking for folder: ", folderName)
|
||||||
fmt.Printf("checking for folder: %s\n", folderName)
|
fmt.Printf("checking for folder: %s\n", folderName)
|
||||||
|
|
||||||
restoreFolderPerm := restoreFolderPermission[folderName]
|
restoreFolderPerm := restoreFolderPermission[folderName]
|
||||||
|
|
||||||
if len(permissions) < 1 {
|
if len(permissions) < 1 {
|
||||||
logger.Ctx(ctx).Info("no permissions found for folder :", folderName)
|
logger.Ctx(ctx).Info("no permissions found in :", folderName)
|
||||||
fmt.Println("no permissions found for folder :", folderName)
|
fmt.Println("no permissions found in :", folderName)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -460,60 +480,12 @@ func getOneDriveChildFolder(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
permissionIn(ctx, client, driveID, itemID, fullName, folderPermission)
|
folderPermission[fullName] = permissionIn(ctx, client, driveID, itemID)
|
||||||
getOneDriveChildFolder(ctx, client, driveID, itemID, fullName, fileSizes, folderPermission, startTime)
|
getOneDriveChildFolder(ctx, client, driveID, itemID, fullName, fileSizes, folderPermission, startTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func permissionIn(
|
func getRestoredDrive(
|
||||||
ctx context.Context,
|
|
||||||
client *msgraphsdk.GraphServiceClient,
|
|
||||||
driveID, itemID, folderName string,
|
|
||||||
permMap map[string][]permissionInfo,
|
|
||||||
) {
|
|
||||||
permMap[folderName] = []permissionInfo{}
|
|
||||||
|
|
||||||
pcr, err := client.
|
|
||||||
DrivesById(driveID).
|
|
||||||
ItemsById(itemID).
|
|
||||||
Permissions().
|
|
||||||
Get(ctx, nil)
|
|
||||||
if err != nil {
|
|
||||||
fatal(ctx, "getting permission", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, perm := range pcr.GetValue() {
|
|
||||||
if perm.GetGrantedToV2() == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
gv2 = perm.GetGrantedToV2()
|
|
||||||
perInfo = permissionInfo{}
|
|
||||||
entityID string
|
|
||||||
)
|
|
||||||
|
|
||||||
if gv2.GetUser() != nil {
|
|
||||||
entityID = ptr.Val(gv2.GetUser().GetId())
|
|
||||||
} else if gv2.GetGroup() != nil {
|
|
||||||
entityID = ptr.Val(gv2.GetGroup().GetId())
|
|
||||||
}
|
|
||||||
|
|
||||||
roles := perm.GetRoles()
|
|
||||||
for _, role := range roles {
|
|
||||||
if role != "owner" {
|
|
||||||
perInfo.entityID = entityID
|
|
||||||
perInfo.roles = append(perInfo.roles, role)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
slices.Sort(perInfo.roles)
|
|
||||||
|
|
||||||
permMap[folderName] = append(permMap[folderName], perInfo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRestoreData(
|
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
client *msgraphsdk.GraphServiceClient,
|
client *msgraphsdk.GraphServiceClient,
|
||||||
driveID, restoreFolderID string,
|
driveID, restoreFolderID string,
|
||||||
@ -546,11 +518,63 @@ func getRestoreData(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
permissionIn(ctx, client, driveID, itemID, itemName, restoreFolder)
|
restoreFolder[itemName] = permissionIn(ctx, client, driveID, itemID)
|
||||||
getOneDriveChildFolder(ctx, client, driveID, itemID, itemName, restoreFile, restoreFolder, startTime)
|
getOneDriveChildFolder(ctx, client, driveID, itemID, itemName, restoreFile, restoreFolder, startTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// permission helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func permissionIn(
|
||||||
|
ctx context.Context,
|
||||||
|
client *msgraphsdk.GraphServiceClient,
|
||||||
|
driveID, itemID string,
|
||||||
|
) []permissionInfo {
|
||||||
|
pi := []permissionInfo{}
|
||||||
|
|
||||||
|
pcr, err := client.
|
||||||
|
DrivesById(driveID).
|
||||||
|
ItemsById(itemID).
|
||||||
|
Permissions().
|
||||||
|
Get(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
fatal(ctx, "getting permission", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, perm := range pcr.GetValue() {
|
||||||
|
if perm.GetGrantedToV2() == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
gv2 = perm.GetGrantedToV2()
|
||||||
|
permInfo = permissionInfo{}
|
||||||
|
entityID string
|
||||||
|
)
|
||||||
|
|
||||||
|
if gv2.GetUser() != nil {
|
||||||
|
entityID = ptr.Val(gv2.GetUser().GetId())
|
||||||
|
} else if gv2.GetGroup() != nil {
|
||||||
|
entityID = ptr.Val(gv2.GetGroup().GetId())
|
||||||
|
}
|
||||||
|
|
||||||
|
roles := filterSlice(perm.GetRoles(), owner)
|
||||||
|
for _, role := range roles {
|
||||||
|
permInfo.entityID = entityID
|
||||||
|
permInfo.roles = append(permInfo.roles, role)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(roles) > 0 {
|
||||||
|
slices.Sort(permInfo.roles)
|
||||||
|
pi = append(pi, permInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pi
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Helpers
|
// Helpers
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@ -583,3 +607,15 @@ func isWithinTimeBound(ctx context.Context, bound, check time.Time, hasTime bool
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func filterSlice(sl []string, remove string) []string {
|
||||||
|
r := []string{}
|
||||||
|
|
||||||
|
for _, s := range sl {
|
||||||
|
if !strings.EqualFold(s, remove) {
|
||||||
|
r = append(r, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user