Skip to content

Commit

Permalink
Replace FluentAssertions with Shouldly
Browse files Browse the repository at this point in the history
  • Loading branch information
damianh committed Feb 7, 2025
1 parent 72b6af3 commit e79a66b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,20 @@ public async Task FindClientByIdAsync_WhenClientExistsWithCollections_ExpectClie
client = await store.FindClientByIdAsync(testClient.ClientId);
}

client.ShouldBeEquivalentTo(testClient);
client.ShouldSatisfyAllConditions(c =>
{
c.ClientId.ShouldBe(testClient.ClientId);
c.ClientName.ShouldBe(testClient.ClientName);
c.AllowedCorsOrigins.ShouldBe(testClient.AllowedCorsOrigins);
c.AllowedGrantTypes.ShouldBe(testClient.AllowedGrantTypes, true);
c.AllowedScopes.ShouldBe(testClient.AllowedScopes, true);
c.Claims.ShouldBe(testClient.Claims);
c.ClientSecrets.ShouldBe(testClient.ClientSecrets, true);
c.IdentityProviderRestrictions.ShouldBe(testClient.IdentityProviderRestrictions);
c.PostLogoutRedirectUris.ShouldBe(testClient.PostLogoutRedirectUris);
c.Properties.ShouldBe(testClient.Properties);
c.RedirectUris.ShouldBe(testClient.RedirectUris);
});
}

[Theory, MemberData(nameof(TestDatabaseProviders))]
Expand Down Expand Up @@ -143,7 +156,13 @@ public async Task FindClientByIdAsync_WhenClientsExistWithManyCollections_Expect
#pragma warning disable xUnit1031 // Do not use blocking task operations in test method, suppressed because the task must have completed to enter this block
var client = task.Result;
#pragma warning restore xUnit1031 // Do not use blocking task operations in test method
client.ShouldBeEquivalentTo(testClient);
client.ShouldSatisfyAllConditions(c =>
{
c.ClientId.ShouldBe(testClient.ClientId);
c.ClientName.ShouldBe(testClient.ClientName);
c.AllowedScopes.ShouldBe(testClient.AllowedScopes, true);
c.AllowedGrantTypes.ShouldBe(testClient.AllowedGrantTypes);
});
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,15 @@ public async Task FindByUserCodeAsync_WhenUserCodeExists_ExpectDataRetrievedCorr
code = await store.FindByUserCodeAsync(testUserCode);
}

code.ShouldBeEquivalentTo(expectedDeviceCodeData);
//assertionOptions => assertionOptions.Excluding(x=> x.Subject));
code.ShouldSatisfyAllConditions(c =>
{
c.ClientId.ShouldBe(expectedDeviceCodeData.ClientId);
c.RequestedScopes.ShouldBe(expectedDeviceCodeData.RequestedScopes);
c.CreationTime.ShouldBe(expectedDeviceCodeData.CreationTime);
c.Lifetime.ShouldBe(expectedDeviceCodeData.Lifetime);
c.IsOpenId.ShouldBe(expectedDeviceCodeData.IsOpenId);

});

code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).ShouldNotBeNull();
}
Expand Down Expand Up @@ -279,8 +286,13 @@ public async Task FindByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDataRetrieved
code = await store.FindByDeviceCodeAsync(testDeviceCode);
}

code.ShouldBeEquivalentTo(expectedDeviceCodeData);
//assertionOptions => assertionOptions.Excluding(x => x.Subject));
code.ShouldSatisfyAllConditions(c =>
{
c.ClientId.ShouldBe(expectedDeviceCodeData.ClientId);
c.CreationTime.ShouldBe(expectedDeviceCodeData.CreationTime);
c.Lifetime.ShouldBe(expectedDeviceCodeData.Lifetime);
c.IsOpenId.ShouldBe(expectedDeviceCodeData.IsOpenId);
});

code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).ShouldNotBeNull();
}
Expand Down Expand Up @@ -358,8 +370,17 @@ public async Task UpdateByUserCodeAsync_WhenDeviceCodeAuthorized_ExpectSubjectAn

// should be changed
var parsedCode = serializer.Deserialize<DeviceCode>(updatedCodes.Data);
parsedCode.ShouldBeEquivalentTo(authorizedDeviceCode);
//parsedCode.ShouldBe(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject));
parsedCode.ShouldSatisfyAllConditions(c =>
{
c.ClientId.ShouldBe(authorizedDeviceCode.ClientId);
c.RequestedScopes.ShouldBe(authorizedDeviceCode.RequestedScopes);
c.AuthorizedScopes = authorizedDeviceCode.AuthorizedScopes;
c.IsAuthorized.ShouldBe(authorizedDeviceCode.IsAuthorized);
c.IsOpenId.ShouldBe(authorizedDeviceCode.IsOpenId);
c.CreationTime.ShouldBe(authorizedDeviceCode.CreationTime);
c.Lifetime.ShouldBe(authorizedDeviceCode.Lifetime);

});
parsedCode.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).ShouldNotBeNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Text.Json;
using Duende.IdentityServer;
using Duende.IdentityServer.Extensions;
using FluentAssertions;

namespace UnitTests.Extensions;

Expand All @@ -24,6 +23,6 @@ public void TestName(string claimType)

var result = claims.ToClaimsDictionary();

result["claim"].Should().BeOfType<JsonElement>();
result["claim"].ShouldBeOfType<JsonElement>();
}
}
7 changes: 2 additions & 5 deletions shared/ShouldlyExtensions/ShouldlyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ public static class ShouldlyExtensions
/// <param name="expected">The expected DateTime value.</param>
/// <param name="tolerance">The allowed TimeSpan difference.</param>
/// <param name="customMessage">A custom error message if the assertion fails.</param>
public static void ShouldBeCloseTo(this DateTime actual, DateTime expected, TimeSpan tolerance,
string? customMessage = null)
public static void ShouldBeCloseTo(this DateTime actual, DateTime expected, TimeSpan tolerance, string? customMessage = null)
{
var difference = (actual - expected).Duration();

if (difference <= tolerance)
{
return;
}

var errorMessage = customMessage ??
$"Expected {actual} to be within {tolerance} of {expected}, but the difference was {difference}.";
throw new ShouldAssertException(errorMessage);
Expand All @@ -31,8 +29,7 @@ public static void ShouldBeCloseTo(this DateTime actual, DateTime expected, Time
/// <param name="expected">The expected DateTime value.</param>
/// <param name="tolerance">The allowed TimeSpan difference.</param>
/// <param name="customMessage">A custom error message if the assertion fails.</param>
public static void ShouldBeCloseTo(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance,
string? customMessage = null)
public static void ShouldBeCloseTo(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, string? customMessage = null)
{
var difference = (actual - expected).Duration();

Expand Down

0 comments on commit e79a66b

Please sign in to comment.