Skip to content

Commit 8b7a805

Browse files
committed
Add test project.
1 parent 0622a83 commit 8b7a805

File tree

12 files changed

+339
-0
lines changed

12 files changed

+339
-0
lines changed

DynamicRoleBasedAuthorization.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "netcore2.2", "netcore2.2",
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleMvc2WebApp", "samples\netcore2.2\SampleMvc2WebApp\SampleMvc2WebApp.csproj", "{A3124728-6766-48A3-BA1A-5CB119924395}"
2121
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{DF22D7D8-4CE9-4A0E-9D9B-FB5D6D637089}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicRoleBasedAuthorization.Tests", "test\DynamicRoleBasedAuthorization.Tests\DynamicRoleBasedAuthorization.Tests.csproj", "{0FC61895-DE38-4526-96EE-C6CB6AA92A3B}"
25+
EndProject
2226
Global
2327
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2428
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +45,10 @@ Global
4145
{A3124728-6766-48A3-BA1A-5CB119924395}.Debug|Any CPU.Build.0 = Debug|Any CPU
4246
{A3124728-6766-48A3-BA1A-5CB119924395}.Release|Any CPU.ActiveCfg = Release|Any CPU
4347
{A3124728-6766-48A3-BA1A-5CB119924395}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{0FC61895-DE38-4526-96EE-C6CB6AA92A3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{0FC61895-DE38-4526-96EE-C6CB6AA92A3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{0FC61895-DE38-4526-96EE-C6CB6AA92A3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{0FC61895-DE38-4526-96EE-C6CB6AA92A3B}.Release|Any CPU.Build.0 = Release|Any CPU
4452
EndGlobalSection
4553
GlobalSection(SolutionProperties) = preSolution
4654
HideSolutionNode = FALSE
@@ -52,6 +60,7 @@ Global
5260
{0833E296-398F-42A4-9531-D125483AB019} = {5FEB9007-1EFA-4814-BC15-DB0370B84E22}
5361
{11B88EB7-66BE-402A-A4E0-144220EBD8B9} = {BAAF9837-1DB5-4032-AB2A-2901E6EE6EF8}
5462
{A3124728-6766-48A3-BA1A-5CB119924395} = {11B88EB7-66BE-402A-A4E0-144220EBD8B9}
63+
{0FC61895-DE38-4526-96EE-C6CB6AA92A3B} = {DF22D7D8-4CE9-4A0E-9D9B-FB5D6D637089}
5564
EndGlobalSection
5665
GlobalSection(ExtensibilityGlobals) = postSolution
5766
SolutionGuid = {18C80710-C9E9-488B-9100-1E4BF8B18038}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace DynamicRoleBasedAuthorization.Tests
5+
{
6+
public class ApplicationDbContext : IdentityDbContext
7+
{
8+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
9+
: base(options)
10+
{
11+
}
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace DynamicRoleBasedAuthorization.Tests.Areas.AuthorizedArea.Controllers
5+
{
6+
[Area("AuthorizedArea"), Authorize]
7+
public class AuthorizedController : Controller
8+
{
9+
public string Action1()
10+
{
11+
return "Action1";
12+
}
13+
14+
public string Action2()
15+
{
16+
return "Action2";
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace DynamicRoleBasedAuthorization.Tests.Controllers
5+
{
6+
public class ActionAuthorizedController : Controller
7+
{
8+
public string NonAuthorizedAction()
9+
{
10+
return "NonAuthorizedAction";
11+
}
12+
13+
[Authorize]
14+
public string AuthorizedAction()
15+
{
16+
return "AuthorizedAction";
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace DynamicRoleBasedAuthorization.Tests.Controllers
5+
{
6+
[Authorize]
7+
public class AuthorizedController : Controller
8+
{
9+
public string Action1()
10+
{
11+
return "Action1";
12+
}
13+
14+
public string Action2()
15+
{
16+
return "Action2";
17+
}
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.2" />
11+
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.2" />
12+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.2" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.2" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.2">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
19+
<PackageReference Include="xunit" Version="2.4.1" />
20+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
24+
<PackageReference Include="coverlet.collector" Version="1.2.0">
25+
<PrivateAssets>all</PrivateAssets>
26+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
27+
</PackageReference>
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<ProjectReference Include="..\..\src\DynamicAuthorization.Mvc.Core\DynamicAuthorization.Mvc.Core.csproj" />
32+
<ProjectReference Include="..\..\src\DynamicAuthorization.Mvc.JsonStore\DynamicAuthorization.Mvc.JsonStore.csproj" />
33+
</ItemGroup>
34+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using DynamicAuthorization.Mvc.Core;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Mvc.Infrastructure;
4+
using Microsoft.AspNetCore.TestHost;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using System.Linq;
7+
using Xunit;
8+
9+
namespace DynamicRoleBasedAuthorization.Tests
10+
{
11+
public class MvcControllerDiscoveryTests
12+
{
13+
private readonly TestServer _testServer;
14+
15+
public MvcControllerDiscoveryTests()
16+
{
17+
var builder = new WebHostBuilder().UseStartup<Startup>();
18+
_testServer = new TestServer(builder);
19+
}
20+
21+
[Fact]
22+
public void When_Controller_Decorated_With_Authorize_Attribute_Should_Return_All_Actions()
23+
{
24+
// Arrange
25+
var actionDescriptorCollectionProvider = _testServer.Services.GetService<IActionDescriptorCollectionProvider>();
26+
var controllerDiscovery = new MvcControllerDiscovery(actionDescriptorCollectionProvider);
27+
28+
// Act
29+
var controllers = controllerDiscovery.GetControllers();
30+
var actionCount = controllers.SingleOrDefault(c => c.Id == ":Authorized")?.Actions?.Count();
31+
32+
// Assert
33+
Assert.NotEmpty(controllers);
34+
Assert.Equal(2, actionCount);
35+
}
36+
37+
[Fact]
38+
public void When_Controller_Not_Decorated_With_Authorize_Attribute_Should_Return_Only_Authorized_Actions()
39+
{
40+
// Arrange
41+
var actionDescriptorCollectionProvider = _testServer.Services.GetService<IActionDescriptorCollectionProvider>();
42+
var controllerDiscovery = new MvcControllerDiscovery(actionDescriptorCollectionProvider);
43+
44+
// Act
45+
var controllers = controllerDiscovery.GetControllers();
46+
var actions = controllers.SingleOrDefault(c => c.Id == ":ActionAuthorized")?.Actions;
47+
48+
// Assert
49+
Assert.NotEmpty(controllers);
50+
Assert.NotNull(actions);
51+
Assert.Equal(1, actions.Count());
52+
Assert.Equal(":ActionAuthorized:AuthorizedAction", actions.FirstOrDefault().Id);
53+
}
54+
55+
[Fact]
56+
public void When_Area_Exist_Should_Return_Authorized_Controllers_And_Actions()
57+
{
58+
// Arrange
59+
var actionDescriptorCollectionProvider = _testServer.Services.GetService<IActionDescriptorCollectionProvider>();
60+
var controllerDiscovery = new MvcControllerDiscovery(actionDescriptorCollectionProvider);
61+
62+
// Act
63+
var controllers = controllerDiscovery.GetControllers();
64+
var areaControllers = controllers.Where(c => c.Id.StartsWith("AuthorizedArea"));
65+
66+
// Assert
67+
Assert.NotEmpty(areaControllers);
68+
Assert.Single(areaControllers);
69+
}
70+
}
71+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace DynamicRoleBasedAuthorization.Tests
9+
{
10+
public class Startup
11+
{
12+
public Startup(IConfiguration configuration)
13+
{
14+
Configuration = configuration;
15+
}
16+
17+
public IConfiguration Configuration { get; }
18+
19+
// This method gets called by the runtime. Use this method to add services to the container.
20+
public void ConfigureServices(IServiceCollection services)
21+
{
22+
services.AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase("InMemoryDbForTesting"));
23+
24+
services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
25+
.AddEntityFrameworkStores<ApplicationDbContext>()
26+
.AddDefaultTokenProviders();
27+
28+
services.AddControllersWithViews();
29+
}
30+
31+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
32+
{
33+
app.UseRouting();
34+
35+
app.UseAuthentication();
36+
app.UseAuthorization();
37+
38+
app.UseEndpoints(endpoints =>
39+
{
40+
endpoints.MapControllerRoute(
41+
name: "default-area",
42+
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
43+
endpoints.MapControllerRoute(
44+
name: "default",
45+
pattern: "{controller=Home}/{action=Index}/{id?}");
46+
});
47+
}
48+
}
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace DynamicRoleBasedAuthorization.Tests.TestSetup
2+
{
3+
internal static class DbInitializer
4+
{
5+
public static void InitializeDb(ApplicationDbContext context)
6+
{
7+
//foreach (var user in InitialUserList.Users)
8+
// context.Users.Add(user);
9+
10+
//context.SaveChanges();
11+
}
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace DynamicRoleBasedAuthorization.Tests.TestSetup
4+
{
5+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
6+
public class TestPriorityAttribute : Attribute
7+
{
8+
public TestPriorityAttribute(int priority)
9+
{
10+
Priority = priority;
11+
}
12+
13+
public int Priority { get; }
14+
}
15+
}

0 commit comments

Comments
 (0)