1 19 20 package org.apache.james.userrepository; 21 22 import org.apache.avalon.framework.container.ContainerUtil; 23 import org.apache.james.services.User; 24 import org.apache.james.services.UsersRepository; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 29 import junit.framework.TestCase; 30 31 34 public class MockUsersRepositoryTest extends TestCase { 35 36 39 protected UsersRepository usersRepository; 40 41 47 protected UsersRepository getUsersRepository() throws Exception { 48 return new MockUsersRepository(); 49 } 50 51 public void testUsersRepositoryEmpty() { 52 assertEquals("users repository not empty", 0, usersRepository.countUsers()); 53 assertFalse("users repository not empty", usersRepository.list().hasNext()); 54 } 55 56 public void testAddUserOnce() { 57 boolean res = usersRepository.addUser("username", "password"); 58 assertTrue("User not added", res); 59 res = usersRepository.addUser("username", "password2"); 60 assertFalse("User added twice!", res); 61 try { 62 usersRepository.addUser("username2", (Object ) "password2"); 63 assertTrue(usersRepository.contains("username2")); 64 User u = new DefaultJamesUser("username3","SHA","password3"); 65 usersRepository.addUser(u); 66 assertTrue(usersRepository.contains("username3")); 67 } catch (UnsupportedOperationException e) { 68 69 } 70 71 try { 72 usersRepository.addUser("username2", new Object ()); 73 fail("adduser should throw an exception if a non string is passed"); 74 } catch (Exception e) { 75 76 } 77 78 } 79 80 public void testUserAddedIsFound() { 81 boolean res = usersRepository.addUser("username", "password"); 82 assertTrue("User not added", res); 83 User user = usersRepository.getUserByName("username"); 84 assertNotNull(user); 85 assertEquals("username does not match", user.getUserName(), "username"); 86 assertTrue("user not contained in the repository", usersRepository.contains("username")); 87 try { 88 assertTrue("case insensitive user not found in the repository", usersRepository.containsCaseInsensitive("userName")); 89 } catch (UnsupportedOperationException e) { 90 } 92 93 User u = usersRepository.getUserByName("uSERNAMe"); 94 assertNull("found the user searching for a different case!", u); 95 96 String realname = usersRepository.getRealName("uSERNAMe"); 97 assertNull("name is not null", realname); 98 } 100 101 public void testUserListing() { 102 ArrayList keys = new ArrayList (3); 103 keys.add("username1"); 104 keys.add("username2"); 105 keys.add("username3"); 106 for (Iterator i = keys.iterator(); i.hasNext(); ) { 107 String username = (String ) i.next(); 108 boolean res = usersRepository.addUser(username, username); 109 assertTrue("User "+username+" not added", res); 110 } 111 assertEquals("Wrong number of users found", keys.size(), usersRepository.countUsers()); 112 113 ArrayList check = new ArrayList (keys); 115 for (Iterator i = usersRepository.list(); i.hasNext(); ) { 116 String username = (String ) i.next(); 117 if (getPasswordsEnabled()) { 118 assertTrue(usersRepository.test(username, username)); 119 User u = usersRepository.getUserByName(username); 120 u.setPassword("newpass"); 121 assertTrue(usersRepository.updateUser(u)); 122 } 123 assertTrue(check.contains(username)); 124 check.remove(username); 125 } 126 assertEquals("Some user has not be found", 0, check.size()); 127 } 128 129 public void testUserPassword() { 130 assertTrue("user not added", usersRepository.addUser("username","password")); 131 assertEquals("didn't accept the correct password ", usersRepository.test("username", "password"), getPasswordsEnabled()); 132 assertFalse("accepted the wrong password #1", usersRepository.test("username", "password2")); 133 assertFalse("accepted the wrong password #2", usersRepository.test("username2", "password")); 134 assertFalse("accepted the wrong password #3", usersRepository.test("username", "Password")); 135 assertFalse("accepted the wrong password #4", usersRepository.test("username", "passwords")); 136 assertFalse("accepted the wrong password #5", usersRepository.test("userName", "password")); 137 } 138 139 protected boolean getPasswordsEnabled() { 140 return true; 141 } 142 143 public void testUserAddRemoveCycle() { 144 assertFalse("accepted login when no user existed", usersRepository.test("username", "password")); 145 try { 146 usersRepository.removeUser("username"); 147 } catch (Exception e) { 150 151 } 152 assertTrue("user not added", usersRepository.addUser("username","password")); 153 assertEquals("didn't accept the correct password", usersRepository.test("username", "password"),getPasswordsEnabled()); 154 User user = usersRepository.getUserByName("username"); 155 user.setPassword("newpass"); 156 try { 157 assertTrue("user not updated", usersRepository.updateUser(user)); 158 assertEquals("new password accepted", usersRepository.test("username", "newpass"), getPasswordsEnabled()); 159 assertFalse("old password rejected", usersRepository.test("username", "password")); 160 } catch (UnsupportedOperationException e) { 161 assertFalse(getPasswordsEnabled()); 163 } 164 try { 165 usersRepository.removeUser("username"); 166 } catch (Exception e) { 167 fail("removing the user failed!"); 168 } 169 assertFalse("user not existing", usersRepository.contains("username")); 170 assertFalse("new password rejected", usersRepository.test("username", "newpass")); 171 assertFalse("updated a non existing user: should fail!", usersRepository.updateUser(user)); 172 } 173 174 175 public void testCaseInsensitivesMethods() { 176 assertTrue("User not added", usersRepository.addUser("userName", "password")); 177 try { 178 assertTrue(usersRepository.containsCaseInsensitive("usERname")); 179 assertNotNull(usersRepository.getUserByNameCaseInsensitive("userNAMe")); 180 } catch (UnsupportedOperationException e) { 181 } 183 assertEquals(usersRepository.addUser("USERNAME", "password"), getAllowMultipleUsersWithDifferentCases()); 184 185 } 186 187 188 191 protected boolean getCheckCase() { 192 return true; 193 } 194 195 protected boolean getAllowMultipleUsersWithDifferentCases() { 196 return getCheckCase(); 197 } 198 199 200 203 protected void setUp() throws Exception { 204 super.setUp(); 205 this.usersRepository = getUsersRepository(); 206 } 207 208 211 protected void tearDown() throws Exception { 212 disposeUsersRepository(); 213 super.tearDown(); 214 } 215 216 217 220 protected void disposeUsersRepository() { 221 ContainerUtil.dispose(this.usersRepository); 222 } 223 224 } 225 | Popular Tags |