Merge TestDefaultValues with TestServerBuilder

This commit is contained in:
Troy Dai 2016-08-22 22:12:11 -07:00
parent abc1b37ee1
commit 562eb7054a
6 changed files with 49 additions and 62 deletions

View File

@ -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;
}
}

View File

@ -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<ArgumentException>(
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));
}

View File

@ -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"

View File

@ -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<OpenIdConnectOptions> 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<OpenIdConnectConfiguration> CreateDefaultOpenIdConnectConfigurationManager() =>
new StaticConfigurationManager<OpenIdConnectConfiguration>(CreateDefaultOpenIdConnectConfiguration());
}
}

View File

@ -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<OpenIdConnectOptions> 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<OpenIdConnectConfiguration> CreateDefaultOpenIdConnectConfigurationManager() =>
new StaticConfigurationManager<OpenIdConnectConfiguration>(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);
}
}
}
}

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
public TestSettings(Action<OpenIdConnectOptions> 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<string, string> actualQuery, ICollection<string> 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<string, string> actualQuery, ICollection<string> errors, bool htmlEncoded) =>
ValidateQueryParameter(OpenIdConnectParameterNames.RedirectUri, _options.Resource, actualQuery, errors, htmlEncoded);