001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.bus.security.credentials;
017:
018: import java.lang.reflect.Field;
019: import java.util.ArrayList;
020: import java.util.Map;
021:
022: import junit.framework.TestCase;
023:
024: import org.acegisecurity.GrantedAuthority;
025: import org.acegisecurity.GrantedAuthorityImpl;
026: import org.acegisecurity.context.SecurityContextHolder;
027: import org.acegisecurity.context.SecurityContextImpl;
028: import org.acegisecurity.providers.cas.CasAuthenticationToken;
029: import org.acegisecurity.ui.cas.CasProcessingFilter;
030: import org.acegisecurity.userdetails.User;
031: import org.kuali.rice.security.credentials.Credentials;
032: import org.kuali.rice.security.credentials.CredentialsSource.CredentialsType;
033: import org.springframework.mock.web.MockHttpServletRequest;
034: import org.springframework.mock.web.MockServletConfig;
035:
036: import edu.yale.its.tp.cas.proxy.ExtendedProxyGrantingTicket;
037: import edu.yale.its.tp.cas.proxy.ProxyTicketReceptor;
038:
039: /**
040: *
041: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
042: * @version $Revision: 1.2.16.1.6.1 $ $Date: 2007/10/17 20:32:06 $
043: * @since 0.9
044: *
045: */
046: public class CasProxyTicketCredentialsSourceTest extends TestCase {
047:
048: private CasProxyTicketCredentialsSource credentialsSource;
049:
050: private ProxyTicketReceptor receptor = new ProxyTicketReceptor();
051:
052: private final String proxyUrl = "https://localhost:8080/cas/proxy";
053:
054: protected void setUp() throws Exception {
055: this .credentialsSource = new CasProxyTicketCredentialsSource();
056: final CasAuthenticationToken token = new CasAuthenticationToken(
057: "test",
058: "cas_user",
059: "ticketId",
060: new GrantedAuthority[] { new GrantedAuthorityImpl(
061: "ROLE_USER") },
062: new User(
063: "cas_user",
064: "password",
065: true,
066: true,
067: true,
068: true,
069: new GrantedAuthority[] { new GrantedAuthorityImpl(
070: "ROLE_USER") }),
071: new ArrayList<String>(), "PGT-IOU");
072: final SecurityContextImpl impl = new SecurityContextImpl();
073: impl.setAuthentication(token);
074:
075: SecurityContextHolder.setContext(impl);
076:
077: final MockHttpServletRequest request = new MockHttpServletRequest();
078: request.setParameter("pgtIou", "PGT-IOU");
079: request.setParameter("pgtId", "PGT-ID");
080:
081: final MockServletConfig config = new MockServletConfig();
082: config.addInitParameter(
083: ProxyTicketReceptor.CAS_PROXYURL_INIT_PARAM, proxyUrl);
084:
085: this .receptor.init(config);
086:
087: final ExtendedProxyGrantingTicket ticket = new ExtendedProxyGrantingTicket(
088: "test", "test");
089:
090: final Field field = receptor.getClass().getDeclaredField(
091: "pgtMap");
092: field.setAccessible(true);
093: final Map map = (Map) field.get(receptor);
094:
095: map.put("PGT-IOU", ticket);
096: }
097:
098: public void testCredentialsType() {
099: assertEquals(CredentialsType.CAS, this .credentialsSource
100: .getSupportedCredentialsType());
101: }
102:
103: public void testGetterWithCasServerInstance() {
104: final Credentials c = this .credentialsSource
105: .getCredentials("http://www.cnn.com");
106: assertNotNull(c);
107: assertTrue(c instanceof UsernamePasswordCredentials);
108: final UsernamePasswordCredentials upc = (UsernamePasswordCredentials) c;
109: assertEquals(CasProcessingFilter.CAS_STATELESS_IDENTIFIER, upc
110: .getUsername());
111: assertEquals("PT", upc.getPassword());
112: }
113: }
|