Search in sources :

Example 1 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenNoJobInQueryStringMustReturnError.

@Test
public void whenNoJobInQueryStringMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn("foo=bar&blaah=blaah");
    // perform the testÎ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(404);
    String expectedOutput = "Parameter 'job' is missing.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 2 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenEmptyHeaderTypeMustReturnError.

@Test
public void whenEmptyHeaderTypeMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    // perform the test
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(403);
    String expectedOutput = "Only push event can be accepted.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 3 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenWrongHeaderTypeMustReturnError.

@Test
public void whenWrongHeaderTypeMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("junk");
    // perform the testÎ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(403);
    String expectedOutput = "Only push event can be accepted.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 4 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenQueryStringIsNullMustThrowException.

@Test
public void whenQueryStringIsNullMustThrowException() throws Exception {
    // Prepare the SUT
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn(null);
    GogsWebHook gogsWebHook = new GogsWebHook();
    try {
        gogsWebHook.doIndex(staplerRequest, staplerResponse);
    } catch (NullPointerException e) {
        String expectedErrMsg = "The queryString in the request is null";
        assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
        log.info("call failed as expected.");
        return;
    }
    fail("The call should have failed.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) Test(org.junit.Test)

Example 5 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method callDoIndexWithNullReqMessageMustThrowException.

@Test
public void callDoIndexWithNullReqMessageMustThrowException() throws IOException {
    GogsWebHook gogsWebHook = new GogsWebHook();
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    try {
        gogsWebHook.doIndex(null, staplerResponse);
    } catch (NullPointerException e) {
        String expectedErrMsg = "Null request submitted to doIndex method";
        assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
        log.info("call failed as expected.");
        return;
    }
    fail("The call should have failed.");
}
Also used : StaplerResponse(org.kohsuke.stapler.StaplerResponse) Test(org.junit.Test)

Aggregations

StaplerResponse (org.kohsuke.stapler.StaplerResponse)11 Test (org.junit.Test)8 StaplerRequest (org.kohsuke.stapler.StaplerRequest)8 File (java.io.File)6 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 IOException2 (hudson.util.IOException2)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Cipher (javax.crypto.Cipher)1 CipherOutputStream (javax.crypto.CipherOutputStream)1 AuthenticationException (org.acegisecurity.AuthenticationException)1 BadCredentialsException (org.acegisecurity.BadCredentialsException)1 UsernameNotFoundException (org.acegisecurity.userdetails.UsernameNotFoundException)1