From bf649a00e1e6c9811b408b7d82a4e8c7ad588acd Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Tue, 11 Mar 2014 15:05:37 -0700 Subject: [PATCH] Reenable some unit tests --- src/Microsoft.AspNet.Identity/UserManager.cs | 4 + .../UserManagerTest.cs | 387 +++++++++++++----- 2 files changed, 288 insertions(+), 103 deletions(-) diff --git a/src/Microsoft.AspNet.Identity/UserManager.cs b/src/Microsoft.AspNet.Identity/UserManager.cs index c2df3c4f8a..9ddb24914d 100644 --- a/src/Microsoft.AspNet.Identity/UserManager.cs +++ b/src/Microsoft.AspNet.Identity/UserManager.cs @@ -376,6 +376,10 @@ namespace Microsoft.AspNet.Identity public virtual async Task Delete(TUser user) { ThrowIfDisposed(); + if (user == null) + { + throw new ArgumentNullException("user"); + } await Store.Delete(user).ConfigureAwait(false); return IdentityResult.Success; } diff --git a/test/Microsoft.AspNet.Identity.Test/UserManagerTest.cs b/test/Microsoft.AspNet.Identity.Test/UserManagerTest.cs index fffe48a947..de2d461d80 100644 --- a/test/Microsoft.AspNet.Identity.Test/UserManagerTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/UserManagerTest.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Security.Claims; using Moq; using System; using System.Linq; @@ -44,105 +46,100 @@ namespace Microsoft.AspNet.Identity.Test } [Fact] - public void UsersEmailMethodsFailWhenStoreNotImplementedTest() + public async Task UsersEmailMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserEmail); - //Assert.Throws(() => manager.FindByEmail(null)); - //Assert.Throws(() => manager.SetEmail(null, null)); - //Assert.Throws(() => manager.GetEmail(null)); - //Assert.Throws(() => manager.IsEmailConfirmed(null)); - //Assert.Throws(() => manager.ConfirmEmail(null, null)); + await Assert.ThrowsAsync(() => manager.FindByEmail(null)); + await Assert.ThrowsAsync(() => manager.SetEmail(null, null)); + await Assert.ThrowsAsync(() => manager.GetEmail(null)); + await Assert.ThrowsAsync(() => manager.IsEmailConfirmed(null)); + await Assert.ThrowsAsync(() => manager.ConfirmEmail(null, null)); } [Fact] - public void UsersPhoneNumberMethodsFailWhenStoreNotImplementedTest() + public async Task UsersPhoneNumberMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserPhoneNumber); - //Assert.Throws(() => manager.SetPhoneNumber(null, null))); - //Assert.Throws(() => manager.SetPhoneNumber(null, null)); - //Assert.Throws(() => manager.GetPhoneNumber(null)); + await Assert.ThrowsAsync(async () => await manager.SetPhoneNumber(null, null)); + await Assert.ThrowsAsync(async () => await manager.SetPhoneNumber(null, null)); + await Assert.ThrowsAsync(async () => await manager.GetPhoneNumber(null)); } - //[Fact] - //public void TokenMethodsThrowWithNoTokenProviderTest() - //{ - // var manager = new UserManager(new NoopUserStore()); - // Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.GenerateUserTokenAsync(null, null))); - // Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.VerifyUserTokenAsync(null, null, null))); - //} + [Fact] + public async Task TokenMethodsThrowWithNoTokenProviderTest() + { + var manager = new UserManager(new NoopUserStore()); + await Assert.ThrowsAsync( + async () => await manager.GenerateUserToken(null, null)); + await Assert.ThrowsAsync( + async () => await manager.VerifyUserToken(null, null, null)); + } [Fact] - public void PasswordMethodsFailWhenStoreNotImplementedTest() + public async Task PasswordMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserPassword); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.CreateAsync(null, null))); - //Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.ChangePasswordAsync(null, null, null))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.AddPasswordAsync(null, null))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.RemovePasswordAsync(null))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.CheckPasswordAsync(null, null))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.HasPasswordAsync(null))); + await Assert.ThrowsAsync(() => manager.Create(null, null)); + await Assert.ThrowsAsync(() => manager.ChangePassword(null, null, null)); + await Assert.ThrowsAsync(() => manager.AddPassword(null, null)); + await Assert.ThrowsAsync(() => manager.RemovePassword(null)); + await Assert.ThrowsAsync(() => manager.CheckPassword(null, null)); + await Assert.ThrowsAsync(() => manager.HasPassword(null)); } [Fact] - public void SecurityStampMethodsFailWhenStoreNotImplementedTest() + public async Task SecurityStampMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserSecurityStamp); - //Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.UpdateSecurityStampAsync("bogus"))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.GetSecurityStampAsync("bogus"))); - //Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.VerifyChangePhoneNumberTokenAsync("bogus", "1", "111-111-1111"))); - //Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.GenerateChangePhoneNumberTokenAsync("bogus", "111-111-1111"))); + await Assert.ThrowsAsync(() => manager.UpdateSecurityStamp("bogus")); + await Assert.ThrowsAsync(() => manager.GetSecurityStamp("bogus")); + await Assert.ThrowsAsync(() => manager.VerifyChangePhoneNumberToken("bogus", "1", "111-111-1111")); + await Assert.ThrowsAsync(() => manager.GenerateChangePhoneNumberToken("bogus", "111-111-1111")); } [Fact] - public void LoginMethodsFailWhenStoreNotImplementedTest() + public async Task LoginMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserLogin); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.AddLoginAsync("bogus", null))); - //Assert.Throws( - // () => AsyncHelper.RunSync(() => manager.RemoveLoginAsync("bogus", null))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.GetLoginsAsync("bogus"))); - //Assert.Throws(() => AsyncHelper.RunSync(() => manager.FindAsync(null))); + await Assert.ThrowsAsync(async () => await manager.AddLogin("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.RemoveLogin("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.GetLogins("bogus")); + await Assert.ThrowsAsync(async () => await manager.Find(null)); } [Fact] - public void ClaimMethodsFailWhenStoreNotImplementedTest() + public async Task ClaimMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserClaim); - //Assert.Throws(() => manager.AddClaim("bogus", null)); - //Assert.Throws(() => manager.RemoveClaim("bogus", null)); - //Assert.Throws(() => manager.GetClaims("bogus")); + await Assert.ThrowsAsync(async () => await manager.AddClaim("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.RemoveClaim("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.GetClaims("bogus")); } [Fact] - public void TwoFactorStoreMethodsFailWhenStoreNotImplementedTest() + public async Task TwoFactorStoreMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserTwoFactor); - //Assert.Throws(() => manager.GetTwoFactorEnabled("bogus")); - //Assert.Throws(() => manager.SetTwoFactorEnabled("bogus", true)); + await Assert.ThrowsAsync(async () => await manager.GetTwoFactorEnabled("bogus")); + await Assert.ThrowsAsync(async () => await manager.SetTwoFactorEnabled("bogus", true)); } [Fact] - public void RoleMethodsFailWhenStoreNotImplementedTest() + public async Task RoleMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserRole); - //Assert.Throws(() => manager.AddToRole("bogus", null).Wait()); - //Assert.Throws(async () => await manager.GetRoles("bogus")); - //Assert.Throws(async () => await manager.RemoveFromRole("bogus", null)); - //Assert.Throws(async () => await manager.IsInRole("bogus", "bogus")); + await Assert.ThrowsAsync(async () => await manager.AddToRole("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.GetRoles("bogus")); + await Assert.ThrowsAsync(async () => await manager.RemoveFromRole("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.IsInRole("bogus", "bogus")); } [Fact] @@ -154,24 +151,24 @@ namespace Microsoft.AspNet.Identity.Test } [Fact] - public void ManagerPublicNullCheckTest() + public async Task ManagerPublicNullCheckTest() { Assert.Throws(() => new UserManager((IUserStore)null)); - var manager = new UserManager(new NoopUserStore()); + var manager = new UserManager(new NotImplementedStore()); Assert.Throws(() => manager.ClaimsIdentityFactory = null); Assert.Throws(() => manager.PasswordHasher = null); - //Assert.Throws(() => manager.CreateIdentity(null, "whatever")); - //Assert.Throws(() => manager.Create(null)); - //Assert.Throws(() => manager.Create(null, null)); - //Assert.Throws(() => manager.Create(new TestUser(), null)); - //Assert.Throws(() => manager.Update(null)); - //Assert.Throws(() => manager.Delete(null)); - //Assert.Throws(() => manager.AddClaim("bogus", null)); - //Assert.Throws(() => manager.FindByName(null)); - //Assert.Throws(() => manager.Find(null, null)); - //Assert.Throws(() => manager.AddLogin("bogus", null)); - //Assert.Throws(() => manager.RemoveLogin("bogus", null)); - //Assert.Throws(() => manager.FindByEmail(null)); + await Assert.ThrowsAsync(async () => await manager.CreateIdentity(null, "whatever")); + await Assert.ThrowsAsync(async () => await manager.Create(null)); + await Assert.ThrowsAsync(async () => await manager.Create(null, null)); + await Assert.ThrowsAsync(async () => await manager.Create(new TestUser(), null)); + await Assert.ThrowsAsync(async () => await manager.Update(null)); + await Assert.ThrowsAsync(async () => await manager.Delete(null)); + await Assert.ThrowsAsync(async () => await manager.AddClaim("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.FindByName(null)); + await Assert.ThrowsAsync(async () => await manager.Find(null, null)); + await Assert.ThrowsAsync(async () => await manager.AddLogin("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.RemoveLogin("bogus", null)); + await Assert.ThrowsAsync(async () => await manager.FindByEmail(null)); Assert.Throws(() => manager.RegisterTwoFactorProvider(null, null)); Assert.Throws(() => manager.RegisterTwoFactorProvider("bogus", null)); } @@ -263,42 +260,42 @@ namespace Microsoft.AspNet.Identity.Test // () => AsyncHelper.RunSync(() => manager.IsLockedOutAsync(null)), error); //} - //[Fact] - //public void MethodsThrowWhenDisposedTest() - //{ - // var manager = new UserManager(new NoopUserStore()); - // manager.Dispose(); - // Assert.Throws(() => manager.AddClaim("bogus", null)); - // Assert.Throws(() => manager.AddLogin("bogus", null)); - // Assert.Throws(() => manager.AddPassword("bogus", null)); - // Assert.Throws(() => manager.AddToRole("bogus", null)); - // Assert.Throws(() => manager.ChangePassword("bogus", null, null)); - // Assert.Throws(() => manager.GetClaims("bogus")); - // Assert.Throws(() => manager.GetLogins("bogus")); - // Assert.Throws(() => manager.GetRoles("bogus")); - // Assert.Throws(() => manager.IsInRole("bogus", null)); - // Assert.Throws(() => manager.RemoveClaim("bogus", null)); - // Assert.Throws(() => manager.RemoveLogin("bogus", null)); - // Assert.Throws(() => manager.RemovePassword("bogus")); - // Assert.Throws(() => manager.RemoveFromRole("bogus", null)); - // Assert.Throws(() => manager.RemoveClaim("bogus", null)); - // Assert.Throws(() => manager.Find("bogus", null)); - // Assert.Throws(() => manager.Find(null)); - // Assert.Throws(() => manager.FindById(null)); - // Assert.Throws(() => manager.FindByName(null)); - // Assert.Throws(() => manager.Create(null)); - // Assert.Throws(() => manager.Create(null, null)); - // Assert.Throws(() => manager.CreateIdentity(null, null)); - // Assert.Throws(() => manager.Update(null)); - // Assert.Throws(() => manager.Delete(null)); - // Assert.Throws(() => manager.UpdateSecurityStamp(null)); - // Assert.Throws(() => manager.GetSecurityStamp(null)); - // Assert.Throws(() => manager.GeneratePasswordResetToken(null)); - // Assert.Throws(() => manager.ResetPassword(null, null, null)); - // Assert.Throws(() => manager.GenerateEmailConfirmationToken(null)); - // Assert.Throws(() => manager.IsEmailConfirmed(null)); - // Assert.Throws(() => manager.ConfirmEmail(null, null)); - //} + [Fact] + public async Task MethodsThrowWhenDisposedTest() + { + var manager = new UserManager(new NoopUserStore()); + manager.Dispose(); + await Assert.ThrowsAsync(() => manager.AddClaim("bogus", null)); + await Assert.ThrowsAsync(() => manager.AddLogin("bogus", null)); + await Assert.ThrowsAsync(() => manager.AddPassword("bogus", null)); + await Assert.ThrowsAsync(() => manager.AddToRole("bogus", null)); + await Assert.ThrowsAsync(() => manager.ChangePassword("bogus", null, null)); + await Assert.ThrowsAsync(() => manager.GetClaims("bogus")); + await Assert.ThrowsAsync(() => manager.GetLogins("bogus")); + await Assert.ThrowsAsync(() => manager.GetRoles("bogus")); + await Assert.ThrowsAsync(() => manager.IsInRole("bogus", null)); + await Assert.ThrowsAsync(() => manager.RemoveClaim("bogus", null)); + await Assert.ThrowsAsync(() => manager.RemoveLogin("bogus", null)); + await Assert.ThrowsAsync(() => manager.RemovePassword("bogus")); + await Assert.ThrowsAsync(() => manager.RemoveFromRole("bogus", null)); + await Assert.ThrowsAsync(() => manager.RemoveClaim("bogus", null)); + await Assert.ThrowsAsync(() => manager.Find("bogus", null)); + await Assert.ThrowsAsync(() => manager.Find(null)); + await Assert.ThrowsAsync(() => manager.FindById(null)); + await Assert.ThrowsAsync(() => manager.FindByName(null)); + await Assert.ThrowsAsync(() => manager.Create(null)); + await Assert.ThrowsAsync(() => manager.Create(null, null)); + await Assert.ThrowsAsync(() => manager.CreateIdentity(null, null)); + await Assert.ThrowsAsync(() => manager.Update(null)); + await Assert.ThrowsAsync(() => manager.Delete(null)); + await Assert.ThrowsAsync(() => manager.UpdateSecurityStamp(null)); + await Assert.ThrowsAsync(() => manager.GetSecurityStamp(null)); + await Assert.ThrowsAsync(() => manager.GeneratePasswordResetToken(null)); + await Assert.ThrowsAsync(() => manager.ResetPassword(null, null, null)); + await Assert.ThrowsAsync(() => manager.GenerateEmailConfirmationToken(null)); + await Assert.ThrowsAsync(() => manager.IsEmailConfirmed(null)); + await Assert.ThrowsAsync(() => manager.ConfirmEmail(null, null)); + } private class TestUser : IUser { @@ -336,6 +333,190 @@ namespace Microsoft.AspNet.Identity.Test { return Task.FromResult(0); } + + + } + + + + private class NotImplementedStore : + IUserPasswordStore, + IUserClaimStore, + IUserLoginStore, + IUserEmailStore, + IUserPhoneNumberStore, + IUserLockoutStore, + IUserTwoFactorStore + { + public void Dispose() + { + throw new NotImplementedException(); + } + + public Task Create(TestUser user) + { + throw new NotImplementedException(); + } + + public Task Update(TestUser user) + { + throw new NotImplementedException(); + } + + public Task Delete(TestUser user) + { + throw new NotImplementedException(); + } + + public Task FindById(string userId) + { + throw new NotImplementedException(); + } + + public Task FindByName(string userName) + { + throw new NotImplementedException(); + } + + public Task SetPasswordHash(TestUser user, string passwordHash) + { + throw new NotImplementedException(); + } + + public Task GetPasswordHash(TestUser user) + { + throw new NotImplementedException(); + } + + public Task HasPassword(TestUser user) + { + throw new NotImplementedException(); + } + + public Task> GetClaims(TestUser user) + { + throw new NotImplementedException(); + } + + public Task AddClaim(TestUser user, Claim claim) + { + throw new NotImplementedException(); + } + + public Task RemoveClaim(TestUser user, Claim claim) + { + throw new NotImplementedException(); + } + + public Task AddLogin(TestUser user, UserLoginInfo login) + { + throw new NotImplementedException(); + } + + public Task RemoveLogin(TestUser user, UserLoginInfo login) + { + throw new NotImplementedException(); + } + + public Task> GetLogins(TestUser user) + { + throw new NotImplementedException(); + } + + public Task Find(UserLoginInfo login) + { + throw new NotImplementedException(); + } + + public Task SetEmail(TestUser user, string email) + { + throw new NotImplementedException(); + } + + public Task GetEmail(TestUser user) + { + throw new NotImplementedException(); + } + + public Task GetEmailConfirmed(TestUser user) + { + throw new NotImplementedException(); + } + + public Task SetEmailConfirmed(TestUser user, bool confirmed) + { + throw new NotImplementedException(); + } + + public Task FindByEmail(string email) + { + throw new NotImplementedException(); + } + + public Task SetPhoneNumber(TestUser user, string phoneNumber) + { + throw new NotImplementedException(); + } + + public Task GetPhoneNumber(TestUser user) + { + throw new NotImplementedException(); + } + + public Task GetPhoneNumberConfirmed(TestUser user) + { + throw new NotImplementedException(); + } + + public Task SetPhoneNumberConfirmed(TestUser user, bool confirmed) + { + throw new NotImplementedException(); + } + + public Task GetLockoutEndDate(TestUser user) + { + throw new NotImplementedException(); + } + + public Task SetLockoutEndDate(TestUser user, DateTimeOffset lockoutEnd) + { + throw new NotImplementedException(); + } + + public Task IncrementAccessFailedCount(TestUser user) + { + throw new NotImplementedException(); + } + + public Task ResetAccessFailedCount(TestUser user) + { + throw new NotImplementedException(); + } + + public Task GetAccessFailedCount(TestUser user) + { + throw new NotImplementedException(); + } + + public Task GetLockoutEnabled(TestUser user) + { + throw new NotImplementedException(); + } + + public Task SetLockoutEnabled(TestUser user, bool enabled) + { + throw new NotImplementedException(); + } + + public Task SetTwoFactorEnabled(TestUser user, bool enabled) + { + throw new NotImplementedException(); + } + + public Task GetTwoFactorEnabled(TestUser user) + { + throw new NotImplementedException(); + } } } } \ No newline at end of file