Skip to content

Commit

Permalink
Fix test warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak committed Aug 24, 2024
1 parent 187492d commit 48f17a7
Show file tree
Hide file tree
Showing 34 changed files with 204 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void SetPropertyWithConflict()
mock.Object.SetProperty("key1", "value");
mock.Object.SetProperty("key1", false);

Assert.Equal(1, mock.Object.Properties.Count);
Assert.Single(mock.Object.Properties);
Assert.False((bool)mock.Object.Properties["key1"]);
}

Expand All @@ -81,7 +81,7 @@ public void VerifyDerivedTypesCanAddArbitraryTypes()
CustomTelemetryEventDetails eventDetails = new CustomTelemetryEventDetails();
eventDetails.SetProperty("foo", new Foo() { Bar = "bar" });

Assert.Equal(1, eventDetails.Properties.Count);
Assert.Single(eventDetails.Properties);
Assert.Equal("bar", (eventDetails.Properties["foo"] as Foo)?.Bar);
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Protocols.Configuration;
using Microsoft.IdentityModel.Protocols.OpenIdConnect.Configuration;
using Microsoft.IdentityModel.TestUtils;
Expand Down Expand Up @@ -176,7 +175,7 @@ public void Defaults()
}

[Fact]
public void FetchMetadataFailureTest()
public async Task FetchMetadataFailureTest()
{
var context = new CompareContext($"{this}.FetchMetadataFailureTest");

Expand All @@ -186,7 +185,7 @@ public void FetchMetadataFailureTest()
// First time to fetch metadata
try
{
var configuration = configManager.GetConfigurationAsync().Result;
var configuration = await configManager.GetConfigurationAsync();
}
catch (Exception firstFetchMetadataFailure)
{
Expand All @@ -196,7 +195,7 @@ public void FetchMetadataFailureTest()
// Fetch metadata again during refresh interval, the exception should be same from above
try
{
var configuration = configManager.GetConfigurationAsync().Result;
var configuration = await configManager.GetConfigurationAsync();
}
catch (Exception secondFetchMetadataFailure)
{
Expand All @@ -211,7 +210,7 @@ public void FetchMetadataFailureTest()
}

[Fact]
public void BootstrapRefreshIntervalTest()
public async Task BootstrapRefreshIntervalTest()
{
var context = new CompareContext($"{this}.BootstrapRefreshIntervalTest");

Expand All @@ -221,7 +220,7 @@ public void BootstrapRefreshIntervalTest()
// First time to fetch metadata.
try
{
var configuration = configManager.GetConfigurationAsync().Result;
var configuration = await configManager.GetConfigurationAsync();
}
catch (Exception firstFetchMetadataFailure)
{
Expand All @@ -237,7 +236,7 @@ public void BootstrapRefreshIntervalTest()
try
{
configManager.RequestRefresh();
var configuration = configManager.GetConfigurationAsync().Result;
var configuration = await configManager.GetConfigurationAsync();
}
catch (Exception secondFetchMetadataFailure)
{
Expand Down Expand Up @@ -267,7 +266,7 @@ public void GetSets()
Type type = typeof(ConfigurationManager<OpenIdConnectConfiguration>);
PropertyInfo[] properties = type.GetProperties();
if (properties.Length != ExpectedPropertyCount)
Assert.True(false, $"Number of properties has changed from {ExpectedPropertyCount} to: " + properties.Length + ", adjust tests");
Assert.Fail($"Number of properties has changed from {ExpectedPropertyCount} to: " + properties.Length + ", adjust tests");

var defaultAutomaticRefreshInterval = ConfigurationManager<OpenIdConnectConfiguration>.DefaultAutomaticRefreshInterval;
var defaultRefreshInterval = ConfigurationManager<OpenIdConnectConfiguration>.DefaultRefreshInterval;
Expand Down Expand Up @@ -299,15 +298,15 @@ public async Task AutomaticRefreshInterval(ConfigurationManagerTheoryData<OpenId
try
{

var configuration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
var configuration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);
IdentityComparer.AreEqual(configuration, theoryData.ExpectedConfiguration, context);

theoryData.ConfigurationManager.MetadataAddress = theoryData.UpdatedMetadataAddress;
TestUtilities.SetField(theoryData.ConfigurationManager, "_syncAfter", theoryData.SyncAfter);
var updatedConfiguration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
var updatedConfiguration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);
// we wait 50 ms here to make the task is finished.
Thread.Sleep(50);
updatedConfiguration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
updatedConfiguration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);
IdentityComparer.AreEqual(updatedConfiguration, theoryData.ExpectedUpdatedConfiguration, context);

theoryData.ExpectedException.ProcessNoException(context);
Expand Down Expand Up @@ -374,15 +373,15 @@ public async Task RequestRefresh(ConfigurationManagerTheoryData<OpenIdConnectCon
{
var context = new CompareContext($"{this}.RequestRefresh");

var configuration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
var configuration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);
IdentityComparer.AreEqual(configuration, theoryData.ExpectedConfiguration, context);

// the first call to RequestRefresh will trigger a refresh with ConfigurationManager.RefreshInterval being ignored.
// Testing RefreshInterval requires a two calls, the second call will trigger a refresh with ConfigurationManager.RefreshInterval being used.
if (theoryData.RequestRefresh)
{
theoryData.ConfigurationManager.RequestRefresh();
configuration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
configuration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);
}

theoryData.ConfigurationManager.RefreshInterval = theoryData.RefreshInterval;
Expand All @@ -395,7 +394,7 @@ public async Task RequestRefresh(ConfigurationManagerTheoryData<OpenIdConnectCon
if (theoryData.SleepTimeInMs > 0)
Thread.Sleep(theoryData.SleepTimeInMs);

var updatedConfiguration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
var updatedConfiguration = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);

IdentityComparer.AreEqual(updatedConfiguration, theoryData.ExpectedUpdatedConfiguration, context);

Expand Down Expand Up @@ -461,7 +460,7 @@ public async Task HttpFailures(ConfigurationManagerTheoryData<OpenIdConnectConfi

try
{
_ = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
_ = await theoryData.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);
theoryData.ExpectedException.ProcessNoException(context);
}
catch (Exception ex)
Expand Down Expand Up @@ -518,11 +517,11 @@ public async Task GetConfigurationAsync()

// Unable to obtain a new configuration, but _currentConfiguration is not null so it should be returned.
configManager = new ConfigurationManager<OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
var configuration = await configManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
var configuration = await configManager.GetConfigurationAsync(CancellationToken.None);
TestUtilities.SetField(configManager, "_lastRequestRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
configManager.RequestRefresh();
configManager.MetadataAddress = "http://127.0.0.1";
var configuration2 = await configManager.GetConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
var configuration2 = await configManager.GetConfigurationAsync(CancellationToken.None);
IdentityComparer.AreEqual(configuration, configuration2, context);
if (!object.ReferenceEquals(configuration, configuration2))
context.Diffs.Add("!object.ReferenceEquals(configuration, configuration2)");
Expand Down Expand Up @@ -614,7 +613,7 @@ public void TestConfigurationComparer()
}

[Theory, MemberData(nameof(ValidateOpenIdConnectConfigurationTestCases), DisableDiscoveryEnumeration = true)]
public void ValidateOpenIdConnectConfigurationTests(ConfigurationManagerTheoryData<OpenIdConnectConfiguration> theoryData)
public async Task ValidateOpenIdConnectConfigurationTests(ConfigurationManagerTheoryData<OpenIdConnectConfiguration> theoryData)
{
TestUtilities.WriteHeader($"{this}.ValidateOpenIdConnectConfigurationTests");
var context = new CompareContext();
Expand All @@ -628,7 +627,7 @@ public void ValidateOpenIdConnectConfigurationTests(ConfigurationManagerTheoryDa
{
//create a listener and enable it for logs
var listener = TestUtils.SampleListener.CreateLoggerListener(EventLevel.Warning);
configuration = configurationManager.GetConfigurationAsync().Result;
configuration = await configurationManager.GetConfigurationAsync();

// we need to sleep here to make sure the task that updates configuration has finished.
Thread.Sleep(250);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
using Xunit;
Expand All @@ -16,12 +17,12 @@ namespace Microsoft.IdentityModel.Protocols.OpenIdConnect.Tests
public class End2EndTests
{
[Theory, MemberData(nameof(OpenIdConnectTheoryData))]
public void OpenIdConnect(OpenIdConnectTheoryData theoryData)
public async Task OpenIdConnect(OpenIdConnectTheoryData theoryData)
{
var context = TestUtilities.WriteHeader($"{this}.OpenIdConnect", theoryData);
try
{
OpenIdConnectConfiguration configuration = OpenIdConnectConfigurationRetriever.GetAsync(theoryData.OpenIdConnectMetadataFileName, new FileDocumentRetriever(), CancellationToken.None).Result;
OpenIdConnectConfiguration configuration = await OpenIdConnectConfigurationRetriever.GetAsync(theoryData.OpenIdConnectMetadataFileName, new FileDocumentRetriever(), CancellationToken.None);

theoryData.AdditionalValidation?.Invoke(configuration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void GetSets()
Type type = typeof(OpenIdConnectConfiguration);
PropertyInfo[] properties = type.GetProperties();
if (properties.Length != 68)
Assert.True(false, "Number of properties has changed from 68 to: " + properties.Length + ", adjust tests");
Assert.Fail("Number of properties has changed from 68 to: " + properties.Length + ", adjust tests");

TestUtilities.CallAllPublicInstanceAndStaticPropertyGets(configuration, "OpenIdConnectConfiguration_GetSets");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task ResolvePopKeyFromCnfClaimAsync(ResolvePopKeyTheoryData theoryD
if (theoryData.Json != null)
cnf = new Cnf(theoryData.Json);

_ = await handler.ResolvePopKeyFromCnfClaimAsync(cnf, theoryData.SignedHttpRequestToken, theoryData.ValidatedAccessToken, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
_ = await handler.ResolvePopKeyFromCnfClaimAsync(cnf, theoryData.SignedHttpRequestToken, theoryData.ValidatedAccessToken, signedHttpRequestValidationContext, CancellationToken.None);

if ((bool)signedHttpRequestValidationContext.CallContext.PropertyBag[theoryData.MethodToCall] == false)
context.AddDiff($"{theoryData.MethodToCall} was not called.");
Expand Down Expand Up @@ -150,7 +150,7 @@ public async Task ResolvePopKeyAsync(ResolvePopKeyTheoryData theoryData)
{
var signedHttpRequestValidationContext = theoryData.BuildSignedHttpRequestValidationContext();
var handler = new SignedHttpRequestHandler();
_ = await handler.ResolvePopKeyAsync(theoryData.SignedHttpRequestToken, theoryData.ValidatedAccessToken, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
_ = await handler.ResolvePopKeyAsync(theoryData.SignedHttpRequestToken, theoryData.ValidatedAccessToken, signedHttpRequestValidationContext, CancellationToken.None);

if ((bool)signedHttpRequestValidationContext.CallContext.PropertyBag[theoryData.MethodToCall] == false)
context.AddDiff($"{theoryData.MethodToCall} was not called.");
Expand Down Expand Up @@ -277,7 +277,7 @@ public async Task ResolvePopKeyFromJweAsync(ResolvePopKeyTheoryData theoryData)
{
var signedHttpRequestValidationContext = theoryData.BuildSignedHttpRequestValidationContext();
var handler = new SignedHttpRequestHandler();
_ = await handler.ResolvePopKeyFromJweAsync(theoryData.PopKeyString, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
_ = await handler.ResolvePopKeyFromJweAsync(theoryData.PopKeyString, signedHttpRequestValidationContext, CancellationToken.None);

theoryData.ExpectedException.ProcessNoException(context);
}
Expand Down Expand Up @@ -401,7 +401,7 @@ public async Task ResolvePopKeyFromJkuAsync(ResolvePopKeyTheoryData theoryData)
{
var signedHttpRequestValidationContext = theoryData.BuildSignedHttpRequestValidationContext();
var handler = new SignedHttpRequestHandlerPublic();
var popKey = await handler.ResolvePopKeyFromJkuAsync(theoryData.JkuSetUrl, null, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
var popKey = await handler.ResolvePopKeyFromJkuAsync(theoryData.JkuSetUrl, null, signedHttpRequestValidationContext, CancellationToken.None);

if (popKey == null)
context.AddDiff("Resolved Pop key is null.");
Expand Down Expand Up @@ -634,7 +634,7 @@ public async Task ResolvePopKeyFromJkuKidAsync(ResolvePopKeyTheoryData theoryDat
var signedHttpRequestValidationContext = theoryData.BuildSignedHttpRequestValidationContext();
var handler = new SignedHttpRequestHandlerPublic();
Cnf cnf = new Cnf { Kid = theoryData.Kid };
var popKey = await handler.ResolvePopKeyFromJkuAsync(theoryData.JkuSetUrl, cnf, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
var popKey = await handler.ResolvePopKeyFromJkuAsync(theoryData.JkuSetUrl, cnf, signedHttpRequestValidationContext, CancellationToken.None);

if (popKey == null)
context.AddDiff("Resolved Pop key is null.");
Expand Down Expand Up @@ -750,7 +750,7 @@ public async Task GetPopKeysFromJkuAsync(ResolvePopKeyTheoryData theoryData)
{
var signedHttpRequestValidationContext = theoryData.BuildSignedHttpRequestValidationContext();
var handler = new SignedHttpRequestHandler();
var popKeys = await handler.GetPopKeysFromJkuAsync(theoryData.JkuSetUrl, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
var popKeys = await handler.GetPopKeysFromJkuAsync(theoryData.JkuSetUrl, signedHttpRequestValidationContext, CancellationToken.None);

if (popKeys.Count != theoryData.ExpectedNumberOfPopKeysReturned)
context.AddDiff($"Number of returned pop keys {popKeys.Count} is not the same as the expected: {theoryData.ExpectedNumberOfPopKeysReturned}.");
Expand Down Expand Up @@ -864,7 +864,7 @@ public async Task ResolvePopKeyFromKeyIdentifierAsync(ResolvePopKeyTheoryData th
{
var signedHttpRequestValidationContext = theoryData.BuildSignedHttpRequestValidationContext();
var handler = new SignedHttpRequestHandlerPublic();
var popKeys = await handler.ResolvePopKeyFromKeyIdentifierAsync(theoryData.Kid, theoryData.SignedHttpRequestToken, theoryData.ValidatedAccessToken, signedHttpRequestValidationContext, CancellationToken.None).ConfigureAwait(false);
var popKeys = await handler.ResolvePopKeyFromKeyIdentifierAsync(theoryData.Kid, theoryData.SignedHttpRequestToken, theoryData.ValidatedAccessToken, signedHttpRequestValidationContext, CancellationToken.None);
theoryData.ExpectedException.ProcessNoException(context);
}
catch (Exception ex)
Expand Down
Loading

0 comments on commit 48f17a7

Please sign in to comment.