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:
parent
90064ce9df
commit
d24fddcf59
|
|
@ -426,15 +426,15 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
{
|
{
|
||||||
var server = CreateServer(
|
var server = CreateServer(
|
||||||
app => { },
|
app => { },
|
||||||
services => services.AddAuthentication().AddFacebook(o => {
|
services => services.AddAuthentication().AddFacebook(o =>
|
||||||
|
{
|
||||||
o.AppId = "whatever";
|
o.AppId = "whatever";
|
||||||
o.AppSecret = "whatever";
|
o.AppSecret = "whatever";
|
||||||
o.SignInScheme = FacebookDefaults.AuthenticationScheme;
|
o.SignInScheme = FacebookDefaults.AuthenticationScheme;
|
||||||
}),
|
}),
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// Gross
|
await context.ChallengeAsync("Facebook");
|
||||||
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
|
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
|
||||||
|
|
@ -446,14 +446,14 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
{
|
{
|
||||||
var server = CreateServer(
|
var server = CreateServer(
|
||||||
app => { },
|
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.AppId = "whatever";
|
||||||
o.AppSecret = "whatever";
|
o.AppSecret = "whatever";
|
||||||
}),
|
}),
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// Gross
|
await context.ChallengeAsync("Facebook");
|
||||||
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
|
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
|
||||||
|
|
@ -465,14 +465,14 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
{
|
{
|
||||||
var server = CreateServer(
|
var server = CreateServer(
|
||||||
app => { },
|
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.AppId = "whatever";
|
||||||
o.AppSecret = "whatever";
|
o.AppSecret = "whatever";
|
||||||
}),
|
}),
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// Gross
|
await context.ChallengeAsync("Facebook");
|
||||||
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
|
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync("https://example.com/challenge"));
|
||||||
|
|
@ -498,10 +498,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
var server = CreateServer(
|
var server = CreateServer(
|
||||||
app => { },
|
app => { },
|
||||||
services => services.AddAuthentication().AddFacebook(o => o.SignInScheme = "Whatever"),
|
services => services.AddAuthentication().AddFacebook(o => o.SignInScheme = "Whatever"),
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// REVIEW: Gross.
|
await Assert.ThrowsAsync<ArgumentException>("AppId", () => context.ChallengeAsync("Facebook"));
|
||||||
Assert.Throws<ArgumentException>("AppId", () => context.ChallengeAsync("Facebook").GetAwaiter().GetResult());
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var transaction = await server.SendAsync("http://example.com/challenge");
|
var transaction = await server.SendAsync("http://example.com/challenge");
|
||||||
|
|
@ -514,10 +513,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
var server = CreateServer(
|
var server = CreateServer(
|
||||||
app => { },
|
app => { },
|
||||||
services => services.AddAuthentication().AddFacebook(o => o.AppId = "Whatever"),
|
services => services.AddAuthentication().AddFacebook(o => o.AppId = "Whatever"),
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// REVIEW: Gross.
|
await Assert.ThrowsAsync<ArgumentException>("AppSecret", () => context.ChallengeAsync("Facebook"));
|
||||||
Assert.Throws<ArgumentException>("AppSecret", () => context.ChallengeAsync("Facebook").GetAwaiter().GetResult());
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var transaction = await server.SendAsync("http://example.com/challenge");
|
var transaction = await server.SendAsync("http://example.com/challenge");
|
||||||
|
|
@ -550,10 +548,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// REVIEW: Gross.
|
await context.ChallengeAsync("Facebook");
|
||||||
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var transaction = await server.SendAsync("http://example.com/challenge");
|
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("https://www.facebook.com/v2.12/dialog/oauth", location);
|
||||||
Assert.Contains("response_type=code", location);
|
Assert.Contains("response_type=code", location);
|
||||||
Assert.Contains("client_id=", 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("scope=", location);
|
||||||
Assert.Contains("state=", location);
|
Assert.Contains("state=", location);
|
||||||
}
|
}
|
||||||
|
|
@ -643,10 +640,9 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
o.AppSecret = "Test App Secret";
|
o.AppSecret = "Test App Secret";
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
context =>
|
async context =>
|
||||||
{
|
{
|
||||||
// REVIEW: gross
|
await context.ChallengeAsync("Facebook");
|
||||||
context.ChallengeAsync("Facebook").GetAwaiter().GetResult();
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
var transaction = await server.SendAsync("http://example.com/challenge");
|
var transaction = await server.SendAsync("http://example.com/challenge");
|
||||||
|
|
@ -672,7 +668,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
{
|
{
|
||||||
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||||
.AddCookie()
|
.AddCookie()
|
||||||
.AddFacebook(o =>
|
.AddFacebook(o =>
|
||||||
{
|
{
|
||||||
o.AppId = "Test App Id";
|
o.AppId = "Test App Id";
|
||||||
o.AppSecret = "Test App Secret";
|
o.AppSecret = "Test App Secret";
|
||||||
|
|
@ -728,7 +724,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
Assert.Contains("&access_token=", finalUserInfoEndpoint);
|
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()
|
var builder = new WebHostBuilder()
|
||||||
.Configure(app =>
|
.Configure(app =>
|
||||||
|
|
@ -736,7 +732,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
|
||||||
configure?.Invoke(app);
|
configure?.Invoke(app);
|
||||||
app.Use(async (context, next) =>
|
app.Use(async (context, next) =>
|
||||||
{
|
{
|
||||||
if (handler == null || !handler(context))
|
if (handler == null || !await handler(context))
|
||||||
{
|
{
|
||||||
await next();
|
await next();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -551,28 +551,26 @@ namespace Microsoft.AspNetCore.Authentication.Google
|
||||||
{
|
{
|
||||||
o.ClientId = "Test Id";
|
o.ClientId = "Test Id";
|
||||||
o.ClientSecret = "Test Secret";
|
o.ClientSecret = "Test Secret";
|
||||||
//AutomaticChallenge = true
|
|
||||||
},
|
},
|
||||||
context =>
|
context =>
|
||||||
|
{
|
||||||
|
var req = context.Request;
|
||||||
|
var res = context.Response;
|
||||||
|
if (req.Path == new PathString("/challenge2"))
|
||||||
{
|
{
|
||||||
var req = context.Request;
|
return context.ChallengeAsync("Google", new AuthenticationProperties(new Dictionary<string, string>()
|
||||||
var res = context.Response;
|
|
||||||
if (req.Path == new PathString("/challenge2"))
|
|
||||||
{
|
{
|
||||||
return context.ChallengeAsync("Google", new AuthenticationProperties(
|
{ "scope", "https://www.googleapis.com/auth/plus.login" },
|
||||||
new Dictionary<string, string>()
|
{ "access_type", "offline" },
|
||||||
{
|
{ "approval_prompt", "force" },
|
||||||
{ "scope", "https://www.googleapis.com/auth/plus.login" },
|
{ "prompt", "consent" },
|
||||||
{ "access_type", "offline" },
|
{ "login_hint", "test@example.com" },
|
||||||
{ "approval_prompt", "force" },
|
{ "include_granted_scopes", "false" }
|
||||||
{ "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");
|
var transaction = await server.SendAsync("https://example.com/challenge2");
|
||||||
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
|
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
|
||||||
var query = transaction.Response.Headers.Location.Query;
|
var query = transaction.Response.Headers.Location.Query;
|
||||||
|
|
@ -1501,4 +1499,4 @@ namespace Microsoft.AspNetCore.Authentication.Google
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue