forked from synopsys-arc-oss/ownership-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from jcoste-orange/JENKINS-20832
[JENKINS-20832] Add support for case insensitive auth realms
- Loading branch information
Showing
6 changed files
with
192 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/synopsys/arc/jenkins/plugins/ownership/util/IdStrategyComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.synopsys.arc.jenkins.plugins.ownership.util; | ||
|
||
import hudson.security.SecurityRealm; | ||
import java.util.Comparator; | ||
import jenkins.model.IdStrategy; | ||
import jenkins.model.Jenkins; | ||
|
||
|
||
public class IdStrategyComparator implements Comparator<String> { | ||
|
||
private final SecurityRealm securityRealm; | ||
private final IdStrategy groupIdStrategy; | ||
private final IdStrategy userIdStrategy; | ||
|
||
public IdStrategyComparator() { | ||
securityRealm = Jenkins.getActiveInstance().getSecurityRealm(); | ||
groupIdStrategy = securityRealm.getGroupIdStrategy(); | ||
userIdStrategy = securityRealm.getUserIdStrategy(); | ||
} | ||
|
||
public int compare(String o1, String o2) { | ||
int r = userIdStrategy.compare(o1, o2); | ||
if (r == 0) { | ||
r = groupIdStrategy.compare(o1, o2); | ||
} | ||
return r; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/test/java/com/synopsys/arc/jenkins/plugins/ownership/OwnershipDescriptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.synopsys.arc.jenkins.plugins.ownership; | ||
|
||
import hudson.model.User; | ||
import hudson.security.HudsonPrivateSecurityRealm; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import jenkins.model.IdStrategy; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
|
||
public class OwnershipDescriptionTest { | ||
private static final IdStrategy CASE_SENSITIVE = new IdStrategy.CaseSensitive(); | ||
|
||
@Rule | ||
public final JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
applyIdStrategy(CASE_SENSITIVE); | ||
} | ||
|
||
private void applyIdStrategy(final IdStrategy idStrategy) throws IOException { | ||
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(false, false, null) { | ||
@Override | ||
public IdStrategy getUserIdStrategy() { | ||
return idStrategy; | ||
} | ||
|
||
@Override | ||
public IdStrategy getGroupIdStrategy() { | ||
return idStrategy; | ||
} | ||
}; | ||
realm.createAccount("owner", "owner"); | ||
j.jenkins.setSecurityRealm(realm); | ||
} | ||
|
||
@Test | ||
public void isOwnerShouldRespectCaseSensitiveIdStrategy() throws Exception { | ||
User user = User.get("owner"); | ||
|
||
OwnershipDescription description = new OwnershipDescription(true, "owner", Collections.<String>emptyList()); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(true)); | ||
|
||
description = new OwnershipDescription(true, "OWNER", Collections.<String>emptyList()); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(false)); | ||
|
||
description = new OwnershipDescription(true, "another.owner", Arrays.asList("owner")); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, true), equalTo(true)); | ||
|
||
description = new OwnershipDescription(true, "ANOTHER.OWNER", Arrays.asList("OWNER")); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, true), equalTo(false)); | ||
} | ||
|
||
@Test | ||
public void isOwnerShouldRespectCaseInsensitiveIdStrategy() throws Exception { | ||
applyIdStrategy(IdStrategy.CASE_INSENSITIVE); | ||
User user = User.get("owner"); | ||
|
||
OwnershipDescription description = new OwnershipDescription(true, "owner", Collections.<String>emptyList()); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(true)); | ||
|
||
description = new OwnershipDescription(true, "OWNER", Collections.<String>emptyList()); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(true)); | ||
|
||
description = new OwnershipDescription(true, "another.owner", Arrays.asList("owner")); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, true), equalTo(true)); | ||
|
||
description = new OwnershipDescription(true, "ANOTHER.OWNER", Arrays.asList("OWNER")); | ||
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, true), equalTo(true)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters