Skip to content

Commit

Permalink
[JENKINS-21371] Support GroupDetails.getMembers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jun 4, 2014
1 parent 396be54 commit 79eeb17
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.466</version>
<version>1.554.1</version>
</parent>
<artifactId>mock-security-realm</artifactId>
<version>1.2-SNAPSHOT</version>
Expand Down
Expand Up @@ -131,6 +131,15 @@ private Map<String,Set<String>> usersAndGroups() {
public String getName() {
return groupname;
}
@Override public Set<String> getMembers() {
Set<String> r = new TreeSet<String>();
for (Map.Entry<String,Set<String>> entry : usersAndGroups().entrySet()) {
if (entry.getValue().contains(groupname)) {
r.add(entry.getKey());
}
}
return r;
}
};
}
}
Expand Down
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright 2014 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.mocksecurityrealm;

import hudson.security.SecurityRealm;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.junit.Test;
import static org.junit.Assert.*;

public class MockSecurityRealmTest {

private final SecurityRealm r = new MockSecurityRealm("alice admin\nbob dev\ncharlie qa\ndebbie admin qa", null, false);

@Test(expected=UsernameNotFoundException.class) public void nonexistentGroup() {
r.loadGroupByGroupname("nonexistent");
}

@Test public void getMembers() {
assertEquals("[alice, debbie]", r.loadGroupByGroupname("admin", true).getMembers().toString());

This comment has been minimized.

Copy link
@stephenc

stephenc Jun 4, 2014

Member

Seriously? you are comparing a List based on the toString()?

assertThat(r.loadGroupByGroupname("admin", true).getMembers(), is(Arrays.asList("alice", "debbie"));

would be better... better yet would be to use hamcrest matchers to ensure that the order of items in the list does not matter... but heck I'd settle for anything that doesn't use toString()

This comment has been minimized.

Copy link
@jglick

jglick Jun 4, 2014

Author Member

In this case the returned value is a TreeSet so the toString is predictable. If I were testing an unknown implementation I would probably just wrap the result in a TreeSet first.

assertEquals("[bob]", r.loadGroupByGroupname("dev", true).getMembers().toString());
assertEquals("[charlie, debbie]", r.loadGroupByGroupname("qa", true).getMembers().toString());
}

}

0 comments on commit 79eeb17

Please sign in to comment.