From 562eb7054ac4d4b73a7d109484effff1ae6c4481 Mon Sep 17 00:00:00 2001 From: Troy Dai Date: Mon, 22 Aug 2016 22:12:11 -0700 Subject: [PATCH] Merge TestDefaultValues with TestServerBuilder --- .../OpenIdConnectChallengeTests.cs | 4 +- .../OpenIdConnectConfigurationTests.cs | 6 +-- .../OpenIdConnectMiddlewareTests.cs | 12 ++--- .../OpenIdConnect/TestDefaultValues.cs | 48 ------------------- .../OpenIdConnect/TestServerBuilder.cs | 37 +++++++++++++- .../OpenIdConnect/TestSettings.cs | 4 +- 6 files changed, 49 insertions(+), 62 deletions(-) delete mode 100644 test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestDefaultValues.cs diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs index b2e2514d61..bf0d9ff0e3 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs @@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect properties.Items.Add(OpenIdConnectDefaults.UserstatePropertiesKey, userState); var server = TestServerBuilder.CreateServer(settings.Options, handler: null, properties: properties); - var transaction = await TestTransaction.SendAsync(server, TestDefaultValues.TestHost + TestServerBuilder.ChallengeWithProperties); + var transaction = await TestTransaction.SendAsync(server, TestServerBuilder.TestHost + TestServerBuilder.ChallengeWithProperties); var res = transaction.Response; Assert.Equal(HttpStatusCode.Redirect, res.StatusCode); @@ -317,6 +317,6 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect Assert.Contains("expires", secondCookie); } - private static string ChallengeEndpoint => TestDefaultValues.TestHost + TestServerBuilder.Challenge; + private static string ChallengeEndpoint => TestServerBuilder.TestHost + TestServerBuilder.Challenge; } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs index 3603c2bf40..0f5338c5c4 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect { var options = new OpenIdConnectOptions { - Authority = TestDefaultValues.DefaultAuthority, + Authority = TestServerBuilder.DefaultAuthority, ClientId = Guid.NewGuid().ToString(), SignInScheme = Guid.NewGuid().ToString() }; @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect TestConfigurationException( new OpenIdConnectOptions { - Authority = TestDefaultValues.DefaultAuthority, + Authority = TestServerBuilder.DefaultAuthority, ClientId = Guid.NewGuid().ToString() }, ex => Assert.Equal("SignInScheme", ex.ParamName)); @@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect new OpenIdConnectOptions { SignInScheme = "TestScheme", - Authority = TestDefaultValues.DefaultAuthority, + Authority = TestServerBuilder.DefaultAuthority, }, ex => Assert.Equal("ClientId", ex.ParamName)); } diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs index 4a750ea41d..f2ea922926 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs @@ -48,10 +48,10 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect [Fact] public async Task SignOutWithDefaultRedirectUri() { - var configuration = TestDefaultValues.CreateDefaultOpenIdConnectConfiguration(); + var configuration = TestServerBuilder.CreateDefaultOpenIdConnectConfiguration(); var server = TestServerBuilder.CreateServer(new OpenIdConnectOptions { - Authority = TestDefaultValues.DefaultAuthority, + Authority = TestServerBuilder.DefaultAuthority, ClientId = "Test Id", Configuration = configuration }); @@ -64,10 +64,10 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect [Fact] public async Task SignOutWithCustomRedirectUri() { - var configuration = TestDefaultValues.CreateDefaultOpenIdConnectConfiguration(); + var configuration = TestServerBuilder.CreateDefaultOpenIdConnectConfiguration(); var server = TestServerBuilder.CreateServer(new OpenIdConnectOptions { - Authority = TestDefaultValues.DefaultAuthority, + Authority = TestServerBuilder.DefaultAuthority, ClientId = "Test Id", Configuration = configuration, PostLogoutRedirectUri = "https://example.com/logout" @@ -81,10 +81,10 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect [Fact] public async Task SignOutWith_Specific_RedirectUri_From_Authentication_Properites() { - var configuration = TestDefaultValues.CreateDefaultOpenIdConnectConfiguration(); + var configuration = TestServerBuilder.CreateDefaultOpenIdConnectConfiguration(); var server = TestServerBuilder.CreateServer(new OpenIdConnectOptions { - Authority = TestDefaultValues.DefaultAuthority, + Authority = TestServerBuilder.DefaultAuthority, ClientId = "Test Id", Configuration = configuration, PostLogoutRedirectUri = "https://example.com/logout" diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestDefaultValues.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestDefaultValues.cs deleted file mode 100644 index c3e92a9042..0000000000 --- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestDefaultValues.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNetCore.Builder; -using Microsoft.IdentityModel.Protocols; -using Microsoft.IdentityModel.Protocols.OpenIdConnect; - -namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect -{ - internal class TestDefaultValues - { - public static readonly string DefaultAuthority = @"https://login.microsoftonline.com/common"; - - public static readonly string TestHost = @"https://example.com"; - - public static OpenIdConnectOptions CreateOpenIdConnectOptions() => - new OpenIdConnectOptions - { - Authority = TestDefaultValues.DefaultAuthority, - ClientId = Guid.NewGuid().ToString(), - Configuration = TestDefaultValues.CreateDefaultOpenIdConnectConfiguration() - }; - - public static OpenIdConnectOptions CreateOpenIdConnectOptions(Action update) - { - var options = CreateOpenIdConnectOptions(); - - if (update != null) - { - update(options); - } - - return options; - } - - public static OpenIdConnectConfiguration CreateDefaultOpenIdConnectConfiguration() => - new OpenIdConnectConfiguration() - { - AuthorizationEndpoint = DefaultAuthority + "/oauth2/authorize", - EndSessionEndpoint = DefaultAuthority + "/oauth2/endsessionendpoint", - TokenEndpoint = DefaultAuthority + "/oauth2/token" - }; - - public static IConfigurationManager CreateDefaultOpenIdConnectConfigurationManager() => - new StaticConfigurationManager(CreateDefaultOpenIdConnectConfiguration()); - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestServerBuilder.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestServerBuilder.cs index f8ab6fdb09..5a672093ea 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestServerBuilder.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestServerBuilder.cs @@ -12,17 +12,52 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Protocols; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect { internal class TestServerBuilder { + public static readonly string DefaultAuthority = @"https://login.microsoftonline.com/common"; + public static readonly string TestHost = @"https://example.com"; public static readonly string Challenge = "/challenge"; public static readonly string ChallengeWithOutContext = "/challengeWithOutContext"; public static readonly string ChallengeWithProperties = "/challengeWithProperties"; public static readonly string Signin = "/signin"; public static readonly string Signout = "/signout"; + public static OpenIdConnectOptions CreateOpenIdConnectOptions() => + new OpenIdConnectOptions + { + Authority = DefaultAuthority, + ClientId = Guid.NewGuid().ToString(), + Configuration = CreateDefaultOpenIdConnectConfiguration() + }; + + public static OpenIdConnectOptions CreateOpenIdConnectOptions(Action update) + { + var options = CreateOpenIdConnectOptions(); + + if (update != null) + { + update(options); + } + + return options; + } + + public static OpenIdConnectConfiguration CreateDefaultOpenIdConnectConfiguration() => + new OpenIdConnectConfiguration() + { + AuthorizationEndpoint = DefaultAuthority + "/oauth2/authorize", + EndSessionEndpoint = DefaultAuthority + "/oauth2/endsessionendpoint", + TokenEndpoint = DefaultAuthority + "/oauth2/token" + }; + + public static IConfigurationManager CreateDefaultOpenIdConnectConfigurationManager() => + new StaticConfigurationManager(CreateDefaultOpenIdConnectConfiguration()); + public static TestServer CreateServer(OpenIdConnectOptions options) { return CreateServer(options, handler: null, properties: null); @@ -94,4 +129,4 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect return new TestServer(builder); } } -} \ No newline at end of file +} diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestSettings.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestSettings.cs index 47605dccea..3e50a7abee 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestSettings.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/TestSettings.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect public TestSettings(Action configure) { - _options = TestDefaultValues.CreateOpenIdConnectOptions(configure); + _options = TestServerBuilder.CreateOpenIdConnectOptions(configure); } public TestSettings(OpenIdConnectOptions options) @@ -193,7 +193,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect ValidateQueryParameter(OpenIdConnectParameterNames.Scope, string.Join(" ", _options.Scope), actualQuery, errors, htmlEncoded); private void ValidateRedirectUri(IDictionary actualQuery, ICollection errors, bool htmlEncoded) => - ValidateQueryParameter(OpenIdConnectParameterNames.RedirectUri, TestDefaultValues.TestHost + _options.CallbackPath, actualQuery, errors, htmlEncoded); + ValidateQueryParameter(OpenIdConnectParameterNames.RedirectUri, TestServerBuilder.TestHost + _options.CallbackPath, actualQuery, errors, htmlEncoded); private void ValidateResource(IDictionary actualQuery, ICollection errors, bool htmlEncoded) => ValidateQueryParameter(OpenIdConnectParameterNames.RedirectUri, _options.Resource, actualQuery, errors, htmlEncoded);