Browse Source

Added tests for authorization.

pull/208/head
Halil İbrahim Kalkan 9 years ago
parent
commit
fc710a3d4f
  1. 6
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs
  2. 8
      src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs
  3. 1
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj
  4. 10
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs
  5. 37
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/AuthTestController.cs
  6. 77
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/AuthTestController_Tests.cs
  7. 33
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs
  8. 11
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeUserClaims.cs

6
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs

@ -3,7 +3,9 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
using Volo.Abp.ObjectMapping; using Volo.Abp.ObjectMapping;
using Volo.Abp.Session;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.AspNetCore.Mvc namespace Volo.Abp.AspNetCore.Mvc
@ -18,6 +20,10 @@ namespace Volo.Abp.AspNetCore.Mvc
public ILoggerFactory LoggerFactory { get; set; } public ILoggerFactory LoggerFactory { get; set; }
public ICurrentUser CurrentUser { get; set; }
public ICurrentTenant CurrentTenant { get; set; }
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;
protected ILogger Logger => _lazyLogger.Value; protected ILogger Logger => _lazyLogger.Value;

8
src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreIntegratedTestBase.cs

@ -32,7 +32,13 @@ namespace Volo.Abp.AspNetCore.TestBase
protected virtual IWebHostBuilder CreateWebHostBuilder() protected virtual IWebHostBuilder CreateWebHostBuilder()
{ {
return new WebHostBuilder() return new WebHostBuilder()
.UseStartup<TStartup>(); .UseStartup<TStartup>()
.ConfigureServices(ConfigureServices);
}
protected virtual void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
} }
protected virtual TestServer CreateTestServer(IWebHostBuilder builder) protected virtual TestServer CreateTestServer(IWebHostBuilder builder)

1
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj

@ -18,6 +18,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

10
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Modularity; using Volo.Abp.AspNetCore.Modularity;
using Volo.Abp.AspNetCore.Mvc.Authorization;
using Volo.Abp.AspNetCore.TestBase; using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Autofac; using Volo.Abp.Autofac;
using Volo.Abp.MemoryDb; using Volo.Abp.MemoryDb;
@ -30,6 +31,14 @@ namespace Volo.Abp.AspNetCore.Mvc
{ {
services.AddLocalization(); //TODO: Move to the framework..? services.AddLocalization(); //TODO: Move to the framework..?
services.AddAuthorization(options =>
{
options.AddPolicy("MyClaimTestPolicy", policy =>
{
policy.RequireClaim("MyCustomClaimType", "42");
});
});
services.Configure<AbpAspNetCoreMvcOptions>(options => services.Configure<AbpAspNetCoreMvcOptions>(options =>
{ {
options.ConventionalControllers.Create(typeof(TestAppModule).Assembly, opts => options.ConventionalControllers.Create(typeof(TestAppModule).Assembly, opts =>
@ -48,6 +57,7 @@ namespace Volo.Abp.AspNetCore.Mvc
{ {
var app = context.GetApplicationBuilder(); var app = context.GetApplicationBuilder();
app.UseMiddleware<FakeAuthenticationMiddleware>();
app.UseUnitOfWork(); app.UseUnitOfWork();
app.UseMvcWithDefaultRoute(); app.UseMvcWithDefaultRoute();
} }

37
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/AuthTestController.cs

@ -0,0 +1,37 @@
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
namespace Volo.Abp.AspNetCore.Mvc.Authorization
{
[Authorize]
public class AuthTestController : AbpController
{
public static Guid FakeUserId { get; } = new Guid();
[AllowAnonymous]
public ActionResult AnonymousTest()
{
return Content("OK");
}
public ActionResult SimpleAuthorizationTest()
{
CurrentUser.Id.ShouldBe(FakeUserId);
return Content("OK");
}
[Authorize("MyClaimTestPolicy")]
public ActionResult CustomPolicyTest()
{
return Content("OK");
}
//[Authorize("TestPermission")]
//public ActionResult PermissionTest()
//{
// return Content("OK");
//}
}
}

77
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/AuthTestController_Tests.cs

@ -0,0 +1,77 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Autofac;
using Volo.Abp.MemoryDb;
using Volo.Abp.Modularity;
using Volo.Abp.Security.Claims;
using Volo.Abp.Session;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.Authorization
{
[DependsOn(
typeof(AbpAspNetCoreTestBaseModule),
typeof(AbpMemoryDbTestModule),
typeof(AbpAspNetCoreMvcModule),
typeof(AbpAutofacModule)
)]
public class AuthTestController_Tests : AspNetCoreMvcTestBase
{
private readonly FakeUserClaims _fakeRequiredService;
public AuthTestController_Tests()
{
_fakeRequiredService = GetRequiredService<FakeUserClaims>();
}
[Fact]
public async Task Should_Call_Anonymous_Method_Without_Authentication()
{
var result = await GetResponseAsStringAsync("/AuthTest/AnonymousTest");
result.ShouldBe("OK");
}
[Fact]
public async Task Should_Call_Simple_Authorized_Method_With_Authenticated_User()
{
_fakeRequiredService.Claims.AddRange(new[]
{
new Claim(AbpClaimTypes.UserId, AuthTestController.FakeUserId.ToString())
});
var result = await GetResponseAsStringAsync("/AuthTest/SimpleAuthorizationTest");
result.ShouldBe("OK");
}
[Fact]
public async Task Custom_Claim_Policy_Should_Work_With_Right_Claim_Provided()
{
_fakeRequiredService.Claims.AddRange(new[]
{
new Claim(AbpClaimTypes.UserId, AuthTestController.FakeUserId.ToString()),
new Claim("MyCustomClaimType", "42")
});
var result = await GetResponseAsStringAsync("/AuthTest/CustomPolicyTest");
result.ShouldBe("OK");
}
[Fact]
public async Task Custom_Claim_Policy_Should_Not_Work_With_Wrong_Claim_Value()
{
_fakeRequiredService.Claims.AddRange(new[]
{
new Claim(AbpClaimTypes.UserId, AuthTestController.FakeUserId.ToString()),
new Claim("MyCustomClaimType", "43")
});
//TODO: We can get a real exception if we properly configure authentication schemas for this project
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await GetResponseAsStringAsync("/AuthTest/CustomPolicyTest")
);
}
}
}

33
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeAuthenticationMiddleware.cs

@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.Mvc.Authorization
{
public class FakeAuthenticationMiddleware
{
private readonly RequestDelegate _next;
private readonly FakeUserClaims _fakeUserClaims;
public FakeAuthenticationMiddleware(RequestDelegate next, FakeUserClaims fakeUserClaims)
{
_next = next;
_fakeUserClaims = fakeUserClaims;
}
public async Task Invoke(HttpContext httpContext)
{
if (_fakeUserClaims.Claims.Any())
{
httpContext.User = new ClaimsPrincipal(new List<ClaimsIdentity>
{
new ClaimsIdentity(_fakeUserClaims.Claims, "FakeSchema")
});
}
await _next(httpContext);
}
}
}

11
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Authorization/FakeUserClaims.cs

@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Security.Claims;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.Authorization
{
public class FakeUserClaims : ISingletonDependency
{
public List<Claim> Claims { get; } = new List<Claim>();
}
}
Loading…
Cancel
Save