|
| 1 | +/* |
| 2 | + * Test tokenMatches in the BuildWebHookAction class |
| 3 | + * Author: Mark Waite |
| 4 | + */ |
| 5 | +package com.dabsquared.gitlabjenkins.webhook.build; |
| 6 | + |
| 7 | +import com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig; |
| 8 | +import com.dabsquared.gitlabjenkins.GitLabPushTrigger; |
| 9 | + |
| 10 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 11 | + |
| 12 | +import hudson.model.FreeStyleProject; |
| 13 | +import hudson.model.Item; |
| 14 | +import hudson.model.Project; |
| 15 | +import hudson.security.ACL; |
| 16 | + |
| 17 | +import org.acegisecurity.Authentication; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.Rule; |
| 22 | +import org.jvnet.hudson.test.JenkinsRule; |
| 23 | + |
| 24 | +import org.kohsuke.stapler.HttpResponses.HttpResponseException; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertFalse; |
| 27 | +import static org.junit.Assert.assertThrows; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test the BuildWebHookAction class |
| 32 | + * |
| 33 | + * @author Mark Waite |
| 34 | + */ |
| 35 | +public class BuildWebHookActionTest { |
| 36 | + |
| 37 | + @Rule |
| 38 | + public JenkinsRule j = new JenkinsRule(); |
| 39 | + |
| 40 | + private FreeStyleProject project; |
| 41 | + private GitLabPushTrigger trigger; |
| 42 | + |
| 43 | + public BuildWebHookActionTest() { |
| 44 | + } |
| 45 | + |
| 46 | + @Before |
| 47 | + public void confgureGitLabConnection() throws Exception { |
| 48 | + j.get(GitLabConnectionConfig.class).setUseAuthenticatedEndpoint(true); |
| 49 | + } |
| 50 | + |
| 51 | + @Before |
| 52 | + public void createFreeStyleProjectWithGitLabTrigger() throws Exception { |
| 53 | + project = j.createFreeStyleProject(); |
| 54 | + trigger = new GitLabPushTrigger(); |
| 55 | + project.addTrigger(trigger); |
| 56 | + } |
| 57 | + |
| 58 | + // trigger token == action token, expected to succeed |
| 59 | + @Test |
| 60 | + public void testNotifierTokenMatches() throws Exception { |
| 61 | + String triggerToken = "testNotifierTokenMatches-token"; |
| 62 | + trigger.setSecretToken(triggerToken); |
| 63 | + String actionToken = triggerToken; |
| 64 | + BuildWebHookActionImpl action = new BuildWebHookActionImpl(project, actionToken); |
| 65 | + action.runNotifier(); |
| 66 | + assertTrue("performOnPost not called, token did not match?", action.performOnPostCalled); |
| 67 | + } |
| 68 | + |
| 69 | + // trigger token != action token, expected to throw an exception |
| 70 | + @Test |
| 71 | + public void testNotifierTokenDoesNotMatchString() throws Exception { |
| 72 | + String triggerToken = "testNotifierTokenDoesNotMatchString-token"; |
| 73 | + trigger.setSecretToken(triggerToken); |
| 74 | + String actionToken = triggerToken + "-no-match"; // Won't match |
| 75 | + BuildWebHookActionImpl action = new BuildWebHookActionImpl(project, actionToken); |
| 76 | + assertThrows(HttpResponseException.class, |
| 77 | + () -> { |
| 78 | + action.runNotifier(); |
| 79 | + } |
| 80 | + ); |
| 81 | + assertFalse("performOnPost was called, unexpected token match?", action.performOnPostCalled); |
| 82 | + } |
| 83 | + |
| 84 | + // trigger token != null action token, expected to throw an exception |
| 85 | + @Test |
| 86 | + public void testNotifierTokenDoesNotMatchNull() throws Exception { |
| 87 | + String triggerToken = "testNotifierTokenDoesNotMatchNull-token"; |
| 88 | + trigger.setSecretToken(triggerToken); |
| 89 | + String actionToken = null; |
| 90 | + BuildWebHookActionImpl action = new BuildWebHookActionImpl(project, actionToken); |
| 91 | + assertThrows(HttpResponseException.class, |
| 92 | + () -> { |
| 93 | + action.runNotifier(); |
| 94 | + } |
| 95 | + ); |
| 96 | + assertFalse("performOnPost was called, unexpected token match?", action.performOnPostCalled); |
| 97 | + } |
| 98 | + |
| 99 | + // null trigger token != action token, expected to succeed |
| 100 | + @Test |
| 101 | + public void testNullNotifierTokenAllowsAccess() throws Exception { |
| 102 | + // String triggerToken = null; |
| 103 | + // trigger.setSecretToken(triggerToken); |
| 104 | + String actionToken = "testNullNotifierTokenAllowsAccess-token"; |
| 105 | + BuildWebHookActionImpl action = new BuildWebHookActionImpl(project, actionToken); |
| 106 | + action.runNotifier(); |
| 107 | + assertTrue("performOnPost not called, token did not match?", action.performOnPostCalled); |
| 108 | + } |
| 109 | + |
| 110 | + public class BuildWebHookActionImpl extends BuildWebHookAction { |
| 111 | + |
| 112 | + // Used for the assertion that tokenMatches() returned true |
| 113 | + public boolean performOnPostCalled = false; |
| 114 | + |
| 115 | + private final MyTriggerNotifier myNotifier; |
| 116 | + |
| 117 | + public BuildWebHookActionImpl() { |
| 118 | + myNotifier = new MyTriggerNotifier(null, null, null); |
| 119 | + } |
| 120 | + |
| 121 | + public BuildWebHookActionImpl(@NonNull Project project, @NonNull String token) { |
| 122 | + myNotifier = new MyTriggerNotifier(project, token, ACL.SYSTEM); |
| 123 | + } |
| 124 | + |
| 125 | + public void runNotifier() { |
| 126 | + myNotifier.run(); |
| 127 | + } |
| 128 | + |
| 129 | + public class MyTriggerNotifier extends TriggerNotifier { |
| 130 | + |
| 131 | + public MyTriggerNotifier(Item project, String secretToken, Authentication authentication) { |
| 132 | + super(project, secretToken, authentication); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected void performOnPost(GitLabPushTrigger trigger) { |
| 137 | + performOnPostCalled = true; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void processForCompatibility() { |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void execute() { |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments