public void Create_TestFormValidation() { var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userProfileServiceMock = new Mock<IUserProfileService>(); var userProfileService = userProfileServiceMock.Object; var userController = new UserController(userService, userProfileService); userController.ViewData.ModelState.AddModelError("rofl", "nao"); //This simulates any validation error var createModel = new CreateViewModel(); var userCredentials = new UserCredentials() { Email = createModel.Email, Password = createModel.Password }; var result = userController.Index(createModel) as ViewResult; //Verify that the controller does not try to call login on IUserService userServiceMock.Verify(a => a.Create(userCredentials), Times.Never()); Assert.IsNotNull(result, "Login Action did not yield a ViewResult"); Assert.AreSame(createModel, result.Model, "Controller did not forward the defective view"); }
public void Login_InvalidCredentials() { var accessTokenProviderMock = new Mock<IUserAccessTokenProvider>(); var accessTokenProvider = accessTokenProviderMock.Object; var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userController = new LoginController(userService, accessTokenProvider); var loginModel = new LoginViewModel() { Email = "testuser", Password = "******" }; var credentials = new UserCredentials() { Email = loginModel.Email, Password = loginModel.Password }; userServiceMock.Setup(a => a.Login(credentials)).Throws(new InvalidUserCredentialsException()); var actionResult = userController.Index(loginModel) as ViewResult; Assert.IsNotNull(actionResult, "Controller did not return a ViewResult"); Assert.AreSame(loginModel, actionResult.Model, "Controller did not forward the defective view"); }
public void Create_TestExistingUser() { var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userProfileServiceMock = new Mock<IUserProfileService>(); var userProfileService = userProfileServiceMock.Object; var userController = new UserController(userService, userProfileService); var createModel = new CreateViewModel { Username = "******", Password = "******", StreetAddress = "rofl", City = "mao", ZipCode = 1000 }; var credentials = new UserCredentials() { Email = createModel.Email, Password = createModel.Password }; userServiceMock.Setup(a => a.Create(credentials)).Throws(new UserExistsException()); var viewResult = userController.Index(createModel) as ViewResult; Assert.IsNotNull(viewResult, "Controller did not return a ViewResult"); Assert.AreSame(createModel, viewResult.Model); userServiceMock.Verify(a => a.Create(credentials)); }
public UserAccessToken Create(UserCredentials credentials) { try { var user = _userRepository.Create(credentials.Email, credentials.Password); _logger.Info("Created user account: " + credentials.Email); return Mapper.Map<UserAccessToken>(user); } catch(UserEntityAlreadyExistsException userEntityAlreadyExists) { _logger.Info("Failed to create account, it already existed: " + credentials.Email); throw new UserExistsException(); } }
public UserAccessToken Login(UserCredentials credentials) { try { var user = _userRepository.Login(credentials.Email, credentials.Password); _logger.Info("Successful Authentication for user: "******"User " + credentials.Email + " has been granted administrative rights"); } return Mapper.Map<UserAccessToken>(user); } catch(UserEntityNotFoundException entityNotFoundException) { _logger.Warn("Failed Authentication for user: " + credentials.Email); throw new InvalidUserCredentialsException(); } }
public void Login_Success() { var accessTokenProviderMock = new Mock<IUserAccessTokenProvider>(); var accessTokenProvider = accessTokenProviderMock.Object; var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userController = new LoginController(userService, accessTokenProvider); var loginModel = new LoginViewModel() { Email = "testuser", Password = "******" }; var credentials = new UserCredentials() { Email = loginModel.Email, Password = loginModel.Password }; var accessToken = new UserAccessToken("123456"); userServiceMock.Setup(a => a.Login(credentials)).Returns(accessToken); var result = userController.Index(loginModel) as RedirectToRouteResult; Assert.IsNotNull(result, "Login Action did not yield a Redirection"); Assert.AreEqual(result.RouteValues["controller"], "Home"); Assert.AreEqual(result.RouteValues["action"], "Index"); //Assert that the controller forwarded the username and password to IUserService correctly userServiceMock.Verify(a => a.Login(credentials), Times.Once()); //Assert that the controller set the session state correctly accessTokenProviderMock.Verify(a => a.SetUserAccessToken(accessToken)); }
public void Create_TestSuccess() { var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userProfileServiceMock = new Mock<IUserProfileService>(); var userProfileService = userProfileServiceMock.Object; var controllerContextMock = new Mock<ControllerContext>(); var userController = new UserController(userService, userProfileService); userController.ControllerContext = controllerContextMock.Object; var createModel = new CreateViewModel { Username = "******", Password = "******", StreetAddress = "rofl", City = "mao", ZipCode = 1000 }; var credentials = new UserCredentials() { Email = createModel.Email, Password = createModel.Password }; var accessToken = new UserAccessToken("123456"); userServiceMock.Setup(a => a.Create(credentials)).Returns(accessToken); userServiceMock.Setup(a => a.Login(credentials)).Returns(accessToken); var viewResult = userController.Index(createModel) as RedirectToRouteResult; Assert.IsNotNull(viewResult, "Controller did not return a RedirectToRouteResult"); var userProfile = new UserProfile() { Username = createModel.Username, Address = new Address() { Street = createModel.StreetAddress, City = createModel.City, ZipCode = createModel.ZipCode } }; userServiceMock.Verify(a => a.Create(credentials)); userProfileServiceMock.Verify(a => a.UpdateProfile(accessToken, userProfile)); }
public bool Equals(UserCredentials other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.Email, Email) && Equals(other.Password, Password); }