public async void Login_fail() { // Arrange var securityAdapter = new Mock <ISecurityAdapter>(); var controller = new AccountApiController(securityAdapter.Object); var model = new LoginModel { LoginEmail = "", Password = "", }; // Act var result = await controller.Login(model); // Assert Assert.IsType <ObjectResult>(result); var objectResult = result as ObjectResult; Assert.True(objectResult.StatusCode == (int)HttpStatusCode.BadRequest); Assert.IsType <List <string> >(objectResult.Value); var errors = objectResult.Value as List <string>; Assert.True(errors.Count == 4); }
public async void Login_success() { // Arrange var securityAdapter = new Mock <ISecurityAdapter>(); var controller = new AccountApiController(securityAdapter.Object); var model = new LoginModel { LoginEmail = "*****@*****.**", Password = "******", RememberMe = false }; securityAdapter.Setup(x => x.UserExists(model.LoginEmail)).Returns(_BoolFromTaskFunction(true)); securityAdapter.Setup(x => x.CheckPassword(model.LoginEmail, model.Password)).Returns(_BoolFromTaskFunction(true)); securityAdapter.Setup(x => x.Login(model.LoginEmail, model.Password, model.RememberMe)).Returns(_BoolFromTaskFunction(true)); // Act var result = await controller.Login(model); // Assert Assert.IsType <ObjectResult>(result); ObjectResult objectResult = result as ObjectResult; Assert.True(objectResult.StatusCode == (int)HttpStatusCode.OK); securityAdapter.Verify(x => x.UserExists(model.LoginEmail), Times.Once); securityAdapter.Verify(x => x.CheckPassword(model.LoginEmail, model.Password), Times.Once); securityAdapter.Verify(x => x.Login(model.LoginEmail, model.Password, model.RememberMe), Times.Once); }
public async Task <IActionResult> UserLogin(LoginViewModel viewModel) { if (ModelState.IsValid) { var loginRequest = viewModel.MapObject <LoginRequest>(); var response = AccountApiController.Login(loginRequest); NotifyUser(response.ResponseStatus, response.Message); var logginUserDetails = new Dictionary <string, string>(); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, response.UserId.ToString()) }; claims.Add(new Claim(ClaimTypes.Role, response.UserType.ToString())); claims.Add(new Claim(ClaimTypes.Email, response.Email.ToString())); claims.Add(new Claim(ClaimTypes.Sid, response.UserName.ToString())); ClaimsIdentity userIdentity = new ClaimsIdentity(claims, "login"); ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity); await HttpContext.SignInAsync(principal); if (response.UserType == UserType.Teacher) { return(RedirectToAction("Index", "Teacher")); } else if (response.UserType == UserType.Student) { return(RedirectToAction("Index", "Student")); } } return(RedirectToAction("Login", viewModel)); }
public void InvalidLogin() { unitOfWork.Data = new MockData { Users = new List <User> { new User { id = 1, username = "******", password = "******", customer_code = "cust" } } }; var result = controller.Login("xxx", "xxx"); Assert.IsInstanceOfType(result, typeof(HttpResponseMessage)); var message = result as HttpResponseMessage; Assert.AreEqual(HttpStatusCode.BadRequest, message?.StatusCode); }
public async Task LoginTest() { LogInRequestModel logInRequestModel = CreateLogInRequestModel(); LogInResponseModel expected = CreateLogInResponseModel(); SetupAccountServiceLoginMock(logInRequestModel, expected); var actual = (JsonResult)await accountApiController.Login(logInRequestModel); Assert.AreEqual(expected, actual.Value); }