Example usage for org.apache.shiro.web.subject WebSubject logout

List of usage examples for org.apache.shiro.web.subject WebSubject logout

Introduction

In this page you can find the example usage for org.apache.shiro.web.subject WebSubject logout.

Prototype

void logout();

Source Link

Document

Logs out this Subject and invalidates and/or removes any associated entities, such as a Session Session and authorization data.

Usage

From source file:no.priv.bang.ukelonn.api.resources.LoginTest.java

License:Apache License

/**
 * Verify that a GET to the LoginServlet will return the current state
 * when no user is logged in//ww  w  .j a v  a2 s.c o  m
 *
 * Used to initialize webapp if the webapp is reloaded.
 *
 * @throws Exception
 */
@Test
public void testGetLoginStateWhenNotLoggedIn() throws Exception {
    // Set up the request
    HttpSession session = mock(HttpSession.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getSession()).thenReturn(session);
    HttpServletResponse response = mock(HttpServletResponse.class);

    // Create mock OSGi services to inject
    MockLogService logservice = new MockLogService();

    // Set up Shiro to be in a logged-in state
    WebSubject subject = createSubjectAndBindItToThread(request, response);
    subject.logout();

    // Create the resource and check the login state with HTTP GET
    Login resource = new Login();
    resource.logservice = logservice;
    LoginResult result = resource.loginStatus();

    // Check the response
    assertEquals(0, result.getRoles().length);
    assertEquals("", result.getErrorMessage());
}

From source file:no.priv.bang.ukelonn.api.UkelonnRestApiServletTest.java

License:Apache License

/**
 * Verify that a GET to the LoginServlet will return the current state
 * when no user is logged in/*from   w  w w.j av  a  2  s  .c  om*/
 *
 * Used to initialize webapp if the webapp is reloaded.
 *
 * @throws Exception
 */
@Test
public void testGetLoginStateWhenNotLoggedIn() throws Exception {
    // Set up the request
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getProtocol()).thenReturn("HTTP/1.1");
    when(request.getMethod()).thenReturn("GET");
    when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8181/ukelonn/api/login"));
    when(request.getRequestURI()).thenReturn("/ukelonn/api/login");
    when(request.getContextPath()).thenReturn("/ukelonn");
    when(request.getServletPath()).thenReturn("/api");
    when(request.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
    HttpSession session = mock(HttpSession.class);
    when(request.getSession()).thenReturn(session);

    // Create the response that will cause a NullPointerException
    // when trying to print the body
    MockHttpServletResponse response = mock(MockHttpServletResponse.class, CALLS_REAL_METHODS);

    // Create mock OSGi services to inject
    MockLogService logservice = new MockLogService();

    // Set up Shiro to be in a logged-in state
    WebSubject subject = createSubjectAndBindItToThread(request, response);
    subject.logout();

    // Create the servlet
    UkelonnRestApiServlet servlet = new UkelonnRestApiServlet();
    servlet.setLogservice(logservice);
    servlet.setUkelonnService(getUkelonnServiceSingleton());

    // Activate the servlet DS component
    servlet.activate();

    // When the servlet is activated it will be plugged into the http whiteboard and configured
    ServletConfig config = createServletConfigWithApplicationAndPackagenameForJerseyResources();
    servlet.init(config);

    // Check the login state with HTTP GET
    servlet.service(request, response);

    // Check the response
    assertEquals(200, response.getStatus());
    assertEquals("application/json", response.getContentType());

    LoginResult result = ServletTestBase.mapper
            .readValue(response.getOutput().toString(StandardCharsets.UTF_8.toString()), LoginResult.class);
    assertEquals(0, result.getRoles().length);
    assertEquals("", result.getErrorMessage());
}