public async Task GivenUserModel_CreateNewUserCommandHandlerConvertsUserModelToEntity_ThenGeneratesAndMapsPasswordInfo_ThenCallsExecuteOnCommand_WhenHandleIsCalled() { //arrange var command = new Mock <Commands.ICreateNewUserCommand>(); var hashutil = new Mock <Helpers.IPasswordHashUtil>(); var entityMapper = new Mock <Mappings.IUserEntityMapper>(); var passwordinfo = new DTOs.PasswordInfo { salt = "salt" }; var userEntity = new Entities.User { username = "******" }; var userModel = new Models.User { username = "******", password = "******" }; command.Setup(c => c.execute(It.IsAny <Entities.User>())).ReturnsAsync(true); hashutil.Setup(h => h.GeneratePasswordInfo(It.IsAny <string>())).Returns(passwordinfo); entityMapper.Setup(m => m.MapFromUserModel(It.IsAny <Models.User>())).Returns(userEntity); entityMapper.Setup(m => m.MapPasswordInfo(It.IsAny <Entities.User>(), It.IsAny <DTOs.PasswordInfo>())).Returns(userEntity); var sut = new CommandHandlers.CreateNewUserCommandHandler(command.Object, hashutil.Object, entityMapper.Object); //act await sut.handle(userModel); //assert entityMapper.Verify(f => f.MapFromUserModel(It.Is <Models.User>(u => u.username == userModel.username)), Times.Once()); entityMapper.Verify(f => f.MapPasswordInfo(It.Is <Entities.User>(u => u.username == userEntity.username), It.Is <DTOs.PasswordInfo>(pi => pi.salt == passwordinfo.salt)), Times.Once()); hashutil.Verify(h => h.GeneratePasswordInfo(It.Is <string>(s => s == userModel.password)), Times.Once()); command.Verify(c => c.execute(It.Is <Entities.User>(u => u.username == userEntity.username)), Times.Once()); }
public Entities.User MapPasswordInfo(Entities.User user, DTOs.PasswordInfo passwordInfo) { user.pwhash = passwordInfo.hash; user.salt = passwordInfo.salt; return(user); }
public void GivenUserEntityAndPasswordInfo_MapPasswordInfoCorrectlyMapsPasswordInfoToUserEntity_WhenCalled() { //arrange var passwordInfo = new DTOs.PasswordInfo { salt = "salt" }; var crypto = new System.Security.Cryptography.SHA512Managed(); passwordInfo.hash = crypto.ComputeHash(System.Text.Encoding.ASCII.GetBytes("apassword")); var userInfo = new Entities.UserInfo { email = "anemail", id = 1 }; var userEntity = new Entities.User { id = 1, userinfo = userInfo, username = "******" }; var sut = new Mappings.UserEntityMapper(); //act userEntity = sut.MapPasswordInfo(userEntity, passwordInfo); //assert Assert.AreEqual(userEntity.username, "username"); Assert.AreEqual(userEntity.id, 1); Assert.AreEqual(userEntity.pwhash, passwordInfo.hash); Assert.AreEqual(userEntity.salt, passwordInfo.salt); Assert.AreEqual(userEntity.userinfo.email, "anemail"); Assert.AreEqual(userEntity.userinfo.id, 1); }