Example usage for org.springframework.mock.web MockHttpSession MockHttpSession

List of usage examples for org.springframework.mock.web MockHttpSession MockHttpSession

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpSession MockHttpSession.

Prototype

public MockHttpSession() 

Source Link

Document

Create a new MockHttpSession with a default MockServletContext .

Usage

From source file:fragment.web.UsersControllerTest.java

@Test(expected = AccessDeniedException.class)
public void testCreateUserUnprivileged() throws Exception {
    User user = createTestUserInTenant(getDefaultTenant());
    asUser(user);/*from www .  j a  v a2  s  . co m*/
    UserForm form = new UserForm();
    form.setCountryList(countryService.getCountries(null, null, null, null, null, null, null));
    com.citrix.cpbm.access.User newUser = form.getUser();
    newUser.setUsername("foobar");
    newUser.setEmail("test@test.com");
    validate(form);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    controller.createStepOne(controller.getTenant(), null, map, new MockHttpSession(), mockRequest);
}

From source file:com.ideabase.repository.test.webservice.RESTfulControllerTest.java

public void testGetItemInPHPResponse() throws Exception {
    final Integer userId = TestCaseRepositoryHelper.fixCreateUser(mUserService, "hasan", "hasankhan");
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setRequestURI("/service/get/" + userId + ".php");
    request.setMethod(METHOD_GET);/* w  ww  . jav a  2 s  . c om*/

    // fix login state
    final Subject subject = getSubjectFromASuccessfulRequest();
    final MockHttpSession session = new MockHttpSession();
    session.setAttribute(WebConstants.SESSION_ATTR_USER_SUBJECT, subject);
    request.setSession(session);

    // execute action
    final ModelAndView modelAndView = mRestfulController.handleRequest(request, response);

    // verify response
    final String responseContent = response.getContentAsString();
    assertNotNull(responseContent);
    LOG.debug("Response content - " + responseContent);
}

From source file:nl.surfnet.coin.teams.control.DetailTeamControllerTest.java

private RequestAttributes getRequestAttributes() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    Person person = new Person();
    person.setId("test");
    session.setAttribute(LoginInterceptor.PERSON_SESSION_KEY, person);
    request.setSession(session);//from  w  ww  .j a  v a2s.  c o m
    return new ServletRequestAttributes(request);
}

From source file:com.ideabase.repository.test.webservice.RESTfulControllerTest.java

public void testGetTermTagCloudByQuery() throws Exception {
    // create some dummy data
    TestCaseRepositoryHelper.fixCreateItems(mRepositoryService, 5);

    final Integer userId = TestCaseRepositoryHelper.fixCreateUser(mUserService, "hasan", "hasankhan");
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();

    // set http request parameters
    request.setRequestURI("/service/tagcloud/query.xml");
    request.setMethod(METHOD_GET);//from  w  ww .ja  v a 2s  .c o  m
    request.setParameter("query", "name:fox");
    request.setParameter("max", "50");
    request.setParameter("select", "name");

    // fix login state
    final Subject subject = getSubjectFromASuccessfulRequest();
    final MockHttpSession session = new MockHttpSession();
    session.setAttribute(WebConstants.SESSION_ATTR_USER_SUBJECT, subject);
    request.setSession(session);

    final ModelAndView modelAndView = mRestfulController.handleRequest(request, response);

    // verify response
    final String responseContent = response.getContentAsString();
    assertNotNull(responseContent);
    LOG.debug("Response content - " + responseContent);
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyNoPromptWithoutExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.ONLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(false);//w ww.ja va2 s. c  om
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(true);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.ONLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());

    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(scopeMap.hashCode(), map.get("scopeMap").hashCode());

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyAutoPromptWithoutExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.ONLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(true);//  ww  w.j av a 2 s . co m
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(false);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, "auto");
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());

    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(scopeMap.hashCode(), map.get("scopeMap").hashCode());

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:fragment.web.RegistrationControllerTest.java

@Test
public void testVerifyEmail() throws Exception {
    User user = createDisabledUser();//from   w ww. j  a v  a  2s.  c o  m
    String auth = user.getAuthorization(0);

    MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute("regAuth", auth);
    mockSession.setAttribute("regParam", user.getParam());

    String view = controller.verifyEmail(getRequestTemplate(HttpMethod.GET, "/verify_email"), map, mockSession);
    String redirect = "redirect:/?verify";

    Assert.assertEquals(redirect, view);
    userDAO.flush();
    User found = userDAO.find(user.getId());
    Assert.assertEquals(user, found);
    Assert.assertTrue(found.isEnabled());

}

From source file:fragment.web.RegistrationControllerTest.java

@Test
public void testVerifyEmailWithNoPasswordSet() throws Exception {
    User user = createUserWithoutPassword();
    String auth = user.getAuthorization(0);

    MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute("regAuth", auth);
    mockSession.setAttribute("regParam", user.getParam());

    String view = controller.verifyEmail(getRequestTemplate(HttpMethod.GET, "/verify_email"), map, mockSession);
    String redirect = "register.setpassword";

    Assert.assertEquals(redirect, view);
    userDAO.flush();//from ww w.j  a v a  2s . c o m
}

From source file:fragment.web.RegistrationControllerTest.java

@Test(expected = UserAuthorizationInvalidException.class)
public void testVerifyEmailBadAuth() throws Exception {
    User user = createDisabledUser();/*  www. ja v  a2s  .co m*/

    MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute("regAuth", "garbage");
    mockSession.setAttribute("regParam", user.getParam());

    controller.verifyEmail(getRequestTemplate(HttpMethod.GET, "/verify_email"), map, mockSession);
}

From source file:fragment.web.RegistrationControllerTest.java

@Test(expected = UserAuthorizationInvalidException.class)
public void testVerifyEmailWrongAuth() throws Exception {
    User user = createDisabledUser();/*from   ww w . j a va  2  s  . c o m*/
    String auth = user.getAuthorization(1);
    MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute("regAuth", auth);
    mockSession.setAttribute("regParam", user.getParam());
    controller.verifyEmail(getRequestTemplate(HttpMethod.GET, "/verify_email"), map, new MockHttpSession());
}