Browse Source

Added tests for auth interception.

pull/216/head
Halil İbrahim Kalkan 9 years ago
parent
commit
1528f71413
  1. 13
      Volo.Abp.sln
  2. 1
      src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs
  3. 11
      src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs
  4. 25
      test/Volo.Abp.Authorization.Tests/Volo.Abp.Authorization.Tests.csproj
  5. 34
      test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AbpAuthorizationTestModule.cs
  6. 12
      test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs
  7. 34
      test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs
  8. 13
      test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/AuthorizationTestPermissionDefinitionProvider.cs
  9. 15
      test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs
  10. 34
      test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs

13
Volo.Abp.sln

@ -280,11 +280,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.EventBus", "src\Vo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.EventBus.Tests", "test\Volo.Abp.EventBus.Tests\Volo.Abp.EventBus.Tests.csproj", "{8C327AA0-BBED-4F8B-A88E-1DD97B04E58F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Caching.Tests", "test\Volo.Abp.Caching.Tests\Volo.Abp.Caching.Tests.csproj", "{B417D97C-330A-42CE-BDC6-93355B0A959A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Caching.Tests", "test\Volo.Abp.Caching.Tests\Volo.Abp.Caching.Tests.csproj", "{B417D97C-330A-42CE-BDC6-93355B0A959A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Serialization", "src\Volo.Abp.Serialization\Volo.Abp.Serialization.csproj", "{38EF3EC8-9915-4216-B646-4BEE07006943}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Serialization", "src\Volo.Abp.Serialization\Volo.Abp.Serialization.csproj", "{38EF3EC8-9915-4216-B646-4BEE07006943}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Serialization.Tests", "test\Volo.Abp.Serialization.Tests\Volo.Abp.Serialization.Tests.csproj", "{65FB5893-7CB6-4694-A692-7E666E347D29}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Serialization.Tests", "test\Volo.Abp.Serialization.Tests\Volo.Abp.Serialization.Tests.csproj", "{65FB5893-7CB6-4694-A692-7E666E347D29}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Authorization.Tests", "test\Volo.Abp.Authorization.Tests\Volo.Abp.Authorization.Tests.csproj", "{B10E37A1-43A1-4042-BAAA-F589302958D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -752,6 +754,10 @@ Global
{65FB5893-7CB6-4694-A692-7E666E347D29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65FB5893-7CB6-4694-A692-7E666E347D29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65FB5893-7CB6-4694-A692-7E666E347D29}.Release|Any CPU.Build.0 = Release|Any CPU
{B10E37A1-43A1-4042-BAAA-F589302958D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B10E37A1-43A1-4042-BAAA-F589302958D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B10E37A1-43A1-4042-BAAA-F589302958D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B10E37A1-43A1-4042-BAAA-F589302958D5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -891,6 +897,7 @@ Global
{B417D97C-330A-42CE-BDC6-93355B0A959A} = {37087D1B-3693-4E96-983D-A69F210BDE53}
{38EF3EC8-9915-4216-B646-4BEE07006943} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8}
{65FB5893-7CB6-4694-A692-7E666E347D29} = {37087D1B-3693-4E96-983D-A69F210BDE53}
{B10E37A1-43A1-4042-BAAA-F589302958D5} = {37087D1B-3693-4E96-983D-A69F210BDE53}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}

1
src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs

@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Aspects;
using Volo.Abp.DependencyInjection;
using Volo.Abp.DynamicProxy;

11
src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs

@ -35,10 +35,15 @@ namespace Volo.Abp.Authorization
protected virtual IAuthorizeData[] GetAuthorizationDataAttributes(MethodInvocationAuthorizationContext context)
{
return context.Method
var classAttributes = context.Method.DeclaringType
.GetCustomAttributes(true)
.OfType<IAuthorizeData>()
.ToArray();
.OfType<IAuthorizeData>();
var methodAttributes = context.Method
.GetCustomAttributes(true)
.OfType<IAuthorizeData>();
return classAttributes.Union(methodAttributes).ToArray();
}
protected async Task CheckAsync(IAuthorizeData authorizationAttribute)

25
test/Volo.Abp.Authorization.Tests/Volo.Abp.Authorization.Tests.csproj

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

34
test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AbpAuthorizationTestModule.cs

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

12
test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs

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

34
test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs

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

13
test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/AuthorizationTestPermissionDefinitionProvider.cs

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

15
test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs

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

34
test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs

@ -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…
Cancel
Save