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.userdetails.jdbc;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.PopulatedDatabase;
21  import org.acegisecurity.userdetails.UserDetails;
22  import org.acegisecurity.userdetails.UsernameNotFoundException;
23  import org.acegisecurity.userdetails.jdbc.JdbcDaoImpl;
24  
25  import org.springframework.jdbc.object.MappingSqlQuery;
26  
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  
30  
31  /***
32   * Tests {@link JdbcDaoImpl}.
33   *
34   * @author Ben Alex
35   * @version $Id: JdbcDaoTests.java,v 1.1 2005/11/29 13:10:11 benalex Exp $
36   */
37  public class JdbcDaoTests extends TestCase {
38      //~ Constructors ===========================================================
39  
40      public JdbcDaoTests() {
41          super();
42      }
43  
44      public JdbcDaoTests(String arg0) {
45          super(arg0);
46      }
47  
48      //~ Methods ================================================================
49  
50      public final void setUp() throws Exception {
51          super.setUp();
52      }
53  
54      public static void main(String[] args) {
55          junit.textui.TestRunner.run(JdbcDaoTests.class);
56      }
57  
58      public void testCheckDaoAccessUserSuccess() throws Exception {
59          JdbcDaoImpl dao = makePopulatedJdbcDao();
60          UserDetails user = dao.loadUserByUsername("marissa");
61          assertEquals("marissa", user.getUsername());
62          assertEquals("koala", user.getPassword());
63          assertTrue(user.isEnabled());
64          assertEquals("ROLE_TELLER", user.getAuthorities()[1].getAuthority());
65          assertEquals("ROLE_SUPERVISOR", user.getAuthorities()[0].getAuthority());
66          assertEquals(2, user.getAuthorities().length);
67      }
68  
69      public void testCheckDaoOnlyReturnsGrantedAuthoritiesGrantedToUser()
70          throws Exception {
71          JdbcDaoImpl dao = makePopulatedJdbcDao();
72          UserDetails user = dao.loadUserByUsername("scott");
73          assertEquals("ROLE_TELLER", user.getAuthorities()[0].getAuthority());
74          assertEquals(1, user.getAuthorities().length);
75      }
76  
77      public void testCheckDaoReturnsCorrectDisabledProperty()
78          throws Exception {
79          JdbcDaoImpl dao = makePopulatedJdbcDao();
80          UserDetails user = dao.loadUserByUsername("peter");
81          assertTrue(!user.isEnabled());
82      }
83  
84      public void testGettersSetters() {
85          JdbcDaoImpl dao = new JdbcDaoImpl();
86          dao.setAuthoritiesByUsernameQuery("SELECT * FROM FOO");
87          assertEquals("SELECT * FROM FOO", dao.getAuthoritiesByUsernameQuery());
88  
89          dao.setUsersByUsernameQuery("SELECT USERS FROM FOO");
90          assertEquals("SELECT USERS FROM FOO", dao.getUsersByUsernameQuery());
91      }
92  
93      public void testLookupFailsIfUserHasNoGrantedAuthorities()
94          throws Exception {
95          JdbcDaoImpl dao = makePopulatedJdbcDao();
96  
97          try {
98              dao.loadUserByUsername("cooper");
99              fail("Should have thrown UsernameNotFoundException");
100         } catch (UsernameNotFoundException expected) {
101             assertEquals("User has no GrantedAuthority", expected.getMessage());
102         }
103     }
104 
105     public void testLookupFailsWithWrongUsername() throws Exception {
106         JdbcDaoImpl dao = makePopulatedJdbcDao();
107 
108         try {
109             dao.loadUserByUsername("UNKNOWN_USER");
110             fail("Should have thrown UsernameNotFoundException");
111         } catch (UsernameNotFoundException expected) {
112             assertTrue(true);
113         }
114     }
115 
116     public void testLookupSuccessWithMixedCase() throws Exception {
117         JdbcDaoImpl dao = makePopulatedJdbcDao();
118         assertEquals("koala", dao.loadUserByUsername("MaRiSSA").getPassword());
119         assertEquals("wombat", dao.loadUserByUsername("ScOTt").getPassword());
120     }
121 
122     public void testRolePrefixWorks() throws Exception {
123         JdbcDaoImpl dao = makePopulatedJdbcDaoWithRolePrefix();
124         assertEquals("ARBITRARY_PREFIX_", dao.getRolePrefix());
125 
126         UserDetails user = dao.loadUserByUsername("marissa");
127         assertEquals("marissa", user.getUsername());
128         assertEquals("ARBITRARY_PREFIX_ROLE_TELLER",
129             user.getAuthorities()[1].getAuthority());
130         assertEquals("ARBITRARY_PREFIX_ROLE_SUPERVISOR",
131             user.getAuthorities()[0].getAuthority());
132         assertEquals(2, user.getAuthorities().length);
133     }
134 
135     public void testStartupFailsIfDataSourceNotSet() throws Exception {
136         JdbcDaoImpl dao = new JdbcDaoImpl();
137 
138         try {
139             dao.afterPropertiesSet();
140             fail("Should have thrown IllegalArgumentException");
141         } catch (IllegalArgumentException expected) {
142             assertTrue(true);
143         }
144     }
145 
146     public void testStartupFailsIfUserMapSetToNull() throws Exception {
147         JdbcDaoImpl dao = new JdbcDaoImpl();
148 
149         try {
150             dao.setDataSource(null);
151             dao.afterPropertiesSet();
152             fail("Should have thrown IllegalArgumentException");
153         } catch (IllegalArgumentException expected) {
154             assertTrue(true);
155         }
156     }
157 
158     private JdbcDaoImpl makePopulatedJdbcDao() throws Exception {
159         JdbcDaoImpl dao = new JdbcDaoImpl();
160         dao.setDataSource(PopulatedDatabase.getDataSource());
161         dao.afterPropertiesSet();
162 
163         return dao;
164     }
165 
166     private JdbcDaoImpl makePopulatedJdbcDaoWithRolePrefix()
167         throws Exception {
168         JdbcDaoImpl dao = new JdbcDaoImpl();
169         dao.setDataSource(PopulatedDatabase.getDataSource());
170         dao.setRolePrefix("ARBITRARY_PREFIX_");
171         dao.afterPropertiesSet();
172 
173         return dao;
174     }
175 
176     //~ Inner Classes ==========================================================
177 
178     private class MockMappingSqlQuery extends MappingSqlQuery {
179         protected Object mapRow(ResultSet arg0, int arg1)
180             throws SQLException {
181             return null;
182         }
183     }
184 }