using Moq; using System; using System.Linq; using System.Threading.Tasks; using Xunit; namespace Microsoft.AspNet.Identity.Test { public class UserManagerTest { [Fact] public void ServiceProviderWireupTest() { var manager = new UserManager(TestServices.DefaultServiceProvider()); Assert.NotNull(manager.PasswordHasher); Assert.NotNull(manager.PasswordValidator); Assert.NotNull(manager.UserValidator); } // TODO: Mock fails in K (this works fine in net45) //[Fact] //public async Task CreateTest() //{ // // Setup // var store = new Mock>(); // var user = new TestUser(); // store.Setup(s => s.Create(user)).Verifiable(); // var userManager = new UserManager(store.Object); // // Act // var result = await userManager.Create(user); // // Assert // Assert.True(result.Succeeded); // store.VerifyAll(); //} [Fact] public void UsersQueryableFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsQueryableUsers); Assert.Throws(() => manager.Users.Count()); } [Fact] public void 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)); } [Fact] public void 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)); } //[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 void 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))); } [Fact] public void 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"))); } [Fact] public void 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))); } [Fact] public void 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")); } [Fact] public void TwoFactorStoreMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserTwoFactor); //Assert.Throws(() => manager.GetTwoFactorEnabled("bogus")); //Assert.Throws(() => manager.SetTwoFactorEnabled("bogus", true)); } [Fact] public void 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")); } [Fact] public void DisposeAfterDisposeWorksTest() { var manager = new UserManager(new NoopUserStore()); manager.Dispose(); manager.Dispose(); } [Fact] public void ManagerPublicNullCheckTest() { Assert.Throws(() => new UserManager((IUserStore)null)); var manager = new UserManager(new NoopUserStore()); 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)); Assert.Throws(() => manager.RegisterTwoFactorProvider(null, null)); Assert.Throws(() => manager.RegisterTwoFactorProvider("bogus", null)); } //[Fact] //public void MethodsFailWithUnknownUserTest() //{ // var db = UnitTestHelper.CreateDefaultDb(); // var manager = new UserManager(new UserStore(db)); // manager.UserTokenProvider = new NoOpTokenProvider(); // var error = "UserId not found."; // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.AddClaimAsync(null, new Claim("a", "b"))), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.AddLoginAsync(null, new UserLoginInfo("", ""))), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.AddPasswordAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.AddToRoleAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.ChangePasswordAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetClaimsAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetLoginsAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetRolesAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.IsInRoleAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.RemoveClaimAsync(null, new Claim("a", "b"))), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.RemoveLoginAsync(null, new UserLoginInfo("", ""))), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.RemovePasswordAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.RemoveFromRoleAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.UpdateSecurityStampAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetSecurityStampAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.HasPasswordAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GeneratePasswordResetTokenAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.ResetPasswordAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.IsEmailConfirmedAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GenerateEmailConfirmationTokenAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.ConfirmEmailAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetEmailAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.SetEmailAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.IsPhoneNumberConfirmedAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.ChangePhoneNumberAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.VerifyChangePhoneNumberTokenAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetPhoneNumberAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.SetPhoneNumberAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetTwoFactorEnabledAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.SetTwoFactorEnabledAsync(null, true)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GenerateTwoFactorTokenAsync(null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.VerifyTwoFactorTokenAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.NotifyTwoFactorTokenAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.GetValidTwoFactorProvidersAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.VerifyUserTokenAsync(null, null, null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.AccessFailedAsync(null)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.SetLockoutEnabledAsync(null, false)), error); // ExceptionHelper.ThrowsWithError( // () => AsyncHelper.RunSync(() => manager.SetLockoutEndDateAsync(null, DateTimeOffset.UtcNow)), error); // ExceptionHelper.ThrowsWithError( // () => 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)); //} private class TestUser : IUser { public string Id { get; private set; } public string UserName { get; set; } } private class NoopUserStore : IUserStore { public Task Create(TestUser user) { return Task.FromResult(0); } public Task Update(TestUser user) { return Task.FromResult(0); } public Task FindById(string userId) { return Task.FromResult(null); } public Task FindByName(string userName) { return Task.FromResult(null); } public void Dispose() { } public Task Delete(TestUser user) { return Task.FromResult(0); } } } }