Search in sources :

Example 81 with MockHttpSession

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

the class OAuth20AuthorizeControllerTests method verifyCodeRedirectToClient.

@Test
public void verifyCodeRedirectToClient() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(HttpMethod.GET.name(), CONTEXT + OAuth20Constants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuth20Constants.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(true);
    this.servicesManager.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();
    session.putValue(Pac4jConstants.USER_PROFILES, profile);
    mockRequest.setSession(session);
    final ModelAndView modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
    final View view = modelAndView.getView();
    assertTrue(view instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) view;
    final String redirectUrl = redirectView.getUrl();
    assertTrue(redirectUrl.startsWith(REDIRECT_URI + "?code=OC-"));
    final String code = StringUtils.substringAfter(redirectUrl, "?code=");
    final OAuthCode oAuthCode = (OAuthCode) this.ticketRegistry.getTicket(code);
    assertNotNull(oAuthCode);
    final Principal principal = oAuthCode.getAuthentication().getPrincipal();
    assertEquals(ID, principal.getId());
    final Map<String, Object> principalAttributes = principal.getAttributes();
    assertEquals(attributes.size(), principalAttributes.size());
    assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE));
}
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) OAuthCode(org.apereo.cas.ticket.code.OAuthCode) RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) RedirectView(org.springframework.web.servlet.view.RedirectView) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Principal(org.apereo.cas.authentication.principal.Principal) Test(org.junit.Test)

Example 82 with MockHttpSession

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

the class OAuth20AuthorizeControllerTests method verifyCodeNoProfile.

@Test
public void verifyCodeNoProfile() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(HttpMethod.GET.name(), CONTEXT + OAuth20Constants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuth20Constants.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(true);
    this.servicesManager.save(service);
    final MockHttpSession session = new MockHttpSession();
    mockRequest.setSession(session);
    final ModelAndView modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuth20Constants.ERROR_VIEW, modelAndView.getViewName());
}
Also used : 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 83 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project uPortal by Jasig.

the class SessionRESTControllerTest method testIsAuthenticated.

@Test
public void testIsAuthenticated() {
    IPerson person = new PersonImpl();
    person.setUserName(USER_NAME);
    person.setFullName("john doe");
    Mockito.when(personManager.getPerson(req)).thenReturn(person);
    Mockito.when(portalEventFactory.getPortalEventSessionId(req, person)).thenReturn("id");
    MockHttpSession session = new MockHttpSession();
    session.setAttribute("session", "true");
    req.setSession(session);
    ModelAndView modelAndView = sessionRESTController.isAuthenticated(req, res);
    Map<String, Object> attributes = (Map<String, Object>) modelAndView.getModel().get("person");
    Assert.assertEquals(USER_NAME, attributes.get("userName"));
}
Also used : IPerson(org.apereo.portal.security.IPerson) PersonImpl(org.apereo.portal.security.provider.PersonImpl) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpSession(org.springframework.mock.web.MockHttpSession) Map(java.util.Map) Test(org.junit.Test)

Example 84 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class DataElementControllerDocumentation method testFilterEqualOk.

@Test
public void testFilterEqualOk() throws Exception {
    MockHttpSession session = getSession("F_DATAELEMENT_PUBLIC_ADD");
    DataElement de = createDataElement('A');
    manager.save(de);
    mvc.perform(get("/dataElements?filter=name:eq:DataElementA").session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.pager.total", new GreaterThan<Integer>(0)));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) GreaterThan(org.mockito.internal.matchers.GreaterThan) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 85 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class ProgramTrackedEntityAttributeGroupDocumentation method testAddAndRemoveMember.

@Test
public void testAddAndRemoveMember() throws Exception {
    MockHttpSession session = getSession("ALL");
    ProgramTrackedEntityAttribute attrA = createProgramTrackedEntityAttribute('A');
    manager.save(attrA);
    ProgramTrackedEntityAttributeGroup group = createProgramTrackedEntityAttributeGroup('A');
    manager.save(group);
    attrA.addGroup(group);
    mvc.perform(post(schema.getRelativeApiEndpoint() + "/" + group.getUid() + "/attributes/" + attrA.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andExpect(status().isNoContent());
    mvc.perform(get(schema.getRelativeApiEndpoint() + "/{id}", group.getUid()).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.attributes.length()").value(1));
    mvc.perform(delete(schema.getRelativeApiEndpoint() + "/" + group.getUid() + "/attributes/" + attrA.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andExpect(status().isNoContent());
    mvc.perform(get(schema.getRelativeApiEndpoint() + "/{id}", group.getUid()).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.attributes.length()").value(0));
}
Also used : ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) MockHttpSession(org.springframework.mock.web.MockHttpSession) ProgramTrackedEntityAttributeGroup(org.hisp.dhis.program.ProgramTrackedEntityAttributeGroup) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Aggregations

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