Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexr03 committed May 10, 2024
1 parent 88a5895 commit a8d8085
Showing 1 changed file with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ public async Task ClaimsTransformationShouldHandleMissingResourceClaim()
claimsPrincipal.Claims.Count(item => ClaimTypes.Role == item.Type).Should().Be(0);
}

[Fact]
public async Task ClaimsTransformationShouldHandleMissingResourceClaimWithRealmRoles()
{
var target = new KeycloakRolesClaimsTransformation(
ClaimTypes.Role,
RolesClaimTransformationSource.All,
ClientId
);
var claimsPrincipal = GetClaimsPrincipal(MyRealmClaimValue, null);

claimsPrincipal = await target.TransformAsync(claimsPrincipal);
claimsPrincipal.HasClaim(ClaimTypes.Role, RealmRoleUserClaim).Should().BeTrue();
claimsPrincipal.HasClaim(ClaimTypes.Role, RealmRoleSuperUserClaim).Should().BeTrue();
claimsPrincipal.Claims.Count(item => ClaimTypes.Role == item.Type).Should().Be(2);
}

[Fact]
public async Task ClaimsTransformationShouldHandleMissingRealmClaimWithResourceRoles()
{
var target = new KeycloakRolesClaimsTransformation(
ClaimTypes.Role,
RolesClaimTransformationSource.All,
ClientId
);
var claimsPrincipal = GetClaimsPrincipal(null, MyResourceClaimValue);

claimsPrincipal = await target.TransformAsync(claimsPrincipal);
claimsPrincipal.HasClaim(ClaimTypes.Role, AppRoleUserClaim).Should().BeTrue();
claimsPrincipal.HasClaim(ClaimTypes.Role, AppRoleSuperUserClaim).Should().BeTrue();
claimsPrincipal.Claims.Count(item => ClaimTypes.Role == item.Type).Should().Be(2);
}

private const string MyResourceClaimValue = /*lang=json,strict*/
"""
{
Expand Down Expand Up @@ -138,17 +170,28 @@ public async Task ClaimsTransformationShouldHandleMissingResourceClaim()

// Get a claims principal that has all the appropriate claim details required for testing
private static ClaimsPrincipal GetClaimsPrincipal(
string realmClaimValue,
string resourceClaimValue
) =>
new(
new ClaimsIdentity(
[
new Claim(ResourceClaimType, resourceClaimValue, JsonValueType, MyUrl, MyUrl),
new Claim(RealmClaimType, realmClaimValue, JsonValueType, MyUrl, MyUrl),
]
)
);
string? realmClaimValue,
string? resourceClaimValue
)
{
var claimsIdentity = new ClaimsIdentity();

if (realmClaimValue != null)
{
claimsIdentity.AddClaim(
new Claim(RealmClaimType, realmClaimValue, JsonValueType, MyUrl, MyUrl)
);
}

if (resourceClaimValue != null)
{
claimsIdentity.AddClaim(
new Claim(ResourceClaimType, resourceClaimValue, JsonValueType, MyUrl, MyUrl)
);
}

return new ClaimsPrincipal(claimsIdentity);
}

// Get a claims principal that has all the appropriate claim details required for testing
private static ClaimsPrincipal GetClaimsPrincipalClaim(string claimValue) =>
Expand Down

0 comments on commit a8d8085

Please sign in to comment.