Skip to content

Commit 0ef0486

Browse files
Added tests for new principal flow behaviours
1 parent ded2a33 commit 0ef0486

File tree

5 files changed

+150
-4
lines changed

5 files changed

+150
-4
lines changed

Source/Csla.test/Csla.Tests.csproj

-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@
8282
<ProjectReference Include="..\Csla\Csla.csproj" />
8383
</ItemGroup>
8484

85-
<ItemGroup>
86-
<Folder Include="Fakes\" />
87-
</ItemGroup>
88-
8985
<ItemGroup>
9086
<Compile Update="Properties\Settings.Designer.cs">
9187
<DesignTimeSharedInput>True</DesignTimeSharedInput>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Principal;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Csla.Configuration;
8+
using Csla.Security;
9+
using Csla.TestHelpers;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace Csla.Test.DataPortal
13+
{
14+
[TestClass]
15+
public class PrincipalFlowTests
16+
{
17+
[TestMethod]
18+
public void Fetch_FakeRemoteDataPortalWithDefaultFlow_DoesNotFlowPrincipal()
19+
{
20+
var principal = new CslaClaimsPrincipal(new GenericIdentity("rocky", "custom"));
21+
var testDIContext = TestDIContextFactory.CreateContext(opts => opts
22+
.DataPortal(dp => dp.UseFakeRemoteDataPortalProxy()),
23+
principal);
24+
25+
var dataPortal = testDIContext.CreateDataPortal<PrincipalInfo>();
26+
var info = dataPortal.Fetch();
27+
28+
Assert.IsFalse(info.IsAuthenticated);
29+
Assert.AreEqual(string.Empty, info.Name);
30+
}
31+
32+
[TestMethod]
33+
public void Fetch_FakeRemoteDataPortalWithFlowEnabled_FlowsPrincipal()
34+
{
35+
var principal = new CslaClaimsPrincipal(new GenericIdentity("rocky", "custom"));
36+
var testDIContext = TestDIContextFactory.CreateContext(opts => opts
37+
.DataPortal(dp => {
38+
dp.UseFakeRemoteDataPortalProxy();
39+
dp.EnableSecurityPrincipalFlowFromClient();
40+
}),
41+
principal) ;
42+
43+
var dataPortal = testDIContext.CreateDataPortal<PrincipalInfo>();
44+
var info = dataPortal.Fetch();
45+
46+
Assert.IsTrue(info.IsAuthenticated);
47+
Assert.AreEqual(principal.Identity.Name, info.Name);
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Csla.Test.DataPortal
8+
{
9+
internal class PrincipalInfo : ReadOnlyBase<PrincipalInfo>
10+
{
11+
public static PropertyInfo<bool> IsAuthenticatedProperty = RegisterProperty<bool>(nameof(IsAuthenticated));
12+
public static PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
13+
14+
public bool IsAuthenticated
15+
{
16+
get { return GetProperty(IsAuthenticatedProperty); }
17+
private set { LoadProperty(IsAuthenticatedProperty, value); }
18+
}
19+
20+
public string Name
21+
{
22+
get { return GetProperty(NameProperty); }
23+
private set { LoadProperty(NameProperty, value); }
24+
}
25+
26+
[Fetch]
27+
private void Fetch()
28+
{
29+
IsAuthenticated = ApplicationContext.Principal.Identity?.IsAuthenticated ?? false;
30+
Name = ApplicationContext.Principal.Identity?.Name ?? string.Empty;
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Csla.Channels.Local;
2+
using Csla.DataPortalClient;
3+
using Csla.Test.Fakes.Server.DataPortal;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Csla.Configuration
7+
{
8+
/// <summary>
9+
/// Extension methods for the DataPortalClientOptions to make FakeRemoteDataPortalProxy easier to use
10+
/// </summary>
11+
internal static class FakeDataPortalProxyExtensions
12+
{
13+
/// <summary>
14+
/// Configure data portal client to use fake remote data portal proxy.
15+
/// </summary>
16+
/// <param name="config">CslaDataPortalConfiguration object being extended</param>
17+
public static DataPortalClientOptions UseFakeRemoteDataPortalProxy(this DataPortalClientOptions config)
18+
{
19+
config.Services.AddTransient(typeof(IDataPortalProxy),
20+
sp =>
21+
{
22+
var implementingProxy = sp.GetRequiredService<LocalProxy>();
23+
return new FakeRemoteDataPortalProxy(implementingProxy);
24+
});
25+
return config;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Csla.Channels.Local;
7+
using Csla.DataPortalClient;
8+
using Csla.Server;
9+
10+
namespace Csla.Test.Fakes.Server.DataPortal
11+
{
12+
/// <summary>
13+
/// Fake proxy implementation that can be used to represent a remote proxy
14+
/// for testing, without the need to step outside of the AppDomain
15+
/// </summary>
16+
internal class FakeRemoteDataPortalProxy : IDataPortalProxy
17+
{
18+
private readonly LocalProxy _implementingProxy;
19+
20+
public FakeRemoteDataPortalProxy(LocalProxy implementingProxy)
21+
{
22+
_implementingProxy = implementingProxy;
23+
}
24+
25+
public bool IsServerRemote { get => true; }
26+
27+
public Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
28+
=> _implementingProxy.Create(objectType, criteria, context, isSync);
29+
30+
public Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
31+
=> _implementingProxy.Delete(objectType, criteria, context, isSync);
32+
33+
public Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
34+
=> _implementingProxy.Fetch(objectType, criteria, context, isSync);
35+
36+
public Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
37+
=> _implementingProxy.Update(obj, context, isSync);
38+
}
39+
}

0 commit comments

Comments
 (0)