Search in sources :

Example 16 with MockHttpServletRequest

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

the class AssumeAnonymousUserFilterTest method setUp.

@BeforeEach
void setUp() {
    clock = new TestingClock();
    securityService = mock(SecurityService.class);
    anonymousAuthenticationProvider = new AnonymousAuthenticationProvider(clock, new AuthorityGranter(securityService));
    filterChain = mock(FilterChain.class);
    response = new MockHttpServletResponse();
    request = new MockHttpServletRequest();
}
Also used : AuthorityGranter(com.thoughtworks.go.server.security.AuthorityGranter) SecurityService(com.thoughtworks.go.server.service.SecurityService) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) TestingClock(com.thoughtworks.go.util.TestingClock) AnonymousAuthenticationProvider(com.thoughtworks.go.server.newsecurity.providers.AnonymousAuthenticationProvider) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest 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 18 with MockHttpServletRequest

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

the class BackupFilterTest method shouldReturn503WhenJsonAPICallIsMadeAndBackupBeingTaken.

@Test
public void shouldReturn503WhenJsonAPICallIsMadeAndBackupBeingTaken() throws Exception {
    when(backupService.isBackingUp()).thenReturn(true);
    when(backupService.backupRunningSinceISO8601()).thenReturn(BACKUP_STARTED_AT);
    when(backupService.backupStartedBy()).thenReturn(BACKUP_STARTED_BY);
    MockHttpServletRequest request = HttpRequestBuilder.GET("/api/agents").withHeader("Accept", "application/json").build();
    backupFilter.doFilter(request, res, chain);
    verify(res, times(1)).setContentType("application/json");
    JsonObject json = new JsonObject();
    assertResponse(json);
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) JsonObject(com.google.gson.JsonObject) Test(org.junit.jupiter.api.Test)

Example 19 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest 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 20 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest 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)

Aggregations

MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)28 MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)19 Test (org.junit.jupiter.api.Test)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ValueSource (org.junit.jupiter.params.provider.ValueSource)6 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)5 ContentTypeAwareResponse (com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse)4 SecurityService (com.thoughtworks.go.server.service.SecurityService)4 FilterChain (javax.servlet.FilterChain)4 JsonObject (com.google.gson.JsonObject)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)2 MockFilterChain (com.thoughtworks.go.http.mocks.MockFilterChain)1 AgentSessionReduceIdleTimeoutFilter (com.thoughtworks.go.server.newsecurity.filters.AgentSessionReduceIdleTimeoutFilter)1 AlwaysCreateSessionFilter (com.thoughtworks.go.server.newsecurity.filters.AlwaysCreateSessionFilter)1