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