    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.jetty;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.mortbay.http.UserPrincipal;
    21
    22
    23  /***
    24   * Tests {@link JettyAcegiUserRealm}.
    25   *
    26   * @author Ben Alex
    27   * @version $Id: JettyAcegiUserRealmTests.java,v 1.2 2005/11/17 00:56:29 benalex Exp $
    28   */
    29  public class JettyAcegiUserRealmTests extends TestCase {
    30      //~ Instance fields ========================================================
    31
    32      private final String ADAPTER_KEY = "my_key";
    33      private final String REALM_NAME = "Acegi Powered Realm";
    34
    35      //~ Constructors ===========================================================
    36
    37      public JettyAcegiUserRealmTests() {
    38          super();
    39      }
    40
    41      public JettyAcegiUserRealmTests(String arg0) {
    42          super(arg0);
    43      }
    44
    45      //~ Methods ================================================================
    46
    47      public final void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      public static void main(String[] args) {
    52          junit.textui.TestRunner.run(JettyAcegiUserRealmTests.class);
    53      }
    54
    55      public void testAdapterAbortsIfAppContextDoesNotContainAnAuthenticationBean()
    56          throws Exception {
    57          try {
    58              JettyAcegiUserRealm adapter = makeAdapter("adaptertest-invalid.xml");
    59              fail("Should have thrown IllegalArgumentException");
    60          } catch (IllegalArgumentException expected) {
    61              assertEquals("Bean context must contain at least one bean of type AuthenticationManager",
    62                  expected.getMessage());
    63          }
    64      }
    65
    66      public void testAdapterAbortsIfNoAppContextSpecified()
    67          throws Exception {
    68          try {
    69              new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY, null);
    70              fail("Should have thrown IllegalArgumentException");
    71          } catch (IllegalArgumentException expected) {
    72              assertEquals("appContextLocation must be specified",
    73                  expected.getMessage());
    74          }
    75
    76          try {
    77              new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY, "");
    78              fail("Should have thrown IllegalArgumentException");
    79          } catch (IllegalArgumentException expected) {
    80              assertEquals("appContextLocation must be specified",
    81                  expected.getMessage());
    82          }
    83      }
    84
    85      public void testAdapterAbortsIfNoKeySpecified() throws Exception {
    86          try {
    87              new JettyAcegiUserRealm(REALM_NAME, null, "SOME_PATH");
    88              fail("Should have thrown IllegalArgumentException");
    89          } catch (IllegalArgumentException expected) {
    90              assertEquals("key must be specified", expected.getMessage());
    91          }
    92
    93          try {
    94              new JettyAcegiUserRealm(REALM_NAME, "", "SOME_PATH");
    95              fail("Should have thrown IllegalArgumentException");
    96          } catch (IllegalArgumentException expected) {
    97              assertEquals("key must be specified", expected.getMessage());
    98          }
    99      }
    100
    101     public void testAdapterAbortsIfNoRealmNameSpecified()
    102         throws Exception {
    103         try {
    104             new JettyAcegiUserRealm(null, ADAPTER_KEY, "SOME_PATH");
    105             fail("Should have thrown IllegalArgumentException");
    106         } catch (IllegalArgumentException expected) {
    107             assertEquals("realm must be specified", expected.getMessage());
    108         }
    109
    110         try {
    111             new JettyAcegiUserRealm(null, ADAPTER_KEY, "SOME_PATH");
    112             fail("Should have thrown IllegalArgumentException");
    113         } catch (IllegalArgumentException expected) {
    114             assertEquals("realm must be specified", expected.getMessage());
    115         }
    116     }
    117
    118     public void testAdapterAbortsWithIncorrectApplicationContextLocation()
    119         throws Exception {
    120         try {
    121             new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY,
    122                 "SOME_INVALID_LOCATION");
    123             fail("Should have thrown IllegalArgumentException");
    124         } catch (IllegalArgumentException expected) {
    125             assertTrue(expected.getMessage().startsWith("Cannot locate"));
    126         }
    127     }
    128
    129     public void testAdapterIdentifiesTheRealmItManages()
    130         throws Exception {
    131         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    132         assertEquals(REALM_NAME, adapter.getName());
    133     }
    134
    135     public void testAdapterStartsUpSuccess() throws Exception {
    136         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    137         assertTrue(true);
    138     }
    139
    140     public void testAuthenticationFailsForIncorrectPassword()
    141         throws Exception {
    142         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    143         assertEquals(null, adapter.authenticate("marissa", "kangaroo", null));
    144     }
    145
    146     public void testAuthenticationFailsForIncorrectUserName()
    147         throws Exception {
    148         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    149         assertEquals(null, adapter.authenticate("melissa", "koala", null));
    150     }
    151
    152     public void testAuthenticationSuccess() throws Exception {
    153         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    154         UserPrincipal result = adapter.authenticate("marissa", "koala", null);
    155
    156         if (!(result instanceof JettyAcegiUserToken)) {
    157             fail("Should have returned JettyAcegiUserToken");
    158         }
    159
    160         JettyAcegiUserToken castResult = (JettyAcegiUserToken) result;
    161         assertEquals("marissa", castResult.getPrincipal());
    162         assertEquals("koala", castResult.getCredentials());
    163         assertEquals("ROLE_TELLER",
    164             castResult.getAuthorities()[0].getAuthority());
    165         assertEquals("ROLE_SUPERVISOR",
    166             castResult.getAuthorities()[1].getAuthority());
    167         assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
    168     }
    169
    170     public void testAuthenticationWithNullPasswordHandledGracefully()
    171         throws Exception {
    172         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    173         assertEquals(null, adapter.authenticate("marissa", null, null));
    174     }
    175
    176     public void testAuthenticationWithNullUserNameHandledGracefully()
    177         throws Exception {
    178         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    179         assertEquals(null, adapter.authenticate(null, "koala", null));
    180     }
    181
    182     public void testDisassociateImplemented() throws Exception {
    183         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    184         adapter.disassociate(new MockUserPrincipal());
    185         assertTrue(true);
    186     }
    187
    188     public void testGetAuthenticationManager() throws Exception {
    189         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    190         assertTrue(adapter.getAuthenticationManager() != null);
    191     }
    192
    193     public void testLogoutImplemented() throws Exception {
    194         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    195         adapter.logout(new MockUserPrincipal());
    196         assertTrue(true);
    197     }
    198
    199     public void testNoArgsConstructor() {
    200         try {
    201             new JettyAcegiUserRealm();
    202             fail("Should have thrown IllegalArgumentException");
    203         } catch (IllegalArgumentException expected) {
    204             assertTrue(true);
    205         }
    206     }
    207
    208     public void testPopRoleImplemented() throws Exception {
    209         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    210         MockUserPrincipal user = new MockUserPrincipal();
    211         assertEquals(user, adapter.popRole(user));
    212     }
    213
    214     public void testPushRoleImplemented() throws Exception {
    215         JettyAcegiUserRealm adapter = makeAdapter("adaptertest-valid.xml");
    216         MockUserPrincipal user = new MockUserPrincipal();
    217         assertEquals(user, adapter.pushRole(user, "SOME_ROLE"));
    218     }
    219
    220     private JettyAcegiUserRealm makeAdapter(String fileName)
    221         throws Exception {
    222         String useFile = "org/acegisecurity/adapters/" + fileName;
    223
    224         return new JettyAcegiUserRealm(REALM_NAME, ADAPTER_KEY, useFile);
    225     }
    226
    227     //~ Inner Classes ==========================================================
    228
    229     private class MockUserPrincipal implements UserPrincipal {
    230         public boolean isAuthenticated() {
    231             throw new UnsupportedOperationException(
    232                 "mock method not implemented");
    233         }
    234
    235         public String getName() {
    236             throw new UnsupportedOperationException(
    237                 "mock method not implemented");
    238         }
    239
    240         public boolean isUserInRole(String arg0) {
    241             throw new UnsupportedOperationException(
    242                 "mock method not implemented");
    243         }
    244     }
    245 }