Skip to content

Commit

Permalink
Cleaner code, newer core requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-beck committed Nov 3, 2019
1 parent d7e87c1 commit 3149ab3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-war</artifactId>
<!--to have access to User.getById-->
<version>1.651.2</version>
<!--to have access to SECURITY-595 methods -->
<version>2.138.4</version>
<type>executable-war</type>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;
import jenkins.model.Jenkins;
import jenkins.security.stapler.DoActionFilter;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.kohsuke.stapler.Function;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.interceptor.InterceptorAnnotation;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.verb.HttpVerbInterceptor;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand Down Expand Up @@ -126,13 +127,10 @@ public void testPluginActive() {
}
}

public void testStaplerDispatches() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
public void testStaplerDispatches() {
List<String> methodsFound = new ArrayList<>();

Method isStaplerRoutableMethod = findIsRoutableMethod();
if (isStaplerRoutableMethod == null) {
return;
}
DoActionFilter filter = new DoActionFilter();

PluginWrapper thisPlugin = determineCurrentPlugin();
if (thisPlugin == null) {
Expand All @@ -147,7 +145,8 @@ public void testStaplerDispatches() throws InvocationTargetException, IllegalAcc
for (ClassInfo classInfo : result.getAllClasses()) {
Class clazz = classInfo.loadClass();
for (Method m : clazz.getDeclaredMethods()) {
if (isStaplerDispatchable(m) && (boolean)isStaplerRoutableMethod.invoke(null, m)) {
Function f = new Function.InstanceFunction(m);
if (isStaplerDispatchable(m) && filter.keep(f)) {
if (!hasStaplerVerbAnnotation(m)) {
methodsFound.add(clazz.getName() + "#" + m.getName());
}
Expand All @@ -157,18 +156,6 @@ public void testStaplerDispatches() throws InvocationTargetException, IllegalAcc
Assert.assertThat("There should be no web methods that lack HTTP verb annotations like @RequirePOST, @GET, @POST, etc. -- see https://jenkins.io/redirect/developer/csrf-protection", methodsFound, is(empty()));
}

private Method findIsRoutableMethod() throws NoSuchMethodException {
try {
Method method = Class.forName("jenkins.security.stapler.TypedFilter").getDeclaredMethod("isRoutableMethod", Method.class);
method.setAccessible(true);
return method;
} catch (ClassNotFoundException e) {
LOGGER.warning("This test requires Jenkins 2.154, Jenkins LTS 2.138.4, or newer to run, use e.g. -Djenkins.version=2.138.4");
// TODO add a fallback implementing similar code directly here
return null;
}
}

private PluginWrapper determineCurrentPlugin() {
String plugin = (String) params.get("artifactId");
if (plugin != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/jvnet/hudson/test/JenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gargoylesoftware.htmlunit.WebRequest;
import hudson.model.User;
import jenkins.security.ApiTokenProperty;
import jenkins.security.apitoken.ApiTokenPropertyConfiguration;
import net.sf.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void assertEqualDataBoundBeansWithSettersFailForField() throws Exception
@Test
public void testTokenHelperMethods() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
// Jenkins 2.129+: ApiTokenPropertyConfiguration.get().setTokenGenerationOnCreationEnabled(true);
ApiTokenPropertyConfiguration.get().setTokenGenerationOnCreationEnabled(true);

JenkinsRule.WebClient wc = j.createWebClient();

Expand Down

0 comments on commit 3149ab3

Please sign in to comment.