Skip to content

Commit

Permalink
Fix static tests (#2732)
Browse files Browse the repository at this point in the history
* Fix static tests

* Reset Switches, use same collection.

---------

Co-authored-by: id4s <[email protected]>
  • Loading branch information
brentschmaltz and HP712 authored Jul 18, 2024
1 parent a75d614 commit 74d8a82
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
9 changes: 9 additions & 0 deletions build/dependenciesTest.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<NewtonsoftVersion>13.0.3</NewtonsoftVersion>
<SystemSecurityClaimsVersion>4.3.0</SystemSecurityClaimsVersion>
<XunitVersion>2.4.0</XunitVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' != 'net461'">
<SystemTextJson>8.0.4</SystemTextJson>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
<SystemTextEncodingsWeb>4.7.2</SystemTextEncodingsWeb>
<SystemTextJson>4.7.2</SystemTextJson>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ public class CaseSensitiveClaimsIdentityTheoryData(string testId) : TheoryDataBa
internal List<string> ExpectedClaims { get; set; }
}

private static ClaimsIdentity CreateCaseSensitiveClaimsIdentity(JObject claims, TokenValidationParameters validationParameters = null)
private static CaseSensitiveClaimsIdentity CreateCaseSensitiveClaimsIdentity(JObject claims, TokenValidationParameters validationParameters = null)
{
AppContext.SetSwitch(AppContextSwitches.UseCaseSensitiveClaimsIdentityTypeSwitch, true);
var handler = new JsonWebTokenHandler();
var claimsIdentity = handler.CreateClaimsIdentityInternal(new JsonWebToken(CreateUnsignedToken(claims)), validationParameters ?? new TokenValidationParameters(), Default.Issuer);
AppContext.SetSwitch(AppContextSwitches.UseCaseSensitiveClaimsIdentityTypeSwitch, false);
return claimsIdentity;
JsonWebToken jsonWebToken = new JsonWebToken(CreateUnsignedToken(claims));
if (validationParameters == null)
return new CaseSensitiveClaimsIdentity(jsonWebToken.Claims);
else
return new CaseSensitiveClaimsIdentity(jsonWebToken.Claims, validationParameters.AuthenticationType, validationParameters.NameClaimType, validationParameters.RoleClaimType);
}

private static string CreateUnsignedToken(JObject payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

namespace Microsoft.IdentityModel.Tokens.Tests
{
[Collection(nameof(ClaimsIdentityFactoryTests))]
[Collection(nameof(CaseSensitiveClaimsIdentityTests))]
public class ClaimsIdentityFactoryTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Create_FromTokenValidationParameters_ReturnsCorrectClaimsIdentity(bool useCaseSensitiveClaimsIdentity)
{
AppContextSwitches.ResetAllSwitches();

AppContext.SetSwitch(AppContextSwitches.UseCaseSensitiveClaimsIdentityTypeSwitch, useCaseSensitiveClaimsIdentity);

var jsonWebToken = new JsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor()));
Expand Down Expand Up @@ -51,6 +53,8 @@ public void Create_FromTokenValidationParameters_ReturnsCorrectClaimsIdentity(bo
[InlineData(false, false)]
public void Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(bool tvpReturnsCaseSensitiveClaimsIdentity, bool tvpReturnsCaseSensitiveClaimsIdentityWithToken)
{
AppContextSwitches.ResetAllSwitches();

AppContext.SetSwitch(AppContextSwitches.UseCaseSensitiveClaimsIdentityTypeSwitch, true);

var jsonWebToken = new JsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor()));
Expand Down Expand Up @@ -85,11 +89,9 @@ public void Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIden
Assert.Equal(tokenValidationParameters.NameClaimType, actualClaimsIdentity.NameClaimType);
Assert.Equal(tokenValidationParameters.RoleClaimType, actualClaimsIdentity.RoleClaimType);

AppContext.SetSwitch(AppContextSwitches.UseCaseSensitiveClaimsIdentityTypeSwitch, false);
AppContextSwitches.ResetAllSwitches();
}



private class DerivedTokenValidationParameters : TokenValidationParameters
{
private bool _returnCaseSensitiveClaimsIdentity;
Expand Down

0 comments on commit 74d8a82

Please sign in to comment.