CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TestDIContextFactory.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="TestDIContextFactory.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Factory for DI context instances for use in tests</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Security.Claims;
11using System.Security.Principal;
12using System.Text;
14using Csla.Core;
17
18namespace Csla.TestHelpers
19{
20
24 public static class TestDIContextFactory
25 {
26
31 public static TestDIContext CreateDefaultContext()
32 {
33 ClaimsPrincipal principal;
34
35 // Create a default security principal
36 principal = CreateDefaultClaimsPrincipal();
37
38 // Delegate to the other overload to create the context
39 return CreateContext(principal);
40 }
41
47 public static TestDIContext CreateContext(ClaimsPrincipal principal)
48 {
49 return CreateContext(null, principal);
50 }
51
57 public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions)
58 {
59 ClaimsPrincipal principal;
60
61 principal = CreateDefaultClaimsPrincipal();
62 return CreateContext(customCslaOptions, principal);
63 }
64
71 public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions, ClaimsPrincipal principal)
72 {
73 IServiceProvider serviceProvider;
74 ApplicationContext context;
75
76 // Initialise DI
77 var services = new ServiceCollection();
78
79 // Add Csla
80 services.TryAddSingleton<Core.IContextManager, ApplicationContextManagerUnitTests>();
81 services.TryAddSingleton<Server.Dashboard.IDashboard, Server.Dashboard.Dashboard>();
82 services.AddCsla(customCslaOptions);
83
84 serviceProvider = services.BuildServiceProvider();
85
86 // Initialise CSLA security
87 context = serviceProvider.GetRequiredService<ApplicationContext>();
88 context.Principal = principal;
89
90 return new TestDIContext(serviceProvider);
91 }
92
97 private static ClaimsPrincipal CreateDefaultClaimsPrincipal()
98 {
99 ClaimsIdentity identity;
100
101 // Create a default security principal
102 identity = new ClaimsIdentity(new GenericIdentity("Fred"));
103 identity.AddClaim(new Claim(ClaimTypes.Role, "Users"));
104 return new ClaimsPrincipal(identity);
105 }
106
107 }
108}