Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update xUnit version and fix test warnings #2796

Merged
merged 7 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/commonTest.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftVersion)" />
<PackageReference Include="xunit.runner.console" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.console" Version="$(XunitRunnerConsoleVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunnerVisualstudioVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion build/dependenciesTest.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<NewtonsoftVersion>13.0.3</NewtonsoftVersion>
<SystemSecurityClaimsVersion>4.3.0</SystemSecurityClaimsVersion>
<SystemTextJson>8.0.4</SystemTextJson>
<XunitVersion>2.4.0</XunitVersion>
<XunitRunnerConsoleVersion>2.9.0</XunitRunnerConsoleVersion>
<XunitRunnerVisualStudioVersion>2.8.2</XunitRunnerVisualStudioVersion>
<XunitVersion>2.9.0</XunitVersion>
</PropertyGroup>
</Project>
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 @@ -341,16 +340,16 @@ public static TheoryData<ConfigurationManagerTheoryData<OpenIdConnectConfigurati

// AutomaticRefreshInterval interval should return same config.
theoryData.Add(new ConfigurationManagerTheoryData<OpenIdConnectConfiguration>("AutomaticRefreshIntervalNotHit")
{
ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
{
ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
"AADCommonV1Json",
new OpenIdConnectConfigurationRetriever(),
InMemoryDocumentRetriever),
ExpectedConfiguration = OpenIdConfigData.AADCommonV1Config,
ExpectedUpdatedConfiguration = OpenIdConfigData.AADCommonV1Config,
SyncAfter = DateTime.UtcNow + TimeSpan.FromDays(2),
UpdatedMetadataAddress = "AADCommonV2Json"
});
ExpectedConfiguration = OpenIdConfigData.AADCommonV1Config,
ExpectedUpdatedConfiguration = OpenIdConfigData.AADCommonV1Config,
SyncAfter = DateTime.UtcNow + TimeSpan.FromDays(2),
UpdatedMetadataAddress = "AADCommonV2Json"
});

// AutomaticRefreshInterval should pick up new bits.
theoryData.Add(new ConfigurationManagerTheoryData<OpenIdConnectConfiguration>("AutomaticRefreshIntervalHit")
Expand All @@ -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 All @@ -638,16 +637,12 @@ public void ValidateOpenIdConnectConfigurationTests(ConfigurationManagerTheoryDa

theoryData.ExpectedException.ProcessNoException(context);
}
catch (AggregateException ex)
catch (Exception ex)
{
// this should throw, because last configuration retrieved was null
Assert.Throws<AggregateException>(() => configuration = configurationManager.GetConfigurationAsync().Result);
await Assert.ThrowsAsync<InvalidOperationException>(async () => configuration = await configurationManager.GetConfigurationAsync());

ex.Handle((x) =>
{
theoryData.ExpectedException.ProcessException(x, context);
return true;
});
theoryData.ExpectedException.ProcessException(ex, context);
}

TestUtilities.AssertFailIfErrors(context);
Expand Down Expand Up @@ -772,9 +767,9 @@ public class ConfigurationManagerTheoryData<T> : TheoryDataBase where T : class
{
public ConfigurationManager<T> ConfigurationManager { get; set; }

public ConfigurationManagerTheoryData() {}
public ConfigurationManagerTheoryData() { }

public ConfigurationManagerTheoryData(string testId) : base(testId) {}
public ConfigurationManagerTheoryData(string testId) : base(testId) { }

public TimeSpan AutomaticRefreshInterval { get; set; }

Expand Down Expand Up @@ -811,7 +806,7 @@ public override string ToString()
return $"{TestId}, {MetadataAddress}, {ExpectedException}";
}

public string UpdatedMetadataAddress { get; set; }
public string UpdatedMetadataAddress { get; set; }
}
}
}
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
Loading
Loading