From 715164d5ac5512ffb75106230bb95b065a347942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 16 Feb 2018 15:18:18 +0300 Subject: [PATCH] Added tests for permission --- .../RolePermissionManagementProvider.cs | 2 +- .../RolePermissionManagerExtensions.cs | 4 +- .../modules/identity/views/users/index.js | 2 +- .../Abp/Identity/AbpIdentityCommonTestBase.cs | 27 ++++++- .../Abp/Identity/PermissionManager_Tests.cs | 70 ++++++++++++++++++- 5 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs index b74071f04a..b7d14dd4cd 100644 --- a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs +++ b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs @@ -41,7 +41,7 @@ namespace Volo.Abp.Identity foreach (var roleName in roleNames) { - var permissionGrant = await PermissionGrantRepository.FindAsync(name, providerName, roleName); + var permissionGrant = await PermissionGrantRepository.FindAsync(name, Name, roleName); if (permissionGrant != null) { return new PermissionValueProviderGrantInfo(true, roleName); diff --git a/src/Volo.Abp.Identity.Domain/Volo/Abp/Permissions/RolePermissionManagerExtensions.cs b/src/Volo.Abp.Identity.Domain/Volo/Abp/Permissions/RolePermissionManagerExtensions.cs index 60a97d2542..3acff7e635 100644 --- a/src/Volo.Abp.Identity.Domain/Volo/Abp/Permissions/RolePermissionManagerExtensions.cs +++ b/src/Volo.Abp.Identity.Domain/Volo/Abp/Permissions/RolePermissionManagerExtensions.cs @@ -15,11 +15,11 @@ namespace Volo.Abp.Permissions return permissionManager.GetAllAsync(RolePermissionManagementProvider.ProviderName, roleName); } - public static Task SetForUserAsync([NotNull] this IPermissionManager permissionManager, Guid userId, [NotNull] string name, bool isGranted) + public static Task SetForRoleAsync([NotNull] this IPermissionManager permissionManager, string roleName, [NotNull] string name, bool isGranted) { Check.NotNull(permissionManager, nameof(permissionManager)); - return permissionManager.SetAsync(name, UserPermissionManagementProvider.ProviderName, userId.ToString(), isGranted); + return permissionManager.SetAsync(name, RolePermissionManagementProvider.ProviderName, roleName, isGranted); } } } \ No newline at end of file diff --git a/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js b/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js index bd8d62c2c3..16ddd5bbcc 100644 --- a/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js +++ b/src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js @@ -56,7 +56,7 @@ action: function (data) { _permissionsModal.open({ providerName: 'User', - providerKey: data.record.name + providerKey: data.record.id }); } }, diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs index 92412904eb..ef3bf0fb16 100644 --- a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/AbpIdentityCommonTestBase.cs @@ -1,4 +1,7 @@ -using Volo.Abp.Modularity; +using System; +using System.Linq; +using Volo.Abp.Identity.EntityFrameworkCore; +using Volo.Abp.Modularity; using Volo.Abp.TestBase; namespace Volo.Abp.Identity @@ -10,5 +13,27 @@ namespace Volo.Abp.Identity { options.UseAutofac(); } + + + protected virtual IdentityUser GetUserAsync(string userName) + { + return UsingDbContext(context => context.Users.FirstOrDefault(u => u.UserName == userName)); + } + + protected virtual void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } } } diff --git a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs index 531ed990c8..51a2419aca 100644 --- a/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs +++ b/test/Volo.Abp.Identity.Tests/Volo/Abp/Identity/PermissionManager_Tests.cs @@ -1,5 +1,11 @@ -using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Shouldly; using Volo.Abp.Permissions; +using Volo.Abp.Session; +using Xunit; namespace Volo.Abp.Identity { @@ -12,9 +18,67 @@ namespace Volo.Abp.Identity _permissionManager = GetRequiredService(); } - public async Task Test1() + [Fact] + public async Task Roles_Should_Have_Configured_Permissions() { - + //admin + var grantInfos = await _permissionManager.GetAllForRoleAsync("admin"); + RoleShouldHavePermission(grantInfos, "admin", TestPermissionNames.MyPermission1); + RoleShouldHavePermission(grantInfos, "admin", TestPermissionNames.MyPermission2); + RoleShouldHavePermission(grantInfos, "admin", TestPermissionNames.MyPermission2_ChildPermission1); + + //moderator + grantInfos = await _permissionManager.GetAllForRoleAsync("moderator"); + RoleShouldHavePermission(grantInfos, "moderator", TestPermissionNames.MyPermission1); + RoleShouldHavePermission(grantInfos, "moderator", TestPermissionNames.MyPermission2); + ShouldNotHavePermission(grantInfos, TestPermissionNames.MyPermission2_ChildPermission1); + + //supporter + grantInfos = await _permissionManager.GetAllForRoleAsync("supporter"); + RoleShouldHavePermission(grantInfos, "supporter", TestPermissionNames.MyPermission1); + ShouldNotHavePermission(grantInfos, TestPermissionNames.MyPermission2); + ShouldNotHavePermission(grantInfos, TestPermissionNames.MyPermission2_ChildPermission1); + } + + [Fact] + public async Task User_Should_Have_Configured_Values() + { + //administrator + var user = GetUserAsync("administrator"); + var grantInfos = await _permissionManager.GetAllForUserAsync(user.Id); + UserShouldHavePermission(grantInfos, user.Id, TestPermissionNames.MyPermission1, "admin"); + UserShouldHavePermission(grantInfos, user.Id, TestPermissionNames.MyPermission2, "admin"); + UserShouldHavePermission(grantInfos, user.Id, TestPermissionNames.MyPermission2_ChildPermission1, "admin"); + } + + private static void RoleShouldHavePermission(List grantInfos, string roleName, string permissionName) + { + grantInfos.ShouldContain( + p => p.Name == permissionName && + p.IsGranted && + p.Providers.Count == 1 && + p.Providers.Any( + pr => pr.Name == RolePermissionValueProvider.ProviderName && + pr.Key == roleName + ) + ); + } + + private static void ShouldNotHavePermission(List grantInfos, string permissionName) + { + grantInfos.ShouldContain( + p => p.Name == permissionName && + !p.IsGranted && + p.Providers.Count == 0 + ); + } + + private static void UserShouldHavePermission(List grantInfos, Guid userId, string permissionName, params string[] inheritedRolesForThisPermission) + { + grantInfos.ShouldContain( + p => p.Name == permissionName && + p.IsGranted + ); } } }