SEC-714: Refactor PreAuthenticatedGrantedAuthoritiesSetter and PreAuthenticatedGrantedAuthoritiesRetriever

http://jira.springframework.org/browse/SEC-714
This commit is contained in:
Luke Taylor
2008-03-13 16:03:18 +00:00
parent 42a80931c1
commit 712f1770d9
10 changed files with 69 additions and 92 deletions

View File

@@ -1,5 +1,6 @@
package org.springframework.security.providers.preauth;
import org.springframework.security.GrantedAuthoritiesContainer;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
@@ -53,8 +54,8 @@ public class PreAuthenticatedGrantedAuthoritiesUserDetailsServiceTests extends T
private void testGetUserDetails(final String userName, final GrantedAuthority[] gas) {
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService();
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(userName, "dummy");
token.setDetails(new PreAuthenticatedGrantedAuthoritiesRetriever() {
public GrantedAuthority[] getPreAuthenticatedGrantedAuthorities() {
token.setDetails(new GrantedAuthoritiesContainer() {
public GrantedAuthority[] getGrantedAuthorities() {
return gas;
}
});

View File

@@ -15,9 +15,7 @@ import junit.framework.TestCase;
import org.springframework.mock.web.MockHttpServletRequest;
/**
*
* @author TSARDD
* @since 18-okt-2007
*/
public class PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetailsTests extends TestCase {
@@ -25,7 +23,7 @@ public class PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetailsTests ext
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails(
getRequest("testUser", new String[] {}));
GrantedAuthority[] gas = new GrantedAuthority[] { new GrantedAuthorityImpl("Role1"), new GrantedAuthorityImpl("Role2") };
details.setPreAuthenticatedGrantedAuthorities(gas);
details.setGrantedAuthorities(gas);
String toString = details.toString();
assertTrue("toString should contain Role1", toString.contains("Role1"));
assertTrue("toString should contain Role2", toString.contains("Role2"));
@@ -37,18 +35,17 @@ public class PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetailsTests ext
GrantedAuthority[] gas = new GrantedAuthority[] { new GrantedAuthorityImpl("Role1"), new GrantedAuthorityImpl("Role2") };
Collection expectedGas = Arrays.asList(gas);
details.setPreAuthenticatedGrantedAuthorities(gas);
Collection returnedGas = Arrays.asList(details.getPreAuthenticatedGrantedAuthorities());
assertTrue("Collections do not contain same elements; expected: " + expectedGas + ", returned: " + returnedGas, expectedGas
.containsAll(returnedGas)
&& returnedGas.containsAll(expectedGas));
details.setGrantedAuthorities(gas);
Collection returnedGas = Arrays.asList(details.getGrantedAuthorities());
assertTrue("Collections do not contain same elements; expected: " + expectedGas + ", returned: " + returnedGas,
expectedGas.containsAll(returnedGas) && returnedGas.containsAll(expectedGas));
}
public final void testGetWithoutSetPreAuthenticatedGrantedAuthorities() {
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails(
getRequest("testUser", new String[] {}));
try {
GrantedAuthority[] gas = details.getPreAuthenticatedGrantedAuthorities();
GrantedAuthority[] gas = details.getGrantedAuthorities();
fail("Expected exception didn't occur");
} catch (IllegalArgumentException expected) {
} catch (Exception unexpected) {

View File

@@ -21,7 +21,6 @@ import org.springframework.mock.web.MockHttpServletRequest;
/**
*
* @author TSARDD
* @since 18-okt-2007
*/
public class J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests extends TestCase {
@@ -92,7 +91,7 @@ public class J2eeBasedPreAuthenticatedWebAuthenticationDetailsSourceTests extend
assertTrue("Returned object not of type PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails, actual type: " + o.getClass(),
o instanceof PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails);
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = (PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails) o;
GrantedAuthority[] gas = details.getPreAuthenticatedGrantedAuthorities();
GrantedAuthority[] gas = details.getGrantedAuthorities();
assertNotNull("Granted authorities should not be null", gas);
assertTrue("Number of granted authorities should be " + expectedRoles.length, gas.length == expectedRoles.length);