Search in sources :

Example 1 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 2 with MockHttpSession

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

the class DelegatedClientAuthenticationActionTests method verifyStartAuthentication.

@Test
public void verifyStartAuthentication() throws Exception {
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, MY_THEME);
    mockRequest.setParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, MY_LOCALE);
    mockRequest.setParameter(CasProtocolConstants.PARAMETER_METHOD, MY_METHOD);
    final MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);
    final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
    when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
    when(servletExternalContext.getNativeResponse()).thenReturn(mockResponse);
    final MockRequestContext mockRequestContext = new MockRequestContext();
    mockRequestContext.setExternalContext(servletExternalContext);
    mockRequestContext.getFlowScope().put(CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.getService(MY_SERVICE));
    final FacebookClient facebookClient = new FacebookClient(MY_KEY, MY_SECRET);
    final TwitterClient twitterClient = new TwitterClient("3nJPbVTVRZWAyUgoUKQ8UA", "h6LZyZJmcW46Vu8R47MYfeXTSYGI30EqnWaSwVhFkbA");
    final Clients clients = new Clients(MY_LOGIN_URL, facebookClient, twitterClient);
    final DelegatedClientAuthenticationAction action = new DelegatedClientAuthenticationAction(clients, null, mock(CentralAuthenticationService.class), "theme", "locale", false);
    final Event event = action.execute(mockRequestContext);
    assertEquals("error", event.getId());
    assertEquals(MY_THEME, mockSession.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
    assertEquals(MY_LOCALE, mockSession.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
    assertEquals(MY_METHOD, mockSession.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
    final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
    final Set<DelegatedClientAuthenticationAction.ProviderLoginPageConfiguration> urls = (Set<DelegatedClientAuthenticationAction.ProviderLoginPageConfiguration>) flowScope.get(DelegatedClientAuthenticationAction.PAC4J_URLS);
    assertFalse(urls.isEmpty());
    assertSame(2, urls.size());
}
Also used : TwitterClient(org.pac4j.oauth.client.TwitterClient) Set(java.util.Set) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FacebookClient(org.pac4j.oauth.client.FacebookClient) MockRequestContext(org.springframework.webflow.test.MockRequestContext) Clients(org.pac4j.core.client.Clients) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MutableAttributeMap(org.springframework.webflow.core.collection.MutableAttributeMap) MockHttpSession(org.springframework.mock.web.MockHttpSession) Event(org.springframework.webflow.execution.Event) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-security by spring-projects.

the class HelloWorldApplicationTests method loginUserValidateLogout.

@Test
public void loginUserValidateLogout() 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(post("/logout").with(csrf()).session(httpSession)).andExpect(unauthenticated());
    this.mockMvc.perform(get("/user/index").session(httpSession)).andExpect(unauthenticated()).andExpect(status().is3xxRedirection());
// @formatter:on
}
Also used : MockHttpSession(org.springframework.mock.web.MockHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testNoTokensRemembered.

@Test
public void testNoTokensRemembered() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
    oAuthRememberMeService.rememberTokens(tokens, request, response);
    Assert.assertEquals(0, oAuthRememberMeService.loadRememberedTokens(request, response).size());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 5 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setUp.

@Before
public void setUp() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Aggregations

MockHttpSession (org.springframework.mock.web.MockHttpSession)238 Test (org.junit.jupiter.api.Test)120 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)92 Test (org.junit.Test)80 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)66 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)36 MvcResult (org.springframework.test.web.servlet.MvcResult)34 HashMap (java.util.HashMap)33 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)28 HttpSession (javax.servlet.http.HttpSession)21 CasProfile (org.pac4j.cas.profile.CasProfile)16 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)16 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 SessionRegistry (org.springframework.security.core.session.SessionRegistry)14 WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)14 AbstractWebApiTest (org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)13 SecurityContext (org.springframework.security.core.context.SecurityContext)13 RedirectView (org.springframework.web.servlet.view.RedirectView)12 lombok.val (lombok.val)11 AuditEvent (org.springframework.boot.actuate.audit.AuditEvent)11