    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.runas;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.Authentication;
    21  import org.acegisecurity.BadCredentialsException;
    22  import org.acegisecurity.GrantedAuthority;
    23  import org.acegisecurity.GrantedAuthorityImpl;
    24  import org.acegisecurity.providers.TestingAuthenticationToken;
    25  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
    26
    27
    28  /***
    29   * Tests {@link RunAsImplAuthenticationProvider}.
    30   */
    31  public class RunAsImplAuthenticationProviderTests extends TestCase {
    32      //~ Constructors ===========================================================
    33
    34      public RunAsImplAuthenticationProviderTests() {
    35          super();
    36      }
    37
    38      public RunAsImplAuthenticationProviderTests(String arg0) {
    39          super(arg0);
    40      }
    41
    42      //~ Methods ================================================================
    43
    44      public static void main(String[] args) {
    45          junit.textui.TestRunner.run(RunAsImplAuthenticationProviderTests.class);
    46      }
    47
    48      public final void setUp() throws Exception {
    49          super.setUp();
    50      }
    51
    52      public void testAuthenticationFailDueToWrongKey() {
    53          RunAsUserToken token = new RunAsUserToken("WRONG_PASSWORD", "Test",
    54                  "Password",
    55                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    56                          "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
    57          RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
    58          provider.setKey("hello_world");
    59
    60          try {
    61              provider.authenticate(token);
    62              fail("Should have thrown BadCredentialsException");
    63          } catch (BadCredentialsException expected) {
    64              assertTrue(true);
    65          }
    66      }
    67
    68      public void testAuthenticationSuccess() {
    69          RunAsUserToken token = new RunAsUserToken("my_password", "Test",
    70                  "Password",
    71                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    72                          "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
    73          RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
    74          provider.setKey("my_password");
    75
    76          Authentication result = provider.authenticate(token);
    77
    78          if (!(result instanceof RunAsUserToken)) {
    79              fail("Should have returned RunAsUserToken");
    80          }
    81
    82          RunAsUserToken resultCast = (RunAsUserToken) result;
    83          assertEquals("my_password".hashCode(), resultCast.getKeyHash());
    84      }
    85
    86      public void testStartupFailsIfNoKey() throws Exception {
    87          RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
    88
    89          try {
    90              provider.afterPropertiesSet();
    91              fail("Should have thrown IllegalArgumentException");
    92          } catch (IllegalArgumentException expected) {
    93              assertTrue(true);
    94          }
    95      }
    96
    97      public void testStartupSuccess() throws Exception {
    98          RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
    99          provider.setKey("hello_world");
    100         assertEquals("hello_world", provider.getKey());
    101         provider.afterPropertiesSet();
    102         assertTrue(true);
    103     }
    104
    105     public void testSupports() {
    106         RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
    107         assertTrue(provider.supports(RunAsUserToken.class));
    108         assertTrue(!provider.supports(TestingAuthenticationToken.class));
    109     }
    110 }