9using System.Collections.Generic;
10using System.Security.Claims;
11using System.Security.Principal;
24 public static class TestDIContextFactory
31 public static TestDIContext CreateDefaultContext()
33 ClaimsPrincipal principal;
36 principal = CreateDefaultClaimsPrincipal();
39 return CreateContext(principal);
47 public static TestDIContext CreateContext(ClaimsPrincipal principal)
49 return CreateContext(
null, principal);
57 public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions)
59 ClaimsPrincipal principal;
61 principal = CreateDefaultClaimsPrincipal();
62 return CreateContext(customCslaOptions, principal);
71 public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions, ClaimsPrincipal principal)
73 IServiceProvider serviceProvider;
74 ApplicationContext context;
77 var services =
new ServiceCollection();
80 services.TryAddSingleton<Core.IContextManager, ApplicationContextManagerUnitTests>();
81 services.TryAddSingleton<Server.Dashboard.IDashboard, Server.Dashboard.Dashboard>();
82 services.AddCsla(customCslaOptions);
84 serviceProvider = services.BuildServiceProvider();
87 context = serviceProvider.GetRequiredService<ApplicationContext>();
88 context.Principal = principal;
90 return new TestDIContext(serviceProvider);
97 private static ClaimsPrincipal CreateDefaultClaimsPrincipal()
99 ClaimsIdentity identity;
102 identity =
new ClaimsIdentity(
new GenericIdentity(
"Fred"));
103 identity.AddClaim(
new Claim(ClaimTypes.Role,
"Users"));
104 return new ClaimsPrincipal(identity);