|
| 1 | +package com.gitee.jenkins.webhook.build; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.hamcrest.Matchers.notNullValue; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.mockito.ArgumentMatchers.any; |
| 8 | +import static org.mockito.ArgumentMatchers.isA; |
| 9 | +import static org.mockito.Mockito.isA; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.never; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | +import static org.mockito.Mockito.when; |
| 14 | + |
| 15 | +import com.gitee.jenkins.trigger.GiteePushTrigger; |
| 16 | +import com.gitee.jenkins.gitee.hook.model.PushHook; |
| 17 | +import hudson.model.FreeStyleProject; |
| 18 | +import hudson.security.ACL; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.Collections; |
| 21 | +import jenkins.plugins.git.GitSCMSource; |
| 22 | +import jenkins.plugins.git.traits.IgnoreOnPushNotificationTrait; |
| 23 | +import jenkins.scm.api.SCMSourceOwner; |
| 24 | +import org.apache.commons.io.IOUtils; |
| 25 | +import org.junit.jupiter.api.BeforeAll; |
| 26 | +import org.junit.jupiter.api.Disabled; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.jvnet.hudson.test.JenkinsRule; |
| 30 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 31 | +import org.kohsuke.stapler.HttpResponses; |
| 32 | +import org.kohsuke.stapler.StaplerResponse2; |
| 33 | +import org.mockito.ArgumentCaptor; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 36 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 37 | +import org.mockito.quality.Strictness; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Robin Müller |
| 41 | + */ |
| 42 | +@WithJenkins |
| 43 | +@ExtendWith(MockitoExtension.class) |
| 44 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 45 | +class PushBuildActionTest { |
| 46 | + |
| 47 | + private static JenkinsRule jenkins; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private StaplerResponse2 response; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private GiteePushTrigger trigger; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + static void setUp(JenkinsRule rule) { |
| 57 | + jenkins = rule; |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void skip_missingRepositoryUrl() throws Exception { |
| 62 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 63 | + testProject.addTrigger(trigger); |
| 64 | + |
| 65 | + new PushBuildAction(testProject, getJson("PushEvent_missingRepositoryUrl.json"), null).execute(response); |
| 66 | + |
| 67 | + verify(trigger, never()).onPost(any(PushHook.class)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void build() { |
| 72 | + assertThrows(HttpResponses.HttpResponseException.class, () -> { |
| 73 | + try { |
| 74 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 75 | + testProject.addTrigger(trigger); |
| 76 | + |
| 77 | + // exception.expect(HttpResponses.HttpResponseException.class); |
| 78 | + new PushBuildAction(testProject, getJson("PushEvent.json"), null).execute(response); |
| 79 | + } finally { |
| 80 | + ArgumentCaptor<PushHook> pushHookArgumentCaptor = ArgumentCaptor.forClass(PushHook.class); |
| 81 | + verify(trigger).onPost(pushHookArgumentCaptor.capture()); |
| 82 | + assertThat(pushHookArgumentCaptor.getValue().getProject(), is(notNullValue())); |
| 83 | + assertThat(pushHookArgumentCaptor.getValue().getProject().getWebUrl(), is(notNullValue())); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void invalidToken() { |
| 90 | + assertThrows(HttpResponses.HttpResponseException.class, () -> { |
| 91 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 92 | + when(trigger.getSecretToken()).thenReturn("secret"); |
| 93 | + testProject.addTrigger(trigger); |
| 94 | + new PushBuildAction(testProject, getJson("PushEvent.json"), "wrong-secret").execute(response); |
| 95 | + |
| 96 | + verify(trigger, never()).onPost(any(PushHook.class)); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + private String getJson(String name) throws Exception { |
| 101 | + return IOUtils.toString(getClass().getResourceAsStream(name), StandardCharsets.UTF_8); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void scmSourceOnUpdateExecuted() { |
| 106 | + GitSCMSource source = new GitSCMSource("http://test"); |
| 107 | + SCMSourceOwner item = mock(SCMSourceOwner.class); |
| 108 | + ACL acl = mock(ACL.class); |
| 109 | + when(item.getSCMSources()).thenReturn(Collections.singletonList(source)); |
| 110 | + when(item.getACL()).thenReturn(acl); |
| 111 | + assertThrows( |
| 112 | + HttpResponses.HttpResponseException.class, |
| 113 | + () -> new PushBuildAction(item, getJson("PushEvent.json"), null).execute(response)); |
| 114 | + // item.onSCMSourceUpdated(source); |
| 115 | + verify(item).onSCMSourceUpdated(isA(GitSCMSource.class)); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + // Test is currently unsupported. A check is missing in the SCMSourceOwnerNotifier. |
| 120 | + @Test |
| 121 | + @Disabled |
| 122 | + void scmSourceOnUpdateNotExecuted() { |
| 123 | + GitSCMSource source = new GitSCMSource("http://test"); |
| 124 | + source.getTraits().add(new IgnoreOnPushNotificationTrait()); |
| 125 | + SCMSourceOwner item = mock(SCMSourceOwner.class); |
| 126 | + when(item.getSCMSources()).thenReturn(Collections.singletonList(source)); |
| 127 | + assertThrows( |
| 128 | + HttpResponses.HttpResponseException.class, |
| 129 | + () -> new PushBuildAction(item, getJson("PushEvent.json"), null).execute(response)); |
| 130 | + verify(item, never()).onSCMSourceUpdated(isA(GitSCMSource.class)); |
| 131 | + } |
| 132 | +} |
0 commit comments