-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add authentication * Add Options * Add additional tests * Update tests * Clean up * More Clear up * More Clean up * Remove thread.sleep * Waits
- Loading branch information
1 parent
c28f6c7
commit 79dee57
Showing
18 changed files
with
622 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace GraphiQl | ||
{ | ||
public class GraphiQlMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly GraphiQlOptions _options; | ||
|
||
public GraphiQlMiddleware(RequestDelegate next, IOptions<GraphiQlOptions> options) | ||
{ | ||
_next = next; | ||
_options = options.Value; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (context.Request.Path.Equals(_options.GraphiQlPath, StringComparison.OrdinalIgnoreCase) | ||
&& _options.IsAuthenticated != null | ||
&& !await _options.IsAuthenticated.Invoke(context)) | ||
{ | ||
return; | ||
} | ||
|
||
await _next(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace GraphiQl | ||
{ | ||
public class GraphiQlOptions | ||
{ | ||
public string GraphiQlPath { get; set; } | ||
|
||
public string GraphQlApiPath { get; set; } | ||
|
||
public Func<HttpContext, Task<bool>> IsAuthenticated { get; set; } | ||
|
||
public GraphiQlOptions() | ||
{ | ||
GraphiQlPath = "/graphql"; | ||
GraphQlApiPath = "/graphql"; | ||
} | ||
} | ||
|
||
public class GraphiQlOptionsSetup : IConfigureOptions<GraphiQlOptions> | ||
{ | ||
public void Configure(GraphiQlOptions options) | ||
{ | ||
if (options.GraphiQlPath == null) | ||
{ | ||
options.GraphiQlPath = "/graphql"; | ||
} | ||
|
||
if (options.GraphQlApiPath == null) | ||
{ | ||
options.GraphQlApiPath = "/graphql"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
tests/GraphiQl.Tests/AuthenticationTest/ConfigureOptionsSetup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GraphiQl.Demo; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace GraphiQl.Tests.AuthenticationTest | ||
{ | ||
public class ConfigureOptionsSetup : SeleniumTest, IAsyncLifetime | ||
{ | ||
private readonly IWebHost _host; | ||
|
||
public ConfigureOptionsSetup() | ||
{ | ||
_host = WebHost.CreateDefaultBuilder() | ||
.ConfigureServices(x => { x.AddTransient<IConfigureOptions<GraphiQlOptions>,GraphiQlTestOptionsSetup>(); }) | ||
.UseStartup<Startup>() | ||
.UseKestrel() | ||
.UseUrls("http://*:5001") | ||
.Build(); | ||
} | ||
|
||
[Fact] | ||
public void RequiresAuthentication() | ||
{ | ||
// Arrange + Act | ||
var result = string.Empty; | ||
RunTest(driver => | ||
{ | ||
driver.Navigate().GoToUrl("http://localhost:5001/graphql"); | ||
|
||
driver.Manage() | ||
.Timeouts() | ||
.ImplicitWait = TimeSpan.FromSeconds(2); | ||
|
||
result = driver.PageSource; | ||
}); | ||
|
||
// Assert | ||
result.ShouldContain("This page requires authentication"); | ||
} | ||
|
||
public async Task InitializeAsync() | ||
=> await _host.StartAsync().ConfigureAwait(false); | ||
|
||
public Task DisposeAsync() | ||
{ | ||
_host.Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
internal class GraphiQlTestOptionsSetup : IConfigureOptions<GraphiQlOptions> | ||
{ | ||
public void Configure(GraphiQlOptions options) | ||
{ | ||
options.IsAuthenticated = context => | ||
{ | ||
context.Response.Clear(); | ||
context.Response.StatusCode = 400; | ||
context.Response.WriteAsync("This page requires authentication"); | ||
|
||
return Task.FromResult(false); | ||
}; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.