Skip to content

Commit

Permalink
#3395 Auth and data portal tests now passing
Browse files Browse the repository at this point in the history
  • Loading branch information
rockfordlhotka committed Dec 24, 2023
1 parent 4da719f commit 7a15a9e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Source/Csla.TestHelpers/TestResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Csla.Test
/// </summary>
public class TestResults
{
private static AsyncLocal<Dictionary<string, string>> _testResults = new AsyncLocal<Dictionary<string, string>>();
private static readonly AsyncLocal<Dictionary<string, string>> _testResults = new();

/// <summary>
/// Add an item to the test results, to indicate an outcome of a particular operation
Expand Down
22 changes: 5 additions & 17 deletions Source/Csla.test/Authorization/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System.Security.Claims;
using Csla.TestHelpers;
using Csla.Configuration;
using Microsoft.Extensions.DependencyInjection;


#if NUNIT
using NUnit.Framework;
Expand Down Expand Up @@ -483,7 +485,6 @@ public void PerTypeAuthEditObject()
/// the data portal with an interface and have that translate
/// to a concrete type is missing. Should be resolved in #3557
/// </summary>
[Ignore]
[TestMethod]
public void PerTypeAuthEditObjectViaInterface()
{
Expand Down Expand Up @@ -581,27 +582,14 @@ public void PerTypeAuthDeleteWithCriteria()
}
}

public class PerTypeAuthDPActivator : Server.IDataPortalActivator
public class PerTypeAuthDPActivator(IServiceProvider serviceProvider) : Server.DefaultDataPortalActivator(serviceProvider)
{
public object CreateInstance(Type requestedType)
{
return Activator.CreateInstance(ResolveType(requestedType));
}

public void FinalizeInstance(object obj)
{
}

public void InitializeInstance(object obj)
{
}

public Type ResolveType(Type requestedType)
public override Type ResolveType(Type requestedType)
{
if (requestedType.Equals(typeof(IPerTypeAuthRoot)))
return typeof(PerTypeAuthRoot);
else
return requestedType;
return base.ResolveType(requestedType);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Csla.test/DataPortal/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Test_DataPortal_Int_Null()
if (ex.InnerException != null)
throw ex.InnerException;
else
throw ex;
throw;
}
}

Expand All @@ -88,7 +88,7 @@ public void Test_DataPortal_String_Null()
if (ex.InnerException != null)
throw ex.InnerException;
else
throw ex;
throw;
}
}

Expand Down
31 changes: 0 additions & 31 deletions Source/Csla.test/DataPortal/DataPortalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,37 +205,6 @@ public void FactoryIsBusyFails()
Assert.Fail("Expected exception");
}

// TODO: Is this a relevant concept any more? These events do not seem to be exposed
[TestMethod()]
public void DataPortalEvents()
{
IDataPortal<DpRoot> dataPortal = _testDIContext.CreateDataPortal<DpRoot>();

// TODO: Not sure how to replicate this in Csla 6
//dataPortal.DataPortalInvoke += new Action<DataPortalEventArgs>(ClientPortal_DataPortalInvoke);
//dataPortal.DataPortalInvokeComplete += new Action<DataPortalEventArgs>(ClientPortal_DataPortalInvokeComplete);

try
{
DpRoot root = dataPortal.Create(new DpRoot.Criteria());

root.Data = "saved";
TestResults.Reinitialise();
root = root.Save();

Assert.AreEqual("true", TestResults.GetResult("dpinvoke"), "DataPortalInvoke not called");
Assert.AreEqual("true", TestResults.GetResult("dpinvokecomplete"), "DataPortalInvokeComplete not called");
Assert.AreEqual("true", TestResults.GetResult("serverinvoke"), "Server DataPortalInvoke not called");
Assert.AreEqual("true", TestResults.GetResult("serverinvokecomplete"), "Server DataPortalInvokeComplete not called");
}
finally
{
// TODO: Not sure how to replicate this in Csla 6
//dataPortal.DataPortalInvoke -= new Action<DataPortalEventArgs>(ClientPortal_DataPortalInvoke);
//dataPortal.DataPortalInvokeComplete -= new Action<DataPortalEventArgs>(ClientPortal_DataPortalInvokeComplete);
}
}

[TestMethod]
public void DataPortalBrokerTests()
{
Expand Down
21 changes: 19 additions & 2 deletions Source/Csla.test/DataPortal/DisposeScopeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class DisposeScopeTest
{

[TestMethod]
public void Test_Scope_DoesNotDispose()
public void Test_Scope_DoesNotDisposeWithNoLocalScope()
{
// CSLA should not dispose of the default service provider.
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<DisposableClass>();
serviceCollection.AddCsla();
serviceCollection.AddCsla(o => o.DataPortal((dpo => dpo.UseLocalProxy(lpo => lpo.UseLocalScope = false))));

var services = serviceCollection.BuildServiceProvider();
IDataPortal<ClassA> dataPortal = services.GetRequiredService<IDataPortal<ClassA>>();
Expand All @@ -32,6 +32,23 @@ public void Test_Scope_DoesNotDispose()
Assert.IsFalse(classA.DisposableClass.IsDisposed, "Object must not be disposed");
}

[TestMethod]
public void Test_Scope_DoesDisposeWithLocalScope()
{
// CSLA should dispose of the temporary server-side service provider.
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<DisposableClass>();
serviceCollection.AddCsla();

var services = serviceCollection.BuildServiceProvider();
IDataPortal<ClassA> dataPortal = services.GetRequiredService<IDataPortal<ClassA>>();

var classA = dataPortal.Fetch();
var classB = classA.ChildB;

Assert.AreEqual(classA.DisposableClass.Id, classB.DisposableClass.Id, "Ids must be the same");
Assert.IsTrue(classA.DisposableClass.IsDisposed, "Object must not be disposed");
}
}

public class DisposableClass
Expand Down
35 changes: 24 additions & 11 deletions Source/Csla.test/DataPortal/InterceptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Csla.Configuration;
using Csla.Core;
using Csla.TestHelpers;
#if !NUNIT && !ANDROID
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void UpdateListWithIntercept()
Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeListRoot"), "InitializeInstance (list) should have run");

Assert.IsFalse(TestResults.ContainsResult("Activate1+InitializeRoot"), "CreateInstance should not have run");
Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeRoot"), "InitializeInstance should have run");
Assert.IsFalse(TestResults.ContainsResult("Activate2+InitializeRoot"), "InitializeInstance should not have run");
}

[TestMethod]
Expand Down Expand Up @@ -261,27 +262,39 @@ public void Complete(Server.InterceptArgs e)
}
}

public class TestActivator : Csla.Server.IDataPortalActivator
public class TestActivator(IServiceProvider serviceProvider) : Csla.Server.DefaultDataPortalActivator(serviceProvider)
{
public object CreateInstance(Type requestedType)
public override object CreateInstance(Type requestedType, params object[] parameters)
{
TestResults.Add("Activate1+" + requestedType.Name, "CreateInstance");
return Activator.CreateInstance(requestedType);
if (requestedType.IsAssignableTo(typeof(IBusinessObject)))
{
TestResults.AddOrOverwrite("Activate1+" + requestedType.Name, "CreateInstance");
}
return base.CreateInstance(requestedType, parameters);
}

public void InitializeInstance(object obj)
public override void InitializeInstance(object obj)
{
TestResults.Add("Activate2+" + obj.GetType().Name, "InitializeInstance");
if (obj.GetType().IsAssignableTo(typeof(IBusinessObject)))
{
TestResults.AddOrOverwrite("Activate2+" + obj.GetType().Name, "InitializeInstance");
}
}

public void FinalizeInstance(object obj)
public override void FinalizeInstance(object obj)
{
TestResults.Add("Activate3+" + obj.GetType().Name, "FinalizeInstance");
if (obj.GetType().IsAssignableTo(typeof(IBusinessObject)))
{
TestResults.AddOrOverwrite("Activate3+" + obj.GetType().Name, "FinalizeInstance");
}
}

public Type ResolveType(Type requestedType)
public override Type ResolveType(Type requestedType)
{
TestResults.Add("Activate4+" + requestedType.Name, "ResolveType");
if (requestedType.IsAssignableTo(typeof(IBusinessObject)))
{
TestResults.AddOrOverwrite("Activate4+" + requestedType.Name, "ResolveType");
}
return requestedType;
}
}
Expand Down

0 comments on commit 7a15a9e

Please sign in to comment.