1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.services.security;
18  
19  import java.util.Iterator;
20  
21  // Junit imports
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.turbine.services.TurbineServices;
26  import org.apache.turbine.util.TurbineConfig;
27  import org.apache.turbine.util.StringUtils;
28  
29  // Jetspeed imports
30  import org.apache.jetspeed.test.JetspeedTestCase;
31  import org.apache.jetspeed.services.JetspeedUserManagement;
32  import org.apache.jetspeed.om.security.JetspeedUser;
33  import org.apache.jetspeed.om.security.JetspeedUserFactory;
34  import org.apache.jetspeed.om.security.UserNamePrincipal;
35  
36  /***
37   * Unit test for UserManagement interface
38   * 
39   * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
40   * @version $Id: TestUserManagement.java,v 1.1 2004/04/07 22:02:43 jford Exp $
41   */
42  
43  public class TestUserManagement extends JetspeedTestCase {    
44      
45      /***
46       * Defines the testcase name for JUnit.
47       *
48       * @param name the testcase's name.
49       */
50      public TestUserManagement( String name ) {
51          super( name );
52      }
53      
54      /***
55       * Start the tests.
56       *
57       * @param args the arguments. Not used
58       */
59      public static void main(String args[]) 
60      {
61          junit.awtui.TestRunner.main( new String[] { TestUserManagement.class.getName() } );
62      }
63   
64      public void setup() 
65      {
66          System.out.println("Setup: Testing Turbine User Management");         
67      }
68  
69      /***
70       * Creates the test suite.
71       *
72       * @return a test suite (<code>TestSuite</code>) that includes all methods
73       *         starting with "test"
74       */
75      public static Test suite() 
76      {
77          // All methods starting with "test" will be executed in the test suite.
78          return new TestSuite( TestUserManagement.class );
79      }
80  
81  
82      /***
83       * Tests getUser method
84       * @throws Exception
85       */
86  
87      public void testGetUser() throws Exception 
88      {
89          setup();
90  
91          UserManagement service = getService();
92          JetspeedUser user = null;
93          try
94          {
95              user = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
96          }
97          catch (Exception e)
98          {
99              fail(StringUtils.stackTrace(e));
100         }
101         assertNotNull(user);
102         assertTrue(user.getUserId().equals("2"));
103         assertTrue(user.getUserName().equals("turbine"));
104         assertTrue(user.getFirstName().equals("Tommy"));
105         assertTrue(user.getLastName().equals("Turbine"));
106         assertTrue(user.getEmail().equals("tommy@jakarta-jetspeed.com"));
107         assertTrue(user.getConfirmed().equals("CONFIRMED"));
108         assertTrue(user.getDisabled() == false);
109         subtestTemp(user);
110 
111         try
112         {
113             JetspeedUserManagement.getUser(new UserNamePrincipal("nobody"));
114         }
115         catch (Exception e)
116         {
117             System.out.println("CAUGHT GETUSER EXCEPTION: " + e.toString());
118             assertTrue(e instanceof UnknownUserException);
119         }
120 
121         System.out.println("Completed getUser Test OK ");
122 
123     }
124 
125     /***
126      * Tests getUsers method
127      * @throws Exception
128      */
129 
130     public void testGetUsers() throws Exception 
131     {
132         setup();
133 
134         UserManagement service = getService();
135         JetspeedUser user = null;
136 
137         try
138         {
139             Iterator users = JetspeedUserManagement.getUsers();
140             while (users.hasNext())
141             {
142                 user = (JetspeedUser)users.next();
143                 //System.out.println("user name = " + user.getUserName());
144                 if (user.getUserName().equals("turbine"))
145                 {
146                     assertTrue(user.getUserId().equals("2"));
147                     assertTrue(user.getLastName().equals("Turbine"));
148                 }
149                 if (user.getUserName().equals("admin"))
150                 {
151                     assertTrue(user.getUserId().equals("1"));
152                     assertTrue(user.getLastName().equals("Admin"));
153                 }
154             }
155         }
156         catch (Exception e)
157         {
158             fail(StringUtils.stackTrace(e));
159         }
160 
161         System.out.println("Completed getUsers Test OK ");
162 
163     }
164 
165 
166     static final String TEMP_LAST_NAME = "UnitTestLastName";
167 
168     /***
169      * Tests saveUser method
170      * @throws Exception
171      */
172 
173     public void testSaveUser() throws Exception 
174     {
175         setup();
176 
177         UserManagement service = getService();
178         JetspeedUser user = null;
179 
180         try
181         {
182             // test updating and retrieving some fields
183             user = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
184             String lastName = user.getLastName();
185             user.setLastName(TEMP_LAST_NAME);
186             user.setDisabled(true);
187 
188             JetspeedUserManagement.saveUser(user);
189             JetspeedUser user2 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
190             assertTrue(user2.getLastName().equals(TEMP_LAST_NAME));
191             assertTrue(user2.getDisabled() == true);
192 
193             user2.setLastName(lastName);
194             user2.setDisabled(false);
195             JetspeedUserManagement.saveUser(user2);
196             JetspeedUser user3 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
197             assertTrue(user3.getLastName().equals(lastName));
198             assertTrue(user3.getDisabled() == false);
199 
200             // test set/get Perm
201             user3.setPerm("unittest", "test");
202             JetspeedUserManagement.saveUser(user3);
203             JetspeedUser user4 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
204             String unitTestValue = (String)user4.getPerm("unittest");
205             assertTrue(unitTestValue.equals("test"));
206             user4.setPerm("unittest", null);
207             JetspeedUserManagement.saveUser(user4);            
208 
209             // try saving to an non-existing user
210             try
211             {
212                 JetspeedUser user5 = JetspeedUserFactory.getInstance();
213                 user5.setLastName("junk");
214                 JetspeedUserManagement.saveUser(user5);       
215             }
216             catch (Exception e)
217             {
218                 assertTrue(e instanceof UnknownUserException);
219             }
220     
221             // test dup on unique username key
222             JetspeedUser user6 = JetspeedUserManagement.getUser(new UserNamePrincipal("turbine"));
223             user6.setUserName("admin");
224             try
225             {
226                 JetspeedUserManagement.saveUser(user6);
227             }
228             catch (Exception e)
229             {
230                 System.out.println("CAUGHT UNIQUE EXCEPTION: " + e.toString());
231                 assertTrue(e instanceof UserException);
232             }
233 
234         }
235         catch (Exception e)
236         {
237             fail(StringUtils.stackTrace(e));
238         }
239 
240         System.out.println("Completed saveUser Test OK ");
241 
242     }
243 
244     /***
245      * Tests addUser method
246      * @throws Exception
247      */
248 
249     public void testAddUser() throws Exception 
250     {
251         setup();
252 
253         UserManagement service = getService();
254 
255         try
256         {
257             // test updating and retrieving some fields
258             JetspeedUser user = JetspeedUserFactory.getInstance();
259             user.setUserName("busby");
260             user.setPassword("woof");
261             user.setLastName("Taylor");
262             user.setFirstName("Busby");
263             user.setEmail("busby@jakarta-jetspeed.com");
264             user.setDisabled(false);
265             user.setConfirmed("NO");
266             user.setPerm("dogfood", "alpo");
267             JetspeedUserManagement.addUser(user);
268 
269             JetspeedUser user2 = JetspeedUserManagement.getUser(new UserNamePrincipal("busby"));
270             assertTrue(user2.getUserName().equals("busby"));
271             assertTrue(user2.getLastName().equals("Taylor"));
272             assertTrue(user2.getFirstName().equals("Busby"));
273             assertTrue(user2.getEmail().equals("busby@jakarta-jetspeed.com"));
274             assertTrue(user2.getConfirmed().equals("NO"));
275             String dogfood = (String)user2.getPerm("dogfood");
276             assertTrue(dogfood.equals("alpo"));
277             assertTrue(user2.getDisabled() == false);
278             assertTrue(user2.getUserId().equals(user.getUserId()));
279 
280             // test adding an existing user
281             JetspeedUser user3 = JetspeedUserFactory.getInstance();
282             user3.setUserName("busby");
283             try
284             {
285                 JetspeedUserManagement.addUser(user3);
286             }
287             catch (Exception e)
288             {
289                 System.out.println("CAUGHT UNIQUE EXCEPTION: " + e.toString());
290                 assertTrue(e instanceof NotUniqueUserException);
291             }
292 
293 
294         }
295         catch (Exception e)
296         {
297             fail(StringUtils.stackTrace(e));
298         }
299 
300         System.out.println("Completed saveUser Test OK ");
301 
302     }
303 
304     /***
305      * Tests removeUser method
306      * @throws Exception
307      */
308 
309     public void testRemoveUser() throws Exception 
310     {
311         setup();
312 
313         UserManagement service = getService();
314 
315         try
316         {
317             // depending on testAddUser running first!
318             JetspeedUserManagement.removeUser(new UserNamePrincipal("busby"));
319 
320             try
321             {
322                 JetspeedUserManagement.removeUser(new UserNamePrincipal("nobody"));
323             }
324             catch (Exception e)
325             {
326                 System.out.println("CAUGHT REMOVE EXCEPTION: " + e.toString());
327                 assertTrue(e instanceof UnknownUserException);
328             }
329 
330             try
331             {
332                 JetspeedUserManagement.getUser(new UserNamePrincipal("busby"));
333             }
334             catch (Exception e)
335             {
336                 System.out.println("CAUGHT REMOVE EXCEPTION: " + e.toString());
337                 assertTrue(e instanceof UnknownUserException);
338             }
339 
340 
341         }
342         catch (Exception e)
343         {
344             fail(StringUtils.stackTrace(e));
345         }
346 
347         System.out.println("Completed saveUser Test OK ");
348 
349     }
350 
351     private void subtestTemp(JetspeedUser user)
352     {
353         user.setTemp("test", new Integer(9));
354         Integer test = (Integer)user.getTemp("test");
355         assertTrue(test.intValue() == 9);
356     }
357 
358   /*
359     Configuration object to run Turbine outside a servlet container
360     ( uses turbine.properties )
361     */
362     private static TurbineConfig config = null;
363     
364     /***
365     Sets up TurbineConfig using the system property:
366     <pre>turbine.properties</pre>
367     */
368     static
369     {
370         try
371         {
372             config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
373             config.init();
374         }
375         catch (Exception e)
376         {
377             fail(StringUtils.stackTrace(e));
378         }
379     }
380 
381     private static UserManagement getService()
382     {
383         return (UserManagement)TurbineServices
384                 .getInstance()
385                 .getService(UserManagement.SERVICE_NAME);
386     }
387 
388 }
389 
390 
391 
392