1+ using DynamicAuthorization . Mvc . Core . Extensions ;
2+ using DynamicAuthorization . Mvc . JsonStore . Extensions ;
3+ using DynamicRoleBasedAuthorization . Tests . TestSetup ;
4+ using Microsoft . AspNetCore . Builder ;
5+ using Microsoft . AspNetCore . Hosting ;
6+ using Microsoft . AspNetCore . Http ;
7+ using Microsoft . AspNetCore . Identity ;
8+ using Microsoft . AspNetCore . Identity . EntityFrameworkCore ;
9+ using Microsoft . AspNetCore . TestHost ;
10+ using Microsoft . EntityFrameworkCore ;
11+ using Microsoft . Extensions . Configuration ;
12+ using Microsoft . Extensions . DependencyInjection ;
13+ using Microsoft . Extensions . Hosting ;
14+ using System . Collections . Generic ;
15+ using System . Threading . Tasks ;
16+ using Xunit ;
17+
18+ namespace DynamicRoleBasedAuthorization . Tests
19+ {
20+ public class ServiceRegistrationTests
21+ {
22+ [ Fact ]
23+ public void Register_DynamicAuthorization_With_TypeOf_DbContext_With_Eight_Generic_Parameter ( )
24+ {
25+ //Arrange
26+ var builder = new WebHostBuilder ( ) . UseStartup < CustomStartup > ( ) ;
27+
28+ // Act
29+ var exception = Record . Exception ( ( ) => new TestServer ( builder ) ) ;
30+
31+ // Assert
32+ Assert . Null ( exception ) ;
33+ }
34+ }
35+
36+ internal class ApplicationUser : IdentityUser < int >
37+ {
38+ public string Name { get ; set ; }
39+
40+ public virtual ICollection < ApplicationUserRole > UserRoles { get ; set ; }
41+ }
42+
43+ internal class ApplicationRole : IdentityRole < int >
44+ {
45+ }
46+
47+ internal class ApplicationUserRole : IdentityUserRole < int >
48+ {
49+ internal ApplicationUser User { get ; set ; }
50+
51+ internal ApplicationRole Role { get ; set ; }
52+ }
53+
54+ internal class ApplicationUserClaim : IdentityUserClaim < int >
55+ {
56+ }
57+
58+ internal class ApplicationUserLogin : IdentityUserLogin < int >
59+ {
60+ }
61+
62+ internal class ApplicationURoleClaim : IdentityRoleClaim < int >
63+ {
64+ }
65+
66+ internal class ApplicationUserToken : IdentityUserToken < int >
67+ {
68+ }
69+
70+ internal class CustomDbContext : IdentityDbContext <
71+ ApplicationUser , ApplicationRole , int ,
72+ ApplicationUserClaim , ApplicationUserRole , ApplicationUserLogin ,
73+ ApplicationURoleClaim , ApplicationUserToken >
74+ {
75+ public CustomDbContext ( DbContextOptions < ApplicationDbContext > options )
76+ : base ( options )
77+ {
78+ }
79+ }
80+
81+ internal class CustomStartup
82+ {
83+ public CustomStartup ( IConfiguration configuration )
84+ {
85+ Configuration = configuration ;
86+ }
87+
88+ public IConfiguration Configuration { get ; }
89+
90+ // This method gets called by the runtime. Use this method to add services to the container.
91+ public void ConfigureServices ( IServiceCollection services )
92+ {
93+ services . AddControllersWithViews ( ) ;
94+
95+ services . AddDbContext < CustomDbContext > ( options => options . UseInMemoryDatabase ( "InMemoryDbForTesting" ) ) ;
96+
97+ services . AddIdentity < ApplicationUser , ApplicationRole > ( options => options . SignIn . RequireConfirmedAccount = false )
98+ . AddEntityFrameworkStores < CustomDbContext > ( )
99+ . AddDefaultTokenProviders ( ) ;
100+
101+ services . ConfigureApplicationCookie ( options =>
102+ {
103+ options . Events . OnRedirectToLogin = context =>
104+ {
105+ context . Response . StatusCode = StatusCodes . Status401Unauthorized ;
106+ return Task . CompletedTask ;
107+ } ;
108+ options . Events . OnRedirectToAccessDenied = context =>
109+ {
110+ context . Response . StatusCode = StatusCodes . Status403Forbidden ;
111+ return Task . CompletedTask ;
112+ } ;
113+ } ) ;
114+
115+ services . AddDynamicAuthorization < CustomDbContext > ( options => options . DefaultAdminUser = InitialData . SuperUser . UserName )
116+ . AddJsonStore ( ) ;
117+
118+ services . AddScoped < DbInitializer > ( ) ;
119+ }
120+
121+ public void Configure ( IApplicationBuilder app , IHostApplicationLifetime applicationLifetime )
122+ {
123+ app . UseRouting ( ) ;
124+
125+ app . UseAuthentication ( ) ;
126+ app . UseAuthorization ( ) ;
127+
128+ app . UseEndpoints ( endpoints =>
129+ {
130+ endpoints . MapControllerRoute (
131+ name : "default-area" ,
132+ pattern : "{area:exists}/{controller=Home}/{action=Index}/{id?}" ) ;
133+ endpoints . MapControllerRoute (
134+ name : "default" ,
135+ pattern : "{controller=Home}/{action=Index}/{id?}" ) ;
136+ } ) ;
137+ }
138+ }
139+ }
0 commit comments