Clean up tests

- Make Facebook test server asynchronous to avoid having to
  block for the result.
- Clean up some formatting.
This commit is contained in:
Patrick Westerhoff 2018-03-19 22:19:57 +01:00
parent 90064ce9df
commit d24fddcf59
2 changed files with 40 additions and 46 deletions

View File

@ -426,15 +426,15 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
{
var server = CreateServer(
app => { },
services => services.AddAuthentication().AddFacebook(o => {
services => services.AddAuthentication().AddFacebook(o =>
{
o.AppId = "whatever";
o.AppSecret = "whatever";
o.SignInScheme = FacebookDefaults.AuthenticationScheme;
}),
context =>
async context =>
{
// Gross
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
await context.ChallengeAsync("Facebook");
return true;
});
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
@ -446,14 +446,14 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
{
var server = CreateServer(
app => { },
services => services.AddAuthentication(o => o.DefaultScheme = FacebookDefaults.AuthenticationScheme).AddFacebook(o => {
services => services.AddAuthentication(o => o.DefaultScheme = FacebookDefaults.AuthenticationScheme).AddFacebook(o =>
{
o.AppId = "whatever";
o.AppSecret = "whatever";
}),
context =>
async context =>
{
// Gross
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
await context.ChallengeAsync("Facebook");
return true;
});
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
@ -465,14 +465,14 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
{
var server = CreateServer(
app => { },
services => services.AddAuthentication(o => o.DefaultSignInScheme = FacebookDefaults.AuthenticationScheme).AddFacebook(o => {
services => services.AddAuthentication(o => o.DefaultSignInScheme = FacebookDefaults.AuthenticationScheme).AddFacebook(o =>
{
o.AppId = "whatever";
o.AppSecret = "whatever";
}),
context =>
async context =>
{
// Gross
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
await context.ChallengeAsync("Facebook");
return true;
});
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
@ -498,10 +498,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
var server = CreateServer(
app => { },
services => services.AddAuthentication().AddFacebook(o => o.SignInScheme = "Whatever"),
context =>
async context =>
{
// REVIEW: Gross.
Assert.Throws<ArgumentException>("AppId", () => context.ChallengeAsync("Facebook").GetAwaiter().GetResult());
await Assert.ThrowsAsync<ArgumentException>("AppId", () => context.ChallengeAsync("Facebook"));
return true;
});
var transaction = await server.SendAsync("http://example.com/challenge");
@ -514,10 +513,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
var server = CreateServer(
app => { },
services => services.AddAuthentication().AddFacebook(o => o.AppId = "Whatever"),
context =>
async context =>
{
// REVIEW: Gross.
Assert.Throws<ArgumentException>("AppSecret", () => context.ChallengeAsync("Facebook").GetAwaiter().GetResult());
await Assert.ThrowsAsync<ArgumentException>("AppSecret", () => context.ChallengeAsync("Facebook"));
return true;
});
var transaction = await server.SendAsync("http://example.com/challenge");
@ -550,10 +548,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
};
});
},
context =>
async context =>
{
// REVIEW: Gross.
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
await context.ChallengeAsync("Facebook");
return true;
});
var transaction = await server.SendAsync("http://example.com/challenge");
@ -620,7 +617,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
Assert.Contains("https://www.facebook.com/v2.12/dialog/oauth", location);
Assert.Contains("response_type=code", location);
Assert.Contains("client_id=", location);
Assert.Contains("redirect_uri="+ UrlEncoder.Default.Encode("http://example.com/signin-facebook"), location);
Assert.Contains("redirect_uri=" + UrlEncoder.Default.Encode("http://example.com/signin-facebook"), location);
Assert.Contains("scope=", location);
Assert.Contains("state=", location);
}
@ -643,10 +640,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
o.AppSecret = "Test App Secret";
});
},
context =>
async context =>
{
// REVIEW: gross
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
await context.ChallengeAsync("Facebook");
return true;
});
var transaction = await server.SendAsync("http://example.com/challenge");
@ -672,7 +668,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
.AddFacebook(o =>
.AddFacebook(o =>
{
o.AppId = "Test App Id";
o.AppSecret = "Test App Secret";
@ -728,7 +724,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
Assert.Contains("&access_token=", finalUserInfoEndpoint);
}
private static TestServer CreateServer(Action<IApplicationBuilder> configure, Action<IServiceCollection> configureServices, Func<HttpContext, bool> handler)
private static TestServer CreateServer(Action<IApplicationBuilder> configure, Action<IServiceCollection> configureServices, Func<HttpContext, Task<bool>> handler)
{
var builder = new WebHostBuilder()
.Configure(app =>
@ -736,7 +732,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
configure?.Invoke(app);
app.Use(async (context, next) =>
{
if (handler == null || !handler(context))
if (handler == null || !await handler(context))
{
await next();
}

View File

@ -551,28 +551,26 @@ namespace Microsoft.AspNetCore.Authentication.Google
{
o.ClientId = "Test Id";
o.ClientSecret = "Test Secret";
//AutomaticChallenge = true
},
context =>
{
var req = context.Request;
var res = context.Response;
if (req.Path == new PathString("/challenge2"))
{
var req = context.Request;
var res = context.Response;
if (req.Path == new PathString("/challenge2"))
return context.ChallengeAsync("Google", new AuthenticationProperties(new Dictionary<string, string>()
{
return context.ChallengeAsync("Google", new AuthenticationProperties(
new Dictionary<string, string>()
{
{ "scope", "https://www.googleapis.com/auth/plus.login" },
{ "access_type", "offline" },
{ "approval_prompt", "force" },
{ "prompt", "consent" },
{ "login_hint", "test@example.com" },
{ "include_granted_scopes", "false" }
}));
}
{ "scope", "https://www.googleapis.com/auth/plus.login" },
{ "access_type", "offline" },
{ "approval_prompt", "force" },
{ "prompt", "consent" },
{ "login_hint", "test@example.com" },
{ "include_granted_scopes", "false" }
}));
}
return Task.FromResult<object>(null);
});
return Task.FromResult<object>(null);
});
var transaction = await server.SendAsync("https://example.com/challenge2");
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
var query = transaction.Response.Headers.Location.Query;
@ -1501,4 +1499,4 @@ namespace Microsoft.AspNetCore.Authentication.Google
}
}
}
}
}