Skip to content

Commit

Permalink
Merge pull request #124 from MRamonLeon/fix-test-selectors-new-core
Browse files Browse the repository at this point in the history
  • Loading branch information
MRamonLeon authored Apr 23, 2021
2 parents 2cebd66 + 7fa3296 commit e4f97f7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.AccessDeniedException2;
import hudson.security.Permission;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.acegisecurity.AccessDeniedException;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFile;
Expand All @@ -35,6 +35,7 @@
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.startsWith;
Expand Down Expand Up @@ -305,13 +306,13 @@ private void assertWhoCanExecute(Runnable run, Permission permission, String che
try (ACLContext ctx = ACL.as(User.getOrCreateByIdOrFullName("reader"))) {
run.run(); // The method should fail
fail(String.format("%s should be only accessible by people with the permission %s, but it's accessible by a person with %s", checkedMethod, permission, Item.READ));
} catch (AccessDeniedException2 e) {
assertThat(e.permission, equalTo(permission));
} catch (AccessDeniedException e) {
assertThat(e.getMessage(), containsString(permission.group.title + "/" + permission.name));
}

try (ACLContext ctx = ACL.as(User.getOrCreateByIdOrFullName(userWithPermission.get(permission)))) {
run.run(); // The method doesn't fail
} catch (AccessDeniedException2 e) {
} catch (AccessDeniedException e) {
fail(String.format("%s should be accessible to people with the permission %s but it failed with the exception: %s", checkedMethod, permission, e));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package org.jenkinsci.plugins.configfiles.sec;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.security.AccessDeniedException2;
import hudson.security.Permission;
import org.hamcrest.core.IsEqual;
import org.acegisecurity.AccessDeniedException;

import java.util.function.Supplier;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.fail;

/**
* A class to run pieces of code with a certain user and assert either the code runs successfully, without even worrying
* about the result, or the code fails with an {@link AccessDeniedException2} with the specified {@link Permission}.
* about the result, or the code fails with an {@link AccessDeniedException} with the specified {@link Permission} reason.
*/
public class PermissionChecker extends ProtectedCodeRunner<Void> {
/**
Expand All @@ -26,16 +26,16 @@ public PermissionChecker(@NonNull Runnable code, @NonNull String user) {
}

/**
* Assert the execution of the code by this user fails with this permission. The code throws an {@link AccessDeniedException2}
* with the permission field being permission. Otherwise it fails.
* Assert the execution of the code by this user fails with this permission. The code throws an {@link AccessDeniedException}
* with the message indicating the same permission. Otherwise it fails.
* @param permission The permission thrown by the code.
*/
public void assertFailWithPermission(Permission permission) {
Throwable t = getThrowable();
if (t instanceof AccessDeniedException2) {
assertThat(((AccessDeniedException2) t).permission, IsEqual.equalTo(permission));
if (t instanceof AccessDeniedException) {
assertThat(t.getMessage(), containsString(permission.group.title + "/" + permission.name));
} else {
fail(String.format("The code run by %s didn't throw an AccessDeniedException2 with %s. If failed with the unexpected throwable: %s", getUser(), permission, t));
fail(String.format("The code run by %s didn't throw an AccessDeniedException with %s. If failed with the unexpected throwable: %s", getUser(), permission, t));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jenkinsci.plugins.configfiles.sec;

import hudson.security.AccessDeniedException2;
import jenkins.model.Jenkins;
import org.acegisecurity.AccessDeniedException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -11,7 +11,7 @@
import java.util.function.Supplier;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

Expand Down Expand Up @@ -42,7 +42,7 @@ public void protectedCodeCheckerTest() {
assertThat(checker.getResult(), is("allowed"));

Throwable t = checker.withUser("reader").getThrowable();
assertThat(t, instanceOf(AccessDeniedException2.class));
assertThat(((AccessDeniedException2) t).permission, equalTo(Jenkins.ADMINISTER));
assertThat(t, instanceOf(AccessDeniedException.class));
assertThat(t.getMessage(), containsString(Jenkins.ADMINISTER.group.title + "/" + Jenkins.ADMINISTER.name));
}
}

0 comments on commit e4f97f7

Please sign in to comment.