-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase code coverage for subscription logic
- Loading branch information
1 parent
6fa4bf9
commit f49c5ba
Showing
5 changed files
with
112 additions
and
63 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
64 changes: 64 additions & 0 deletions
64
src/FakeXrmEasy.Core/CommercialLicense/SubscriptionPlanManager.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,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(); | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/FakeXrmEasy.Core.Tests/CommercialLicense/SubscriptionPlanManagerTests.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,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
21
tests/FakeXrmEasy.Core.Tests/CommercialLicense/UserReaderTests.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,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()); | ||
} | ||
} | ||
} |