Skip to content

Commit

Permalink
Merge pull request #15 from hbiarge/master
Browse files Browse the repository at this point in the history
Add events to the TestServerHandler
  • Loading branch information
hbiarge authored Jan 30, 2019
2 parents 3c49a02 + 46c6ed0 commit a00cc13
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<RepositoryUrl>http://github.com/xabaril/Acheve.TestHost</RepositoryUrl>
<Authors>Xabaril Contributors</Authors>
<Company>Xabaril</Company>
<Version>1.4.2</Version>
<Version>2.0.0</Version>
<Description>Achve.TestHost is a nuget package to improve TestServer experiences.
For more information see http://github.com/Xabaril/Acheve.TestHost</Description>
<Tags>TestHost;TestServer</Tags>
Expand Down
21 changes: 11 additions & 10 deletions samples/Sample.IntegrationTests/Specs/ValuesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -20,9 +21,8 @@ public VauesWithDefaultUserTests(TestHostFixture fixture)
}

[Fact]
public async Task WithRequestBuilder()
public async Task Authorized_User_Should_Get_200()
{
// Or you can create a request and assign the identity to the RequestBuilder
var response = await _fixture.Server.CreateHttpApiRequest<ValuesController>(controller=>controller.Values())
.WithIdentity(Identities.User)
.GetAsync();
Expand All @@ -31,20 +31,20 @@ public async Task WithRequestBuilder()
}

[Fact]
public async Task WithEmptyRequestBuilder()
public async Task User_With_No_Claims_Is_Unauthorized()
{
// Or you can create a request and assign the identity to the RequestBuilder
var response = await _fixture.Server.CreateHttpApiRequest<ValuesController>(controller => controller.Values())
.WithIdentity(Identities.Empty)
.GetAsync();

response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
response.Headers.WwwAuthenticate.Count.Should().Be(1);
response.Headers.WwwAuthenticate.First().Scheme.Should().Be("TestServer");
}

