Added tests for permission

pull/208/head
Halil İbrahim Kalkan 8 years ago
parent f162d2de02
commit 715164d5ac

@ -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);

@ -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);
}
}
}

@ -56,7 +56,7 @@
action: function (data) {
_permissionsModal.open({
providerName: 'User',
providerKey: data.record.name
providerKey: data.record.id
});
}
},

@ -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<IIdentityDbContext> action)
{
using (var dbContext = GetRequiredService<IIdentityDbContext>())
{
action.Invoke(dbContext);
}
}
protected virtual T UsingDbContext<T>(Func<IIdentityDbContext, T> action)
{
using (var dbContext = GetRequiredService<IIdentityDbContext>())
{
return action.Invoke(dbContext);
}
}
}
}

@ -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<IPermissionManager>();
}
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<PermissionWithGrantedProviders> 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<PermissionWithGrantedProviders> grantInfos, string permissionName)
{
grantInfos.ShouldContain(
p => p.Name == permissionName &&
!p.IsGranted &&
p.Providers.Count == 0
);
}
private static void UserShouldHavePermission(List<PermissionWithGrantedProviders> grantInfos, Guid userId, string permissionName, params string[] inheritedRolesForThisPermission)
{
grantInfos.ShouldContain(
p => p.Name == permissionName &&
p.IsGranted
);
}
}
}

Loading…
Cancel
Save