Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
Add Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Sep 27, 2024
1 parent 6c7acf8 commit 6c7db48
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ To properly set up and use the Client Assertion Generator in your ASP.NET Core a

Then you can use `ClientAssertionGeneratorService`, which provides the following methods:
- `GetClientAssertionAsync`
- `GetToken(clientAssertion)`
- `GetTokenAsync(clientAssertion)`

## Testing the PDNDClientAssertionGenerator
This project includes a test application, **PDNDClientAssertionGenerator.Api**, designed to help you test the software with your own configuration. This application acts as a sandbox where you can validate the behavior of the PDNDClientAssertionGenerator components.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<string> GetClientAssertionAsync()
[HttpGet("GetToken", Name = "GetToken")]
public async Task<PDNDTokenResponse> GetToken([FromQuery] string clientAssertion)
{
return await _clientAssertionGenerator.GetToken(clientAssertion);
return await _clientAssertionGenerator.GetTokenAsync(clientAssertion);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task GetToken_ShouldCall_RequestAccessTokenAsync_WithClientAssertio
var clientAssertion = "testClientAssertion";

// Act
await clientAssertionGeneratorService.GetToken(clientAssertion);
await clientAssertionGeneratorService.GetTokenAsync(clientAssertion);

// Assert
oauth2ServiceMock.Verify(o => o.RequestAccessTokenAsync(clientAssertion), Times.Once);
Expand Down
52 changes: 52 additions & 0 deletions src/PDNDClientAssertionGenerator.Tests/DateTimeUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// (c) 2024 Francesco Del Re <francesco.delre.87@gmail.com>
// This code is licensed under MIT license (see LICENSE.txt for details)
using PDNDClientAssertionGenerator.Utils;

namespace PDNDClientAssertionGenerator.Tests
{
public class DateTimeUtilsTests
{
[Fact]
public void ToUnixTimestamp_ShouldReturnCorrectUnixTimestamp_ForGivenDateTime()
{
// Arrange
DateTime dateTime = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc);
int expectedUnixTimestamp = 1704067200;

// Act
int actualUnixTimestamp = dateTime.ToUnixTimestamp();

// Assert
Assert.Equal(expectedUnixTimestamp, actualUnixTimestamp);
}

[Fact]
public void ToUnixTimestamp_ShouldHandleDateTimeInDifferentTimeZones_Correctly()
{
// Arrange
DateTime dateTimeLocal = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Local);
DateTime dateTimeUtc = dateTimeLocal.ToUniversalTime();
int expectedUnixTimestamp = (int)new DateTimeOffset(dateTimeUtc).ToUnixTimeSeconds();

// Act
int actualUnixTimestamp = dateTimeLocal.ToUnixTimestamp();

// Assert
Assert.Equal(expectedUnixTimestamp, actualUnixTimestamp);
}

[Fact]
public void ToUnixTimestamp_ShouldReturnCorrectUnixTimestamp_ForEpochStart()
{
// Arrange
DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
int expectedUnixTimestamp = 0;

// Act
int actualUnixTimestamp = epochStart.ToUnixTimestamp();

// Assert
Assert.Equal(expectedUnixTimestamp, actualUnixTimestamp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
65 changes: 65 additions & 0 deletions src/PDNDClientAssertionGenerator.Tests/SecurityUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// (c) 2024 Francesco Del Re <francesco.delre.87@gmail.com>
// This code is licensed under MIT license (see LICENSE.txt for details)
using PDNDClientAssertionGenerator.Utils;

namespace PDNDClientAssertionGenerator.Tests
{
public class SecurityUtilsTests
{
[Fact]
public void ExtractBase64Key_ShouldRemoveHeaderFooterAndWhitespace_FromValidPemContent()
{
// Arrange
string pemContent = @"
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAK5QpQfskLq1djoi0yRz4ksblMdxI0m5lBw9fAWvntA59NgIUlHw
fNhgUJmOygwoQ6dzQbPUZp0ZEOtR10Q+/gECAwEAAQJAR1XlnMvJ0IbG4P1Rb0P/
gRJyOgkMxybMfzoVr8f+f4IkH2XfsnBhdCdHkHhbtRfct+dM+7Rp3JFd+n+6IOCC
RQIhAPPAz9jYPX+3oBlfV92MdhLB3UjsoXTvGaMDHrG7PjCFAiEAuSMOFJNGFmRJ
kYmujA6SeyDEzJHpxEnbx9FA41gKkBcCIQDHaXsDBL2/WPBOFcOfTLNfBQXoTpEu
AfWRAd5Nvg0I9QIhAL3n2dfYFXAGpPCTg2MgttVfSe+oAapTknnCK6CRz58nAiEA
4lYtY4jOOBVZWz1vUpcsWgGVpRfyRbGmJfrJ6UAKfBM=
-----END RSA PRIVATE KEY-----
";

string expectedBase64Key = "MIIBOgIBAAJBAK5QpQfskLq1djoi0yRz4ksblMdxI0m5lBw9fAWvntA59NgIUlHwfNhgUJmOygwoQ6dzQbPUZp0ZEOtR10Q+/gECAwEAAQJAR1XlnMvJ0IbG4P1Rb0P/gRJyOgkMxybMfzoVr8f+f4IkH2XfsnBhdCdHkHhbtRfct+dM+7Rp3JFd+n+6IOCCRQIhAPPAz9jYPX+3oBlfV92MdhLB3UjsoXTvGaMDHrG7PjCFAiEAuSMOFJNGFmRJkYmujA6SeyDEzJHpxEnbx9FA41gKkBcCIQDHaXsDBL2/WPBOFcOfTLNfBQXoTpEuAfWRAd5Nvg0I9QIhAL3n2dfYFXAGpPCTg2MgttVfSe+oAapTknnCK6CRz58nAiEA4lYtY4jOOBVZWz1vUpcsWgGVpRfyRbGmJfrJ6UAKfBM=";

// Act
string actualBase64Key = SecurityUtils.ExtractBase64Key(pemContent);

// Assert
Assert.Equal(expectedBase64Key, actualBase64Key);
}

[Fact]
public void ExtractBase64Key_ShouldReturnEmptyString_WhenPemContentIsEmpty()
{
// Arrange
string pemContent = "";
string expectedBase64Key = "";

// Act
string actualBase64Key = SecurityUtils.ExtractBase64Key(pemContent);

// Assert
Assert.Equal(expectedBase64Key, actualBase64Key);
}

[Fact]
public void ExtractBase64Key_ShouldReturnEmptyString_WhenPemContentIsOnlyHeaderFooter()
{
// Arrange
string pemContent = @"
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
";
string expectedBase64Key = "";

// Act
string actualBase64Key = SecurityUtils.ExtractBase64Key(pemContent);

// Assert
Assert.Equal(expectedBase64Key, actualBase64Key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace PDNDClientAssertionGenerator.Interfaces
public interface IClientAssertionGenerator
{
Task<string> GetClientAssertionAsync();
Task<PDNDTokenResponse> GetToken(string clientAssertion);
Task<PDNDTokenResponse> GetTokenAsync(string clientAssertion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<string> GetClientAssertionAsync()

// Asynchronously requests an OAuth2 access token using the provided client assertion.
// Delegates the actual token request to the OAuth2 service.
public async Task<PDNDTokenResponse> GetToken(string clientAssertion)
public async Task<PDNDTokenResponse> GetTokenAsync(string clientAssertion)
{
return await _oauth2Service.RequestAccessTokenAsync(clientAssertion);
}
Expand Down
3 changes: 2 additions & 1 deletion src/PDNDClientAssertionGenerator/Utils/SecurityUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public static RSAParameters GetSecurityParameters(string keyPath)
/// </summary>
/// <param name="pemContent">The PEM formatted string.</param>
/// <returns>The base64 encoded key.</returns>
private static string ExtractBase64Key(string pemContent)
public static string ExtractBase64Key(string pemContent)
{
// Remove the header, footer, and any newlines or whitespaces
return pemContent
.Replace("-----BEGIN RSA PRIVATE KEY-----", string.Empty)
.Replace("-----END RSA PRIVATE KEY-----", string.Empty)
.Replace("\n", string.Empty)
.Replace("\r", string.Empty)
.Replace(" ", string.Empty)
.Trim();
}
}
Expand Down

0 comments on commit 6c7db48

Please sign in to comment.