[Fact]
public async Task WithRequestBuilderAndSpecificScheme()
public async Task Authorized_User_Should_Get_200_Using_A_Specific_Scheme()
{
// Or you can create a request and assign the identity to the RequestBuilder
var response = await _fixture.Server.CreateHttpApiRequest<ValuesController>(controller => controller.ValuesWithSchema())
.WithIdentity(Identities.User, "Bearer")
.GetAsync();
Expand All @@ -55,16 +55,17 @@ public async Task WithRequestBuilderAndSpecificScheme()
[Fact]
public async Task WithRequestBuilderAndSpecificSchemeUnauthorized()
{
// Or you can create a request and assign the identity to the RequestBuilder
var response = await _fixture.Server.CreateHttpApiRequest<ValuesController>(controller => controller.ValuesWithSchema())
.WithIdentity(Identities.User)
.WithIdentity(Identities.User) // We are not using the expected "Bearer" schema
.GetAsync();

response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
response.Headers.WwwAuthenticate.Count.Should().Be(1);
response.Headers.WwwAuthenticate.First().Scheme.Should().Be("Bearer");
}

[Fact]
public async Task Anonymous()
public async Task Authentication_Is_Not_Performed_For_Non_Protected_Endpoints()
{
var response = await _fixture.Server.CreateHttpApiRequest<ValuesController>(controller => controller.PublicValues())
.GetAsync();
Expand Down
27 changes: 21 additions & 6 deletions samples/Sample.IntegrationTests/TestStartup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Reflection;
using System.Security.Claims;
using System.Reflection;
using System.Threading.Tasks;
using Acheve.AspNetCore.TestHost.Security;
using Acheve.TestHost;
using Microsoft.AspNetCore.Builder;
Expand All @@ -14,12 +13,28 @@ public class TestStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(TestServerAuthenticationDefaults.AuthenticationScheme)
.AddTestServerAuthentication()
.AddTestServerAuthentication("Bearer", options =>
services.AddAuthentication(TestServerDefaults.AuthenticationScheme)
.AddTestServer(options =>
{
options.Events = new TestServerEvents
{
OnMessageReceived = context => Task.CompletedTask,
OnTokenValidated = context => Task.CompletedTask,
OnAuthenticationFailed = context => Task.CompletedTask,
OnChallenge = context => Task.CompletedTask
};
})
.AddTestServer("Bearer", options =>
{
options.NameClaimType = "name";
options.RoleClaimType = "role";
options.Events = new TestServerEvents
{
OnMessageReceived = context => Task.CompletedTask,
OnTokenValidated = context => Task.CompletedTask,
OnAuthenticationFailed = context => Task.CompletedTask,
OnChallenge = context => Task.CompletedTask
};
});

var mvcCoreBuilder = services.AddMvcCore()
Expand Down
30 changes: 15 additions & 15 deletions src/Acheve.TestHost/AuthenticationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@ namespace Acheve.AspNetCore.TestHost.Security
{
public static class AuthenticationBuilderExtensions
{
public static AuthenticationBuilder AddTestServerAuthentication(this AuthenticationBuilder builder)
=> builder.AddTestServerAuthentication(
authenticationScheme: TestServerAuthenticationDefaults.AuthenticationScheme,
public static AuthenticationBuilder AddTestServer(this AuthenticationBuilder builder)
=> builder.AddTestServer(
authenticationScheme: TestServerDefaults.AuthenticationScheme,
displayName: null,
configureOptions: _ => { });

public static AuthenticationBuilder AddTestServerAuthentication(
public static AuthenticationBuilder AddTestServer(
this AuthenticationBuilder builder,
string authenticationScheme)
=> builder.AddTestServerAuthentication(
=> builder.AddTestServer(
authenticationScheme: authenticationScheme,
displayName: null,
configureOptions: _ => { });

public static AuthenticationBuilder AddTestServerAuthentication(
public static AuthenticationBuilder AddTestServer(
this AuthenticationBuilder builder,
Action<TestServerAuthenticationOptions> configureOptions)
=> builder.AddTestServerAuthentication(
authenticationScheme: TestServerAuthenticationDefaults.AuthenticationScheme,
Action<TestServerOptions> configureOptions)
=> builder.AddTestServer(
authenticationScheme: TestServerDefaults.AuthenticationScheme,
displayName: null,
configureOptions: configureOptions);

public static AuthenticationBuilder AddTestServerAuthentication(
public static AuthenticationBuilder AddTestServer(
this AuthenticationBuilder builder,
string authenticationScheme,
Action<TestServerAuthenticationOptions> configureOptions)
=> builder.AddTestServerAuthentication(
Action<TestServerOptions> configureOptions)
=> builder.AddTestServer(
authenticationScheme: authenticationScheme,
displayName: null,
configureOptions: configureOptions);

public static AuthenticationBuilder AddTestServerAuthentication(
public static AuthenticationBuilder AddTestServer(
this AuthenticationBuilder builder,
string authenticationScheme,
string displayName,
Action<TestServerAuthenticationOptions> configureOptions)
Action<TestServerOptions> configureOptions)
{
return builder.AddScheme<TestServerAuthenticationOptions, TestServerAuthenticationHandler>(
return builder.AddScheme<TestServerOptions, TestServerHandler>(
authenticationScheme: authenticationScheme,
displayName: displayName,
configureOptions: configureOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/Acheve.TestHost/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public static class HttpClientExtensions
public static HttpClient WithDefaultIdentity(this HttpClient httpClient, IEnumerable<Claim> claims)
{
var headerName =
AuthenticationHeaderHelper.GetHeaderName(TestServerAuthenticationDefaults.AuthenticationScheme);
AuthenticationHeaderHelper.GetHeaderName(TestServerDefaults.AuthenticationScheme);

httpClient.DefaultRequestHeaders.Add(
name: headerName,
value: $"{TestServerAuthenticationDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");
value: $"{TestServerDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");

return httpClient;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Acheve.TestHost/RequestBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public static class RequestBuilderExtensions
public static RequestBuilder WithIdentity(this RequestBuilder requestBuilder, IEnumerable<Claim> claims)
{
var headerName =
AuthenticationHeaderHelper.GetHeaderName(TestServerAuthenticationDefaults.AuthenticationScheme);
AuthenticationHeaderHelper.GetHeaderName(TestServerDefaults.AuthenticationScheme);

requestBuilder.AddHeader(
headerName,
$"{TestServerAuthenticationDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");
$"{TestServerDefaults.AuthenticationScheme} {DefautClaimsEncoder.Encode(claims)}");

return requestBuilder;
}
Expand Down
17 changes: 17 additions & 0 deletions src/Acheve.TestHost/Security/AuthenticationFailedContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using System;

namespace Acheve.TestHost
{
public class AuthenticationFailedContext : ResultContext<TestServerOptions>
{
public AuthenticationFailedContext(
HttpContext context,
AuthenticationScheme scheme,
TestServerOptions options)
: base(context, scheme, options) { }

public Exception Exception { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/Acheve.TestHost/Security/MessageReceivedContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;

namespace Acheve.TestHost
{
public class MessageReceivedContext : ResultContext<TestServerOptions>
{
public MessageReceivedContext(
HttpContext context,
AuthenticationScheme scheme,
TestServerOptions options)
: base(context, scheme, options) { }

public string Token { get; set; }
}
}
70 changes: 0 additions & 70 deletions src/Acheve.TestHost/Security/TestServerAuthenticationHandler.cs

This file was deleted.

24 changes: 24 additions & 0 deletions src/Acheve.TestHost/Security/TestServerChallengeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using System;

namespace Acheve.TestHost
{
public class TestServerChallengeContext : PropertiesContext<TestServerOptions>
{
public TestServerChallengeContext(
HttpContext context,
AuthenticationScheme scheme,
TestServerOptions options,
AuthenticationProperties properties)
: base(context, scheme, options, properties) { }

public Exception AuthenticateFailure { get; set; }

public string Error { get; set; }

public bool Handled { get; private set; }

public void HandleResponse() => Handled = true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Acheve.TestHost
{
public static class TestServerAuthenticationDefaults
public static class TestServerDefaults
{
public const string AuthenticationScheme = "TestServer";
}
Expand Down
24 changes: 24 additions & 0 deletions src/Acheve.TestHost/Security/TestServerEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;

namespace Acheve.TestHost
{
public class TestServerEvents
{
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.CompletedTask;

public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.CompletedTask;

public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => Task.CompletedTask;

public Func<TestServerChallengeContext, Task> OnChallenge { get; set; } = context => Task.CompletedTask;

public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);

public virtual Task MessageReceived(MessageReceivedContext context) => OnMessageReceived(context);

public virtual Task TokenValidated(TokenValidatedContext context) => OnTokenValidated(context);

public virtual Task Challenge(TestServerChallengeContext context) => OnChallenge(context);
}
}
Loading

0 comments on commit a00cc13

Please sign in to comment.