Skip to content

Commit

Permalink
touched up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HP712 committed Jul 16, 2024
1 parent eb6526a commit d916ced
Showing 1 changed file with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,48 +45,39 @@ public void Create_FromTokenValidationParameters_ReturnsCorrectClaimsIdentity(bo
AppContext.SetSwitch(AppContextSwitches.UseClaimsIdentityTypeSwitch, false);
}

[Fact]
public void Create_FromDerivedTokenValidationParameters_HonorsSetSecurityToken()
{
var jsonWebToken = new JsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor()));
var tokenValidationParameters = new DerivedTokenValidationParameters(returnCaseSensitiveClaimsIdentityWithToken: true);
tokenValidationParameters.AuthenticationType = "custom-authentication-type";
tokenValidationParameters.NameClaimType = "custom-name";
tokenValidationParameters.RoleClaimType = "custom-role";

var actualClaimsIdentity = tokenValidationParameters.CreateClaimsIdentity(jsonWebToken, Default.Issuer);

// The SecurityToken set in derived TokenValidationParameters is honored.
Assert.IsType<CaseSensitiveClaimsIdentity>(actualClaimsIdentity);

var securityToken = ((CaseSensitiveClaimsIdentity)actualClaimsIdentity).SecurityToken;
Assert.NotNull(securityToken);
Assert.IsType<TvpJsonWebToken>(securityToken);
Assert.NotEqual(jsonWebToken, securityToken);

Assert.Equal(tokenValidationParameters.AuthenticationType, actualClaimsIdentity.AuthenticationType);
Assert.Equal(tokenValidationParameters.NameClaimType, actualClaimsIdentity.NameClaimType);
Assert.Equal(tokenValidationParameters.RoleClaimType, actualClaimsIdentity.RoleClaimType);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(bool tvpReturnsCaseSensitiveClaimsIdentityWithoutToken)
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(bool tvpReturnsCaseSensitiveClaimsIdentity, bool tvpReturnsCaseSensitiveClaimsIdentityWithToken)
{
var jsonWebToken = new JsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor()));
var tokenValidationParameters = new DerivedTokenValidationParameters(returnCaseSensitiveClaimsIdentityWithoutToken: tvpReturnsCaseSensitiveClaimsIdentityWithoutToken);
var tokenValidationParameters = new DerivedTokenValidationParameters(tvpReturnsCaseSensitiveClaimsIdentity, tvpReturnsCaseSensitiveClaimsIdentityWithToken);
tokenValidationParameters.AuthenticationType = "custom-authentication-type";
tokenValidationParameters.NameClaimType = "custom-name";
tokenValidationParameters.RoleClaimType = "custom-role";

var actualClaimsIdentity = tokenValidationParameters.CreateClaimsIdentity(jsonWebToken, Default.Issuer);

Assert.IsType<CaseSensitiveClaimsIdentity>(actualClaimsIdentity);

var securityToken = ((CaseSensitiveClaimsIdentity)actualClaimsIdentity).SecurityToken;
Assert.NotNull(securityToken);
Assert.Equal(jsonWebToken, securityToken);
if (tvpReturnsCaseSensitiveClaimsIdentity)
{
Assert.IsType<CaseSensitiveClaimsIdentity>(actualClaimsIdentity);
if (tvpReturnsCaseSensitiveClaimsIdentityWithToken)
{
var securityToken = ((CaseSensitiveClaimsIdentity)actualClaimsIdentity).SecurityToken;
Assert.NotNull(securityToken);
Assert.IsType<TvpJsonWebToken>(securityToken);
Assert.NotEqual(jsonWebToken, securityToken);
}
else
{
Assert.Null(((CaseSensitiveClaimsIdentity)actualClaimsIdentity).SecurityToken);
}
}
else
{
Assert.IsType<ClaimsIdentity>(actualClaimsIdentity);
}

Assert.Equal(tokenValidationParameters.AuthenticationType, actualClaimsIdentity.AuthenticationType);
Assert.Equal(tokenValidationParameters.NameClaimType, actualClaimsIdentity.NameClaimType);
Expand All @@ -97,28 +88,30 @@ public void Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIden

private class DerivedTokenValidationParameters : TokenValidationParameters
{
private bool _returnCaseSensitiveClaimsIdentity;
private bool _returnCaseSensitiveClaimsIdentityWithToken;
private bool _returnCaseSensitiveClaimsIdentityWithoutToken;

public DerivedTokenValidationParameters(bool returnCaseSensitiveClaimsIdentityWithToken = false, bool returnCaseSensitiveClaimsIdentityWithoutToken = false)
public DerivedTokenValidationParameters(bool returnCaseSensitiveClaimsIdentity = false, bool returnCaseSensitiveClaimsIdentityWithToken = false)
{
_returnCaseSensitiveClaimsIdentity = returnCaseSensitiveClaimsIdentity;
_returnCaseSensitiveClaimsIdentityWithToken = returnCaseSensitiveClaimsIdentityWithToken;
_returnCaseSensitiveClaimsIdentityWithoutToken = returnCaseSensitiveClaimsIdentityWithoutToken;
}

public override ClaimsIdentity CreateClaimsIdentity(SecurityToken securityToken, string issuer)
{
if (_returnCaseSensitiveClaimsIdentityWithToken)
if (_returnCaseSensitiveClaimsIdentity)
{
return new CaseSensitiveClaimsIdentity(AuthenticationType, NameClaimType, RoleClaimType)
if (_returnCaseSensitiveClaimsIdentityWithToken)
{
SecurityToken = new TvpJsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor())),
};
}

if (_returnCaseSensitiveClaimsIdentityWithoutToken)
{
return new CaseSensitiveClaimsIdentity(AuthenticationType, NameClaimType, RoleClaimType);
return new CaseSensitiveClaimsIdentity(AuthenticationType, NameClaimType, RoleClaimType)
{
SecurityToken = new TvpJsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor())),
};
}
else
{
return new CaseSensitiveClaimsIdentity(AuthenticationType, NameClaimType, RoleClaimType);
}
}

return new ClaimsIdentity(AuthenticationType, NameClaimType, RoleClaimType);
Expand Down

0 comments on commit d916ced

Please sign in to comment.