public void Reregister_WhenCalledEndUserHasAlreadyActivatedAccount_NotAuthorizedExceptionExpected() { // ARRANGE var actualUser = new User { Email = "*****@*****.**", IsAccountActivated = true }; _userRepository.SingleOrDefaultAsync(Arg.Any <Expression <Func <User, bool> > >()).Returns(x => { var store = new List <User> { actualUser }; return(store.Single(x.ArgAt <Expression <Func <User, bool> > >(0).Compile())); }); // ACT var userForReregister = new UserForReregisterDto { Email = actualUser.Email }; Func <Task> action = async() => await _sut.Reregister(userForReregister); // ASSERT action.Should().Throw <NotAuthorizedException>() .WithMessage("The user account is already activated"); }
public void Reregister_WhenCalledEndUserWithGivenEmailDoesNotExist_EntityNotFoundExceptionExpected() { // ARRANGE _userRepository.SingleOrDefaultAsync(Arg.Any <Expression <Func <User, bool> > >()).Returns((User)null); // ACT var userForReregister = new UserForReregisterDto { Email = "*****@*****.**" }; Func <Task> action = async() => await _sut.Reregister(userForReregister); // ASSERT action.Should().Throw <EntityNotFoundException>() .WithMessage($"The user with email: {userForReregister.Email} does not exist in data store"); }
public async Task <IActionResult> Reregister(UserForReregisterDto userForReregister) { try { await Task.Run(() => _authService.Reregister(userForReregister)); } catch (Exception ex) { if (ex is EntityNotFoundException || ex is NotAuthorizedException) { return(BadRequest("User does not exist or his account is already activated")); } throw; } await _mediator.Publish(new UserRegisteredNotificationEvent(userForReregister.Email)); return(StatusCode(StatusCodes.Status201Created)); }
public async Task Reregister(UserForReregisterDto userForReregister) { if (userForReregister == null) { throw new ArgumentNullException(nameof(userForReregister)); } var user = await UserRepository.SingleOrDefaultAsync(x => x.Email == userForReregister.Email.ToLower()); if (user == null) { throw new EntityNotFoundException( $"The user with email: {userForReregister.Email} does not exist in data store"); } if (user.IsAccountActivated) { throw new NotAuthorizedException("The user account is already activated"); } var currentActivationToken = await AccountActivationTokenRepository.SingleOrDefaultAsync(x => x.UserId == user.Id); var newActivationToken = CreateToken <AccountActivationToken>(); if (currentActivationToken == null) { user.ActivationToken = newActivationToken; UserRepository.Update(user); } else { currentActivationToken.Token = newActivationToken.Token; AccountActivationTokenRepository.Update(currentActivationToken); } await _unitOfWork.CompleteAsync(); }