Example usage for org.springframework.mock.web MockHttpServletRequest setParameter

List of usage examples for org.springframework.mock.web MockHttpServletRequest setParameter

Introduction

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

Prototype

public void setParameter(String name, String... values) 

Source Link

Document

Set an array of values for the specified HTTP parameter.

Usage

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

@Test
public void verifyOKWithOfflineToken() throws Exception {
    final Service service = new SimpleWebApplicationServiceImpl("id");

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getId()).thenReturn(ID);
    when(serviceTicket.getService()).thenReturn(service);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getType()).thenReturn(TokenType.OFFLINE);
    when(accessToken.getServiceTicket()).thenReturn(serviceTicket);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken);

    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);//from   w ww  .  j a v a  2  s . c o m
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(ID);
    when(principal.getAttributes()).thenReturn(map);

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

    final Assertion assertion = mock(Assertion.class);
    when(assertion.getPrimaryAuthentication()).thenReturn(authentication);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.validateServiceTicket(serviceTicket.getId(), serviceTicket.getService()))
            .thenReturn(assertion);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_ID);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"id\":\"" + ID + "\",\"attributes\":[{\"" + NAME + "\":\"" + VALUE + "\"},{\""
            + NAME2 + "\":[\"" + VALUE + "\",\"" + VALUE + "\"]}]}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("id").asText(), receivedObj.get("id").asText());

    final JsonNode expectedAttributes = expectedObj.get("attributes");
    final JsonNode receivedAttributes = receivedObj.get("attributes");

    assertEquals(expectedAttributes.findValue(NAME).asText(), receivedAttributes.findValue(NAME).asText());
    assertEquals(expectedAttributes.findValues(NAME2), receivedAttributes.findValues(NAME2));
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testGetConfirmRemoveXslExport() throws AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(//from  w  w w .  ja  va2 s.  c  o  m
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    request.setParameter("id_xsl_export", Integer.toString(_xslExport.getIdXslExport()));
    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    _instance.getConfirmRemoveXslExport(request);
    AdminMessage message = AdminMessageService.getMessage(request);
    assertNotNull(message);
    assertTrue(message.getRequestParameters().containsKey(SecurityTokenService.PARAMETER_TOKEN));
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testDoRemoveXslExport() throws AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(//  w  ww . j  a  v a2s  . com
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    request.setParameter("id_xsl_export", Integer.toString(_xslExport.getIdXslExport()));
    request.setParameter(SecurityTokenService.PARAMETER_TOKEN,
            SecurityTokenService.getInstance().getToken(request, "jsp/admin/xsl/DoRemoveXslExport.jsp"));
    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    _instance.doRemoveXslExport(request);

    XslExport stored = XslExportHome.findByPrimaryKey(_xslExport.getIdXslExport());
    assertNull(stored);
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testDoRemoveXslExportNoToken() throws AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(/*from   w w w. j  a  v  a 2s  .c o m*/
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    request.setParameter("id_xsl_export", Integer.toString(_xslExport.getIdXslExport()));
    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    try {
        _instance.doRemoveXslExport(request);
        fail("Should have thrown");
    } catch (AccessDeniedException e) {
        XslExport stored = XslExportHome.findByPrimaryKey(_xslExport.getIdXslExport());
        assertNotNull(stored);
    }
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testDoRemoveXslExportInvalidToken() throws AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(/*from w w  w. j a v a2s  . c  o  m*/
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    request.setParameter("id_xsl_export", Integer.toString(_xslExport.getIdXslExport()));
    request.setParameter(SecurityTokenService.PARAMETER_TOKEN,
            SecurityTokenService.getInstance().getToken(request, "jsp/admin/xsl/DoRemoveXslExport.jsp") + "b");
    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);

    try {
        _instance.doRemoveXslExport(request);
        fail("Should have thrown");
    } catch (AccessDeniedException e) {
        XslExport stored = XslExportHome.findByPrimaryKey(_xslExport.getIdXslExport());
        assertNotNull(stored);
    }
}

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

@Test
public void verifyOK() throws Exception {
    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);

    final Service service = new SimpleWebApplicationServiceImpl("id");

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getType()).thenReturn(TokenType.ONLINE);
    when(accessToken.getService()).thenReturn(service);
    when(accessToken.getTicketGrantingTicket()).thenReturn(ticketGrantingTicket);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getId()).thenReturn(ID);
    when(serviceTicket.getService()).thenReturn(service);

    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);//from  w w  w  .j a v a  2 s  .c om
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(ID);
    when(principal.getAttributes()).thenReturn(map);

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

    final Assertion assertion = mock(Assertion.class);
    when(assertion.getPrimaryAuthentication()).thenReturn(authentication);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.grantServiceTicket(accessToken.getTicketGrantingTicket().getId(),
            accessToken.getService())).thenReturn(serviceTicket);
    when(centralAuthenticationService.validateServiceTicket(serviceTicket.getId(), serviceTicket.getService()))
            .thenReturn(assertion);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_ID);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"id\":\"" + ID + "\",\"attributes\":[{\"" + NAME + "\":\"" + VALUE + "\"},{\""
            + NAME2 + "\":[\"" + VALUE + "\",\"" + VALUE + "\"]}]}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("id").asText(), receivedObj.get("id").asText());

    final JsonNode expectedAttributes = expectedObj.get("attributes");
    final JsonNode receivedAttributes = receivedObj.get("attributes");

    assertEquals(expectedAttributes.findValue(NAME).asText(), receivedAttributes.findValue(NAME).asText());
    assertEquals(expectedAttributes.findValues(NAME2), receivedAttributes.findValues(NAME2));
}

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

