mirror of
https://github.com/bitwarden/server.git
synced 2025-04-12 00:28:11 -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.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Test.AdminConsole.Helpers;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Bit.Test.Common.Helpers;
|
||||
@ -43,7 +43,7 @@ public class OrganizationClaimsExtensionsTests
|
||||
];
|
||||
}
|
||||
|
||||
var permissions = GetTestCustomPermissions();
|
||||
var permissions = PermissionsHelpers.GetAllPermissions();
|
||||
foreach (var permission in permissions)
|
||||
{
|
||||
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 =>
|
||||
AuthorizationHelpers.AllRoles().Select(o => new[] { o });
|
||||
PermissionsHelpers.AllRoles().Select(o => new[] { o });
|
||||
|
||||
[Theory]
|
||||
[BitMemberAutoData(nameof(CanExportManagedCollections))]
|
||||
|
@ -4,7 +4,7 @@ using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Test.AdminConsole.Helpers;
|
||||
|
||||
public static class AuthorizationHelpers
|
||||
public static class PermissionsHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Return a new Permission object with inverted permissions.
|
||||
@ -36,6 +36,24 @@ public static class AuthorizationHelpers
|
||||
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>
|
||||
/// Returns a sequence of all possible roles and permissions represented as CurrentContextOrganization objects.
|
||||
/// Used largely for authorization testing.
|
@ -3,7 +3,7 @@ using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.AdminConsole.Helpers;
|
||||
|
||||
public class AuthorizationHelpersTests
|
||||
public class PermissionsHelpersTests
|
||||
{
|
||||
[Fact]
|
||||
public void Permissions_Invert_InvertsAllPermissions()
|
Loading…
x
Reference in New Issue
Block a user