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.rememberme;
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  
26  
27  /***
28   * Tests {@link RememberMeAuthenticationProvider}.
29   *
30   * @author Ben Alex
31   * @version $Id: RememberMeAuthenticationProviderTests.java,v 1.4 2005/11/30 01:23:34 benalex Exp $
32   */
33  public class RememberMeAuthenticationProviderTests extends TestCase {
34      //~ Constructors ===========================================================
35  
36      public RememberMeAuthenticationProviderTests() {
37          super();
38      }
39  
40      public RememberMeAuthenticationProviderTests(String arg0) {
41          super(arg0);
42      }
43  
44      //~ Methods ================================================================
45  
46      public final void setUp() throws Exception {
47          super.setUp();
48      }
49  
50      public static void main(String[] args) {
51          junit.textui.TestRunner.run(RememberMeAuthenticationProviderTests.class);
52      }
53  
54      public void testDetectsAnInvalidKey() throws Exception {
55          RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
56          aap.setKey("qwerty");
57  
58          RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("WRONG_KEY",
59                  "Test",
60                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
61                          "ROLE_TWO")});
62  
63          try {
64              Authentication result = aap.authenticate(token);
65              fail("Should have thrown BadCredentialsException");
66          } catch (BadCredentialsException expected) {
67              assertEquals("The presented RememberMeAuthenticationToken does not contain the expected key",
68                  expected.getMessage());
69          }
70      }
71  
72      public void testDetectsMissingKey() throws Exception {
73          RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
74  
75          try {
76              aap.afterPropertiesSet();
77              fail("Should have thrown IllegalArgumentException");
78          } catch (IllegalArgumentException expected) {
79              assertTrue(true);
80          }
81      }
82  
83      public void testGettersSetters() throws Exception {
84          RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
85          aap.setKey("qwerty");
86          aap.afterPropertiesSet();
87          assertEquals("qwerty", aap.getKey());
88      }
89  
90      public void testIgnoresClassesItDoesNotSupport() throws Exception {
91          RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
92          aap.setKey("qwerty");
93  
94          TestingAuthenticationToken token = new TestingAuthenticationToken("user",
95                  "password",
96                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A")});
97          assertFalse(aap.supports(TestingAuthenticationToken.class));
98  
99          // Try it anyway
100         assertNull(aap.authenticate(token));
101     }
102 
103     public void testNormalOperation() throws Exception {
104         RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
105         aap.setKey("qwerty");
106 
107         RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("qwerty",
108                 "Test",
109                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
110                         "ROLE_TWO")});
111 
112         Authentication result = aap.authenticate(token);
113 
114         assertEquals(result, token);
115     }
116 
117     public void testSupports() {
118         RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
119         assertTrue(aap.supports(RememberMeAuthenticationToken.class));
120         assertFalse(aap.supports(TestingAuthenticationToken.class));
121     }
122 }