use of org.springframework.mock.web.MockHttpSession in project spring-security by spring-projects.
the class HelloWorldApplicationTests method loginUserAccessProtected.
@Test
public void loginUserAccessProtected() throws Exception {
// @formatter:off
MvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password")).andExpect(authenticated()).andReturn();
// @formatter:on
MockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);
// @formatter:off
this.mockMvc.perform(get("/user/index").session(httpSession)).andExpect(status().isOk());
// @formatter:on
}
use of org.springframework.mock.web.MockHttpSession in project uPortal by Jasig.
the class SessionAttributeProfileMapperImplTest method setUp.
@Before
public void setUp() {
session = new MockHttpSession();
MockitoAnnotations.initMocks(this);
when(request.getSession(false)).thenReturn(session);
mapper.setAttributeName("key");
Map<String, String> mappings = new HashMap<String, String>();
mappings.put("key1", "fname1");
mappings.put("key2", "fname2");
mapper.setMappings(mappings);
// intentionally does not mapper.setDefaultProfileName("profile");
// so that can test the no-default-set case
}
use of org.springframework.mock.web.MockHttpSession in project gocd by gocd.
the class AuthenticationProcessingFilterTest method setUp.
@Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
session = new MockHttpSession();
request.setSession(session);
localizer = mock(Localizer.class);
filter = new AuthenticationProcessingFilter(mock(GoConfigService.class), localizer);
}
use of org.springframework.mock.web.MockHttpSession in project cas by apereo.
the class OAuth20AuthorizeControllerTests method verifyTokenRedirectToClientApproved.
@Test
public void verifyTokenRedirectToClientApproved() 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.TOKEN.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);
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();
mockRequest.setSession(session);
session.putValue(Pac4jConstants.USER_PROFILES, profile);
session.putValue(OAuth20Constants.BYPASS_APPROVAL_PROMPT, "true");
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 + "#access_token="));
final String code = StringUtils.substringBetween(redirectUrl, "#access_token=", "&token_type=bearer");
final AccessToken accessToken = (AccessToken) this.ticketRegistry.getTicket(code);
assertNotNull(accessToken);
final Principal principal = accessToken.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 verifyTokenRedirectToClient.
@Test
public void verifyTokenRedirectToClient() 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.TOKEN.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();
mockRequest.setSession(session);
session.putValue(Pac4jConstants.USER_PROFILES, profile);
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 + "#access_token="));
final String code = StringUtils.substringBetween(redirectUrl, "#access_token=", "&token_type=bearer");
final AccessToken accessToken = (AccessToken) this.ticketRegistry.getTicket(code);
assertNotNull(accessToken);
final Principal principal = accessToken.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));
}
Aggregations