UserController { @RequestMapping(value = "login", method = RequestMethod.POST) public LoginResponse login(@NonNull @RequestBody final LoginRequest login) throws InvalidLoginException { if (login.getName() == null || !userService.containsName(login.getName()) || login.getPassword() == null || login.getPassword().isEmpty()) { throw new InvalidLoginException(); } if (!authenticationService.userPasswordCheck(login.getName(), login.getPassword())) { throw new InvalidLoginException(); } final UserEntity userEntity = userService.findByName(login.getName()); return new LoginResponse(userEntity.getId().toString(), userEntity.getName(), authenticationService.authenticationToken(login.getName(), login.getPassword())); } @Autowired  UserController(final UserService userService, final AuthenticationService authenticationService); @RequestMapping(value = "login", method = RequestMethod.POST) LoginResponse login(@NonNull @RequestBody final LoginRequest login); @RequestMapping(value = "check", method = RequestMethod.GET) LoginResponse check(final HttpServletResponse response); @RequestMapping(value = "role/{role}", method = RequestMethod.GET) Boolean claimContainsRole(@PathVariable final String role,
        final HttpServletRequest request);  }
@Test public void testNoKeyMatch() { Mockito.when(mockUserService.containsName(USERNAME)).thenReturn(false); final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(PASSWORD); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertTrue(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); }
@Test public void testPasswordNoMatch() { Mockito.when(mockUserService.containsName(USERNAME)).thenReturn(true); Mockito.when(mockAuthenticationService.userPasswordCheck(USERNAME, PASSWORD)).thenReturn(false); final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(PASSWORD); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertTrue(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); Mockito.verify(mockAuthenticationService).userPasswordCheck(USERNAME, PASSWORD); }
@Test public void testPasswordMatch() { Mockito.when(mockUserService.containsName(USERNAME)).thenReturn(true); Mockito.when(mockAuthenticationService.userPasswordCheck(USERNAME, PASSWORD)).thenReturn(true); Mockito.when(mockUserEntity.getId()).thenReturn(UUID.randomUUID()); Mockito.when(mockUserEntity.getName()).thenReturn(USERNAME); Mockito.when(mockAuthenticationService.authenticationToken(USERNAME, PASSWORD)).thenReturn(""); Mockito.when(mockUserService.findByName(USERNAME)).thenReturn(mockUserEntity); final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(PASSWORD); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertFalse(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); Mockito.verify(mockAuthenticationService).userPasswordCheck(USERNAME, PASSWORD); Mockito.verify(mockAuthenticationService).authenticationToken(USERNAME, PASSWORD); Mockito.verify(mockUserEntity).getId(); Mockito.verify(mockUserEntity).getName(); Mockito.verify(mockUserService).findByName(USERNAME); }
@Test public void testValidUserWithEmptyPassword() { Mockito.when(mockUserService.containsName(USERNAME)).thenReturn(true); final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(""); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertTrue(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); }
@Test public void testValidUserWithNullPassword() { Mockito.when(mockUserService.containsName(USERNAME)).thenReturn(true); final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(null); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertTrue(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); }
@Ignore("Null is not allowed") @Test(expected = InvalidLoginException.class) public void testNullLogin() { userController.login(null); }
@Test(expected = InvalidLoginException.class) public void testNullName() { final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(null); userController.login(loginRequest); }
@Test public void testNullPassword() { final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(null); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertTrue(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); }
@Test public void testEmptyPassword() { final LoginRequest loginRequest = new LoginRequest(); loginRequest.setName(USERNAME); loginRequest.setPassword(""); boolean exceptionThrown = false; try { userController.login(loginRequest); } catch (final InvalidLoginException e) { exceptionThrown = true; } assertTrue(exceptionThrown); Mockito.verify(mockUserService).containsName(USERNAME); }