@Test
public void testDoAddMemberHappyFlow() throws Exception {
    ListAppender auditAppender = getAuditLogAppender();
    auditAppender.list.clear();//from   ww w  .j ava  2 s  .  c  o  m

    MockHttpServletRequest request = getRequest();
    Team team1 = getTeam1();
    Person person = getPerson1();
    person.setDisplayName("Member 1");
    request.getSession().setAttribute(LoginInterceptor.PERSON_SESSION_KEY, person);

    // request team
    String token = TokenUtil.generateSessionToken();
    request.setParameter("team", team1.getId());

    GrouperTeamService teamService = mock(GrouperTeamService.class);
    when(teamService.findTeamById(team1.getId())).thenReturn(team1);
    autoWireMock(addMemberController, teamService, GrouperTeamService.class);

    autoWireMock(addMemberController, messageSource, MessageSource.class);
    autoWireMock(addMemberController, new Returns(Locale.ENGLISH), LocaleResolver.class);

    InvitationForm form = new InvitationForm();
    form.setEmails("nonmember@example.com");
    form.setInviter(person);
    form.setMessage("A nice invite message");
    form.setTeamId(team1.getId());

    ControllerUtil controllerUtil = mock(ControllerUtil.class);
    when(controllerUtil.hasUserAdministrativePrivileges(person, team1.getId())).thenReturn(true);
    when(controllerUtil.getTeamById(team1.getId())).thenReturn(team1);

    autoWireMock(addMemberController, controllerUtil, ControllerUtil.class);

    Configuration freemarkerConfiguration = getFreemarkerConfig();
    autoWireMock(addMemberController, freemarkerConfiguration, Configuration.class);

    TeamEnvironment environment = new TeamEnvironment();
    environment.setTeamsURL("http://localhost:8060/teams");
    addMemberController.setTeamEnvironment(environment);

    autoWireRemainingResources(addMemberController);

    String result = addMemberController.addMembersToTeam(token, form,
            new DirectFieldBindingResult(form, "invitationForm"), request, token, new SimpleSessionStatus(),
            getModelMap());

    assertEquals("redirect:detailteam.shtml?team=" + team1.getId() + "&view=app", result);

    /*
    Assert auditing output
     */
    assertEquals("Two audit events should be appended to audit log: one detailed, one global", 2,
            auditAppender.list.size());
    LoggingEvent auditEvent = (LoggingEvent) auditAppender.list.get(0);
    assertTrue("Detailed audit event should contain invitee's email address",
            auditEvent.getFormattedMessage().contains("nonmember@example.com"));
    assertTrue("Detailed audit event should contain inviter's name",
            auditEvent.getFormattedMessage().contains("member-1"));
}

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

@Test
public void verifyOKWithScopes() throws Exception {
    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);

    final Service service = new SimpleWebApplicationServiceImpl("id");

    final Set<String> scopes = new HashSet<>();
    scopes.add(NAME);//from ww  w.j  a  v  a 2  s.co m
    scopes.add(NAME2);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getType()).thenReturn(TokenType.ONLINE);
    when(accessToken.getService()).thenReturn(service);
    when(accessToken.getTicketGrantingTicket()).thenReturn(ticketGrantingTicket);
    when(accessToken.getScopes()).thenReturn(scopes);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getId()).thenReturn(ID);
    when(serviceTicket.getService()).thenReturn(service);

    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(ID);
    when(principal.getAttributes()).thenReturn(new HashMap<String, Object>());

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

    final Assertion assertion = mock(Assertion.class);
    when(assertion.getPrimaryAuthentication()).thenReturn(authentication);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.grantServiceTicket(accessToken.getTicketGrantingTicket().getId(),
            accessToken.getService())).thenReturn(serviceTicket);
    when(centralAuthenticationService.validateServiceTicket(serviceTicket.getId(), serviceTicket.getService()))
            .thenReturn(assertion);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_ID);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"id\":\"" + ID + "\",\"scope\":[\"" + NAME + "\",\"" + NAME2 + "\"]}";
    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("id").asText(), receivedObj.get("id").asText());

    assertEquals(expectedObj.get("scope").size(), receivedObj.get("scope").size());

    for (final JsonNode expectedNode : expectedObj.get("scope")) {
        Boolean found = Boolean.FALSE;

        for (final JsonNode receivedNode : receivedObj.get("scope")) {
            if (receivedNode.asText().equals(expectedNode.asText())) {
                found = Boolean.TRUE;
                break;
            }
        }

        assertEquals(found, Boolean.TRUE);
    }
}

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

@Test
public void verifySetupFailsNoTicket() throws Exception {
    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(SERVICE_TICKET_ID)).thenReturn(null);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    mockRequest.setParameter(OAuthConstants.TICKET, SERVICE_TICKET_ID);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//w w  w.  jav a  2s  .  c o  m
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_GRANT + "\",\"error_description\":\""
            + OAuthConstants.EXPIRED_ST_DESCRIPTION + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

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

@Test
public void verifySetupOK() throws Exception {
    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.getId()).thenReturn(TICKET_GRANTING_TICKET_ID);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(SERVICE_TICKET_ID)).thenReturn(serviceTicket);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    mockRequest.setParameter(OAuthConstants.TICKET, SERVICE_TICKET_ID);
    final MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl().endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL));
}