Fix inherited hub auth and added tests (#535)

This commit is contained in:
BrennanConroy 2017-06-06 23:55:34 -07:00 committed by David Fowler
parent 680c48d584
commit 1fad901843
2 changed files with 55 additions and 1 deletions

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.SignalR
public void MapHub<THub>(string path, Action<HttpSocketOptions> socketOptions) where THub : Hub<IClientProxy>
{
// find auth attributes
var authorizeAttribute = typeof(THub).GetCustomAttribute<AuthorizeAttribute>();
var authorizeAttribute = typeof(THub).GetCustomAttribute<AuthorizeAttribute>(inherit: true);
var options = new HttpSocketOptions();
if (authorizeAttribute != null)
{

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
@ -29,6 +30,50 @@ namespace Microsoft.AspNetCore.SignalR.Tests
Assert.Equal("Duplicate definitions of 'OverloadedMethod'. Overloading is not supported.", ex.Message);
}
[Fact]
public void MapHubFindsAuthAttributeOnHub()
{
var authCount = 0;
var builder = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(services =>
{
services.AddSignalR();
})
.Configure(app =>
{
app.UseSignalR(options => options.MapHub<AuthHub>("path", httpSocketOptions =>
{
authCount += httpSocketOptions.AuthorizationData.Count;
}));
})
.Build();
Assert.Equal(1, authCount);
}
[Fact]
public void MapHubFindsAuthAttributeOnInheritedHub()
{
var authCount = 0;
var builder = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(services =>
{
services.AddSignalR();
})
.Configure(app =>
{
app.UseSignalR(options => options.MapHub<InheritedAuthHub>("path", httpSocketOptions =>
{
authCount += httpSocketOptions.AuthorizationData.Count;
}));
})
.Build();
Assert.Equal(1, authCount);
}
private class InvalidHub : Hub
{
public void OverloadedMethod(int num)
@ -39,5 +84,14 @@ namespace Microsoft.AspNetCore.SignalR.Tests
{
}
}
private class InheritedAuthHub : AuthHub
{
}
[Authorize]
private class AuthHub : Hub
{
}
}
}