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));
}
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());
}
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"));
}
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)));
}
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));
}
Aggregations