Login { @POST @Consumes(MediaType.APPLICATION_JSON) public LoginResult doLogin(LoginCredentials credentials) { Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(credentials.getUsername(), credentials.getPassword().toCharArray(), true); try { subject.login(token); return createLoginResultFromSubject(subject); } catch(UnknownAccountException e) { logservice.log(LogService.LOG_WARNING, "Login error: unknown account", e); return new LoginResult("Unknown account"); } catch (IncorrectCredentialsException e) { logservice.log(LogService.LOG_WARNING, "Login error: wrong password", e); return new LoginResult("Wrong password"); } catch (LockedAccountException e) { logservice.log(LogService.LOG_WARNING, "Login error: locked account", e); return new LoginResult("Locked account"); } catch (AuthenticationException e) { logservice.log(LogService.LOG_WARNING, "Login error: general authentication error", e); return new LoginResult("Unknown error"); } catch (Exception e) { logservice.log(LogService.LOG_ERROR, "Login error: internal server error", e); throw new InternalServerErrorException(); } finally { token.clear(); } }  @GET LoginResult loginStatus(); @POST @Consumes(MediaType.APPLICATION_JSON) LoginResult doLogin(LoginCredentials credentials);  }
@Test public void testLoginOk() throws Exception { LoginCredentials credentials = new LoginCredentials("jad", "1ad"); HttpServletRequest request = buildLoginRequest(credentials); HttpServletResponse response = mock(HttpServletResponse.class); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; createSubjectAndBindItToThread(request, response); LoginResult result = resource.doLogin(credentials); assertThat(result.getRoles()).isNotEmpty(); assertEquals("", result.getErrorMessage()); }
@Test public void testAdminLoginOk() throws Exception { LoginCredentials credentials = new LoginCredentials("admin", "admin"); HttpServletRequest request = buildLoginRequest(credentials); HttpServletResponse response = mock(HttpServletResponse.class); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; createSubjectAndBindItToThread(request, response); LoginResult result = resource.doLogin(credentials); assertThat(result.getRoles()).isNotEmpty(); assertEquals("", result.getErrorMessage()); }
@Test public void testLoginUnknownUser() throws Exception { LoginCredentials credentials = new LoginCredentials("unknown", "unknown"); HttpServletRequest request = buildLoginRequest(credentials); HttpServletResponse response = mock(HttpServletResponse.class); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; createSubjectAndBindItToThread(request, response); LoginResult result = resource.doLogin(credentials); assertEquals(0, result.getRoles().length); assertEquals("Unknown account", result.getErrorMessage()); }
@Test public void testLoginWrongPassword() throws Exception { LoginCredentials credentials = new LoginCredentials("jad", "wrong"); HttpServletRequest request = buildLoginRequest(credentials); HttpServletResponse response = mock(HttpServletResponse.class); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; createSubjectAndBindItToThread(request, response); LoginResult result = resource.doLogin(credentials); assertEquals(0, result.getRoles().length); assertEquals("Wrong password", result.getErrorMessage()); }
@Test public void testLoginLockedAccount() throws Exception { try { lockAccount("jad"); LoginCredentials credentials = new LoginCredentials("jad", "wrong"); HttpServletRequest request = buildLoginRequest(credentials); HttpServletResponse response = mock(HttpServletResponse.class); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; createSubjectAndBindItToThread(request, response); LoginResult result = resource.doLogin(credentials); assertEquals(0, result.getRoles().length); assertEquals("Locked account", result.getErrorMessage()); } finally { unlockAccount("jad"); } }
@Test public void testLoginWithAuthenticationException() { createSubjectThrowingExceptionAndBindItToThread(AuthenticationException.class); LoginCredentials credentials = new LoginCredentials("jad", "wrong"); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; LoginResult result = resource.doLogin(credentials); assertEquals(0, result.getRoles().length); assertEquals("Unknown error", result.getErrorMessage()); }
@Test public void testLoginWithUnexpectedException() { createSubjectThrowingExceptionAndBindItToThread(IllegalArgumentException.class); LoginCredentials credentials = new LoginCredentials("jad", "wrong"); MockLogService logservice = new MockLogService(); Login resource = new Login(); resource.logservice = logservice; assertThrows(InternalServerErrorException.class, () -> { resource.doLogin(credentials); }); }