Skip to content

Commit

Permalink
Replace deprecated test methods with recommended alternatives (#249)
Browse files Browse the repository at this point in the history
The PresetData methods are replaced with a mock security realm that
provides exactly the permissions desired.

Also adds one Override annotation that was missing.
  • Loading branch information
MarkEWaite authored Dec 10, 2024
1 parent 8b50704 commit 2fff83e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/test/java/org/jenkinsci/plugins/impliedlabels/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.recipes.PresetData;
import org.jvnet.hudson.test.recipes.PresetData.DataSet;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

public class ConfigTest {

Expand All @@ -84,7 +83,7 @@ public void setUp() throws IOException {
new Implication("rhel || fedora", "linux"),
new Implication("||", "invalid"));
config.implications(implications);
controllerLabel = j.jenkins.get().getSelfLabel().getName();
controllerLabel = Jenkins.get().getSelfLabel().getName();
}

@Test
Expand Down Expand Up @@ -155,9 +154,13 @@ public void validateExpression() {
assertThat(config.doCheckExpression("!||&&").getMessage(), containsString("Invalid label expression"));
}

@PresetData(DataSet.NO_ANONYMOUS_READACCESS)
@Test
public void notAuthorizedToRead() throws Exception {
// Create a security realm that allows no read
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy authorizationStrategy = new MockAuthorizationStrategy();
j.jenkins.setAuthorizationStrategy(authorizationStrategy);

WebClient wc = j.createWebClient();

try {
Expand All @@ -168,12 +171,20 @@ public void notAuthorizedToRead() throws Exception {
}
}

@PresetData(DataSet.ANONYMOUS_READONLY)
@Test
public void notAuthorizedToConfigure() throws Exception {
// Create a security realm that only allows a-read-only-user to read
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy authorizationStrategy = new MockAuthorizationStrategy();
authorizationStrategy.grant(Jenkins.READ).everywhere().to("a-read-only-user");
j.jenkins.setAuthorizationStrategy(authorizationStrategy);

WebClient wc = j.createWebClient();
wc.getOptions().setPrintContentOnFailingStatusCode(false);

// Login as the user that can only read
wc.login("a-read-only-user");

try {
wc.goTo("label-implications");
} catch (FailingHttpStatusCodeException ex) {
Expand Down Expand Up @@ -215,6 +226,7 @@ public SameMembers(Collection<T> items) {
this.items = new HashSet<>(items);
}

@Override
public void describeTo(Description description) {
description.appendText("Implies: " + items);
}
Expand Down

0 comments on commit 2fff83e

Please sign in to comment.