Browse Source

authorization tests with role

pull/2659/head
Mehmet Perk 7 years ago
parent
commit
cfba922932
  1. 25
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs
  2. 14
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs
  3. 3
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs
  4. 13
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedServiceWithRole.cs
  5. 15
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs
  6. 27
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedServiceWithRole.cs

25
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthorizationTestBase.cs

@ -1,4 +1,10 @@
using Volo.Abp.Testing;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading;
using Volo.Abp.Security.Claims;
using Volo.Abp.Testing;
namespace Volo.Abp.Authorization
{
@ -8,5 +14,20 @@ namespace Volo.Abp.Authorization
{
options.UseAutofac();
}
protected override void AfterAddApplication(IServiceCollection services)
{
var claims = new List<Claim>() {
new Claim(AbpClaimTypes.UserName, "Douglas"),
new Claim(AbpClaimTypes.UserId, "1fcf46b2-28c3-48d0-8bac-fa53268a2775"),
new Claim(AbpClaimTypes.Role, "MyRole")
};
var identity = new ClaimsIdentity(claims);
var claimsPrincipal = new ClaimsPrincipal(identity);
var principalAccessor = Substitute.For<ICurrentPrincipalAccessor>();
principalAccessor.Principal.Returns(ci => claimsPrincipal);
Thread.CurrentPrincipal = claimsPrincipal;
}
}
}
}

14
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/Authorization_Tests.cs

@ -9,11 +9,13 @@ namespace Volo.Abp.Authorization
public class Authorization_Tests : AuthorizationTestBase
{
private readonly IMyAuthorizedService1 _myAuthorizedService1;
private readonly IMyAuthorizedServiceWithRole _myAuthorizedServiceWithRole;
private readonly IPermissionDefinitionManager _permissionDefinitionManager;
public Authorization_Tests()
{
_myAuthorizedService1 = GetRequiredService<IMyAuthorizedService1>();
_myAuthorizedServiceWithRole = GetRequiredService<IMyAuthorizedServiceWithRole>();
_permissionDefinitionManager = GetRequiredService<IPermissionDefinitionManager>();
}
@ -58,16 +60,24 @@ namespace Volo.Abp.Authorization
{
await Assert.ThrowsAsync<AbpAuthorizationException>(async () =>
{
await _myAuthorizedService1.ProtectedByRole().ConfigureAwait(false);
await _myAuthorizedServiceWithRole.ProtectedByAnotherRole().ConfigureAwait(false);
}).ConfigureAwait(false);
}
[Fact]
public async Task Should_Allow_To_Call_Method_If_Has_No_Role_ProtectedByRole_Async()
{
int result = await _myAuthorizedServiceWithRole.ProtectedByRole().ConfigureAwait(false);
result.ShouldBe(42);
}
[Fact]
public async Task Should_Not_Allow_To_Call_Method_If_Has_No_Role_ProtectedByScheme_Async()
{
await Assert.ThrowsAsync<AbpAuthorizationException>(async () =>
{
await _myAuthorizedService1.ProtectedByScheme().ConfigureAwait(false);
await _myAuthorizedServiceWithRole.ProtectedByScheme().ConfigureAwait(false);
}).ConfigureAwait(false);
}
}

3
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedService1.cs

@ -12,8 +12,5 @@ namespace Volo.Abp.Authorization.TestServices
Task<int> ProtectedByClassAsync();
Task<int> ProtectedByRole();
Task<int> ProtectedByScheme();
}
}

13
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/IMyAuthorizedServiceWithRole.cs

@ -0,0 +1,13 @@
using System.Threading.Tasks;
namespace Volo.Abp.Authorization.TestServices
{
public interface IMyAuthorizedServiceWithRole
{
Task<int> ProtectedByRole();
Task<int> ProtectedByScheme();
Task<int> ProtectedByAnotherRole();
}
}

15
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedService1.cs

@ -30,18 +30,5 @@ namespace Volo.Abp.Authorization.TestServices
await Task.Delay(10).ConfigureAwait(false);
return 42;
}
[Authorize(Roles = "MyRole")]
public virtual Task<int> ProtectedByRole()
{
return Task.FromResult(42);
}
[Authorize(AuthenticationSchemes = "Bearer")]
[Authorize(Roles = "MyRole")]
public virtual Task<int> ProtectedByScheme()
{
return Task.FromResult(42);
}
}
}
}

27
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/TestServices/MyAuthorizedServiceWithRole.cs

@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Authorization;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Authorization.TestServices
{
[Authorize(Roles = "MyRole")]
public class MyAuthorizedServiceWithRole : IMyAuthorizedServiceWithRole, ITransientDependency
{
public virtual Task<int> ProtectedByRole()
{
return Task.FromResult(42);
}
[Authorize(Roles = "MyAnotherRole")]
public virtual Task<int> ProtectedByAnotherRole()
{
return Task.FromResult(42);
}
[Authorize(AuthenticationSchemes = "Bearer")]
public virtual Task<int> ProtectedByScheme()
{
return Task.FromResult(42);
}
}
}
Loading…
Cancel
Save