Add missing virtuals

Enable unit test verifying methods are virtual

Fixes https://github.com/aspnet/Identity/issues/349
This commit is contained in:
Hao Kung 2015-02-06 13:25:34 -08:00
parent d45b796529
commit 228995c84c
5 changed files with 44 additions and 42 deletions

View File

@ -6,7 +6,6 @@ using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.Framework.OptionsModel;
@ -66,7 +65,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user">The <see cref="TUser"/> the token will be generated from.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
/// <returns>A <see cref="Task{TResult}"/> that contains the protected token.</returns>
public async Task<string> GenerateAsync(string purpose, UserManager<TUser> manager, TUser user,
public virtual async Task<string> GenerateAsync(string purpose, UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken))
{
if (user == null)
@ -100,7 +99,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
/// <returns>A <see cref="Task{TResult}"/> that is true if the token is valid, otherwise false.</returns>
public async Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user,
public virtual async Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken))
{
try
@ -156,7 +155,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
/// <returns>True if a token generated by this instance can be used as a Two Factor Authentication token, otherwise false.</returns>
/// <remarks>This method will always return false for instances of <see cref="DataProtectorTokenProvider{TUser}"/>.</remarks>
public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user,
public virtual Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(false);
@ -170,7 +169,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the started task.</returns>
public Task NotifyAsync(string token, UserManager<TUser> manager, TUser user,
public virtual Task NotifyAsync(string token, UserManager<TUser> manager, TUser user,
CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(0);

View File

@ -25,53 +25,53 @@ namespace Microsoft.AspNet.Identity
return this;
}
public IdentityBuilder AddUserValidator<T>() where T : class
public virtual IdentityBuilder AddUserValidator<T>() where T : class
{
return AddScoped(typeof(IUserValidator<>).MakeGenericType(UserType), typeof(T));
}
public IdentityBuilder AddRoleValidator<T>() where T : class
public virtual IdentityBuilder AddRoleValidator<T>() where T : class
{
return AddScoped(typeof(IRoleValidator<>).MakeGenericType(RoleType), typeof(T));
}
public IdentityBuilder AddErrorDescriber<TDescriber>() where TDescriber : IdentityErrorDescriber
public virtual IdentityBuilder AddErrorDescriber<TDescriber>() where TDescriber : IdentityErrorDescriber
{
Services.AddScoped<IdentityErrorDescriber, TDescriber>();
return this;
}
public IdentityBuilder AddPasswordValidator<T>() where T : class
public virtual IdentityBuilder AddPasswordValidator<T>() where T : class
{
return AddScoped(typeof(IPasswordValidator<>).MakeGenericType(UserType), typeof(T));
}
public IdentityBuilder AddUserStore<T>() where T : class
public virtual IdentityBuilder AddUserStore<T>() where T : class
{
return AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(T));
}
public IdentityBuilder AddRoleStore<T>() where T : class
public virtual IdentityBuilder AddRoleStore<T>() where T : class
{
return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(T));
}
public IdentityBuilder AddTokenProvider<TProvider>() where TProvider : class
public virtual IdentityBuilder AddTokenProvider<TProvider>() where TProvider : class
{
return AddTokenProvider(typeof(TProvider));
}
public IdentityBuilder AddTokenProvider(Type provider)
public virtual IdentityBuilder AddTokenProvider(Type provider)
{
return AddScoped(typeof(IUserTokenProvider<>).MakeGenericType(UserType), provider);
}
public IdentityBuilder AddMessageProvider<TProvider>() where TProvider : IIdentityMessageProvider
public virtual IdentityBuilder AddMessageProvider<TProvider>() where TProvider : IIdentityMessageProvider
{
return AddScoped(typeof(IIdentityMessageProvider), typeof(TProvider));
}
public IdentityBuilder AddDefaultTokenProviders()
public virtual IdentityBuilder AddDefaultTokenProviders()
{
Services.Configure<DataProtectionTokenProviderOptions>(options =>
{
@ -83,12 +83,12 @@ namespace Microsoft.AspNet.Identity
.AddTokenProvider(typeof(EmailTokenProvider<>).MakeGenericType(UserType));
}
public IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
public virtual IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
{
return AddScoped(typeof(UserManager<>).MakeGenericType(UserType), typeof(TUserManager));
}
public IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
public virtual IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
{
return AddScoped(typeof(RoleManager<>).MakeGenericType(RoleType), typeof(TRoleManager));
}

View File

@ -224,7 +224,7 @@ namespace Microsoft.AspNet.Identity
return await LogResultAsync(true, user);
}
public async Task<bool> IsTwoFactorClientRememberedAsync(TUser user,
public virtual async Task<bool> IsTwoFactorClientRememberedAsync(TUser user,
CancellationToken cancellationToken = default(CancellationToken))
{
var userId = await UserManager.GetUserIdAsync(user, cancellationToken);
@ -306,7 +306,7 @@ namespace Microsoft.AspNet.Identity
return await UserManager.FindByIdAsync(info.UserId, cancellationToken);
}
public async Task<SignInResult> ExternalLoginSignInAsync(string loginProvider, string providerKey, bool isPersistent,
public virtual async Task<SignInResult> ExternalLoginSignInAsync(string loginProvider, string providerKey, bool isPersistent,
CancellationToken cancellationToken = default(CancellationToken))
{
var user = await UserManager.FindByLoginAsync(loginProvider, providerKey, cancellationToken);
@ -361,7 +361,7 @@ namespace Microsoft.AspNet.Identity
return new ExternalLoginInfo(auth.Identity, provider, providerKey, auth.Description.Caption);
}
public AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
{
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
properties.Dictionary[LoginProviderKey] = provider;

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNet.Identity
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string Normalize(string key)
public virtual string Normalize(string key)
{
if (key == null)
{

View File

@ -16,29 +16,32 @@ namespace Microsoft.AspNet.Identity.Test
protected const BindingFlags PublicInstance
= BindingFlags.Instance | BindingFlags.Public;
//protected const BindingFlags AnyInstance
// = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
protected const BindingFlags AnyInstance
= BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
//[Fact]
//public void Public_inheritable_apis_should_be_virtual()
//{
// var nonVirtualMethods
// = (from type in GetAllTypes(TargetAssembly.GetTypes())
// where type.IsVisible
// && !type.IsSealed
// && type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly)
// && type.Namespace != null
// && !type.Namespace.EndsWith(".Compiled")
// from method in type.GetMethods(PublicInstance)
// where GetBasestTypeInAssembly(method.DeclaringType) == type
// && !(method.IsVirtual && !method.IsFinal)
// select type.Name + "." + method.Name)
// .ToList();
[Fact]
public void Public_inheritable_apis_should_be_virtual()
{
var nonVirtualMethods
= (from type in GetAllTypes(TargetAssembly.GetTypes())
where type.IsVisible
&& !type.IsSealed
&& type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly)
&& type.Namespace != null
&& !type.Namespace.EndsWith(".Compiled")
from method in type.GetMethods(PublicInstance)
where GetBasestTypeInAssembly(method.DeclaringType) == type
&& !(method.IsVirtual && !method.IsFinal)
&& !method.Name.StartsWith("get_")
&& !method.Name.StartsWith("set_")
&& !method.Name.Equals("Dispose")
select type.Name + "." + method.Name)
.ToList();
// Assert.False(
// nonVirtualMethods.Any(),
// "\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods));
//}
Assert.False(
nonVirtualMethods.Any(),
"\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods));
}
//[Fact]
//public void Public_api_arguments_should_have_not_null_annotation()