Search in sources :

Example 11 with MockHttpServletRequest

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

the class ThreadLocalUserFilterTest method shouldSetUserToThreadLocalWhenFilterIsCalledAndRemoveUserFromThreadLocalOnceRequestIsCompleted.

@Test
void shouldSetUserToThreadLocalWhenFilterIsCalledAndRemoveUserFromThreadLocalOnceRequestIsCompleted() throws ServletException, IOException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final AuthenticationToken<UsernamePassword> authenticationToken = SessionUtilsHelper.createUsernamePasswordAuthentication("bob", "p@ssw0rd", 0L);
    SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
    final GoUserPrinciple[] currentUserInFilter = { null };
    final FilterChain filterChain = new MockFilterChain(mock(Servlet.class), spy(new OncePerRequestFilter() {

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            currentUserInFilter[0] = SessionUtils.getCurrentUser();
        }
    }));
    new ThreadLocalUserFilter().doFilter(request, response, filterChain);
    assertThat(currentUserInFilter[0]).isNotNull();
    assertThat(SessionUtils.getCurrentUser().getUsername()).isEqualTo("anonymous");
    assertThat(SessionUtils.getCurrentUser().getAuthorities()).containsExactly(GoAuthority.ROLE_ANONYMOUS.asAuthority());
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(com.thoughtworks.go.http.mocks.MockFilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) Servlet(javax.servlet.Servlet) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) MockFilterChain(com.thoughtworks.go.http.mocks.MockFilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 12 with MockHttpServletRequest

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

the class UserEnabledCheckFilterWithRedirectToLoginPageTest method setUp.

@BeforeEach
void setUp() {
    filter = new UserEnabledCheckFilterWithRedirectToLoginPage(null, null);
    request = new MockHttpServletRequest();
    response = 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 13 with MockHttpServletRequest

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

the class RequestRejectedExceptionHandlerTest method shouldReturn404WithHtmlResponse.

@Test
void shouldReturn404WithHtmlResponse() throws IOException {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("foo/bar").withHeader("Accept", "application/json").build();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    new RequestRejectedExceptionHandler().handle(request, response, "Something went wrong", HttpStatus.BAD_REQUEST);
    assertThat(response).isBadRequest().hasNoBody();
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with MockHttpServletRequest

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

the class RequestRejectedExceptionHandlerTest method shouldHandleUrlsAsApiRequest.

@ParameterizedTest
@ValueSource(strings = { "/remoting/foo", "/add-on/bar/api/foo", "/api/foo", "/cctray.xml" })
void shouldHandleUrlsAsApiRequest(String url) throws IOException {
    final MockHttpServletRequest request = HttpRequestBuilder.GET(url).withHeader("Accept", "application/json").build();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    new RequestRejectedExceptionHandler().handle(request, response, "Something went wrong", HttpStatus.BAD_REQUEST);
    assertThat(response).isBadRequest().hasBody("{\n" + "  \"message\": \"Something went wrong\"\n" + "}");
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with MockHttpServletRequest

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

the class AbstractUserEnabledCheckFilterTest method setUp.

@BeforeEach
void setUp() throws Exception {
    userService = mock(UserService.class);
    securityService = mock(SecurityService.class);
    filter = spy(new AbstractUserEnabledCheckFilter(userService, securityService) {

        @Override
        void handleFailure(HttpServletRequest request, HttpServletResponse response, String errorMessage) throws IOException {
        }
    });
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    filterChain = mock(FilterChain.class);
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) UserService(com.thoughtworks.go.server.service.UserService) SecurityService(com.thoughtworks.go.server.service.SecurityService) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

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