    1   /* Copyright 2004 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.adapters.cas;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.MockAuthenticationManager;
    21
    22  import org.springframework.mock.web.MockHttpServletRequest;
    23
    24
    25  /***
    26   * Tests {@link CasPasswordHandler}.
    27   *
    28   * @author Ben Alex
    29   * @version $Id: CasPasswordHandlerTests.java,v 1.3 2005/11/17 00:56:09 benalex Exp $
    30   */
    31  public class CasPasswordHandlerTests extends TestCase {
    32      //~ Constructors ===========================================================
    33
    34      public CasPasswordHandlerTests() {
    35          super();
    36      }
    37
    38      public CasPasswordHandlerTests(String arg0) {
    39          super(arg0);
    40      }
    41
    42      //~ Methods ================================================================
    43
    44      public final void setUp() throws Exception {
    45          super.setUp();
    46      }
    47
    48      public static void main(String[] args) {
    49          junit.textui.TestRunner.run(CasPasswordHandlerTests.class);
    50      }
    51
    52      public void testDeniesAccessWhenAuthenticationManagerThrowsException()
    53          throws Exception {
    54          CasPasswordHandler handler = new CasPasswordHandler();
    55          handler.setAuthenticationManager(new MockAuthenticationManager(false));
    56          handler.afterPropertiesSet();
    57
    58          assertFalse(handler.authenticate(new MockHttpServletRequest(),
    59                  "username", "password"));
    60      }
    61
    62      public void testDetectsEmptyAuthenticationManager()
    63          throws Exception {
    64          CasPasswordHandler handler = new CasPasswordHandler();
    65
    66          try {
    67              handler.afterPropertiesSet();
    68              fail("Should have thrown IllegalArgumentException");
    69          } catch (IllegalArgumentException expected) {
    70              assertEquals("An AuthenticationManager is required",
    71                  expected.getMessage());
    72          }
    73      }
    74
    75      public void testGettersSetters() {
    76          CasPasswordHandler handler = new CasPasswordHandler();
    77          handler.setAuthenticationManager(new MockAuthenticationManager(false));
    78          assertTrue(handler.getAuthenticationManager() != null);
    79      }
    80
    81      public void testGracefullyHandlesEmptyUsernamesAndPassword()
    82          throws Exception {
    83          CasPasswordHandler handler = new CasPasswordHandler();
    84          handler.setAuthenticationManager(new MockAuthenticationManager(true));
    85          handler.afterPropertiesSet();
    86
    87          // If empty or null username we return false
    88          assertFalse(handler.authenticate(new MockHttpServletRequest(), "",
    89                  "password"));
    90          assertFalse(handler.authenticate(new MockHttpServletRequest(), null,
    91                  "password"));
    92
    93          // We authenticate with null passwords (they might not have one)
    94          assertTrue(handler.authenticate(new MockHttpServletRequest(), "user",
    95                  null));
    96          assertTrue(handler.authenticate(new MockHttpServletRequest(), "user", ""));
    97      }
    98
    99      public void testNormalOperation() throws Exception {
    100         CasPasswordHandler handler = new CasPasswordHandler();
    101         handler.setAuthenticationManager(new MockAuthenticationManager(true));
    102         handler.afterPropertiesSet();
    103
    104         assertTrue(handler.authenticate(new MockHttpServletRequest(),
    105                 "username", "password"));
    106     }
    107 }