mirror of https://github.com/abpframework/abp.git
committed by
GitHub
13 changed files with 535 additions and 4 deletions
@ -0,0 +1,18 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp2.2</TargetFramework> |
||||
|
|
||||
|
<IsPackable>false</IsPackable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.PermissionManagement.Application\Volo.Abp.PermissionManagement.Application.csproj" /> |
||||
|
<ProjectReference Include="..\Volo.Abp.PermissionManagement.Tests\Volo.Abp.PermissionManagement.Tests.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using NSubstitute; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class AbpPermissionManagementApplicationTestBase : PermissionManagementTestBase<AbpPermissionManagementApplicationTestModule> |
||||
|
{ |
||||
|
protected Guid? CurrentUserId { get; set; } |
||||
|
|
||||
|
protected AbpPermissionManagementApplicationTestBase() |
||||
|
{ |
||||
|
CurrentUserId = Guid.NewGuid(); |
||||
|
} |
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
var currentUser = Substitute.For<ICurrentUser>(); |
||||
|
//currentUser.Id.Returns(ci => CurrentUserId);
|
||||
|
currentUser.IsAuthenticated.Returns(true); |
||||
|
|
||||
|
services.AddSingleton(currentUser); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpPermissionManagementApplicationModule), |
||||
|
typeof(AbpPermissionManagementTestModule) |
||||
|
)] |
||||
|
public class AbpPermissionManagementApplicationTestModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
context.Services.AddAlwaysAllowAuthorization(); |
||||
|
|
||||
|
context.Services.Configure<PermissionManagementOptions>(options => |
||||
|
{ |
||||
|
options.ProviderPolicies[UserPermissionValueProvider.ProviderName] = UserPermissionValueProvider.ProviderName; |
||||
|
options.ProviderPolicies["Test"] = "Test"; |
||||
|
options.ManagementProviders.Add<TestPermissionManagementProvider>(); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,96 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement.Application.Tests.Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class PermissionAppService_Tests : AbpPermissionManagementApplicationTestBase |
||||
|
{ |
||||
|
private readonly IPermissionAppService _permissionAppService; |
||||
|
private readonly IPermissionGrantRepository _permissionGrantRepository; |
||||
|
|
||||
|
public PermissionAppService_Tests() |
||||
|
{ |
||||
|
_permissionAppService = GetRequiredService<IPermissionAppService>(); |
||||
|
_permissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetAsync() |
||||
|
{ |
||||
|
var permissionListResultDto = await _permissionAppService.GetAsync(UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()); |
||||
|
|
||||
|
permissionListResultDto.ShouldNotBeNull(); |
||||
|
permissionListResultDto.EntityDisplayName.ShouldBe(PermissionTestDataBuilder.User1Id.ToString()); |
||||
|
permissionListResultDto.Groups.Count.ShouldBe(1); |
||||
|
permissionListResultDto.Groups.ShouldContain(x => x.Name == "TestGroup"); |
||||
|
|
||||
|
permissionListResultDto.Groups.First().Permissions.Count.ShouldBe(4); |
||||
|
permissionListResultDto.Groups.First().Permissions.ShouldContain(x => x.Name == "MyPermission1"); |
||||
|
permissionListResultDto.Groups.First().Permissions.ShouldContain(x => x.Name == "MyPermission2"); |
||||
|
permissionListResultDto.Groups.First().Permissions.ShouldContain(x => x.Name == "MyPermission2.ChildPermission1"); |
||||
|
permissionListResultDto.Groups.First().Permissions.ShouldContain(x => x.Name == "MyPermission3"); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdateAsync() |
||||
|
{ |
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission1", "Test", |
||||
|
"Test")).ShouldBeNull(); |
||||
|
|
||||
|
await _permissionAppService.UpdateAsync("Test", |
||||
|
"Test", new UpdatePermissionsDto() |
||||
|
{ |
||||
|
Permissions = new UpdatePermissionDto[] |
||||
|
{ |
||||
|
new UpdatePermissionDto() |
||||
|
{ |
||||
|
IsGranted = true, |
||||
|
Name = "MyPermission1" |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission1", "Test", |
||||
|
"Test")).ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Update_Revoke_Test() |
||||
|
{ |
||||
|
_permissionGrantRepository.Insert( |
||||
|
new PermissionGrant( |
||||
|
Guid.NewGuid(), |
||||
|
"MyPermission1", |
||||
|
"Test", |
||||
|
"Test" |
||||
|
) |
||||
|
); |
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission1", "Test", |
||||
|
"Test")).ShouldNotBeNull(); |
||||
|
|
||||
|
await _permissionAppService.UpdateAsync("Test", |
||||
|
"Test", new UpdatePermissionsDto() |
||||
|
{ |
||||
|
Permissions = new UpdatePermissionDto[] |
||||
|
{ |
||||
|
new UpdatePermissionDto() |
||||
|
{ |
||||
|
IsGranted = false, |
||||
|
Name = "MyPermission1" |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission1", "Test", |
||||
|
"Test")).ShouldBeNull(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using Volo.Abp.Guids; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class TestPermissionManagementProvider : PermissionManagementProvider |
||||
|
{ |
||||
|
public override string Name => "Test"; |
||||
|
|
||||
|
public TestPermissionManagementProvider( |
||||
|
IPermissionGrantRepository permissionGrantRepository, |
||||
|
IGuidGenerator guidGenerator, |
||||
|
ICurrentTenant currentTenant) |
||||
|
: base( |
||||
|
permissionGrantRepository, |
||||
|
guidGenerator, |
||||
|
currentTenant) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class PermissionDataSeeder_Tests : PermissionTestBase |
||||
|
{ |
||||
|
private readonly IPermissionDataSeeder _permissionDataSeeder; |
||||
|
private readonly IPermissionGrantRepository _grantpermissionGrantRepository; |
||||
|
|
||||
|
public PermissionDataSeeder_Tests() |
||||
|
{ |
||||
|
_permissionDataSeeder = GetRequiredService<IPermissionDataSeeder>(); |
||||
|
_grantpermissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SeedAsync() |
||||
|
{ |
||||
|
(await _grantpermissionGrantRepository.FindAsync("MyPermission1", "Test", "Test")).ShouldBeNull(); |
||||
|
(await _grantpermissionGrantRepository.FindAsync("MyPermission2", "Test", "Test")).ShouldBeNull(); |
||||
|
|
||||
|
await _permissionDataSeeder.SeedAsync("Test", "Test", new List<string>() |
||||
|
{ |
||||
|
"MyPermission1", |
||||
|
"MyPermission2" |
||||
|
}); |
||||
|
|
||||
|
(await _grantpermissionGrantRepository.FindAsync("MyPermission1", "Test", "Test")).ShouldNotBeNull(); |
||||
|
(await _grantpermissionGrantRepository.FindAsync("MyPermission2", "Test", "Test")).ShouldNotBeNull(); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Caching; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class PermissionGrantCacheItemInvalidator_Tests : PermissionTestBase |
||||
|
{ |
||||
|
private readonly IDistributedCache<PermissionGrantCacheItem> _cache; |
||||
|
private readonly IPermissionStore _permissionStore; |
||||
|
private readonly IPermissionGrantRepository _permissionGrantRepository; |
||||
|
|
||||
|
public PermissionGrantCacheItemInvalidator_Tests() |
||||
|
{ |
||||
|
_cache = GetRequiredService<IDistributedCache<PermissionGrantCacheItem>>(); |
||||
|
_permissionStore = GetRequiredService<IPermissionStore>(); |
||||
|
_permissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task PermissionStore_IsGrantedAsync_Should_Cache_PermissionGrant() |
||||
|
{ |
||||
|
(await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()))).ShouldBeNull(); |
||||
|
|
||||
|
|
||||
|
await _permissionStore.IsGrantedAsync("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()); |
||||
|
|
||||
|
|
||||
|
(await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()))).ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Cache_Should_Invalidator_WhenPermissionGrantChanged() |
||||
|
{ |
||||
|
// IsGrantedAsync will cache PermissionGrant
|
||||
|
await _permissionStore.IsGrantedAsync("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()); |
||||
|
|
||||
|
var permissionGrant = await _permissionGrantRepository.FindAsync("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()); |
||||
|
permissionGrant.ShouldNotBeNull(); |
||||
|
await _permissionGrantRepository.DeleteAsync(permissionGrant); |
||||
|
|
||||
|
(await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString()))).ShouldBeNull(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class PermissionManagementProvider_Tests : PermissionTestBase |
||||
|
{ |
||||
|
private readonly IPermissionManagementProvider _permissionManagementProvider; |
||||
|
private readonly IPermissionGrantRepository _permissionGrantRepository; |
||||
|
|
||||
|
|
||||
|
public PermissionManagementProvider_Tests() |
||||
|
{ |
||||
|
_permissionManagementProvider = GetRequiredService<TestPermissionManagementProvider>(); |
||||
|
_permissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public async Task CheckAsync() |
||||
|
{ |
||||
|
_permissionGrantRepository.Insert( |
||||
|
new PermissionGrant( |
||||
|
Guid.NewGuid(), |
||||
|
"MyPermission1", |
||||
|
"Test", |
||||
|
"Test" |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
var permissionValueProviderGrantInfo = await _permissionManagementProvider.CheckAsync("MyPermission1", |
||||
|
"Test", |
||||
|
"Test"); |
||||
|
|
||||
|
permissionValueProviderGrantInfo.IsGranted.ShouldBeTrue(); |
||||
|
permissionValueProviderGrantInfo.ProviderKey.ShouldBe("Test"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Check_Should_Return_NonGranted_When_ProviderName_NotEquals_Name() |
||||
|
{ |
||||
|
var permissionValueProviderGrantInfo = await _permissionManagementProvider.CheckAsync("MyPermission1", |
||||
|
"TestNotExist", |
||||
|
"Test"); |
||||
|
|
||||
|
permissionValueProviderGrantInfo.IsGranted.ShouldBeFalse(); |
||||
|
permissionValueProviderGrantInfo.ProviderKey.ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SetAsync() |
||||
|
{ |
||||
|
_permissionGrantRepository.Insert( |
||||
|
new PermissionGrant( |
||||
|
Guid.NewGuid(), |
||||
|
"MyPermission1", |
||||
|
"Test", |
||||
|
"Test" |
||||
|
) |
||||
|
); |
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission1", |
||||
|
"Test", |
||||
|
"Test")).ShouldNotBeNull(); |
||||
|
|
||||
|
await _permissionManagementProvider.SetAsync("MyPermission1", |
||||
|
"Test", |
||||
|
false); |
||||
|
|
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission1", |
||||
|
"Test", |
||||
|
"Test")).ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,108 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class PermissionManager_Tests : PermissionTestBase |
||||
|
{ |
||||
|
private readonly IPermissionManager _permissionManager; |
||||
|
private readonly IPermissionGrantRepository _permissionGrantRepository; |
||||
|
|
||||
|
public PermissionManager_Tests() |
||||
|
{ |
||||
|
_permissionManager = GetRequiredService<IPermissionManager>(); |
||||
|
_permissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetAsync() |
||||
|
{ |
||||
|
await _permissionGrantRepository.InsertAsync(new PermissionGrant( |
||||
|
Guid.NewGuid(), |
||||
|
"MyPermission1", |
||||
|
"Test", |
||||
|
"Test") |
||||
|
); |
||||
|
|
||||
|
var grantedProviders = await _permissionManager.GetAsync("MyPermission1", |
||||
|
"Test", |
||||
|
"Test"); |
||||
|
|
||||
|
grantedProviders.ShouldNotBeNull(); |
||||
|
grantedProviders.IsGranted.ShouldBeTrue(); |
||||
|
grantedProviders.Name.ShouldBe("MyPermission1"); |
||||
|
grantedProviders.Providers.ShouldContain(x => x.Key == "Test"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Get_Should_Exception_When_Permission_Undefined() |
||||
|
{ |
||||
|
await Assert.ThrowsAsync<AbpException>(async () => await _permissionManager.GetAsync( |
||||
|
"MyPermission1NotExist", |
||||
|
"Test", |
||||
|
"Test")); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetAllAsync() |
||||
|
{ |
||||
|
await _permissionGrantRepository.InsertAsync(new PermissionGrant( |
||||
|
Guid.NewGuid(), |
||||
|
"MyPermission1", |
||||
|
"Test", |
||||
|
"Test") |
||||
|
); |
||||
|
|
||||
|
await _permissionGrantRepository.InsertAsync(new PermissionGrant( |
||||
|
Guid.NewGuid(), |
||||
|
"MyPermission2", |
||||
|
"Test", |
||||
|
"Test") |
||||
|
); |
||||
|
|
||||
|
var permissionWithGrantedProviders = await _permissionManager.GetAllAsync( |
||||
|
"Test", |
||||
|
"Test"); |
||||
|
|
||||
|
permissionWithGrantedProviders.ShouldNotBeNull(); |
||||
|
permissionWithGrantedProviders.ShouldContain(x => |
||||
|
x.IsGranted && x.Name == "MyPermission1" && x.Providers.Any(p => p.Key == "Test")); |
||||
|
permissionWithGrantedProviders.ShouldContain(x => |
||||
|
x.IsGranted && x.Name == "MyPermission2" && x.Providers.Any(p => p.Key == "Test")); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task SetAsync() |
||||
|
{ |
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission2", |
||||
|
"Test", |
||||
|
"Test")).ShouldBeNull(); |
||||
|
|
||||
|
await _permissionManager.SetAsync( |
||||
|
"MyPermission2", |
||||
|
"Test", |
||||
|
"Test", true); |
||||
|
|
||||
|
(await _permissionGrantRepository.FindAsync("MyPermission2", |
||||
|
"Test", |
||||
|
"Test")).ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Set_Should_Exception_When_Permission_Undefined() |
||||
|
{ |
||||
|
await Assert.ThrowsAsync<AbpException>(async () => await _permissionManager.SetAsync( |
||||
|
"MyPermission1NotExist", |
||||
|
"Test", |
||||
|
"Test", |
||||
|
true)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class PermissionStore_Tests : PermissionTestBase |
||||
|
{ |
||||
|
private readonly IPermissionStore _permissionStore; |
||||
|
|
||||
|
public PermissionStore_Tests() |
||||
|
{ |
||||
|
_permissionStore = GetRequiredService<IPermissionStore>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync() |
||||
|
{ |
||||
|
(await _permissionStore.IsGrantedAsync("MyPermission1", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString())).ShouldBeTrue(); |
||||
|
|
||||
|
|
||||
|
(await _permissionStore.IsGrantedAsync("MyPermission1NotExist", |
||||
|
UserPermissionValueProvider.ProviderName, |
||||
|
PermissionTestDataBuilder.User1Id.ToString())).ShouldBeFalse(); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue