public void deleteUserTest() { MockAccountDataMapper mapper = new MockAccountDataMapper(false); MockTokenService tokenService = new MockTokenService(); AccountService service = new AccountService(mapper, tokenService); AuthorizedUserDTO user = new AuthorizedUserDTO { username = "******" }; Exception ex = Assert.Throws <InvalidParametersException>(() => service.deleteUser(user)); AuthorizedUserDTO user2 = new AuthorizedUserDTO { username = "******", password = "******" }; Exception ex2 = Assert.Throws <AppObjectNotFoundException>(() => service.deleteUser(user2)); mapper = new MockAccountDataMapper(true); service = new AccountService(mapper, tokenService); AuthorizedUserDTO user3 = new AuthorizedUserDTO { username = "******", password = "******" }; Exception ex3 = Assert.Throws <AppValidationException>(() => service.deleteUser(user3)); AuthorizedUserDTO user4 = new AuthorizedUserDTO { username = "******", password = "******" }; service.deleteUser(user4); Assert.Equal(mapper.getDeleteCallCount(), 1); }
// Deletes an existing user public void deleteUser(AuthorizedUserDTO user) { if (user.username == null || user.username == "" || user.password == null || user.password == "") { throw new InvalidParametersException("To delete this user the user needs to provide his username and password."); } User currentUser = _accountMapper.findUserByUsername(user.username); if (currentUser == null) { throw new AppObjectNotFoundException("User not found"); } if (user.password != currentUser.password || !_tokenService.validateUserToken(user.accessToken, currentUser.id)) { throw new AppValidationException(); } _accountMapper.deleteUser(currentUser.id); return; }
public IActionResult deleteUser(AuthorizedUserDTO user) { string accessToken = Request.Headers["Authorization"]; user.accessToken = accessToken; try { _accountService.deleteUser(user); return(Ok()); } catch (InvalidParametersException e) { return(BadRequest(e.Message)); } catch (AppObjectNotFoundException e) { return(NotFound(e.Message)); } catch (AppValidationException) { return(Unauthorized()); } }
public IActionResult login(UserDTO user) { try { AuthorizedUserDTO authenticatedUser = _accountService.authenticateUser(user); return(Ok(new { accessToken = authenticatedUser.accessToken, username = authenticatedUser.username, fullName = authenticatedUser.fullName })); } catch (InvalidParametersException e) { return(BadRequest(e.Message)); } catch (AppObjectNotFoundException e) { return(NotFound(e.Message)); } catch (AppValidationException) { return(Unauthorized()); } }
public void authenticateUserTest() { MockAccountDataMapper mapper = new MockAccountDataMapper(false); MockTokenService tokenService = new MockTokenService(); AccountService service = new AccountService(mapper, tokenService); UserDTO user = new UserDTO { username = "******" }; Exception ex = Assert.Throws <InvalidParametersException>(() => service.authenticateUser(user)); UserDTO user2 = new UserDTO { username = "******", password = "******" }; Exception ex2 = Assert.Throws <AppObjectNotFoundException>(() => service.authenticateUser(user2)); Assert.Equal(mapper.getFindCallCount(), 1); mapper = new MockAccountDataMapper(true); service = new AccountService(mapper, tokenService); UserDTO user3 = new UserDTO { username = "******", password = "******" }; Exception ex3 = Assert.Throws <AppValidationException>(() => service.authenticateUser(user3)); UserDTO user4 = new UserDTO { username = "******", password = "******" }; AuthorizedUserDTO signedIn = service.authenticateUser(user4); Assert.Equal(signedIn.username, "test"); Assert.Equal(signedIn.accessToken, "test"); Assert.Equal(signedIn.fullName, "test"); }