public async Task ShouldFailAuthenticationIfWrongPassword() { var dbContext = InfrastructureFactory.GetInMemoryContext(nameof(ShouldFailAuthenticationIfWrongPassword)); var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext); var user = EntityFactory.GetUser("johndoe", "Pass@123"); await unitOfWork.AddAndSaveAsync(user); var handler = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime()); Assert.ThrowsAsync <UserAuthenticationFailedException>(() => handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@13", UserAuthConstants.TokenExpirationTime), default)); }
public async Task ShouldReturnAuthenticatedUser() { var dbContext = InfrastructureFactory.GetInMemoryContext(nameof(ShouldReturnAuthenticatedUser)); var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext); var user = EntityFactory.GetUser("johndoe", "Pass@123"); await unitOfWork.AddAndSaveAsync(user); var handler = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime()); var authentication = await handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@123", UserAuthConstants.TokenExpirationTime), default); Assert.AreEqual("johndoe", authentication.User.Nickname); Assert.IsFalse(string.IsNullOrEmpty(authentication.Token)); }
public async Task ShouldCreateTokenOnSucesfulAuth() { var dbContext = InfrastructureFactory.GetInMemoryContext(nameof(ShouldChangePasswordSaltAndReHashPasswordOnSuccesfulAuth)); var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext); var user = EntityFactory.GetUser("johndoe", "Pass@123"); await unitOfWork.AddAndSaveAsync(user); var handler = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime()); var authentication = await handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@123", UserAuthConstants.TokenExpirationTime), default); var tokenCount = await dbContext.UserTokens.CountAsync(u => u.Nickname == "johndoe"); Assert.AreEqual(1, tokenCount); }
public async Task ShouldChangePasswordSaltAndReHashPasswordOnSuccesfulAuth() { var dbContext = InfrastructureFactory.GetInMemoryContext(nameof(ShouldChangePasswordSaltAndReHashPasswordOnSuccesfulAuth)); var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext); var user = EntityFactory.GetUser("johndoe", "Pass@123"); await unitOfWork.AddAndSaveAsync(user); var salt = user.PasswordSalt; var passwordHash = user.PasswordHash; var lastLoginDate = user.LastLoginDate; var handler = new AuthenticateUserCommandHandler(unitOfWork, InfrastructureFactory.GetSha1Hasher(), InfrastructureFactory.GetMachineDateTime()); var authentication = await handler.Handle(new AuthenticateUserCommand("johndoe", "Pass@123", UserAuthConstants.TokenExpirationTime), default); user = await unitOfWork.GetSingleAsync <UserEntity>(u => u.Nickname == "johndoe"); Assert.AreNotEqual(salt, user.PasswordSalt); Assert.AreNotEqual(passwordHash, user.PasswordSalt); Assert.AreNotEqual(lastLoginDate, user.LastLoginDate); }
public async Task ShouldCreateNewUser() { var dbContext = InfrastructureFactory.GetInMemoryContext(nameof(ShouldCreateNewUser)); var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext); var handler = new CreateUserCommandHandler(unitOfWork, InfrastructureFactory.GetMachineDateTime(), InfrastructureFactory.GetSha1Hasher()); await handler.Handle(new CreateUserCommand("johndoe", "Pass@123"), default); var user = unitOfWork.GetSingleAsync <UserEntity>(u => u.Nickname == "johndoe"); Assert.NotNull(user); }
public async Task ShouldFailIfAttemptingToCreateExistingUser() { var dbContext = InfrastructureFactory.GetInMemoryContext(nameof(ShouldFailIfAttemptingToCreateExistingUser)); var unitOfWork = InfrastructureFactory.GetEfUnitOfWork(dbContext); await unitOfWork.AddAndSaveAsync(EntityFactory.GetUser("johndoe", "Pass@123")); var handler = new CreateUserCommandHandler(unitOfWork, InfrastructureFactory.GetMachineDateTime(), InfrastructureFactory.GetSha1Hasher()); Assert.ThrowsAsync <CreatingExistingUserException>(() => handler.Handle(new CreateUserCommand("johndoe", "Pass@123"), default)); }