public UserService(IPasswordHasher passwordHasher, IUserDeletedEventHandler userDeletedEventHandler) { if (passwordHasher == null) { throw new ArgumentNullException(nameof(passwordHasher)); } if (userDeletedEventHandler == null) { throw new ArgumentNullException(nameof(userDeletedEventHandler)); } _passwordHasher = passwordHasher; _userDeletedEventHandler = userDeletedEventHandler; }
public async Task CreateUser_uses_IPasswordHasher_correctly() { IPasswordHasher passwordHasherMock = Substitute.For <IPasswordHasher>(); passwordHasherMock.HashPassword(Arg.Any <string>()).Returns("hash"); IUserDeletedEventHandler userDeletedEventHandlerStub = Substitute.For <IUserDeletedEventHandler>(); IUserRepository userRepositoryStub = Substitute.For <IUserRepository>(); userRepositoryStub.Create(Arg.Any <User>()).Returns(Task.FromResult(Result.CreateSuccess())); UserService sut = new UserService(passwordHasherMock, userDeletedEventHandlerStub); await sut.CreateUser(userRepositoryStub, "*****@*****.**", "Test", "validpassword"); passwordHasherMock.Received(1).HashPassword("validpassword"); }
public async Task create_user_with_existing_email() { IPasswordHasher passwordHasherStub = Substitute.For <IPasswordHasher>(); passwordHasherStub.HashPassword(Arg.Any <string>()).Returns("hash"); IUserDeletedEventHandler userDeletedEventHandlerStub = Substitute.For <IUserDeletedEventHandler>(); IUserRepository userRepositoryStub = Substitute.For <IUserRepository>(); userRepositoryStub.Create(Arg.Any <User>()).Returns(Task.FromResult(Result.CreateError("UserAlreadyExists", "The user already exsists."))); UserService sut = new UserService(passwordHasherStub, userDeletedEventHandlerStub); Result <User> user = await sut.CreateUser(userRepositoryStub, "*****@*****.**", "Test", "longpassword"); user.IsSuccess.Should().BeFalse(); }
public async Task authentication_should_succeed_with_correct_email_and_password_hash() { IPasswordHasher passwordHasher = Substitute.For <IPasswordHasher>(); passwordHasher.VerifyPassword(Arg.Any <string>(), Arg.Any <string>()).Returns(true); IUserDeletedEventHandler userDeletedEventHandler = Substitute.For <IUserDeletedEventHandler>(); IUserRepository userRepository = Substitute.For <IUserRepository>(); User user = new User(1234, "*****@*****.**", "Test", "hash", false); userRepository.FindByEmail(Arg.Any <string>()).Returns(Task.FromResult(Result.CreateSuccess(user))); UserService sut = new UserService(passwordHasher, userDeletedEventHandler); Result <User> result = await sut.Authenticate(userRepository, "*****@*****.**", "validpassword"); result.Should().BeEquivalentTo(Result.CreateSuccess(user)); }
public async Task create_user() { IPasswordHasher passwordHasher = Substitute.For <IPasswordHasher>(); passwordHasher.HashPassword(Arg.Any <string>()).Returns("hash"); IUserDeletedEventHandler userDeletedEventHandler = Substitute.For <IUserDeletedEventHandler>(); IUserRepository userRepository = Substitute.For <IUserRepository>(); userRepository.Create(Arg.Any <User>()).Returns(Task.FromResult(Result.CreateSuccess())); UserService sut = new UserService(passwordHasher, userDeletedEventHandler); Result <User> user = await sut.CreateUser(userRepository, "*****@*****.**", "Test", "validpassword"); Result <User> expected = Result.CreateSuccess(new User(0, "*****@*****.**", "Test", "hash", false)); user.Should().BeEquivalentTo(expected); }
public async Task delete_user_trigger_UserDeletedEventHandler() //Hugo { IPasswordHasher passwordHasher = Substitute.For <IPasswordHasher>(); passwordHasher.HashPassword(Arg.Any <string>()).Returns("hash"); IUserDeletedEventHandler userDeletedEventHandler = Substitute.For <IUserDeletedEventHandler>(); IUserRepository userRepository = Substitute.For <IUserRepository>(); User user = new User(1234, "*****@*****.**", "Test", "hash", false); userRepository.FindById(Arg.Any <int>()).Returns(Result.CreateSuccess(user)); UserService sut = new UserService(passwordHasher, userDeletedEventHandler); var unitOfWork = Substitute.For <IUnitOfWork>(); var pollRepository = Substitute.For <IPollRepository>(); await sut.DeleteUser(unitOfWork, userRepository, pollRepository, 1234); await userDeletedEventHandler.Received(1).Handle(unitOfWork, pollRepository, 1234); }