using System.Collections.Generic; using System.Security.Claims; 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 async Task UsersEmailMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserEmail); 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 async Task UsersPhoneNumberMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserPhoneNumber); 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 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 async Task PasswordMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserPassword); 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 async Task SecurityStampMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserSecurityStamp); 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 async Task LoginMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserLogin); 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 async Task ClaimMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserClaim); 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 async Task TwoFactorStoreMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserTwoFactor); await Assert.ThrowsAsync(async () => await manager.GetTwoFactorEnabled("bogus")); await Assert.ThrowsAsync(async () => await manager.SetTwoFactorEnabled("bogus", true)); } [Fact] public async Task RoleMethodsFailWhenStoreNotImplementedTest() { var manager = new UserManager(new NoopUserStore()); Assert.False(manager.SupportsUserRole); 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] public void DisposeAfterDisposeWorksTest() { var manager = new UserManager(new NoopUserStore()); manager.Dispose(); manager.Dispose(); } [Fact] public async Task ManagerPublicNullCheckTest() { Assert.Throws(() => new UserManager((IUserStore)null)); var manager = new UserManager(new NotImplementedStore()); Assert.Throws(() => manager.ClaimsIdentityFactory = null); Assert.Throws(() => manager.PasswordHasher = 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)); } //[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 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 { 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); } } 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(); } } } }