Skip to content

Commit 228995c

Browse files
committed
Add missing virtuals
Enable unit test verifying methods are virtual Fixes aspnet/Identity#349
1 parent d45b796 commit 228995c

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

src/Microsoft.AspNet.Identity/DataProtectionTokenProvider.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
109
using Microsoft.AspNet.Security.DataProtection;
1110
using Microsoft.Framework.OptionsModel;
1211

@@ -66,7 +65,7 @@ public DataProtectorTokenProvider(IDataProtectionProvider dataProtectionProvider
6665
/// <param name="user">The <see cref="TUser"/> the token will be generated from.</param>
6766
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
6867
/// <returns>A <see cref="Task{TResult}"/> that contains the protected token.</returns>
69-
public async Task<string> GenerateAsync(string purpose, UserManager<TUser> manager, TUser user,
68+
public virtual async Task<string> GenerateAsync(string purpose, UserManager<TUser> manager, TUser user,
7069
CancellationToken cancellationToken = default(CancellationToken))
7170
{
7271
if (user == null)
@@ -100,7 +99,7 @@ public async Task<string> GenerateAsync(string purpose, UserManager<TUser> manag
10099
/// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
101100
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
102101
/// <returns>A <see cref="Task{TResult}"/> that is true if the token is valid, otherwise false.</returns>
103-
public async Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user,
102+
public virtual async Task<bool> ValidateAsync(string purpose, string token, UserManager<TUser> manager, TUser user,
104103
CancellationToken cancellationToken = default(CancellationToken))
105104
{
106105
try
@@ -156,7 +155,7 @@ public async Task<bool> ValidateAsync(string purpose, string token, UserManager<
156155
/// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
157156
/// <returns>True if a token generated by this instance can be used as a Two Factor Authentication token, otherwise false.</returns>
158157
/// <remarks>This method will always return false for instances of <see cref="DataProtectorTokenProvider{TUser}"/>.</remarks>
159-
public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user,
158+
public virtual Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUser user,
160159
CancellationToken cancellationToken = default(CancellationToken))
161160
{
162161
return Task.FromResult(false);
@@ -170,7 +169,7 @@ public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TUser> manager, TUs
170169
/// <param name="user">The <see cref="TUser"/> the token was generated for.</param>
171170
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the tasks to complete.</param>
172171
/// <returns>A <see cref="Task{TResult}"/> that represents the started task.</returns>
173-
public Task NotifyAsync(string token, UserManager<TUser> manager, TUser user,
172+
public virtual Task NotifyAsync(string token, UserManager<TUser> manager, TUser user,
174173
CancellationToken cancellationToken = default(CancellationToken))
175174
{
176175
return Task.FromResult(0);

src/Microsoft.AspNet.Identity/IdentityBuilder.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,53 @@ private IdentityBuilder AddScoped(Type serviceType, Type concreteType)
2525
return this;
2626
}
2727

28-
public IdentityBuilder AddUserValidator<T>() where T : class
28+
public virtual IdentityBuilder AddUserValidator<T>() where T : class
2929
{
3030
return AddScoped(typeof(IUserValidator<>).MakeGenericType(UserType), typeof(T));
3131
}
3232

33-
public IdentityBuilder AddRoleValidator<T>() where T : class
33+
public virtual IdentityBuilder AddRoleValidator<T>() where T : class
3434
{
3535
return AddScoped(typeof(IRoleValidator<>).MakeGenericType(RoleType), typeof(T));
3636
}
3737

38-
public IdentityBuilder AddErrorDescriber<TDescriber>() where TDescriber : IdentityErrorDescriber
38+
public virtual IdentityBuilder AddErrorDescriber<TDescriber>() where TDescriber : IdentityErrorDescriber
3939
{
4040
Services.AddScoped<IdentityErrorDescriber, TDescriber>();
4141
return this;
4242
}
4343

44-
public IdentityBuilder AddPasswordValidator<T>() where T : class
44+
public virtual IdentityBuilder AddPasswordValidator<T>() where T : class
4545
{
4646
return AddScoped(typeof(IPasswordValidator<>).MakeGenericType(UserType), typeof(T));
4747
}
4848

49-
public IdentityBuilder AddUserStore<T>() where T : class
49+
public virtual IdentityBuilder AddUserStore<T>() where T : class
5050
{
5151
return AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(T));
5252
}
5353

54-
public IdentityBuilder AddRoleStore<T>() where T : class
54+
public virtual IdentityBuilder AddRoleStore<T>() where T : class
5555
{
5656
return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(T));
5757
}
5858

59-
public IdentityBuilder AddTokenProvider<TProvider>() where TProvider : class
59+
public virtual IdentityBuilder AddTokenProvider<TProvider>() where TProvider : class
6060
{
6161
return AddTokenProvider(typeof(TProvider));
6262
}
6363

64-
public IdentityBuilder AddTokenProvider(Type provider)
64+
public virtual IdentityBuilder AddTokenProvider(Type provider)
6565
{
6666
return AddScoped(typeof(IUserTokenProvider<>).MakeGenericType(UserType), provider);
6767
}
6868

69-
public IdentityBuilder AddMessageProvider<TProvider>() where TProvider : IIdentityMessageProvider
69+
public virtual IdentityBuilder AddMessageProvider<TProvider>() where TProvider : IIdentityMessageProvider
7070
{
7171
return AddScoped(typeof(IIdentityMessageProvider), typeof(TProvider));
7272
}
7373

74-
public IdentityBuilder AddDefaultTokenProviders()
74+
public virtual IdentityBuilder AddDefaultTokenProviders()
7575
{
7676
Services.Configure<DataProtectionTokenProviderOptions>(options =>
7777
{
@@ -83,12 +83,12 @@ public IdentityBuilder AddDefaultTokenProviders()
8383
.AddTokenProvider(typeof(EmailTokenProvider<>).MakeGenericType(UserType));
8484
}
8585

86-
public IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
86+
public virtual IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
8787
{
8888
return AddScoped(typeof(UserManager<>).MakeGenericType(UserType), typeof(TUserManager));
8989
}
9090

91-
public IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
91+
public virtual IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
9292
{
9393
return AddScoped(typeof(RoleManager<>).MakeGenericType(RoleType), typeof(TRoleManager));
9494
}

src/Microsoft.AspNet.Identity/SignInManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public virtual async Task<bool> SendTwoFactorCodeAsync(string provider,
224224
return await LogResultAsync(true, user);
225225
}
226226

227-
public async Task<bool> IsTwoFactorClientRememberedAsync(TUser user,
227+
public virtual async Task<bool> IsTwoFactorClientRememberedAsync(TUser user,
228228
CancellationToken cancellationToken = default(CancellationToken))
229229
{
230230
var userId = await UserManager.GetUserIdAsync(user, cancellationToken);
@@ -306,7 +306,7 @@ public virtual async Task<TUser> GetTwoFactorAuthenticationUserAsync(
306306
return await UserManager.FindByIdAsync(info.UserId, cancellationToken);
307307
}
308308

309-
public async Task<SignInResult> ExternalLoginSignInAsync(string loginProvider, string providerKey, bool isPersistent,
309+
public virtual async Task<SignInResult> ExternalLoginSignInAsync(string loginProvider, string providerKey, bool isPersistent,
310310
CancellationToken cancellationToken = default(CancellationToken))
311311
{
312312
var user = await UserManager.FindByLoginAsync(loginProvider, providerKey, cancellationToken);
@@ -361,7 +361,7 @@ public virtual async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string ex
361361
return new ExternalLoginInfo(auth.Identity, provider, providerKey, auth.Description.Caption);
362362
}
363363

364-
public AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
364+
public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
365365
{
366366
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
367367
properties.Dictionary[LoginProviderKey] = provider;

src/Microsoft.AspNet.Identity/UpperInvariantLookupNormalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class UpperInvariantLookupNormalizer : ILookupNormalizer
1515
/// </summary>
1616
/// <param name="key"></param>
1717
/// <returns></returns>
18-
public string Normalize(string key)
18+
public virtual string Normalize(string key)
1919
{
2020
if (key == null)
2121
{

test/Shared/ApiConsistencyTestBase.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,32 @@ public abstract class ApiConsistencyTestBase
1616
protected const BindingFlags PublicInstance
1717
= BindingFlags.Instance | BindingFlags.Public;
1818

19-
//protected const BindingFlags AnyInstance
20-
// = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
19+
protected const BindingFlags AnyInstance
20+
= BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
2121

22-
//[Fact]
23-
//public void Public_inheritable_apis_should_be_virtual()
24-
//{
25-
// var nonVirtualMethods
26-
// = (from type in GetAllTypes(TargetAssembly.GetTypes())
27-
// where type.IsVisible
28-
// && !type.IsSealed
29-
// && type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly)
30-
// && type.Namespace != null
31-
// && !type.Namespace.EndsWith(".Compiled")
32-
// from method in type.GetMethods(PublicInstance)
33-
// where GetBasestTypeInAssembly(method.DeclaringType) == type
34-
// && !(method.IsVirtual && !method.IsFinal)
35-
// select type.Name + "." + method.Name)
36-
// .ToList();
22+
[Fact]
23+
public void Public_inheritable_apis_should_be_virtual()
24+
{
25+
var nonVirtualMethods
26+
= (from type in GetAllTypes(TargetAssembly.GetTypes())
27+
where type.IsVisible
28+
&& !type.IsSealed
29+
&& type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly)
30+
&& type.Namespace != null
31+
&& !type.Namespace.EndsWith(".Compiled")
32+
from method in type.GetMethods(PublicInstance)
33+
where GetBasestTypeInAssembly(method.DeclaringType) == type
34+
&& !(method.IsVirtual && !method.IsFinal)
35+
&& !method.Name.StartsWith("get_")
36+
&& !method.Name.StartsWith("set_")
37+
&& !method.Name.Equals("Dispose")
38+
select type.Name + "." + method.Name)
39+
.ToList();
3740

38-
// Assert.False(
39-
// nonVirtualMethods.Any(),
40-
// "\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods));
41-
//}
41+
Assert.False(
42+
nonVirtualMethods.Any(),
43+
"\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods));
44+
}
4245

4346
//[Fact]
4447
//public void Public_api_arguments_should_have_not_null_annotation()

0 commit comments

Comments
 (0)