forked from dnnsoftware/Dnn.Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a serialization issue with PermissionInfo
This is a work in progress
- Loading branch information
Showing
5 changed files
with
212 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
DNN Platform/Tests/DotNetNuke.Tests.Core/Security/Permissions/PermissionInfoBaseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information | ||
|
||
namespace DotNetNuke.Tests.Core.Security.Permissions | ||
{ | ||
using DotNetNuke.Common.Utilities; | ||
using DotNetNuke.Security.Permissions; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class PermissionInfoBaseTests | ||
{ | ||
[Test] | ||
public void SerializesXMLProperly() | ||
{ | ||
// Arrange | ||
var derived = new DerivedPermissionInfo | ||
{ | ||
AllowAccess = true, | ||
DisplayName = "Test Name", | ||
RoleID = 123, | ||
RoleName = "Test Role", | ||
UserID = 234, | ||
Username = "Test User", | ||
}; | ||
|
||
// Act | ||
var xml = XmlUtils.Serialize(derived); | ||
|
||
// Assert | ||
Assert.IsNotNull(xml); | ||
Assert.True(xml.Contains("allowaccess")); | ||
Assert.True(xml.Contains("displayname")); | ||
Assert.True(xml.Contains("roleid")); | ||
Assert.True(xml.Contains("rolename")); | ||
Assert.True(xml.Contains("userid")); | ||
Assert.True(xml.Contains("username")); | ||
} | ||
} | ||
|
||
public class DerivedPermissionInfo : PermissionInfoBase | ||
{ | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
DNN Platform/Tests/DotNetNuke.Tests.Core/Security/Permissions/PermissionInfoTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information | ||
|
||
namespace DotNetNuke.Tests.Core.Security.Permissions | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
using DotNetNuke.Abstractions.Security.Permissions; | ||
using DotNetNuke.Common.Utilities; | ||
using DotNetNuke.Security.Permissions; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class PermissionInfoTests | ||
{ | ||
[Test] | ||
public void SerializesJson_IncluddingObsoleteProperties() | ||
{ | ||
// Arrange | ||
var permissionInfo = new PermissionInfo | ||
{ | ||
ModuleDefID = 123, | ||
PermissionCode = "test", | ||
PermissionID = 456, | ||
PermissionKey = "testKey", | ||
PermissionName = "testName", | ||
}; | ||
|
||
// Act | ||
var json = Json.Serialize(permissionInfo); | ||
|
||
// Assert | ||
Assert.NotNull(json); | ||
Assert.False(json.Contains(nameof(permissionInfo.ModuleDefId))); | ||
Assert.True(json.Contains(nameof(permissionInfo.PermissionCode))); | ||
Assert.True(json.Contains(nameof(permissionInfo.PermissionID))); // old obsolete casing. | ||
Assert.True(json.Contains(nameof(permissionInfo.PermissionId))); // new casing. | ||
Assert.True(json.Contains(nameof(permissionInfo.PermissionKey))); | ||
Assert.False(json.Contains(nameof(permissionInfo.PermissionName))); | ||
} | ||
|
||
[Test] | ||
public void DeserializesJson_Properly() | ||
{ | ||
// Arrange | ||
var json = "{\"PermissionCode\":\"test\",\"PermissionID\":456,\"PermissionKey\":\"testKey\"}"; | ||
|
||
// Act | ||
var permissionInfo = Json.Deserialize<PermissionInfo>(json); | ||
|
||
// Assert | ||
Assert.NotNull(permissionInfo); | ||
Assert.AreEqual("test", permissionInfo.PermissionCode); | ||
Assert.AreEqual(456, permissionInfo.PermissionID); | ||
Assert.AreEqual(456, ((IPermissionDefinitionInfo)permissionInfo).PermissionId); | ||
Assert.AreEqual("testKey", permissionInfo.PermissionKey); | ||
} | ||
|
||
[Test] | ||
public void SerializesXml_IncludesObsoleteProperties() | ||
{ | ||
// Arrange | ||
var permissionInfo = new PermissionInfo | ||
{ | ||
ModuleDefID = 123, | ||
PermissionCode = "test", | ||
PermissionID = 456, | ||
PermissionKey = "testKey", | ||
PermissionName = "testName", | ||
}; | ||
// Act | ||
var xml = XmlUtils.Serialize(permissionInfo); | ||
|
||
// Assert | ||
Assert.NotNull(xml); | ||
Assert.False(xml.IndexOf("moduledefid", StringComparison.OrdinalIgnoreCase) >= 0); | ||
Assert.True(xml.Contains("permissioncode")); | ||
Assert.True(xml.Contains("permissionid")); | ||
Assert.True(xml.Contains("permissionkey")); | ||
Assert.False(xml.IndexOf("permissionname", StringComparison.OrdinalIgnoreCase) >= 0); | ||
} | ||
|
||
[Test] | ||
public void Deserializes_Properly() | ||
{ | ||
// Arrange | ||
var xml = "<PermissionInfo><permissioncode>test</permissioncode><permissionid>456</permissionid><permissionkey>testKey</permissionkey></PermissionInfo>"; | ||
var stream = new MemoryStream(); | ||
var writer = new StreamWriter(stream, Encoding.UTF8); | ||
writer.Write(xml); | ||
writer.Flush(); | ||
stream.Position = 0; | ||
|
||
// Act | ||
var permissionInfo = (PermissionInfo)XmlUtils.Deserialize(stream, typeof(PermissionInfo)); | ||
|
||
// Assert | ||
Assert.NotNull(permissionInfo); | ||
Assert.AreEqual("test", permissionInfo.PermissionCode); | ||
Assert.AreEqual(456, permissionInfo.PermissionID); | ||
Assert.AreEqual(456, ((IPermissionDefinitionInfo)permissionInfo).PermissionId); | ||
Assert.AreEqual("testKey", permissionInfo.PermissionKey); | ||
} | ||
} | ||
} |