mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-04-18 21:30:30 +00:00
Refactored inline authority list into member variable.
This commit is contained in:
parent
e864dfa796
commit
6601b3da5f
@ -15,17 +15,17 @@
|
|||||||
|
|
||||||
package org.springframework.security.providers.cas;
|
package org.springframework.security.providers.cas;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.jasig.cas.client.validation.Assertion;
|
import org.jasig.cas.client.validation.Assertion;
|
||||||
import org.jasig.cas.client.validation.AssertionImpl;
|
import org.jasig.cas.client.validation.AssertionImpl;
|
||||||
import org.springframework.security.GrantedAuthority;
|
import org.springframework.security.GrantedAuthority;
|
||||||
import org.springframework.security.GrantedAuthorityImpl;
|
|
||||||
|
|
||||||
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
||||||
|
|
||||||
import org.springframework.security.userdetails.User;
|
import org.springframework.security.userdetails.User;
|
||||||
import org.springframework.security.userdetails.UserDetails;
|
import org.springframework.security.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.util.AuthorityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link CasAuthenticationToken}.
|
* Tests {@link CasAuthenticationToken}.
|
||||||
@ -34,29 +34,14 @@ import org.springframework.security.userdetails.UserDetails;
|
|||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class CasAuthenticationTokenTests extends TestCase {
|
public class CasAuthenticationTokenTests extends TestCase {
|
||||||
//~ Constructors ===================================================================================================
|
private final List<GrantedAuthority> ROLES = AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_TWO");
|
||||||
|
|
||||||
public CasAuthenticationTokenTests() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public CasAuthenticationTokenTests(String arg0) {
|
|
||||||
super(arg0);
|
|
||||||
}
|
|
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
junit.textui.TestRunner.run(CasAuthenticationTokenTests.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
private UserDetails makeUserDetails() {
|
private UserDetails makeUserDetails() {
|
||||||
return makeUserDetails("user");
|
return makeUserDetails("user");
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserDetails makeUserDetails(final String name) {
|
private UserDetails makeUserDetails(final String name) {
|
||||||
return new User(name, "password", true, true, true, true,
|
return new User(name, "password", true, true, true, true, ROLES);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void setUp() throws Exception {
|
public final void setUp() throws Exception {
|
||||||
@ -66,55 +51,37 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
public void testConstructorRejectsNulls() {
|
public void testConstructorRejectsNulls() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
try {
|
try {
|
||||||
new CasAuthenticationToken(null, makeUserDetails(), "Password",
|
new CasAuthenticationToken(null, makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
|
||||||
fail("Should have thrown IllegalArgumentException");
|
fail("Should have thrown IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new CasAuthenticationToken("key", null, "Password",
|
new CasAuthenticationToken("key", null, "Password", ROLES, makeUserDetails(), assertion);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
|
||||||
fail("Should have thrown IllegalArgumentException");
|
fail("Should have thrown IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new CasAuthenticationToken("key", makeUserDetails(), null,
|
new CasAuthenticationToken("key", makeUserDetails(), null, ROLES, makeUserDetails(), assertion);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
|
||||||
fail("Should have thrown IllegalArgumentException");
|
fail("Should have thrown IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), null);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), null);
|
|
||||||
fail("Should have thrown IllegalArgumentException");
|
fail("Should have thrown IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, null, assertion);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
null, assertion);
|
|
||||||
fail("Should have thrown IllegalArgumentException");
|
fail("Should have thrown IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
new CasAuthenticationToken("key", makeUserDetails(), "Password", AuthorityUtils.createAuthorityList("ROLE_1", null), makeUserDetails(), assertion);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), null, new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
|
||||||
fail("Should have thrown IllegalArgumentException");
|
fail("Should have thrown IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
@ -124,12 +91,10 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
public void testEqualsWhenEqual() {
|
public void testEqualsWhenEqual() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
|
|
||||||
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
|
|
||||||
CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
|
|
||||||
assertEquals(token1, token2);
|
assertEquals(token1, token2);
|
||||||
@ -138,8 +103,7 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
public void testGetters() {
|
public void testGetters() {
|
||||||
// Build the proxy list returned in the ticket from CAS
|
// Build the proxy list returned in the ticket from CAS
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
assertEquals("key".hashCode(), token.getKeyHash());
|
assertEquals("key".hashCode(), token.getKeyHash());
|
||||||
assertEquals(makeUserDetails(), token.getPrincipal());
|
assertEquals(makeUserDetails(), token.getPrincipal());
|
||||||
@ -151,10 +115,8 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testNoArgConstructorDoesntExist() {
|
public void testNoArgConstructorDoesntExist() {
|
||||||
Class clazz = CasAuthenticationToken.class;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
clazz.getDeclaredConstructor((Class[]) null);
|
CasAuthenticationToken.class.getDeclaredConstructor((Class[]) null);
|
||||||
fail("Should have thrown NoSuchMethodException");
|
fail("Should have thrown NoSuchMethodException");
|
||||||
} catch (NoSuchMethodException expected) {
|
} catch (NoSuchMethodException expected) {
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
@ -164,13 +126,11 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
public void testNotEqualsDueToAbstractParentEqualsCheck() {
|
public void testNotEqualsDueToAbstractParentEqualsCheck() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
|
|
||||||
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
|
|
||||||
CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails("OTHER_NAME"), "Password",
|
CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails("OTHER_NAME"), "Password",
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
ROLES, makeUserDetails(), assertion);
|
||||||
makeUserDetails(), assertion);
|
|
||||||
|
|
||||||
assertTrue(!token1.equals(token2));
|
assertTrue(!token1.equals(token2));
|
||||||
}
|
}
|
||||||
@ -178,26 +138,21 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
public void testNotEqualsDueToDifferentAuthenticationClass() {
|
public void testNotEqualsDueToDifferentAuthenticationClass() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
|
|
||||||
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
|
|
||||||
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password",
|
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password", ROLES);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
|
|
||||||
|
|
||||||
assertTrue(!token1.equals(token2));
|
assertTrue(!token1.equals(token2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNotEqualsDueToKey() {
|
public void testNotEqualsDueToKey() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
|
|
||||||
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
|
|
||||||
CasAuthenticationToken token2 = new CasAuthenticationToken("DIFFERENT_KEY", makeUserDetails(), "Password",
|
CasAuthenticationToken token2 = new CasAuthenticationToken("DIFFERENT_KEY", makeUserDetails(), "Password",
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
ROLES, makeUserDetails(), assertion);
|
||||||
makeUserDetails(), assertion);
|
|
||||||
|
|
||||||
assertTrue(!token1.equals(token2));
|
assertTrue(!token1.equals(token2));
|
||||||
}
|
}
|
||||||
@ -206,12 +161,10 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
final Assertion assertion2 = new AssertionImpl("test");
|
final Assertion assertion2 = new AssertionImpl("test");
|
||||||
|
|
||||||
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
|
|
||||||
CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion2);
|
makeUserDetails(), assertion2);
|
||||||
|
|
||||||
assertTrue(!token1.equals(token2));
|
assertTrue(!token1.equals(token2));
|
||||||
@ -219,8 +172,7 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public void testSetAuthenticated() {
|
public void testSetAuthenticated() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
assertTrue(token.isAuthenticated());
|
assertTrue(token.isAuthenticated());
|
||||||
token.setAuthenticated(false);
|
token.setAuthenticated(false);
|
||||||
@ -229,8 +181,7 @@ public class CasAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public void testToString() {
|
public void testToString() {
|
||||||
final Assertion assertion = new AssertionImpl("test");
|
final Assertion assertion = new AssertionImpl("test");
|
||||||
CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
|
CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",ROLES,
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
|
|
||||||
makeUserDetails(), assertion);
|
makeUserDetails(), assertion);
|
||||||
String result = token.toString();
|
String result = token.toString();
|
||||||
assertTrue(result.lastIndexOf("Credentials (Service/Proxy Ticket):") != -1);
|
assertTrue(result.lastIndexOf("Credentials (Service/Proxy Ticket):") != -1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user