public void CreateLoginExceptionAuthTest() { var configuration = new Mock <IConfiguration>(); configuration.Setup(x => x[It.IsAny <string>()]).Returns("mySuperSecretKey"); var macri = new UserDTO() { UserName = "******", Name = "UserTest", Surname = "UserTest", Password = "******", Mail = "*****@*****.**", IsAdmin = true }; var loginServicesMock = new Mock <ILoginServices>(); loginServicesMock.Setup(l => l.CreateSession("UserTest", "UserTest")).Throws(new Exception()); var controller = new AuthController(configuration.Object, loginServicesMock.Object, new LoggerStub()); LoginModelIn loginModel = new LoginModelIn(); loginModel.UserName = "******"; loginModel.Password = "******"; var result = controller.Login(loginModel); var createdResult = result as BadRequestObjectResult; Assert.AreEqual(400, createdResult.StatusCode); }
public void CreateLoginOkAuthTest() { var configuration = new Mock <IConfiguration>(); configuration.Setup(x => x[It.IsAny <string>()]).Returns("mySuperSecretKey"); var macri = new UserDTO() { UserName = "******", Name = "Mauricio", Surname = "Macri", Password = "******", Mail = "*****@*****.**", IsAdmin = true }; var controller = new AuthController(configuration.Object, new LoginServicesMock(macri), new LoggerStub()); LoginModelIn loginModel = new LoginModelIn(); loginModel.UserName = "******"; loginModel.Password = "******"; var result = controller.Login(loginModel); var createdResult = result as OkObjectResult; Assert.AreEqual(200, createdResult.StatusCode); }
public void CreateLoginInvalidModelInAuthTest() { var configuration = new Mock <IConfiguration>(); configuration.Setup(x => x[It.IsAny <string>()]).Returns("mySuperSecretKey"); var userTest = new UserDTO() { UserName = "******", Name = "UserTest", Surname = "UserTest", Password = "******", Mail = "*****@*****.**", IsAdmin = true }; var controller = new AuthController(configuration.Object, new LoginServicesMock(userTest), new LoggerStub()); controller.ModelState.AddModelError("UserName is required", ""); controller.ModelState.AddModelError("Password is required", ""); LoginModelIn loginModel = new LoginModelIn(); var result = controller.Login(loginModel); var createdResult = result as BadRequestObjectResult; Assert.AreEqual(400, createdResult.StatusCode); }
public IActionResult Login([FromBody] LoginModelIn login) { if (ModelState.IsValid) { try { Guid token = sessionLogic.Login(login.Mail, login.Password); if (token == null) { return(BadRequest("You have entered a wrong mail or password. Try again!")); } UserEntity admin = userLogic.GetByMail(login.Mail); return(Ok(new LoginModelOut { Token = token, Mail = admin.Mail, IsAdmin = admin.IsAdmin })); } catch (ArgumentException e) { return(Unauthorized(e.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } else { return(Unauthorized(ModelState)); } }
private IActionResult AuthenticateWithValidModel(LoginModelIn user) { IActionResult result; try { UserDto logged = loginService.Login(user.Username, user.Password); string tokenString = GenerateJSONWebToken(logged); result = Ok(new { Token = tokenString }); } catch (WrongPasswordException e2) { ErrorModelOut error = new ErrorModelOut() { ErrorMessage = e2.Message }; result = BadRequest(error); } catch (ServiceException e) { result = GenerateResponse(e); } return(result); }
public void LoginInvalidModelTest() { LoginModelIn credentials = new LoginModelIn(); controllerToTest.ModelState.AddModelError("", "Error"); IActionResult result = controllerToTest.Authenticate(credentials); BadRequestObjectResult badRequestResult = result as BadRequestObjectResult; Assert.IsNotNull(badRequestResult); }
public IActionResult Authenticate([FromBody] LoginModelIn user) { IActionResult result; if (ModelState.IsValid) { result = AuthenticateWithValidModel(user); } else { result = BadRequest(ModelState); } return(result); }
public void LoginAdminSuccesfullyTest() { //arrange loginService.Setup(l => l.Login("aUsername", "aPassword")).Returns(testUser); //act LoginModelIn credentials = new LoginModelIn() { Username = "******", Password = "******" }; IActionResult result = controllerToTest.Authenticate(credentials); OkObjectResult okResult = result as OkObjectResult; //assert Assert.IsNotNull(okResult); }
public IActionResult Login([FromBody] LoginModelIn loginModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { return(TryToLogin(loginModel)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void TestInitialize() { testUser = new UserEntity { Id = 1, CompleteName = "Juan Perez", Mail = "*****@*****.**", Password = "******", Requests = new List <RequestEntity>(), IsAdmin = false }; login = new LoginModelIn { Mail = "*****@*****.**", Password = "******" }; sessionLogicMock = new Mock <ISessionLogic>(); userLogicMock = new Mock <IUserLogic>(); sessionController = new SessionController(sessionLogicMock.Object, userLogicMock.Object); }
public void LoginWrongPasswordTest() { //arrange Exception toThrow = new WrongPasswordException(); loginService.Setup(l => l.Login("aUsername", "otherPassword")).Throws(toThrow); //act LoginModelIn credentials = new LoginModelIn() { Username = "******", Password = "******" }; IActionResult result = controllerToTest.Authenticate(credentials); BadRequestObjectResult badRequestResult = result as BadRequestObjectResult; ErrorModelOut error = badRequestResult.Value as ErrorModelOut; //assert loginService.VerifyAll(); Assert.IsNotNull(badRequestResult); Assert.IsNotNull(error); Assert.AreEqual(toThrow.Message, error.ErrorMessage); }
public void LoginNotFoundTest() { //Arrange. Exception internalEx = new UserNotFoundException(); Exception toThrow = new ServiceException(internalEx.Message, ErrorType.ENTITY_NOT_FOUND); loginService.Setup(l => l.Login("otherUsername", "aPassword")).Throws(toThrow); //Act. LoginModelIn credentials = new LoginModelIn() { Username = "******", Password = "******" }; IActionResult result = controllerToTest.Authenticate(credentials); BadRequestObjectResult badRequestResult = result as BadRequestObjectResult; ErrorModelOut error = badRequestResult.Value as ErrorModelOut; //Assert. loginService.VerifyAll(); Assert.IsNotNull(badRequestResult); Assert.IsNotNull(error); Assert.AreEqual(toThrow.Message, error.ErrorMessage); }
private IActionResult TryToLogin(LoginModelIn loginModel) { loginServices.CreateSession(loginModel.UserName, loginModel.Password); var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Secret"])); var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); var tokenOptions = new JwtSecurityToken( issuer: "http://localhost:5000", audience: "http://localhost:5000", claims: new List <Claim> { new Claim(ClaimTypes.Role, loginServices.LoggedUser.IsAdmin ? "Administrator" : "Follower"), new Claim("UserName", loginModel.UserName), new Claim("Password", loginModel.Password), }, expires: DateTime.Now.AddDays(1), signingCredentials: signinCredentials ); var tokenString = new JwtSecurityTokenHandler().WriteToken(tokenOptions); logger.Log(loginModel.UserName, "Logged into the system"); return(Ok(new { Token = tokenString })); }
public void LoginNoDataAccessTest() { //Arrange. Exception internalEx = new DataInaccessibleException(); Exception toThrow = new ServiceException(internalEx.Message, ErrorType.DATA_INACCESSIBLE); loginService.Setup(us => us.Login(It.IsAny <string>(), It.IsAny <string>())).Throws(toThrow); LoginModelIn credentials = new LoginModelIn() { Username = "******", Password = "******" }; //Act. IActionResult result = controllerToTest.Authenticate(credentials); ObjectResult noData = result as ObjectResult; ErrorModelOut error = noData.Value as ErrorModelOut; //Assert. Assert.IsNotNull(result); Assert.IsNotNull(noData); Assert.AreEqual(500, noData.StatusCode); Assert.IsNotNull(error); Assert.AreEqual(toThrow.Message, error.ErrorMessage); }