|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2019 CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.jenkinsci.plugins.github_branch_source; |
| 26 | + |
| 27 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 28 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 29 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.Test; |
| 32 | +import org.jvnet.hudson.test.Issue; |
| 33 | +import org.jvnet.hudson.test.JenkinsRule; |
| 34 | +import org.kohsuke.github.GitHub; |
| 35 | + |
| 36 | +public class GitHubOrgWebHookTest { |
| 37 | + |
| 38 | + @Rule public JenkinsRule r = new JenkinsRule(); |
| 39 | + @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); |
| 40 | + |
| 41 | + @Issue("JENKINS-58942") |
| 42 | + @Test public void registerCustom() throws Exception { |
| 43 | + System.setProperty("jenkins.hook.url", "https://mycorp/hook-proxy/"); |
| 44 | + wireMockRule.stubFor(get(urlEqualTo("/api/users/myorg")).willReturn(aResponse().withBody("{\"login\":\"myorg\"}"))); |
| 45 | + wireMockRule.stubFor(get(urlEqualTo("/api/orgs/myorg")).willReturn(aResponse().withBody("{\"login\":\"myorg\",\"html_url\":\"https://github.com/myorg\"}"))); |
| 46 | + wireMockRule.stubFor(get(urlEqualTo("/api/orgs/myorg/hooks")).willReturn(aResponse().withBody("[]"))); |
| 47 | + wireMockRule.stubFor(post(urlEqualTo("/api/orgs/myorg/hooks")).withRequestBody(matchingJsonPath("$.config.url", equalTo("https://mycorp/hook-proxy/github-webhook/"))).willReturn(aResponse().withBody("{}"))); |
| 48 | + GitHub hub = Connector.connect("http://localhost:" + wireMockRule.port() + "/api/", null); |
| 49 | + try { |
| 50 | + GitHubOrgWebHook.register(hub, "myorg"); |
| 51 | + } finally { |
| 52 | + Connector.release(hub); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments