public async Task Set_401_Response_StatusCode_When_InvalidPasswordException_Occurs() { var invalidPasswordException = new InvalidPasswordException(); var exceptionContext = CustomExceptionFilterTestHelper.GetExceptionContext(invalidPasswordException); CustomExceptionFilterAttribute.OnException(exceptionContext); Assert.AreEqual((int)HttpStatusCode.Unauthorized, exceptionContext.HttpContext.Response.StatusCode); }
static void Main(string[] args) { string txt = Console.ReadLine(); if (txt != "123") { InvalidPasswordException ex = new InvalidPasswordException("틀린 암호"); throw ex; } Console.WriteLine("올바른 암호"); }
public void Login_WithIncorrectPasswordForExistingUsername_ThrowsInvalidPasswordException() { string expectedUsername = "******"; string password = "******"; _mockUnitOfWork.Setup(s => s.UserRepository.GetByUsername(expectedUsername)).ReturnsAsync(new User() { Username = expectedUsername }); _mockPasswordHasher.Setup(s => s.VerifyHashedPassword(It.IsAny <string>(), password)).Returns(PasswordVerificationResult.Failed); InvalidPasswordException exception = Assert.ThrowsAsync <InvalidPasswordException>(() => _authenticationService.Login(expectedUsername, password)); string actualUsername = exception.Username; Assert.AreEqual(expectedUsername, actualUsername); }
public void ChangePassword_WithIncorrectOldPassword_ThrowsInvalidPasswordException() { User expectedUser = new User() { Username = "******", PasswordHash = "testpassword" }; _mockPasswordHasher.Setup(s => s.VerifyHashedPassword(It.IsAny <string>(), expectedUser.PasswordHash)).Returns(PasswordVerificationResult.Failed); InvalidPasswordException exception = Assert.ThrowsAsync <InvalidPasswordException>(() => _authenticationService.ChangePassword(expectedUser, expectedUser.PasswordHash, It.IsAny <string>(), It.IsAny <string>())); string actualUsername = exception.Username; Assert.AreEqual(expectedUser.Username, actualUsername); }
private Exception CatchException(Exception ex) { throw ex switch { UsernameExistsException _ => new BusinessException(ErrorCode.UsernameAlreadyExists, ex), NotAuthorizedException _ => new BusinessException(ErrorCode.NotAuthorized, ex), TooManyRequestsException _ => new BusinessException(ErrorCode.TooManyRequests, ex), PasswordResetRequiredException _ => new BusinessException(ErrorCode.PasswordResetRequired, ex), UserNotFoundException _ => new BusinessException(ErrorCode.UserNotFound, ex), UserNotConfirmedException _ => new BusinessException(ErrorCode.UserNotConfirmed, ex), InvalidPasswordException _ => new BusinessException(ErrorCode.InvalidPassword, ex), CodeMismatchException _ => new BusinessException(ErrorCode.CodeMismatch, ex), ExpiredCodeException _ => new BusinessException(ErrorCode.ExpiredCode, ex), LimitExceededException _ => new BusinessException(ErrorCode.LimitExceeded, ex), BusinessException _ => ex, _ => new CriticalException(ErrorCode.InternalServerError, ex), }; }
public void Login_IncorrectPass_() { string username = "******"; string password = "******"; _accountService.Setup(a => a.GetByUsername(username)).ReturnsAsync(new Account { AccountOwner = new User() { Username = username } }); _passHasher.Setup(a => a.VerifyHashedPassword(It.IsAny <string>(), password)).Returns(PasswordVerificationResult.Failed); InvalidPasswordException exception = Assert.ThrowsAsync <InvalidPasswordException>(() => _authentication.Login(username, password)); string realUsername = exception.Username; Assert.AreEqual(username, realUsername); }
public async Task Login_WithIncorrectPasswordForExistingUsername_ThrowsInvalidPasswordExceptionForUsername() { // Arrange string expectedUsername = "******"; string password = "******"; User user = new User() { Username = expectedUsername }; _mockAccountSvc.Setup(s => s.GetByUsername(expectedUsername)).ReturnsAsync(new Account { AccountHolder = user }); _mockPwdHash.Setup(s => s.VerifyHashedPassword(user, It.IsAny <string>(), password)).Returns(PasswordVerificationResult.Failed); // Act InvalidPasswordException exception = await Assert.ThrowsAsync <InvalidPasswordException>(() => _authenticationSvc.Login(expectedUsername, password)); // Assert string actualUsername = exception.Username; Assert.Equal(expectedUsername, actualUsername); }
public void Login_WithInCorrectPasswordForExistingUsername_ThrowsInvalidPasswordExceptionForUsername() { // Arrange string expectedUsername = "******"; string password = "******"; _mockAccountService.Setup(s => s.GetByUsername(expectedUsername)).ReturnsAsync(new Account() { AccountHolder = new User() { Username = expectedUsername } }); _mockPasswordHasher.Setup(p => p.VerifyHashedPassword(It.IsAny <string>(), password)).Returns(PasswordVerificationResult.Failed); // Act InvalidPasswordException exception = Assert.ThrowsAsync <InvalidPasswordException>(() => _authenticationService.Login(expectedUsername, password)); // Assert string actualUsername = exception.Username; Assert.AreEqual(expectedUsername, actualUsername); }
public void TestExceptions() { { AlreadyExistsException v1 = new AlreadyExistsException("ex"); AlreadyExistsException v2 = (AlreadyExistsException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ConfigurationException v1 = new ConfigurationException("ex"); ConfigurationException v2 = (ConfigurationException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ConnectionBrokenException v1 = new ConnectionBrokenException("ex"); ConnectionBrokenException v2 = (ConnectionBrokenException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ConnectionFailedException v1 = new ConnectionFailedException("ex"); ConnectionFailedException v2 = (ConnectionFailedException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ConnectorException v1 = new ConnectorException("ex"); ConnectorException v2 = (ConnectorException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ConnectorIOException v1 = new ConnectorIOException("ex"); ConnectorIOException v2 = (ConnectorIOException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ConnectorSecurityException v1 = new ConnectorSecurityException("ex"); ConnectorSecurityException v2 = (ConnectorSecurityException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { InvalidCredentialException v1 = new InvalidCredentialException("ex"); InvalidCredentialException v2 = (InvalidCredentialException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { InvalidPasswordException v1 = new InvalidPasswordException("ex"); InvalidPasswordException v2 = (InvalidPasswordException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { PasswordExpiredException v1 = new PasswordExpiredException("ex"); v1.Uid = (new Uid("myuid")); PasswordExpiredException v2 = (PasswordExpiredException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); Assert.AreEqual("myuid", v2.Uid.GetUidValue()); } { OperationTimeoutException v1 = new OperationTimeoutException("ex"); OperationTimeoutException v2 = (OperationTimeoutException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { PermissionDeniedException v1 = new PermissionDeniedException("ex"); PermissionDeniedException v2 = (PermissionDeniedException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { UnknownUidException v1 = new UnknownUidException("ex"); UnknownUidException v2 = (UnknownUidException)CloneObject(v1); Assert.AreEqual("ex", v2.Message); } { ArgumentException v1 = new ArgumentException("my msg"); ArgumentException v2 = (ArgumentException)CloneObject(v1); Assert.AreEqual("my msg", v2.Message); } { ArgumentNullException v1 = new ArgumentNullException(null, "my msg 1"); ArgumentException v2 = (ArgumentException)CloneObject(v1); Assert.AreEqual("my msg 1", v2.Message); } { Exception v1 = new Exception("my msg2"); Exception v2 = (Exception)CloneObject(v1); Assert.AreEqual("my msg2", v2.Message); } }