@Test public void shouldNotAuthenticateAnInvalidUser() { mockStatic(Encoder.class); when(Encoder.hash("defaultPassword")).thenReturn("hashedPassword"); User user = new User(); user.setUserName("defaultUserName"); user.setPassword("defaultPassword"); when(userService.selectUserByUserNameAndPassword("defaultUserName", "hashedPassword")).thenReturn(null); UserToken userToken = userAuthenticationService.authenticateUser(user); verify(userService).selectUserByUserNameAndPassword("defaultUserName", "hashedPassword"); assertThat(userToken.isAuthenticated(), is(false)); }
public UserToken authenticateUser(User user) { User fetchedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (fetchedUser == null || fetchedUser.getRestrictLogin()) return new UserToken(user.getUserName(), null, AUTHENTICATION_FAILED); return new UserToken(fetchedUser.getUserName(), fetchedUser.getId(), AUTHENTICATION_SUCCESSFUL); }
UserAuthenticationService { public UserToken authenticateUser(User user) { User fetchedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (fetchedUser == null || fetchedUser.getRestrictLogin()) return new UserToken(user.getUserName(), null, AUTHENTICATION_FAILED); return new UserToken(fetchedUser.getUserName(), fetchedUser.getId(), AUTHENTICATION_SUCCESSFUL); } }
UserAuthenticationService { public UserToken authenticateUser(User user) { User fetchedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (fetchedUser == null || fetchedUser.getRestrictLogin()) return new UserToken(user.getUserName(), null, AUTHENTICATION_FAILED); return new UserToken(fetchedUser.getUserName(), fetchedUser.getId(), AUTHENTICATION_SUCCESSFUL); } @Autowired  UserAuthenticationService(UserService userService); }
UserAuthenticationService { public UserToken authenticateUser(User user) { User fetchedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (fetchedUser == null || fetchedUser.getRestrictLogin()) return new UserToken(user.getUserName(), null, AUTHENTICATION_FAILED); return new UserToken(fetchedUser.getUserName(), fetchedUser.getId(), AUTHENTICATION_SUCCESSFUL); } @Autowired  UserAuthenticationService(UserService userService); UserToken authenticateUser(User user); }
UserAuthenticationService { public UserToken authenticateUser(User user) { User fetchedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (fetchedUser == null || fetchedUser.getRestrictLogin()) return new UserToken(user.getUserName(), null, AUTHENTICATION_FAILED); return new UserToken(fetchedUser.getUserName(), fetchedUser.getId(), AUTHENTICATION_SUCCESSFUL); } @Autowired  UserAuthenticationService(UserService userService); UserToken authenticateUser(User user);  }