public async Task VerifyAccountAlreadyVerified() { AuthToken result; const string password = "******"; const string email = "*****@*****.**"; const string tokenString = "token"; using (var context = new QuizContext(ManagerTestHelper.Options)) { var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>()); await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true); await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), true); await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(2), true); await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.AccountVerification, tokenString, true); await context.SaveChangesAsync(); result = await sut.VerifyAccount(new OneTimeTokenRequest { Token = tokenString, DeviceId = "device" }); } Assert.IsNull(result); }
public async Task UpdatePasswordWithTokenWrongType() { bool result; const string password = "******"; const string email = "*****@*****.**"; const string newPassword = "******"; const string tokenString = "token"; Models.DbModels.User updatedUser; using (var context = new QuizContext(ManagerTestHelper.Options)) { var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>()); await ManagerTestHelper.AddUserAsync(context, 0); var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false); await ManagerTestHelper.AddUserAsync(context, 2); await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.AccountVerification, tokenString, true); await context.SaveChangesAsync(); result = await sut.UpdatePassword(tokenString, newPassword); updatedUser = await context.Users.FindAsync(user.Id); } Assert.IsFalse(result); Assert.IsTrue(AuthenticationHelper.CompareByteArrays(AuthenticationHelper.EncryptPassword(password), updatedUser.PasswordHash)); }
public async Task VerifyAccount() { AuthToken result; const string password = "******"; const string email = "*****@*****.**"; const string tokenString = "token"; OneTimeToken token; Models.DbModels.User user; Models.DbModels.User updatedUser; using (var context = new QuizContext(ManagerTestHelper.Options)) { var authManager = new Mock <IAuthManager>(); authManager .Setup(c => c.GenerateTokenAsync(It.IsAny <int>(), It.IsAny <string>())) .Returns((int u, string d) => Task.FromResult(new AuthToken { UserId = u })); var sut = ManagerTestHelper.GetUserManager(context, authManager.Object); await ManagerTestHelper.AddUserAsync(context, 0); user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false); await ManagerTestHelper.AddUserAsync(context, 2); await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.AccountVerification, tokenString, true); await context.SaveChangesAsync(); result = await sut.VerifyAccount(new OneTimeTokenRequest { Token = tokenString, DeviceId = "device" }); token = await context.OneTimeTokens.FirstOrDefaultAsync(t => t.Email == email && t.TokenType == (byte)OneTimeTokenType.AccountVerification); updatedUser = await context.Users.FindAsync(user.Id); } Assert.AreEqual(result.UserId, user.Id); Assert.IsNull(token); Assert.IsTrue(updatedUser.IsVerified); }
public async Task UpdatePasswordWithTokenUserNotFound() { bool result; const string email = "*****@*****.**"; const string newPassword = "******"; const string tokenString = "token"; using (var context = new QuizContext(ManagerTestHelper.Options)) { var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>()); await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true); await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1), true); await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.ForgotPassword, tokenString, true); await context.SaveChangesAsync(); result = await sut.UpdatePassword(tokenString, newPassword); } Assert.IsFalse(result); }
public async Task UpdatePasswordWithToken() { bool result; const string password = "******"; const string email = "*****@*****.**"; const string newPassword = "******"; const string tokenString = "token"; Models.DbModels.User updatedUser; OneTimeToken deletedToken; using (var context = new QuizContext(ManagerTestHelper.Options)) { var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>()); await ManagerTestHelper.AddUserAsync(context, 0); var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false); await ManagerTestHelper.AddUserAsync(context, 2); await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.ForgotPassword, tokenString, true); await context.SaveChangesAsync(); result = await sut.UpdatePassword(tokenString, newPassword); updatedUser = await context.Users.FindAsync(user.Id); deletedToken = await context.OneTimeTokens.FirstOrDefaultAsync(t => t.Email == email && t.Token == tokenString); } Assert.IsTrue(result); Assert.IsTrue(AuthenticationHelper.CompareByteArrays(AuthenticationHelper.EncryptPassword(newPassword), updatedUser.PasswordHash)); Assert.IsNull(deletedToken); }