Search in sources :

Example 21 with MockHttpServletResponse

use of com.thoughtworks.go.http.mocks.MockHttpServletResponse in project gocd by gocd.

the class AgentRemoteInvokerServiceExporterTest method setup.

@BeforeEach
void setup() throws Exception {
    when(env.get(AGENT_EXTRA_PROPERTIES)).thenReturn("gocd.agent.remoting.legacy=true");
    req = new MockHttpServletRequest();
    req.addHeader("X-Agent-GUID", AGENT_UUID);
    res = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with MockHttpServletResponse

use of com.thoughtworks.go.http.mocks.MockHttpServletResponse in project gocd by gocd.

the class GenericAccessDeniedHandlerTest method shouldRedirectToLoginPageWhenSecurityIsEnabledAndUserIsNotAuthenticated.

@Test
void shouldRedirectToLoginPageWhenSecurityIsEnabledAndUserIsNotAuthenticated() throws IOException {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/foo").build();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    SecurityService securityService = mock(SecurityService.class);
    SessionUtilsHelper.loginAsAnonymous(request);
    when(securityService.isSecurityEnabled()).thenReturn(true);
    new GenericAccessDeniedHandler(securityService).handle(request, response, SC_FORBIDDEN, "Some error");
    MockHttpServletResponseAssert.assertThat(response).redirectsTo("/go/auth/login");
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) SecurityService(com.thoughtworks.go.server.service.SecurityService) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 23 with MockHttpServletResponse

use of com.thoughtworks.go.http.mocks.MockHttpServletResponse in project gocd by gocd.

the class GenericAccessDeniedHandlerTest method shouldAccessDeniedWhenUserIsAuthenticated.

@Test
void shouldAccessDeniedWhenUserIsAuthenticated() throws IOException {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/admin/").build();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    SecurityService securityService = mock(SecurityService.class);
    SessionUtilsHelper.loginAsRandomUser(request, GoAuthority.ROLE_USER.asAuthority());
    when(securityService.isSecurityEnabled()).thenReturn(true);
    new GenericAccessDeniedHandler(securityService).handle(request, response, SC_FORBIDDEN, "Some error");
    MockHttpServletResponseAssert.assertThat(response).isForbidden();
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) SecurityService(com.thoughtworks.go.server.service.SecurityService) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 24 with MockHttpServletResponse

use of com.thoughtworks.go.http.mocks.MockHttpServletResponse in project gocd by gocd.

the class BasicAuthenticationWithRedirectToLoginFilterTest method shouldInvokeHandler.

@Test
void shouldInvokeHandler() throws IOException {
    final BasicAuthenticationWithRedirectToLoginFilter filter = new BasicAuthenticationWithRedirectToLoginFilter(null, null);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final String message = "foo";
    SavedRequest savedRequest = mock(SavedRequest.class);
    SessionUtils.saveRequest(request, savedRequest);
    HttpSession originalSession = request.getSession(true);
    filter.onAuthenticationFailure(request, response, message);
    assertThat(SessionUtils.getAuthenticationError(request)).isEqualTo("foo");
    assertThat(request.getSession(false)).isNotSameAs(originalSession);
    assertThat(SessionUtils.savedRequest(request)).isSameAs(savedRequest);
    assertThat(SessionUtils.hasAuthenticationToken(request)).isFalse();
    MockHttpServletResponseAssert.assertThat(response).redirectsTo("/go/auth/login");
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.jupiter.api.Test)

Example 25 with MockHttpServletResponse

use of com.thoughtworks.go.http.mocks.MockHttpServletResponse in project gocd by gocd.

the class DenyIfRefererIsNotFilesFilterTest method setUp.

@BeforeEach
void setUp() throws Exception {
    response = new MockHttpServletResponse();
    chain = mock(FilterChain.class);
}
Also used : FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)30 MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)20 BeforeEach (org.junit.jupiter.api.BeforeEach)16 Test (org.junit.jupiter.api.Test)13 FilterChain (javax.servlet.FilterChain)11 SecurityService (com.thoughtworks.go.server.service.SecurityService)7 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)7 TestingClock (com.thoughtworks.go.util.TestingClock)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 UsernamePassword (com.thoughtworks.go.server.newsecurity.models.UsernamePassword)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 AnonymousAuthenticationProvider (com.thoughtworks.go.server.newsecurity.providers.AnonymousAuthenticationProvider)2 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)2 HttpSession (javax.servlet.http.HttpSession)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)2 GsonBuilder (com.google.gson.GsonBuilder)1 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)1