forked from jenkinsci/github-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookManagerTest.java
More file actions
280 lines (225 loc) · 10.3 KB
/
WebhookManagerTest.java
File metadata and controls
280 lines (225 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package org.jenkinsci.plugins.github.webhook;
import com.cloudbees.jenkins.GitHubPushTrigger;
import com.cloudbees.jenkins.GitHubRepositoryName;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.plugins.git.GitSCM;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHRepository;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.Lists.asList;
import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.jenkinsci.plugins.github.test.HookSecretHelper.storeSecretIn;
import static org.jenkinsci.plugins.github.webhook.WebhookManager.forHookUrl;
import static org.junit.Assert.assertThat;
import static org.kohsuke.github.GHEvent.CREATE;
import static org.kohsuke.github.GHEvent.PULL_REQUEST;
import static org.kohsuke.github.GHEvent.PUSH;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anySetOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author lanwen (Merkushev Kirill)
*/
@RunWith(MockitoJUnitRunner.class)
public class WebhookManagerTest {
public static final GitSCM GIT_SCM = new GitSCM("ssh://git@github.com/dummy/dummy.git");
public static final URL HOOK_ENDPOINT = endpoint("http://hook.endpoint/");
public static final URL ANOTHER_HOOK_ENDPOINT = endpoint("http://another.url/");
@Rule
public JenkinsRule jenkins = new JenkinsRule();
@Spy
private WebhookManager manager = forHookUrl(HOOK_ENDPOINT);
@Spy
private GitHubRepositoryName nonactive = new GitHubRepositoryName("github.com", "dummy", "dummy");
@Spy
private GitHubRepositoryName active = new GitHubRepositoryName("github.com", "dummy", "active");
@Mock
private GHRepository repo;
@Test
public void shouldDoNothingOnNoAdminRights() throws Exception {
manager.unregisterFor(nonactive, newArrayList(active));
verify(manager, never()).withAdminAccess();
verify(manager, never()).fetchHooks();
}
@Test
public void shouldSearchBothWebAndServiceHookOnNonActiveName() throws Exception {
doReturn(newArrayList(repo)).when(nonactive).resolve(any(Predicate.class));
when(repo.hasAdminAccess()).thenReturn(true);
manager.unregisterFor(nonactive, newArrayList(active));
verify(manager, times(1)).serviceWebhookFor(HOOK_ENDPOINT);
verify(manager, times(1)).webhookFor(HOOK_ENDPOINT);
verify(manager, times(1)).fetchHooks();
}
@Test
public void shouldSearchOnlyServiceHookOnActiveName() throws Exception {
doReturn(newArrayList(repo)).when(active).resolve(any(Predicate.class));
when(repo.hasAdminAccess()).thenReturn(true);
manager.unregisterFor(active, newArrayList(active));
verify(manager, times(1)).serviceWebhookFor(HOOK_ENDPOINT);
verify(manager, never()).webhookFor(HOOK_ENDPOINT);
verify(manager, times(1)).fetchHooks();
}
@Test
@WithoutJenkins
public void shouldMatchAdminAccessWhenTrue() throws Exception {
when(repo.hasAdminAccess()).thenReturn(true);
assertThat("has admin access", manager.withAdminAccess().apply(repo), is(true));
}
@Test
@WithoutJenkins
public void shouldMatchAdminAccessWhenFalse() throws Exception {
when(repo.hasAdminAccess()).thenReturn(false);
assertThat("has no admin access", manager.withAdminAccess().apply(repo), is(false));
}
@Test
@WithoutJenkins
public void shouldMatchWebHook() {
when(repo.hasAdminAccess()).thenReturn(false);
GHHook hook = hook(HOOK_ENDPOINT, PUSH);
assertThat("webhook has web name and url prop", manager.webhookFor(HOOK_ENDPOINT).apply(hook), is(true));
}
@Test
@WithoutJenkins
public void shouldNotMatchOtherUrlWebHook() {
when(repo.hasAdminAccess()).thenReturn(false);
GHHook hook = hook(ANOTHER_HOOK_ENDPOINT, PUSH);
assertThat("webhook has web name and another url prop",
manager.webhookFor(HOOK_ENDPOINT).apply(hook), is(false));
}
@Test
public void shouldMergeEventsOnRegisterNewAndDeleteOldOnes() throws IOException {
doReturn(newArrayList(repo)).when(nonactive).resolve(any(Predicate.class));
when(repo.hasAdminAccess()).thenReturn(true);
Predicate<GHHook> del = spy(Predicate.class);
when(manager.deleteWebhook()).thenReturn(del);
GHHook hook = hook(HOOK_ENDPOINT, CREATE);
GHHook prhook = hook(HOOK_ENDPOINT, PULL_REQUEST);
when(repo.getHooks()).thenReturn(newArrayList(hook, prhook));
manager.createHookSubscribedTo(copyOf(newArrayList(PUSH))).apply(nonactive);
verify(del, times(2)).apply(any(GHHook.class));
verify(manager).createWebhook(HOOK_ENDPOINT, EnumSet.copyOf(newArrayList(CREATE, PULL_REQUEST, PUSH)));
}
@Test
public void shouldNotReplaceAlreadyRegisteredHook() throws IOException {
doReturn(newArrayList(repo)).when(nonactive).resolve(any(Predicate.class));
when(repo.hasAdminAccess()).thenReturn(true);
GHHook hook = hook(HOOK_ENDPOINT, PUSH);
when(repo.getHooks()).thenReturn(newArrayList(hook));
manager.createHookSubscribedTo(copyOf(newArrayList(PUSH))).apply(nonactive);
verify(manager, never()).deleteWebhook();
verify(manager, never()).createWebhook(any(URL.class), anySetOf(GHEvent.class));
}
@Test
@Issue( "JENKINS-62116" )
public void shouldNotReplaceAlreadyRegisteredHookWithMoreEvents() throws IOException {
doReturn(newArrayList(repo)).when(nonactive).resolve(any(Predicate.class));
when(repo.hasAdminAccess()).thenReturn(true);
GHHook hook = hook(HOOK_ENDPOINT, PUSH, CREATE);
when(repo.getHooks()).thenReturn(newArrayList(hook));
manager.createHookSubscribedTo(copyOf(newArrayList(PUSH))).apply(nonactive);
verify(manager, never()).deleteWebhook();
verify(manager, never()).createWebhook(any(URL.class), anySetOf(GHEvent.class));
}
@Test
public void shouldNotAddPushEventByDefaultForProjectWithoutTrigger() throws IOException {
FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(GIT_SCM);
manager.registerFor((Item)project).run();
verify(manager, never()).createHookSubscribedTo(anyListOf(GHEvent.class));
}
@Test
public void shouldAddPushEventByDefault() throws IOException {
FreeStyleProject project = jenkins.createFreeStyleProject();
project.addTrigger(new GitHubPushTrigger());
project.setScm(GIT_SCM);
manager.registerFor((Item)project).run();
verify(manager).createHookSubscribedTo(newArrayList(PUSH));
}
@Test
public void shouldReturnNullOnGettingEmptyEventsListToSubscribe() throws IOException {
doReturn(newArrayList(repo)).when(active).resolve(any(Predicate.class));
when(repo.hasAdminAccess()).thenReturn(true);
assertThat("empty events list not allowed to be registered",
forHookUrl(HOOK_ENDPOINT)
.createHookSubscribedTo(Collections.<GHEvent>emptyList()).apply(active), nullValue());
}
@Test
public void shouldSelectOnlyHookManagedCreds() {
GitHubServerConfig conf = new GitHubServerConfig("");
conf.setManageHooks(false);
GitHubPlugin.configuration().getConfigs().add(conf);
assertThat(forHookUrl(HOOK_ENDPOINT).createHookSubscribedTo(Lists.newArrayList(PUSH))
.apply(new GitHubRepositoryName("github.com", "name", "repo")), nullValue());
}
@Test
public void shouldNotSelectCredsWithCustomHost() {
GitHubServerConfig conf = new GitHubServerConfig("");
conf.setApiUrl(ANOTHER_HOOK_ENDPOINT.toString());
conf.setManageHooks(false);
GitHubPlugin.configuration().getConfigs().add(conf);
assertThat(forHookUrl(HOOK_ENDPOINT).createHookSubscribedTo(Lists.newArrayList(PUSH))
.apply(new GitHubRepositoryName("github.com", "name", "repo")), nullValue());
}
@Test
public void shouldSendSecretIfDefined() throws Exception {
String secretText = "secret_text";
storeSecretIn(GitHubPlugin.configuration(), secretText);
manager.createWebhook(HOOK_ENDPOINT, ImmutableSet.of(PUSH)).apply(repo);
verify(repo).createHook(
anyString(),
(Map<String, String>) argThat(hasEntry("secret", secretText)),
anySetOf(GHEvent.class),
anyBoolean()
);
}
private GHHook hook(URL endpoint, GHEvent event, GHEvent... events) {
GHHook hook = mock(GHHook.class);
when(hook.getName()).thenReturn("web");
when(hook.getConfig()).thenReturn(ImmutableMap.of("url", endpoint.toExternalForm()));
when(hook.getEvents()).thenReturn(EnumSet.copyOf(asList(event, events)));
return hook;
}
private static URL endpoint(String endpoint) {
try {
return new URL(endpoint);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}