public async Task <IOperationResult <AuthenticationViewModel> > Login(AuthenticationRequest authenticationRequest) { try { IOperationResult <string> validationLoginModelResult = ValidateLoginModel(authenticationRequest); if (!validationLoginModelResult.Success) { return(OperationResult <AuthenticationViewModel> .Fail(validationLoginModelResult.Message)); } string encrypPassword = _encrypService.EncrypText(authenticationRequest.Password); SystemUser user = await _systemUserRepository.FindAsync(user => user.Username == authenticationRequest.Username && user.Password == encrypPassword); IOperationResult <string> validationUserResult = ValidateULogedUser(user); if (!validationUserResult.Success) { return(OperationResult <AuthenticationViewModel> .Fail(validationUserResult.Message)); } AuthenticationViewModel authenticationResponse = _userService.GetToken(user); return(OperationResult <AuthenticationViewModel> .Ok(authenticationResponse)); } catch { return(OperationResult <AuthenticationViewModel> .Fail("Ha ocurrido un error en la autenticación del usuario")); } }
public async Task LoginWhenEncrypServiceThrowShouldReturnFailOperationResult() { ISystemUserRepository systemUserRepository = Substitute.For <ISystemUserRepository>(); IUserService userService = Substitute.For <IUserService>(); IEncrypService encrypService = Substitute.For <IEncrypService>(); SystemUser userResult = new SystemUser { Active = true }; systemUserRepository.FindAsync(Arg.Any <Expression <Func <SystemUser, bool> > >()).Returns(userResult); encrypService.EncrypText(Arg.Any <string>()).Throws(new Exception()); AuthenticationManager authenticationManager = new AuthenticationManager(systemUserRepository, userService, encrypService); AuthenticationRequest authenticationRequest = new AuthenticationRequest { Username = "******", Password = "******" }; IOperationResult <AuthenticationViewModel> actual = await authenticationManager.Login(authenticationRequest); Assert.IsFalse(actual.Success); Assert.AreEqual("Ha ocurrido un error en la autenticación del usuario", actual.Message); }
public async Task <IOperationResult <bool> > Activate(string email, string encriptedUsername) { try { SystemUser systemUser = await _systemUserRepository.FindAsync(user => user.Email == email); ValidateUserToActive(systemUser, encriptedUsername); systemUser.Active = true; await _systemUserRepository.SaveAsync(); return(OperationResult <bool> .Ok(true)); } catch { return(OperationResult <bool> .Fail("Ha ocurrido un error activando el usuario comuniquese con el administrador del sistema para mas informacion")); } }
public async Task LoginWhenUserNotFoundOrPasswordIsIncorrectShouldReturnFailOperationResult() { ISystemUserRepository systemUserRepository = Substitute.For <ISystemUserRepository>(); IUserService userService = Substitute.For <IUserService>(); IEncrypService encrypService = Substitute.For <IEncrypService>(); systemUserRepository.FindAsync(Arg.Any <Expression <Func <SystemUser, bool> > >()).Returns(default(SystemUser)); AuthenticationManager authenticationManager = new AuthenticationManager(systemUserRepository, userService, encrypService); AuthenticationRequest authenticationRequest = new AuthenticationRequest { Username = "******", Password = "******" }; IOperationResult <AuthenticationViewModel> actual = await authenticationManager.Login(authenticationRequest); Assert.IsFalse(actual.Success); Assert.AreEqual("Usuario o contraseña incorrecto", actual.Message); }
public async Task LoginWhenAllSuccessShouldReturnSuccessOperationResult() { ISystemUserRepository systemUserRepository = Substitute.For <ISystemUserRepository>(); IUserService userService = Substitute.For <IUserService>(); IEncrypService encrypService = Substitute.For <IEncrypService>(); SystemUser userResult = new SystemUser { Active = true }; systemUserRepository.FindAsync(Arg.Any <Expression <Func <SystemUser, bool> > >()).Returns(userResult); AuthenticationManager authenticationManager = new AuthenticationManager(systemUserRepository, userService, encrypService); AuthenticationRequest authenticationRequest = new AuthenticationRequest { Username = "******", Password = "******" }; IOperationResult <AuthenticationViewModel> actual = await authenticationManager.Login(authenticationRequest); Assert.IsTrue(actual.Success); }
public async Task LoginWhenUserIsInactiveShouldReturnFailOperationResult() { ISystemUserRepository systemUserRepository = Substitute.For <ISystemUserRepository>(); IUserService userService = Substitute.For <IUserService>(); IEncrypService encrypService = Substitute.For <IEncrypService>(); SystemUser userResult = new SystemUser { Active = false }; systemUserRepository.FindAsync(Arg.Any <Expression <Func <SystemUser, bool> > >()).Returns(userResult); AuthenticationManager authenticationManager = new AuthenticationManager(systemUserRepository, userService, encrypService); AuthenticationRequest authenticationRequest = new AuthenticationRequest { Username = "******", Password = "******" }; IOperationResult <AuthenticationViewModel> actual = await authenticationManager.Login(authenticationRequest); Assert.IsFalse(actual.Success); Assert.AreEqual("Este usuario no se encuentra activo", actual.Message); }