LoginServiceImpl implements LoginService { @Override public UserLogin login(Login login) { if (login == null || StringUtils.isBlank(login.getPassword()) || StringUtils.isBlank(login.getUserName())) { return null; } UserLogin user = userLoginDAO.login(login); if (user == null || StringUtils.isBlank(user.getSha2()) || StringUtils.isBlank(user.getSalt())) { return null; } boolean loggedIn = SHA2PasswordUtil.checkPassword(user.getSha2().getBytes(), login.getPassword().getBytes(), user.getSalt().getBytes()); if (!loggedIn) { user = null; } return user; } LoginServiceImpl(); @Override UserLogin login(Login login); @Override UserLogin addUser(Login user, long role, String firstName, String middleName,
            String lastName, String transRoleDesc); @Override List<UserLogin> getAllUsers(); @Override void deleteUser(UserLogin user); @Override Properties getUserRoleList(); static final String CONNECT_ADMIN_USER; }
@Test public void testLogin(){ Login login = new Login(); login.setUserName("ABCD"); login.setPassword("ABCDEFGH"); UserLogin returnedUser = new UserLogin(); returnedUser.setSalt(salt); returnedUser.setSha2("osoSSNvkHcYvZs0+Y54qW9oGVOVz6WB9UVn15NRAq0x4doJ3pjvlT9hlu9bFFxy71YELxql5O+w6+0UctMRP6Q=="); Mockito.when(userDAO.login(Mockito.isA(Login.class))).thenReturn(returnedUser); UserLogin loggedInUser = loginService.login(login); assertEquals(returnedUser, loggedInUser); }
@Test public void testLogin_badPassword(){ Login login = new Login(); login.setUserName("ABCD"); login.setPassword("ABCDEFGH"); UserLogin returnedUser = new UserLogin(); returnedUser.setSalt(salt); returnedUser.setSha2("ThIsIsThEwRoNgHaSh"); Mockito.when(userDAO.login(Mockito.isA(Login.class))).thenReturn(returnedUser); UserLogin loggedInUser = loginService.login(login); assertEquals(null, loggedInUser); }
@Test public void testLogin_noPassword(){ Login login = new Login(); login.setUserName("ABCD"); login.setPassword(""); UserLogin returnedUser = new UserLogin(); returnedUser.setSalt(salt); returnedUser.setSha2(""); Mockito.when(userDAO.login(Mockito.isA(Login.class))).thenReturn(returnedUser); UserLogin loggedInUser = loginService.login(login); assertEquals(null, loggedInUser); }
@Test public void testLogin_noDBPassword(){ Login login = new Login(); login.setUserName("ABCD"); login.setPassword("1234"); UserLogin returnedUser = new UserLogin(); returnedUser.setSalt(salt); returnedUser.setSha2(""); Mockito.when(userDAO.login(Mockito.isA(Login.class))).thenReturn(returnedUser); UserLogin loggedInUser = loginService.login(login); assertEquals(null, loggedInUser); }