mirror of https://github.com/abpframework/abp.git
8 changed files with 337 additions and 7 deletions
@ -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