    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.providers.cas.populator;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.GrantedAuthority;
    21  import org.acegisecurity.GrantedAuthorityImpl;
    22  import org.acegisecurity.userdetails.UserDetailsService;
    23  import org.acegisecurity.userdetails.User;
    24  import org.acegisecurity.userdetails.UserDetails;
    25  import org.acegisecurity.userdetails.UsernameNotFoundException;
    26
    27  import org.springframework.dao.DataAccessException;
    28  import org.springframework.dao.DataRetrievalFailureException;
    29
    30
    31  /***
    32   * Tests {@link DaoCasAuthoritiesPopulator}.
    33   *
    34   * @author Ben Alex
    35   * @version $Id: DaoCasAuthoritiesPopulatorTests.java,v 1.9 2005/11/30 00:20:11 benalex Exp $
    36   */
    37  public class DaoCasAuthoritiesPopulatorTests extends TestCase {
    38      //~ Constructors ===========================================================
    39
    40      public DaoCasAuthoritiesPopulatorTests() {
    41          super();
    42      }
    43
    44      public DaoCasAuthoritiesPopulatorTests(String arg0) {
    45          super(arg0);
    46      }
    47
    48      //~ Methods ================================================================
    49
    50      public final void setUp() throws Exception {
    51          super.setUp();
    52      }
    53
    54      public static void main(String[] args) {
    55          junit.textui.TestRunner.run(DaoCasAuthoritiesPopulatorTests.class);
    56      }
    57
    58      public void testDetectsMissingAuthenticationDao() throws Exception {
    59          DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
    60
    61          try {
    62              populator.afterPropertiesSet();
    63              fail("Should have thrown IllegalArgumentException");
    64          } catch (IllegalArgumentException expected) {
    65              assertEquals("An authenticationDao must be set",
    66                  expected.getMessage());
    67          }
    68      }
    69
    70      public void testGetGrantedAuthoritiesForInvalidUsername()
    71          throws Exception {
    72          DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
    73          populator.setUserDetailsService(new MockAuthenticationDaoUserMarissa());
    74          populator.afterPropertiesSet();
    75
    76          try {
    77              populator.getUserDetails("scott");
    78              fail("Should have thrown UsernameNotFoundException");
    79          } catch (UsernameNotFoundException expected) {
    80              assertTrue(true);
    81          }
    82      }
    83
    84      public void testGetGrantedAuthoritiesForValidUsername()
    85          throws Exception {
    86          DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
    87          populator.setUserDetailsService(new MockAuthenticationDaoUserMarissa());
    88          populator.afterPropertiesSet();
    89
    90          UserDetails results = populator.getUserDetails("marissa");
    91          assertEquals(2, results.getAuthorities().length);
    92          assertEquals(new GrantedAuthorityImpl("ROLE_ONE"),
    93              results.getAuthorities()[0]);
    94          assertEquals(new GrantedAuthorityImpl("ROLE_TWO"),
    95              results.getAuthorities()[1]);
    96      }
    97
    98      public void testGetGrantedAuthoritiesWhenDaoThrowsException()
    99          throws Exception {
    100         DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
    101         populator.setUserDetailsService(new MockAuthenticationDaoSimulateBackendError());
    102         populator.afterPropertiesSet();
    103
    104         try {
    105             populator.getUserDetails("THE_DAO_WILL_FAIL");
    106             fail("Should have thrown DataRetrievalFailureException");
    107         } catch (DataRetrievalFailureException expected) {
    108             assertTrue(true);
    109         }
    110     }
    111
    112     public void testGettersSetters() {
    113         DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
    114         UserDetailsService dao = new MockAuthenticationDaoUserMarissa();
    115         populator.setUserDetailsService(dao);
    116         assertEquals(dao, populator.getUserDetailsService());
    117     }
    118
    119     //~ Inner Classes ==========================================================
    120
    121     private class MockAuthenticationDaoSimulateBackendError
    122         implements UserDetailsService {
    123         public long getRefreshDuration() {
    124             return 0;
    125         }
    126
    127         public UserDetails loadUserByUsername(String username)
    128             throws UsernameNotFoundException, DataAccessException {
    129             throw new DataRetrievalFailureException(
    130                 "This mock simulator is designed to fail");
    131         }
    132     }
    133
    134     private class MockAuthenticationDaoUserMarissa implements UserDetailsService {
    135         public long getRefreshDuration() {
    136             return 0;
    137         }
    138
    139         public UserDetails loadUserByUsername(String username)
    140             throws UsernameNotFoundException, DataAccessException {
    141             if ("marissa".equals(username)) {
    142                 return new User("marissa", "koala", true, true, true, true,
    143                     new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    144                             "ROLE_TWO")});
    145             } else {
    146                 throw new UsernameNotFoundException("Could not find: "
    147                     + username);
    148             }
    149         }
    150     }
    151 }