Skip to content

Commit c23fef0

Browse files
committed
fix: update tests to use overload with context (AzureAD#3130)
1 parent ec9b6e7 commit c23fef0

File tree

10 files changed

+29
-21
lines changed

10 files changed

+29
-21
lines changed

test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandlerTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,13 @@ public void GetEncryptionKeys(CreateTokenTheoryData theoryData)
672672
var jwtTokenFromJsonHandlerWithKid = new JsonWebToken(jweFromJsonHandlerWithKid);
673673
var encryptionKeysFromJsonHandlerWithKid = theoryData.JsonWebTokenHandler.GetContentEncryptionKeys(jwtTokenFromJsonHandlerWithKid, theoryData.ValidationParameters, theoryData.Configuration);
674674

675-
Assert.True(IdentityComparer.AreEqual(encryptionKeysFromJsonHandlerWithKid, theoryData.ExpectedDecryptionKeys));
675+
IdentityComparer.AreEqual(encryptionKeysFromJsonHandlerWithKid, theoryData.ExpectedDecryptionKeys, context);
676676
theoryData.ExpectedException.ProcessNoException(context);
677677
}
678678
catch (Exception ex)
679679
{
680680
theoryData.ExpectedException.ProcessException(ex, context);
681681
}
682-
683682
TestUtilities.AssertFailIfErrors(context);
684683
}
685684

test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void CompareJwtSecurityTokenWithJsonSecurityTokenMultipleAudiences()
288288
string header = "{}";
289289
var jsonWebToken = new JsonWebToken(header, payload);
290290
var jwtSecurityToken = new JwtSecurityToken($"{Base64UrlEncoder.Encode(header)}.{Base64UrlEncoder.Encode(payload)}.");
291-
IdentityComparer.AreEqual(jsonWebToken.Claims, jwtSecurityToken.Claims);
291+
IdentityComparer.AreEqual(jsonWebToken.Claims, jwtSecurityToken.Claims, context);
292292
IdentityComparer.AreEqual(jsonWebToken.Audiences, jwtSecurityToken.Audiences, context);
293293
TestUtilities.AssertFailIfErrors(context);
294294
}

test/Microsoft.IdentityModel.Logging.Tests/IdentityModelTelemetryUtilTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public void SetTelemetry(TelemetryTheoryData theoryData)
2424
{
2525
IdentityModelTelemetryUtil.SetTelemetryData(theoryData.HttpRequestMessage, theoryData.AdditionalHeaders);
2626
// check if the resulting headers are as expected
27-
if (!IdentityComparer.AreEqual(theoryData.ExpectedHeaders, theoryData.HttpRequestMessage?.Headers))
28-
throw new ArgumentException("resulting headers do not match the expected headers.");
27+
IdentityComparer.AreEqual(theoryData.ExpectedHeaders, theoryData.HttpRequestMessage?.Headers, testContext);
2928

3029
theoryData.ExpectedException.ProcessNoException(testContext);
3130
}

