mirror of https://github.com/abpframework/abp.git
10 changed files with 185 additions and 7 deletions
@ -0,0 +1,25 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp2.0</TargetFramework> |
||||
|
<LangVersion>latest</LangVersion> |
||||
|
<AssemblyName>Volo.Abp.Authorization.Tests</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Authorization.Tests</PackageId> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,34 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Authorization.TestServices; |
||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpAutofacModule))] |
||||
|
[DependsOn(typeof(AbpAuthorizationModule))] |
||||
|
public class AbpAuthorizationTestModule : AbpModule |
||||
|
{ |
||||
|
public override void PreConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.OnRegistred(context => |
||||
|
{ |
||||
|
if (typeof(IMyAuthorizedService1).IsAssignableFrom(context.ImplementationType)) |
||||
|
{ |
||||
|
context.Interceptors.TryAdd<AuthorizationInterceptor>(); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public override void ConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<PermissionOptions>(options => |
||||
|
{ |
||||
|
options.DefinitionProviders.TryAdd<AuthorizationTestPermissionDefinitionProvider>(); |
||||
|
}); |
||||
|
|
||||
|
services.AddAssemblyOf<AbpAuthorizationTestModule>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Volo.Abp.TestBase; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public class AuthorizationTestBase : AbpIntegratedTest<AbpAuthorizationTestModule> |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Authorization.TestServices; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public class Authorization_Tests : AuthorizationTestBase |
||||
|
{ |
||||
|
private readonly IMyAuthorizedService1 _myAuthorizedService1; |
||||
|
|
||||
|
public Authorization_Tests() |
||||
|
{ |
||||
|
_myAuthorizedService1 = GetRequiredService<IMyAuthorizedService1>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Not_Allow_To_Call_Method_If_Has_No_Permission_ProtectedByClass() |
||||
|
{ |
||||
|
Assert.Throws<AbpAuthorizationException>(() => |
||||
|
{ |
||||
|
_myAuthorizedService1.ProtectedByClass(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Not_Allow_To_Call_Method_If_Has_No_Permission_ProtectedByClass_Async() |
||||
|
{ |
||||
|
await Assert.ThrowsAsync<AbpAuthorizationException>(async () => |
||||
|
{ |
||||
|
await _myAuthorizedService1.ProtectedByClassAsync(); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.TestServices |
||||
|
{ |
||||
|
public class AuthorizationTestPermissionDefinitionProvider : PermissionDefinitionProvider |
||||
|
{ |
||||
|
public override void Define(IPermissionDefinitionContext context) |
||||
|
{ |
||||
|
var group = context.AddGroup("TestGroup"); |
||||
|
group.AddPermission("MyAuthorizedService1"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.TestServices |
||||
|
{ |
||||
|
public interface IMyAuthorizedService1 |
||||
|
{ |
||||
|
int Anonymous(); |
||||
|
|
||||
|
Task<int> AnonymousAsync(); |
||||
|
|
||||
|
int ProtectedByClass(); |
||||
|
|
||||
|
Task<int> ProtectedByClassAsync(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.TestServices |
||||
|
{ |
||||
|
[Authorize("MyAuthorizedService1")] |
||||
|
public class MyAuthorizedService1 : IMyAuthorizedService1, ITransientDependency |
||||
|
{ |
||||
|
[AllowAnonymous] |
||||
|
public virtual int Anonymous() |
||||
|
{ |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
[AllowAnonymous] |
||||
|
public virtual async Task<int> AnonymousAsync() |
||||
|
{ |
||||
|
await Task.Delay(10); |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
public virtual int ProtectedByClass() |
||||
|
{ |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<int> ProtectedByClassAsync() |
||||
|
{ |
||||
|
await Task.Delay(10); |
||||
|
return 42; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue