Skip to content

Commit

Permalink
Increase code coverage for subscription logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Jan 28, 2024
1 parent 6fa4bf9 commit f49c5ba
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 63 deletions.
9 changes: 0 additions & 9 deletions src/FakeXrmEasy.Core/CommercialLicense/SubscriptionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,5 @@ internal class SubscriptionInfo: ISubscriptionInfo
/// The subscription's end date
/// </summary>
public DateTime EndDate { get; set; }

/// <summary>
///
/// </summary>
/// <param name="licenseKey"></param>
internal void FromLicenseKey(string licenseKey)
{

}
}
}
56 changes: 2 additions & 54 deletions src/FakeXrmEasy.Core/CommercialLicense/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,15 @@ internal static class SubscriptionManager

internal static bool _renewalRequested = false;
internal static bool _upgradeRequested = false;

private static string GenerateHash(string input)
{
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);

StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}

private static ISubscriptionInfo GetSubscriptionInfoFromKey(string licenseKey)
{
try
{
var encodedBaseKey = licenseKey.Substring(0, licenseKey.Length - 32);
var hash = licenseKey.Substring(licenseKey.Length - 32, 32);
var computedHash = GenerateHash(encodedBaseKey);

if (!computedHash.Equals(hash))
{
throw new InvalidLicenseKeyException();
}

var decodedBaseKey = Encoding.UTF8.GetString(Convert.FromBase64String(encodedBaseKey));
var baseKeyParts = decodedBaseKey.Split('-');

var expiryDate = DateTime.ParseExact(baseKeyParts[4], "yyyyMMdd", CultureInfo.InvariantCulture);
var numberOfUsers = int.Parse(baseKeyParts[3]);

var sku = (StockKeepingUnits) Enum.Parse(typeof(StockKeepingUnits), baseKeyParts[0]);
var autoRenews = "1".Equals(baseKeyParts[2]);

return new SubscriptionInfo()
{
SKU = sku,
CustomerId = baseKeyParts[1],
NumberOfUsers = numberOfUsers,
EndDate = expiryDate,
AutoRenews = autoRenews
};
}
catch
{
throw new InvalidLicenseKeyException();
}
}

private static void SetLicenseKey(string licenseKey)
{
lock (_subscriptionInfoLock)
{
if (_subscriptionInfo == null)
{
_subscriptionInfo = GetSubscriptionInfoFromKey(licenseKey);
var subscriptionPlanManager = new SubscriptionPlanManager();
_subscriptionInfo = subscriptionPlanManager.GetSubscriptionInfoFromKey(licenseKey);
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions src/FakeXrmEasy.Core/CommercialLicense/SubscriptionPlanManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Globalization;
using System.Text;
using FakeXrmEasy.Abstractions.CommercialLicense;
using FakeXrmEasy.Core.CommercialLicense.Exceptions;

namespace FakeXrmEasy.Core.CommercialLicense
{
internal class SubscriptionPlanManager
{
private static string GenerateHash(string input)
{
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);

StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}

internal ISubscriptionInfo GetSubscriptionInfoFromKey(string licenseKey)
{
try
{
var encodedBaseKey = licenseKey.Substring(0, licenseKey.Length - 32);
var hash = licenseKey.Substring(licenseKey.Length - 32, 32);
var computedHash = GenerateHash(encodedBaseKey);

if (!computedHash.Equals(hash))
{
throw new InvalidLicenseKeyException();
}

var decodedBaseKey = Encoding.UTF8.GetString(Convert.FromBase64String(encodedBaseKey));
var baseKeyParts = decodedBaseKey.Split('-');

var expiryDate = DateTime.ParseExact(baseKeyParts[4], "yyyyMMdd", CultureInfo.InvariantCulture);
var numberOfUsers = int.Parse(baseKeyParts[3]);

var sku = (StockKeepingUnits) Enum.Parse(typeof(StockKeepingUnits), baseKeyParts[0]);
var autoRenews = "1".Equals(baseKeyParts[2]);

return new SubscriptionInfo()
{
SKU = sku,
CustomerId = baseKeyParts[1],
NumberOfUsers = numberOfUsers,
EndDate = expiryDate,
AutoRenews = autoRenews
};
}
catch
{
throw new InvalidLicenseKeyException();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FakeXrmEasy.Core.CommercialLicense;
using FakeXrmEasy.Core.CommercialLicense.Exceptions;
using Xunit;

namespace FakeXrmEasy.Core.Tests.CommercialLicense
{
public class SubscriptionPlanManagerTests
{
private readonly SubscriptionPlanManager _subscriptionPlanManager;

public SubscriptionPlanManagerTests()
{
_subscriptionPlanManager = new SubscriptionPlanManager();
}

[Fact]
public void Should_raise_invalid_license_key_exception()
{
var invalidKey =
"asdasdkjakdhu38768a79aysdaiushdakjshdajshda79878s97d89as7d9a87sda98sdyausydusydausdajbdahsdjhasgdahsgda78sda8s7d6a986d98as6d9a8d";

Assert.Throws<InvalidLicenseKeyException>(() => _subscriptionPlanManager.GetSubscriptionInfoFromKey(invalidKey));
}
}
}
21 changes: 21 additions & 0 deletions tests/FakeXrmEasy.Core.Tests/CommercialLicense/UserReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FakeXrmEasy.Core.CommercialLicense;
using Xunit;

namespace FakeXrmEasy.Core.Tests.CommercialLicense
{
public class UserReaderTests
{
private readonly UserReader _userReader;

public UserReaderTests()
{
_userReader = new UserReader();
}

[Fact]
public void Should_return_current_user()
{
Assert.Equal(System.Security.Principal.WindowsIdentity.GetCurrent().Name, _userReader.GetCurrentUserName());
}
}
}

0 comments on commit f49c5ba

Please sign in to comment.