test/Microsoft.IdentityModel.Protocols.OpenIdConnect.Tests/OpenIdConnectConfigurationRetrieverTests.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public void Properties()
139139
private async Task<OpenIdConnectConfiguration> GetConfigurationFromHttpAsync(string uri, ExpectedException expectedException, OpenIdConnectConfiguration expectedConfiguration = null)
140140
{
141141
OpenIdConnectConfiguration openIdConnectConfiguration = null;
142+
var testContext = new CompareContext($"{this}.GetConfigurationFromHttpAsync");
143+
142144
try
143145
{
144146
openIdConnectConfiguration = await OpenIdConnectConfigurationRetriever.GetAsync(uri, CancellationToken.None);
@@ -151,7 +153,7 @@ private async Task<OpenIdConnectConfiguration> GetConfigurationFromHttpAsync(str
151153

152154
if (expectedConfiguration != null)
153155
{
154-
Assert.True(IdentityComparer.AreEqual(openIdConnectConfiguration, expectedConfiguration));
156+
Assert.True(IdentityComparer.AreEqual(openIdConnectConfiguration, expectedConfiguration, testContext));
155157
}
156158

157159
return openIdConnectConfiguration;
@@ -179,6 +181,8 @@ private async Task<OpenIdConnectConfiguration> GetConfigurationAsync(string uri,
179181
private async Task<OpenIdConnectConfiguration> GetConfigurationFromTextAsync(string primaryDocument, string secondaryDocument, ExpectedException expectedException, OpenIdConnectConfiguration expectedConfiguration = null)
180182
{
181183
OpenIdConnectConfiguration openIdConnectConfiguration = null;
184+
var testContext = new CompareContext($"{this}.GetConfigurationFromTextAsync");
185+
182186
try
183187
{
184188
openIdConnectConfiguration = await OpenIdConnectConfigurationRetriever.GetAsync(
@@ -192,7 +196,7 @@ private async Task<OpenIdConnectConfiguration> GetConfigurationFromTextAsync(str
192196

193197
if (expectedConfiguration != null)
194198
{
195-
Assert.True(IdentityComparer.AreEqual(openIdConnectConfiguration, expectedConfiguration));
199+
Assert.True(IdentityComparer.AreEqual(openIdConnectConfiguration, expectedConfiguration, testContext));
196200
}
197201

198202
return openIdConnectConfiguration;
@@ -201,6 +205,8 @@ private async Task<OpenIdConnectConfiguration> GetConfigurationFromTextAsync(str
201205
private async Task<OpenIdConnectConfiguration> GetConfigurationFromMixedAsync(string primaryDocument, ExpectedException expectedException, OpenIdConnectConfiguration expectedConfiguration = null)
202206
{
203207
OpenIdConnectConfiguration openIdConnectConfiguration = null;
208+
var testContext = new CompareContext($"{this}.GetConfigurationFromMixedAsync");
209+
204210
try
205211
{
206212
openIdConnectConfiguration = await OpenIdConnectConfigurationRetriever.GetAsync("primary",
@@ -214,7 +220,7 @@ private async Task<OpenIdConnectConfiguration> GetConfigurationFromMixedAsync(st
214220

215221
if (expectedConfiguration != null)
216222
{
217-
Assert.True(IdentityComparer.AreEqual(openIdConnectConfiguration, expectedConfiguration));
223+
Assert.True(IdentityComparer.AreEqual(openIdConnectConfiguration, expectedConfiguration, testContext));
218224
}
219225

220226
return openIdConnectConfiguration;

test/Microsoft.IdentityModel.Protocols.Tests/ExtensibilityTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public async Task ConfigurationManagerUsingCustomClass()
9292
configManager.RequestRefresh();
9393
configuration2 = await configManager.GetConfigurationAsync();
9494

95-
if (IdentityComparer.AreEqual(configuration.Issuer, configuration2.Issuer))
95+
if (IdentityComparer.AreEqual(configuration.Issuer, configuration2.Issuer, CompareContext.Default))
9696
await Task.Delay(1000);
9797
else
9898
break;
9999
}
100100

101-
if (IdentityComparer.AreEqual(configuration.Issuer, configuration2.Issuer))
101+
if (IdentityComparer.AreEqual(configuration.Issuer, configuration2.Issuer, CompareContext.Default))
102102
context.Diffs.Add($"Expected: {configuration.Issuer}, to be different from: {configuration2.Issuer}");
103103

104104
TestUtilities.AssertFailIfErrors(context);

test/Microsoft.IdentityModel.TestUtils/TestUtilities.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static void GetSet(GetSetContext context)
198198
{
199199
context.Errors.Add(propertyKV.Key + ": initial value != null && expected == null, initial value: " + initialValue.ToString());
200200
}
201-
else if (initialValue != null && !IdentityComparer.AreEqual(initialValue, propertyKV.Value[0]))
201+
else if (initialValue != null && !IdentityComparer.AreEqual(initialValue, propertyKV.Value[0], CompareContext.Default))
202202
{
203203
context.Errors.Add(propertyKV.Key + ", initial value != expected. expected: " + propertyKV.Value[0].ToString() + ", was: " + initialValue.ToString());
204204
}
@@ -422,7 +422,6 @@ public static void AssertFailIfErrors(CompareContext context)
422422
{
423423
AssertFailIfErrors(context.Title, context.Diffs);
424424
}
425-
426425
public static void AssertFailIfErrors(string testId, List<string> errors)
427426
{
428427
if (errors.Count != 0)

test/Microsoft.IdentityModel.Validators.Tests/MicrosoftIdentityIssuerValidatorTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public async Task Validate_NullOrEmptyParameters_ThrowsException()
156156
ValidationResult<ValidatedIssuer> validatedIssuer = await ValidateIssuerAsync(string.Empty, jwtSecurityToken, validator);
157157
Assert.False(validatedIssuer.IsValid);
158158

159-
IdentityComparer.AreEqual(LogMessages.IDX40003, exception.Message);
159+
IdentityComparer.AreEqual(LogMessages.IDX40003, exception.Message, context);
160160

161161
Assert.Throws<ArgumentNullException>(ValidatorConstants.SecurityToken, () => validator.Validate(ValidatorConstants.AadIssuer, null, validationParams));
162162
await Assert.ThrowsAsync<ArgumentNullException>(async () => await ValidateIssuerAsync(ValidatorConstants.AadIssuer, null, validator));

test/Microsoft.IdentityModel.Xml.Tests/DSigSerializerTests.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Linq;
78
using System.Security.Cryptography;
89
using System.Text;
910
using System.Xml;
@@ -115,7 +116,7 @@ public void WriteKeyInfo(DSigSerializerTheoryData theoryData)
115116
theoryData.Serializer.WriteKeyInfo(writer, keyInfo);
116117
writer.Flush();
117118
var xml = Encoding.UTF8.GetString(ms.ToArray());
118-
IdentityComparer.AreEqual(theoryData.Xml, xml);
119+
IdentityComparer.AreEqual(theoryData.Xml, xml, context);
119120
}
120121
catch (Exception ex)
121122
{
@@ -251,7 +252,7 @@ public void WriteSignature(DSigSerializerTheoryData theoryData)
251252
theoryData.Serializer.WriteSignature(writer, signature);
252253
writer.Flush();
253254
var xml = Encoding.UTF8.GetString(ms.ToArray());
254-
IdentityComparer.AreEqual(theoryData.Xml, xml);
255+
IdentityComparer.AreEqual(theoryData.Xml, xml, context);
255256
}
256257
catch (Exception ex)
257258
{

test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -801,13 +801,14 @@ public static TheoryData<JwtTheoryData> ActorTheoryData
801801
public void BootstrapContext(JwtTheoryData theoryData)
802802
{
803803
TestUtilities.WriteHeader($"{this}.BootstrapContext", theoryData);
804+
var testContext = new CompareContext();
804805

805806
var claimsPrincipal = theoryData.TokenHandler.ValidateToken(theoryData.Token, theoryData.ValidationParameters, out SecurityToken securityToken);
806807
var bootstrapContext = (claimsPrincipal.Identity as ClaimsIdentity).BootstrapContext as string;
807808
if (theoryData.ValidationParameters.SaveSigninToken)
808809
{
809810
Assert.NotNull(bootstrapContext);
810-
Assert.True(IdentityComparer.AreEqual(claimsPrincipal, theoryData.TokenHandler.ValidateToken(bootstrapContext, theoryData.ValidationParameters, out SecurityToken validatedToken)));
811+
Assert.True(IdentityComparer.AreEqual(claimsPrincipal, theoryData.TokenHandler.ValidateToken(bootstrapContext, theoryData.ValidationParameters, out SecurityToken validatedToken), testContext));
811812
}
812813
else
813814
{
@@ -3083,8 +3084,8 @@ public void GetEncryptionKeys(CreateTokenTheoryData theoryData)
30833084

30843085
var encryptionKeysFromJwtHandlerWithNoKid = theoryData.JwtSecurityTokenHandler.GetContentEncryptionKeys(jwtTokenFromJwtHandlerWithNoKid, theoryData.ValidationParameters);
30853086

3086-
IdentityComparer.AreEqual(encryptionKeysFromJwtHandlerWithKid, theoryData.ExpectedDecryptionKeys);
3087-
IdentityComparer.AreEqual(encryptionKeysFromJwtHandlerWithNoKid, theoryData.ExpectedDecryptionKeys);
3087+
IdentityComparer.AreEqual(encryptionKeysFromJwtHandlerWithKid, theoryData.ExpectedDecryptionKeys, context);
3088+
IdentityComparer.AreEqual(encryptionKeysFromJwtHandlerWithNoKid, theoryData.ExpectedDecryptionKeys, context);
30883089
IdentityComparer.AreEqual(encryptionKeysFromJwtHandlerWithKid, encryptionKeysFromJwtHandlerWithNoKid, context);
30893090
theoryData.ExpectedException.ProcessNoException(context);
30903091
}

test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenTests.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public void EmbeddedTokenConstructor1(string testId, JwtSecurityTokenTestVariati
185185
{
186186
JwtSecurityToken outerJwt = null;
187187
JwtSecurityToken innerJwt = null;
188+
var testContext = new CompareContext($"{this}.EmbeddedTokenConstructor1");
188189

189190
// create inner token
190191
try
@@ -229,7 +230,7 @@ public void EmbeddedTokenConstructor1(string testId, JwtSecurityTokenTestVariati
229230

230231
if (null != outerTokenVariation.ExpectedJwtSecurityToken)
231232
{
232-
Assert.True(IdentityComparer.AreEqual(outerTokenVariation.ExpectedJwtSecurityToken, outerJwt));
233+
Assert.True(IdentityComparer.AreEqual(outerTokenVariation.ExpectedJwtSecurityToken, outerJwt, testContext));
233234
}
234235
}
235236
catch (Exception ex)
@@ -247,7 +248,7 @@ public void EmbeddedTokenConstructor1(string testId, JwtSecurityTokenTestVariati
247248

248249
if (null != innerTokenVariation && null != innerTokenVariation.ExpectedJwtSecurityToken)
249250
{
250-
Assert.True(IdentityComparer.AreEqual(innerTokenVariation.ExpectedJwtSecurityToken, innerJwt));
251+
Assert.True(IdentityComparer.AreEqual(innerTokenVariation.ExpectedJwtSecurityToken, innerJwt, testContext));
251252
}
252253
}
253254
catch (Exception ex)
@@ -394,6 +395,8 @@ private static void ParseJweParts(string jwe, out string headerPart, out string
394395
private void RunConstructionTest(JwtSecurityTokenTestVariation variation)
395396
{
396397
JwtSecurityToken jwt = null;
398+
var testContext = new CompareContext($"{this}.RunConstructionTest");
399+
397400
try
398401
{
399402
jwt = CreateToken(variation);
@@ -414,7 +417,7 @@ private void RunConstructionTest(JwtSecurityTokenTestVariation variation)
414417

415418
if (null != variation.ExpectedJwtSecurityToken)
416419
{
417-
Assert.True(IdentityComparer.AreEqual(variation.ExpectedJwtSecurityToken, jwt));
420+
Assert.True(IdentityComparer.AreEqual(variation.ExpectedJwtSecurityToken, jwt, testContext));
418421
}
419422
}
420423
catch (Exception ex)

0 commit comments

Comments
 (0)