mirror of
https://github.com/bitwarden/server.git
synced 2025-04-21 04:55:08 -05:00
Use reflection to test permission combinations
This commit is contained in:
parent
60cd7e6353
commit
2e8a6ddb90
@ -3,7 +3,7 @@ using Bit.Api.AdminConsole.Authorization;
|
|||||||
using Bit.Core.Context;
|
using Bit.Core.Context;
|
||||||
using Bit.Core.Entities;
|
using Bit.Core.Entities;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Models.Data;
|
using Bit.Core.Test.AdminConsole.Helpers;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using Bit.Test.Common.Helpers;
|
using Bit.Test.Common.Helpers;
|
||||||
@ -43,7 +43,7 @@ public class OrganizationClaimsExtensionsTests
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
var permissions = GetTestCustomPermissions();
|
var permissions = PermissionsHelpers.GetAllPermissions();
|
||||||
foreach (var permission in permissions)
|
foreach (var permission in permissions)
|
||||||
{
|
{
|
||||||
yield return
|
yield return
|
||||||
@ -57,20 +57,4 @@ public class OrganizationClaimsExtensionsTests
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<Permissions> GetTestCustomPermissions()
|
|
||||||
{
|
|
||||||
yield return new Permissions { AccessEventLogs = true };
|
|
||||||
yield return new Permissions { AccessImportExport = true };
|
|
||||||
yield return new Permissions { AccessReports = true };
|
|
||||||
yield return new Permissions { CreateNewCollections = true };
|
|
||||||
yield return new Permissions { EditAnyCollection = true };
|
|
||||||
yield return new Permissions { DeleteAnyCollection = true };
|
|
||||||
yield return new Permissions { ManageGroups = true };
|
|
||||||
yield return new Permissions { ManagePolicies = true };
|
|
||||||
yield return new Permissions { ManageSso = true };
|
|
||||||
yield return new Permissions { ManageUsers = true };
|
|
||||||
yield return new Permissions { ManageResetPassword = true };
|
|
||||||
yield return new Permissions { ManageScim = true };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ public class VaultExportAuthorizationHandlerTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<object[]> CanExportManagedCollections =>
|
public static IEnumerable<object[]> CanExportManagedCollections =>
|
||||||
AuthorizationHelpers.AllRoles().Select(o => new[] { o });
|
PermissionsHelpers.AllRoles().Select(o => new[] { o });
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[BitMemberAutoData(nameof(CanExportManagedCollections))]
|
[BitMemberAutoData(nameof(CanExportManagedCollections))]
|
||||||
|
@ -4,7 +4,7 @@ using Bit.Core.Models.Data;
|
|||||||
|
|
||||||
namespace Bit.Core.Test.AdminConsole.Helpers;
|
namespace Bit.Core.Test.AdminConsole.Helpers;
|
||||||
|
|
||||||
public static class AuthorizationHelpers
|
public static class PermissionsHelpers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Return a new Permission object with inverted permissions.
|
/// Return a new Permission object with inverted permissions.
|
||||||
@ -36,6 +36,24 @@ public static class AuthorizationHelpers
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a sequence of Permission objects, where each Permission object has a different permission flag set.
|
||||||
|
/// </summary>
|
||||||
|
public static IEnumerable<Permissions> GetAllPermissions()
|
||||||
|
{
|
||||||
|
// Get all boolean properties of input object
|
||||||
|
var props = typeof(Permissions)
|
||||||
|
.GetProperties()
|
||||||
|
.Where(p => p.PropertyType == typeof(bool));
|
||||||
|
|
||||||
|
foreach (var prop in props)
|
||||||
|
{
|
||||||
|
var result = new Permissions();
|
||||||
|
prop.SetValue(result, true);
|
||||||
|
yield return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a sequence of all possible roles and permissions represented as CurrentContextOrganization objects.
|
/// Returns a sequence of all possible roles and permissions represented as CurrentContextOrganization objects.
|
||||||
/// Used largely for authorization testing.
|
/// Used largely for authorization testing.
|
@ -3,7 +3,7 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Bit.Core.Test.AdminConsole.Helpers;
|
namespace Bit.Core.Test.AdminConsole.Helpers;
|
||||||
|
|
||||||
public class AuthorizationHelpersTests
|
public class PermissionsHelpersTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Permissions_Invert_InvertsAllPermissions()
|
public void Permissions_Invert_InvertsAllPermissions()
|
Loading…
x
Reference in New Issue
Block a user