mirror of https://github.com/abpframework/abp.git
committed by
GitHub
6 changed files with 138 additions and 1 deletions
@ -0,0 +1,49 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization; |
||||
|
|
||||
|
public class PermissionChecker_Tests: AuthorizationTestBase |
||||
|
{ |
||||
|
private readonly IPermissionChecker _permissionChecker; |
||||
|
|
||||
|
public PermissionChecker_Tests() |
||||
|
{ |
||||
|
_permissionChecker = GetRequiredService<IPermissionChecker>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync() |
||||
|
{ |
||||
|
(await _permissionChecker.IsGrantedAsync("MyPermission5")).ShouldBe(true); |
||||
|
(await _permissionChecker.IsGrantedAsync("UndefinedPermission")).ShouldBe(false); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGranted_Multiple_Result_Async() |
||||
|
{ |
||||
|
var result = await _permissionChecker.IsGrantedAsync(new [] |
||||
|
{ |
||||
|
"MyPermission1", |
||||
|
"MyPermission2", |
||||
|
"UndefinedPermission", |
||||
|
"MyPermission3", |
||||
|
"MyPermission4", |
||||
|
"MyPermission5", |
||||
|
"MyPermission6", |
||||
|
"MyPermission7" |
||||
|
}); |
||||
|
|
||||
|
result.Result["MyPermission1"].ShouldBe(PermissionGrantResult.Undefined); |
||||
|
result.Result["MyPermission2"].ShouldBe(PermissionGrantResult.Prohibited); |
||||
|
result.Result["UndefinedPermission"].ShouldBe(PermissionGrantResult.Prohibited); |
||||
|
result.Result["MyPermission3"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
result.Result["MyPermission4"].ShouldBe(PermissionGrantResult.Prohibited); |
||||
|
result.Result["MyPermission5"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
result.Result["MyPermission6"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
result.Result["MyPermission7"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.TestServices; |
||||
|
|
||||
|
public class TestPermissionValueProvider1 : PermissionValueProvider |
||||
|
{ |
||||
|
public TestPermissionValueProvider1(IPermissionStore permissionStore) : base(permissionStore) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public override string Name => "TestPermissionValueProvider1"; |
||||
|
|
||||
|
public override Task<PermissionGrantResult> CheckAsync(PermissionValueCheckContext context) |
||||
|
{ |
||||
|
var result = PermissionGrantResult.Undefined; |
||||
|
if (context.Permission.Name == "MyPermission6") |
||||
|
{ |
||||
|
result = PermissionGrantResult.Granted; |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(result); |
||||
|
} |
||||
|
|
||||
|
public override Task<MultiplePermissionGrantResult> CheckAsync(PermissionValuesCheckContext context) |
||||
|
{ |
||||
|
var result = new MultiplePermissionGrantResult(); |
||||
|
foreach (var name in context.Permissions.Select(x => x.Name)) |
||||
|
{ |
||||
|
result.Result.Add(name, name == "MyPermission6" |
||||
|
? PermissionGrantResult.Granted |
||||
|
: PermissionGrantResult.Undefined); |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(result); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.TestServices; |
||||
|
|
||||
|
public class TestPermissionValueProvider2 : PermissionValueProvider |
||||
|
{ |
||||
|
public TestPermissionValueProvider2(IPermissionStore permissionStore) : base(permissionStore) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public override string Name => "TestPermissionValueProvider2"; |
||||
|
|
||||
|
public override Task<PermissionGrantResult> CheckAsync(PermissionValueCheckContext context) |
||||
|
{ |
||||
|
var result = PermissionGrantResult.Undefined; |
||||
|
if (context.Permission.Name == "MyPermission7") |
||||
|
{ |
||||
|
result = PermissionGrantResult.Granted; |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(result); |
||||
|
} |
||||
|
|
||||
|
public override Task<MultiplePermissionGrantResult> CheckAsync(PermissionValuesCheckContext context) |
||||
|
{ |
||||
|
var result = new MultiplePermissionGrantResult(); |
||||
|
foreach (var name in context.Permissions.Select(x => x.Name)) |
||||
|
{ |
||||
|
result.Result.Add(name, name == "MyPermission7" |
||||
|
? PermissionGrantResult.Granted |
||||
|
: PermissionGrantResult.Undefined); |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(result); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue