Skip to content

Commit

Permalink
Merge pull request #2932 from TheCakeMonster/CSLA6_AuthTestFixes
Browse files Browse the repository at this point in the history
Fixes to more base tests in line with CSLA 6
  • Loading branch information
rockfordlhotka authored May 11, 2022
2 parents d5afe45 + 56b4460 commit 535aa4e
Show file tree
Hide file tree
Showing 33 changed files with 439 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.9" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Source/Csla.Blazor.Test/Csla.Blazor.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.9" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.9" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
13 changes: 13 additions & 0 deletions Source/Csla.TestHelpers/ApplicationContextManagerUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Csla.TestHelpers
{
public class ApplicationContextManagerUnitTests : Csla.Core.ApplicationContextManagerAsyncLocal
{
public Guid InstanceId { get; private set; } = Guid.NewGuid();

public DateTime CreatedAt { get; private set; } = DateTime.Now;
}
}
4 changes: 2 additions & 2 deletions Source/Csla.TestHelpers/TestDIContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions)
/// <returns>A TestDIContext that can be used to perform testing dependent upon DI</returns>
public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions, ClaimsPrincipal principal)
{
ServiceProvider serviceProvider;
IServiceProvider serviceProvider;
ApplicationContext context;

// Initialise DI
var services = new ServiceCollection();

// Add Csla
services.TryAddScoped<Core.IContextManager, Core.ApplicationContextManager>();
services.TryAddSingleton<Core.IContextManager, ApplicationContextManagerUnitTests>();
services.TryAddSingleton<Server.Dashboard.IDashboard, Server.Dashboard.Dashboard>();
services.AddCsla(customCslaOptions);

Expand Down
6 changes: 4 additions & 2 deletions Source/Csla.test/AppContext/TestContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading;
Expand All @@ -14,6 +15,7 @@ public class TestContextManager : IContextManager
{
[ThreadStatic]
private static HybridDictionary _myContext = new HybridDictionary();
private readonly AsyncLocal<IPrincipal> _principal = new();

private const string _localContextName = "Csla.ClientContext";
private const string _clientContextName = "Csla.ClientContext";
Expand All @@ -30,7 +32,7 @@ public bool IsValid

public IPrincipal GetUser()
{
IPrincipal result = Thread.CurrentPrincipal;
IPrincipal result = _principal.Value;
if (result == null)
{
result = new System.Security.Claims.ClaimsPrincipal();
Expand All @@ -41,7 +43,7 @@ public IPrincipal GetUser()

public void SetUser(IPrincipal principal)
{
Thread.CurrentPrincipal = principal;
_principal.Value = principal;
}

public ContextDictionary GetLocalContext()
Expand Down
50 changes: 30 additions & 20 deletions Source/Csla.test/Authorization/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,16 @@ public void TestAuthCloneRules()
[TestMethod()]
public void TestAuthBeginEditRules()
{
IDataPortal<DataPortal.DpRoot> dataPortal = _adminDIContext.CreateDataPortal<DataPortal.DpRoot>();
Guid managerInstanceId;
TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
IDataPortal<DataPortal.DpRoot> dataPortal = customDIContext.CreateDataPortal<DataPortal.DpRoot>();
ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();

TestResults.Reinitialise();

DataPortal.DpRoot root = dataPortal.Create(new DataPortal.DpRoot.Criteria());

Assert.AreEqual(true, System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.AreEqual(true, applicationContext.Principal.IsInRole("Admin"));

root.Data = "Something new";

Expand All @@ -159,6 +162,10 @@ public void TestAuthBeginEditRules()
root.Data = "Something new 1";

//Is it denying read properly?
managerInstanceId = ((ApplicationContextManagerUnitTests)applicationContext.ContextManager).InstanceId;
Debug.WriteLine(managerInstanceId);
string result = root.DenyReadOnProperty;
//Assert.AreEqual(managerInstanceId.ToString(), result);
Assert.AreEqual("[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty,
"Read should have been denied");

Expand Down Expand Up @@ -259,7 +266,9 @@ public void TestAuthBeginEditRules()
[TestMethod()]
public void TestAuthorizationAfterEditCycle()
{
IDataPortal<PermissionsRoot> dataPortal = _adminDIContext.CreateDataPortal<PermissionsRoot>();
TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
IDataPortal<PermissionsRoot> dataPortal = customDIContext.CreateDataPortal<PermissionsRoot>();
ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();

TestResults.Reinitialise();

Expand All @@ -271,11 +280,10 @@ public void TestAuthorizationAfterEditCycle()
pr.FirstName = "ba";
pr.CancelEdit();

// TODO: This test no longer makes any sense; can't do this can we?
// Csla.ApplicationContext.User = new ClaimsPrincipal();
applicationContext.User = new ClaimsPrincipal();

PermissionsRoot prClone = pr.Clone();
// Csla.ApplicationContext.User = GetPrincipal("Admin");
applicationContext.User = GetPrincipal("Admin");
prClone.FirstName = "somethiansdfasdf";
}

Expand Down Expand Up @@ -308,7 +316,9 @@ public void TestUnauthorizedAccessToSet()
[TestMethod]
public void TestAuthorizedAccess()
{
IDataPortal<PermissionsRoot> dataPortal = _adminDIContext.CreateDataPortal<PermissionsRoot>();
TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
IDataPortal<PermissionsRoot> dataPortal = customDIContext.CreateDataPortal<PermissionsRoot>();
ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();

TestResults.Reinitialise();

Expand All @@ -318,33 +328,33 @@ public void TestAuthorizedAccess()
pr.FirstName = "something";
string something = pr.FirstName;

Assert.AreEqual(true, System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.AreEqual(true, applicationContext.Principal.IsInRole("Admin"));

// TODO: This no longer makes sense; can't do this anymore?
//set to null so the other testmethods continue to throw exceptions
//Csla.ApplicationContext.User = new ClaimsPrincipal();
applicationContext.User = new ClaimsPrincipal();

Assert.AreEqual(false, System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.AreEqual(false, applicationContext.Principal.IsInRole("Admin"));
}

[TestMethod]
public void TestAuthExecute()
{
IDataPortal<PermissionsRoot> dataPortal = _adminDIContext.CreateDataPortal<PermissionsRoot>();
TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
IDataPortal<PermissionsRoot> dataPortal = customDIContext.CreateDataPortal<PermissionsRoot>();
ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();

TestResults.Reinitialise();

PermissionsRoot pr = dataPortal.Create();
//should work, because we are now logged in as an admin
pr.DoWork();

Assert.AreEqual(true, System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.AreEqual(true, applicationContext.Principal.IsInRole("Admin"));

// TODO: This no longer makes sense; can't do this anymore?
//set to null so the other testmethods continue to throw exceptions
//Csla.ApplicationContext.User = new ClaimsPrincipal();
applicationContext.User = new ClaimsPrincipal();

Assert.AreEqual(false, System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.AreEqual(false, applicationContext.Principal.IsInRole("Admin"));
}

[TestMethod]
Expand Down Expand Up @@ -374,8 +384,8 @@ public void TestAuthRuleSetsOnStaticHasPermissionMethodsWhenAddingAuthzRuleSetEx

var root = dataPortal.Create();

Assert.IsTrue(System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.IsFalse(System.Threading.Thread.CurrentPrincipal.IsInRole("User"));
Assert.IsTrue(applicationContext.Principal.IsInRole("Admin"));
Assert.IsFalse(applicationContext.Principal.IsInRole("User"));

// implicit usage of ApplicationContext.RuleSet
applicationContext.RuleSet = ApplicationContext.DefaultRuleSet;
Expand Down Expand Up @@ -403,8 +413,8 @@ public void TestAuthRuleSetsOnStaticHasPermissionMethodsWhenAddingAuthzRuleSetUs

var root = dataPortal.Create();

Assert.IsTrue(System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
Assert.IsFalse(System.Threading.Thread.CurrentPrincipal.IsInRole("User"));
Assert.IsTrue(applicationContext.Principal.IsInRole("Admin"));
Assert.IsFalse(applicationContext.Principal.IsInRole("User"));

//BusinessRules.AddRule(typeof(PermissionsRoot), new Csla.Rules.CommonRules.IsInRole(AuthorizationActions.DeleteObject, "User"), ApplicationContext.DefaultRuleSet);
//BusinessRules.AddRule(typeof(PermissionsRoot), new Csla.Rules.CommonRules.IsInRole(AuthorizationActions.DeleteObject, "Admin"), "custom1");
Expand Down
23 changes: 5 additions & 18 deletions Source/Csla.test/Authorization/PermissionsRoot2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,18 @@ public void DoWork()

public static void AddObjectAuthorizationRules()
{
// add rules for multiple rulesets
BusinessRules.AddRule(typeof(PermissionsRoot2), new IsInRole(AuthorizationActions.DeleteObject, "User"));
BusinessRules.AddRule(typeof(PermissionsRoot2), new IsInRole(AuthorizationActions.DeleteObject, "Admin"), "custom1");
BusinessRules.AddRule(typeof(PermissionsRoot2), new IsInRole(AuthorizationActions.DeleteObject, "User", "Admin"), "custom2");
}

protected override void AddBusinessRules()
{
BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Rules.AuthorizationActions.ReadProperty, FirstNameProperty, new List<string> { "Admin" }));
BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Rules.AuthorizationActions.WriteProperty, FirstNameProperty, new List<string> { "Admin" }));
BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Rules.AuthorizationActions.ExecuteMethod, DoWorkMethod, new List<string> { "Admin" }));

// TODO: I moved this from AddObjectAuthorizationRules; is that right?
//// add rules for default ruleset
var orgRuleSet = BusinessRules.RuleSet;
try
{
BusinessRules.RuleSet = ApplicationContext.DefaultRuleSet;
BusinessRules.AddRule(typeof(PermissionsRoot2), new IsInRole(AuthorizationActions.DeleteObject, "User"));
BusinessRules.RuleSet = "custom1";
BusinessRules.AddRule(typeof(PermissionsRoot2), new IsInRole(AuthorizationActions.DeleteObject, "Admin"));
BusinessRules.RuleSet = "custom2";
BusinessRules.AddRule(typeof(PermissionsRoot2), new IsInRole(AuthorizationActions.DeleteObject, "User", "Admin"));
}
finally
{
BusinessRules.RuleSet = orgRuleSet;
}
}
}

#endregion

Expand Down
25 changes: 9 additions & 16 deletions Source/Csla.test/Basic/Child.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,19 @@ namespace Csla.Test.Basic
[Serializable()]
public class Child : BusinessBase<Child>
{
private string _data = "";
private Guid _guid = System.Guid.NewGuid();

public static PropertyInfo<string> DataProperty = RegisterProperty<string>(nameof(Data));
public static PropertyInfo<Guid> GuidProperty = RegisterProperty<Guid>(nameof(Guid));
public static PropertyInfo<GrandChildren> GrandChildrenProperty = RegisterProperty<GrandChildren>(c => c.GrandChildren);

protected override object GetIdValue()
{
return _data;
return ReadProperty(DataProperty);
}

public string Data
{
get { return _data; }
set
{
if (_data != value)
{
_data = value;
MarkDirty();
}
}
get { return GetProperty(DataProperty); }
set { SetProperty(DataProperty, value); }
}

public override bool Equals(object obj)
Expand All @@ -45,7 +37,7 @@ public override bool Equals(object obj)
return false;
}

return _data == ((Child)(obj))._data;
return ReadProperty(DataProperty) == ((Child)(obj)).ReadProperty(DataProperty);
}

public override int GetHashCode()
Expand All @@ -55,7 +47,7 @@ public override int GetHashCode()

public Guid Guid
{
get { return _guid; }
get { return ReadProperty(GuidProperty); }
}

public GrandChildren GrandChildren
Expand Down Expand Up @@ -84,7 +76,8 @@ public Child()
[CreateChild]
private void Create(string data, [Inject] IChildDataPortal<GrandChildren> childDataPortal)
{
_data = data;
LoadProperty(GuidProperty, Guid.NewGuid());
LoadProperty(DataProperty, data);
LoadProperty(GrandChildrenProperty, childDataPortal.CreateChild());
}

Expand Down
Loading

0 comments on commit 535aa4e

Please sign in to comment.