Search in sources :

Example 26 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project cas by apereo.

the class OAuth20AuthorizeControllerTests method verifyRedirectToApproval.

@Test
public void verifyRedirectToApproval() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.RESPONSE_TYPE, OAuth20ResponseTypes.CODE.name().toLowerCase());
    mockRequest.setServerName(CAS_SERVER);
    mockRequest.setServerPort(CAS_PORT);
    mockRequest.setScheme(CAS_SCHEME);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
    service.setBypassApprovalPrompt(false);
    oAuth20AuthorizeEndpointController.getServicesManager().save(service);
    final CasProfile profile = new CasProfile();
    profile.setId(ID);
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
    attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
    profile.addAttributes(attributes);
    final MockHttpSession session = new MockHttpSession();
    mockRequest.setSession(session);
    session.putValue(Pac4jConstants.USER_PROFILES, profile);
    final ModelAndView modelAndView = oAuth20AuthorizeEndpointController.handleRequestInternal(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
    final Map<String, Object> model = modelAndView.getModel();
    assertEquals(CommonHelper.addParameter(AUTHORIZE_URL, OAuthConstants.BYPASS_APPROVAL_PROMPT, "true"), model.get("callbackUrl"));
    assertEquals(SERVICE_NAME, model.get("serviceName"));
}
Also used : CasProfile(org.pac4j.cas.profile.CasProfile) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuthRegisteredService(org.apereo.cas.support.oauth.services.OAuthRegisteredService) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 27 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project gocd by gocd.

the class FlashLoadingFilterIntegrationTest method shouldLoadExistingFlashFromSession.

@Test
public void shouldLoadExistingFlashFromSession() throws IOException, ServletException {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    FlashMessageService.Flash oldFlash = new FlashMessageService.Flash();
    oldFlash.put("my_key", new FlashMessageModel("my other message", "warning"));
    session.putValue(FlashLoadingFilter.FLASH_SESSION_KEY, oldFlash);
    req.setSession(session);
    MockHttpServletResponse res = new MockHttpServletResponse();
    FilterChain filterChain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) {
            flash = service.get("my_key");
        }
    };
    filter.doFilter(req, res, filterChain);
    assertThat(flash.toString(), is("my other message"));
    assertThat(flash.getFlashClass(), is("warning"));
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockFilterChain(org.springframework.mock.web.MockFilterChain) FlashMessageModel(com.thoughtworks.go.presentation.FlashMessageModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 28 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project gocd by gocd.

the class TokenInterceptorTest method shouldCreateSessionTokenForNonPOSTRequest.

@Test
public void shouldCreateSessionTokenForNonPOSTRequest() throws Exception {
    when(request.getMethod()).thenReturn("GET");
    MockHttpSession session = new MockHttpSession();
    when(request.getSession()).thenReturn(session);
    TokenInterceptor interceptor = new TokenInterceptor(manager);
    assertThat(interceptor.preHandle(request, response, mock(Object.class)), is(true));
    verify(request).getMethod();
    verify(manager).create(session);
    verify(request).getSession();
}
Also used : MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 29 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project ORCID-Source by ORCID.

the class FundingsControllerTest method testGetFundingsJson.

@Test
public void testGetFundingsJson() {
    when(localeManager.getLocale()).thenReturn(new Locale("us", "EN"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    request.addPreferredLocale(new Locale("us", "EN"));
    List<FundingForm> fundings = fundingController.getFundingsJson(request, "1");
    assertNotNull(fundings);
    assertEquals(1, fundings.size());
    FundingForm funding = fundings.get(0);
    List<Contributor> contributors = funding.getContributors();
    Contributor contributor = contributors.get(0);
    assertNull(contributor.getEmail());
    assertEquals("Jaylen Kessler", contributor.getCreditName().getValue());
    contributor = contributors.get(1);
    assertNull(contributor.getEmail());
    assertEquals("John Smith", contributor.getCreditName().getValue());
    contributor = contributors.get(2);
    assertNull(contributor.getEmail());
    assertEquals("Credit Name", contributor.getCreditName().getValue());
    // contributor is an ORCID user with private name
    contributor = contributors.get(3);
    assertNull(contributor.getEmail());
    assertNull(contributor.getCreditName().getValue());
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FundingForm(org.orcid.pojo.ajaxForm.FundingForm) MockHttpSession(org.springframework.mock.web.MockHttpSession) Contributor(org.orcid.pojo.ajaxForm.Contributor) Test(org.junit.Test) BaseControllerTest(org.orcid.frontend.web.util.BaseControllerTest)

Example 30 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project cloudstack by apache.

the class HttpUtilsTest method validateSessionKeyTest.

@Test
public void validateSessionKeyTest() {
    HttpSession session = null;
    Map<String, Object[]> params = null;
    String sessionKeyString = null;
    Cookie[] cookies = null;
    final String sessionKeyValue = "randomUniqueSessionID";
    // session and sessionKeyString null test
    assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    sessionKeyString = "sessionkey";
    assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    // param and cookie null test
    session = new MockHttpSession();
    session.setAttribute(sessionKeyString, sessionKeyValue);
    assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    // param null, cookies not null test
    params = null;
    cookies = new Cookie[] { new Cookie(sessionKeyString, sessionKeyValue) };
    assertFalse(HttpUtils.validateSessionKey(session, params, cookies, "randomString"));
    assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    // param not null, cookies null test
    params = new HashMap<String, Object[]>();
    params.put(sessionKeyString, new String[] { "randomString" });
    cookies = null;
    assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    params.put(sessionKeyString, new String[] { sessionKeyValue });
    assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    // both param and cookies not null test
    params = new HashMap<String, Object[]>();
    cookies = new Cookie[] { new Cookie(sessionKeyString, sessionKeyValue) };
    params.put(sessionKeyString, new String[] { "incorrectValue" });
    assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
    params.put(sessionKeyString, new String[] { sessionKeyValue });
    assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
}
Also used : Cookie(javax.servlet.http.Cookie) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Aggregations

MockHttpSession (org.springframework.mock.web.MockHttpSession)102 Test (org.junit.Test)80 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)42 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)33 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)23 AbstractWebApiTest (org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)13 MockFilterChain (org.springframework.mock.web.MockFilterChain)12 HashMap (java.util.HashMap)11 FieldDescriptor (org.springframework.restdocs.payload.FieldDescriptor)11 ModelAndView (org.springframework.web.servlet.ModelAndView)10 FilterChain (javax.servlet.FilterChain)8 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)8 MockServletContext (org.springframework.mock.web.MockServletContext)8 SessionRegistry (org.springframework.security.core.session.SessionRegistry)8 ConcurrentSessionFilter (org.springframework.security.web.session.ConcurrentSessionFilter)8 DataElement (org.hisp.dhis.dataelement.DataElement)7 CasProfile (org.pac4j.cas.profile.CasProfile)7 InputStream (java.io.InputStream)6 Principal (org.apereo.cas.authentication.principal.Principal)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6