1 19 20 package com.sslexplorer.jdbc; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertNotNull; 25 import static org.junit.Assert.assertTrue; 26 import static org.junit.Assert.fail; 27 28 import java.util.Arrays ; 29 import java.util.Collections ; 30 import java.util.List ; 31 32 import org.junit.BeforeClass; 33 import org.junit.Test; 34 35 import com.sslexplorer.security.Role; 36 import com.sslexplorer.security.User; 37 import com.sslexplorer.security.UserNotFoundException; 38 import com.sslexplorer.testcontainer.AbstractTest; 39 40 43 public class JDBCUserDatabaseTest extends AbstractTest { 44 45 48 @BeforeClass 49 public static void oneTimeSetUp() throws Exception { 50 setUp(""); 51 } 52 53 57 @Test 58 public void checkInitialState() throws Exception { 59 User[] listAllUsers = getDefaultUserDatabase().listAllUsers("*"); 60 assertEquals("There should only be the one user and he is the super user.", listAllUsers.length, 1); 61 } 62 63 67 @Test 68 public void simpleCreateUser() throws Exception { 69 User user = createAccount("jb", "qwqwqw", "james@3sp.com", "James Robinson"); 70 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 2); 71 deleteAccount(user); 72 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1); 73 } 74 75 79 @Test 80 public void createNUsers() throws Exception { 81 User user1 = createAccount("jb1", "qwqwqw", "james@3sp.com", "James Robinson1"); 82 User user2 = createAccount("jb2", "qwqwqw", "james@3sp.com", "James Robinson2"); 83 User user3 = createAccount("jb3", "qwqwqw", "james@3sp.com", "James Robinson3"); 84 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 4); 85 deleteAccount(user1, user2, user3); 86 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1); 87 } 88 89 93 @Test 94 public void loginSuccessfull() throws Exception { 95 String username = "jb"; 96 String password = "qwqwqw"; 97 User user = createAccount(username, password, "james@3sp.com", "James Robinson"); 98 User adminUser = getDefaultUserDatabase().getAccount(USERNAME); 99 getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD); 100 User loggedONUser = getDefaultUserDatabase().logon(username, password); 101 assertNotNull("There should be a valid user.", loggedONUser); 102 deleteAccount(user); 103 } 104 105 109 @Test 110 public void checkPassword() throws Exception { 111 String username = "jb"; 112 String password = "qwqwqw"; 113 User user = createAccount(username, password, "james@3sp.com", "James Robinson"); 114 User adminUser = getDefaultUserDatabase().getAccount(USERNAME); 115 getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD); 116 assertTrue("There password should be checked successfully.", getDefaultUserDatabase().checkPassword(username, password)); 117 deleteAccount(user); 118 } 119 120 124 @Test 125 public void loginFailed() throws Exception { 126 String username = "jb"; 127 String password = "qwqwqw"; 128 User user = createAccount(username, password, "james@3sp.com", "James Robinson"); 129 User adminUser = getDefaultUserDatabase().getAccount(USERNAME); 130 getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD); 131 132 try { 133 getDefaultUserDatabase().logon("wrong", password); 134 fail(); 136 } catch (Exception e) { 137 assertNotNull("An exception should have been thrown", e); 138 } 139 deleteAccount(user); 140 } 141 142 146 @Test 147 public void checkBadPassword() throws Exception { 148 String username = "jb"; 149 String password = "qwqwqw"; 150 User user = createAccount(username, password, "james@3sp.com", "James Robinson"); 151 User adminUser = getDefaultUserDatabase().getAccount(USERNAME); 152 getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD); 153 assertFalse("There password should be wrong.", getDefaultUserDatabase().checkPassword(username, "pileof")); 154 deleteAccount(user); 155 } 156 157 161 @Test 162 public void updateUserChangeEmail() throws Exception { 163 String email = "james@3sp.com"; 164 String username = "jb"; 165 User user = createAccount(username, "qwqwqw", email, "James Robinson"); 166 assertTrue("The email should be set.", user.getEmail().equals(email)); 167 String newEmail = "jb@3sp.com"; 168 updateAccount(user, newEmail, user.getFullname(), user.getRoles()); 169 user = getAccount(username); 170 assertTrue("The email should be set.", user.getEmail().equals(newEmail)); 171 deleteAccount(user); 172 } 173 174 178 @Test 179 public void cantChangeUsername() throws Exception { 180 String username = "jb"; 181 User user = createAccount(username, "qwqwqw", "ja,es@3sp.com", "James Robninson"); 182 assertTrue("The user name should be set.", user.getPrincipalName().equals(username)); 183 String newUsername = "jimbob"; 184 updateAccount(user, user.getFullname(), user.getEmail(), user.getRoles()); 185 user = getAccount(username); 186 assertTrue("The user name should be set.", !user.getPrincipalName().equals(newUsername)); 187 assertTrue("The user name should be set.", user.getPrincipalName().equals(username)); 188 deleteAccount(user); 189 } 190 191 195 @Test 196 public void updateUserChangeFullName() throws Exception { 197 String fullName = "James Robninson"; 198 String username = "jb"; 199 User user = createAccount(username, "qwqwqw", "ja,es@3sp.com", fullName); 200 assertTrue("The full name should be set.", user.getFullname().equals(fullName)); 201 String newFullName = "James Douglas Robinson"; 202 updateAccount(user, user.getFullname(), newFullName, user.getRoles()); 203 user = getAccount(username); 204 assertTrue("The full name should be set.", user.getFullname().equals(newFullName)); 205 deleteAccount(user); 206 } 207 208 212 @Test(expected = UserNotFoundException.class) 213 public void deleteUnknownUser() throws Exception { 214 String fullName = "James Robninson"; 215 String username = "jb"; 216 User user = createAccount(username, "qwqwqw", "ja,es@3sp.com", fullName); 217 user = getAccount(username); 218 deleteAccount(user, user); 219 fail("Delete user should have thrown an exception"); 220 } 221 222 226 @Test 227 public void createNormalRole() throws Exception { 228 Role[] currentRoles = getDefaultUserDatabase().listAllRoles("*"); 229 int currentNumberOfRoles = currentRoles.length; 230 String roleName = "jb"; 231 Role role = createRole(roleName); 232 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllRoles("*").length, currentNumberOfRoles + 1); 233 deleteRole(role); 234 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllRoles("*").length, currentNumberOfRoles); 235 } 236 237 241 @Test 242 public void assignRolesToUser() throws Exception { 243 String userName = "jb"; 244 Role role = createRole("Group1"); 245 User user = createAccount(userName, "qwqwqw", "james@3sp.com", "James Robninson"); 246 user = updateAccountRoles(user, Collections.singleton(role)); 247 assertEquals("The roles should be the same.", role.getPrincipalName(), user.getRoles()[0].getPrincipalName()); 248 user = updateAccountRoles(user, Collections.<Role>emptyList()); 249 User[] usersInRole = getUserService().getDefaultUserDatabase().getUsersInRole(role); 250 assertEquals("There should be the user in the list.", usersInRole.length, 0); 251 User retrievedUser2 = getUserService().getDefaultUserDatabase().getAccount(user.getPrincipalName()); 252 List <Role> usersRoles2 = Arrays.asList(retrievedUser2.getRoles()); 253 assertTrue("The role should not have any users.", usersRoles2.isEmpty()); 254 deleteAccount(user); 255 deleteRole(role); 256 } 257 258 261 @Test 262 public void listingUsers() throws Exception { 263 User[] currentUsers = getDefaultUserDatabase().listAllUsers("*"); 264 int currentNumberOfUsers = currentUsers.length; 265 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1); 266 User user1 = createAccount("aaaa", "aaaa", "aaaa@3sp.com", "AAAA"); 268 User user2 = createAccount("abbb", "abbb", "abbb@3sp.com", "ABBB"); 269 User user3 = createAccount("aabb", "aabb", "aabb@3sp.com", "AABB"); 270 User user4 = createAccount("aaab", "aaab", "aaab@3sp.com", "AAAB"); 271 User user5 = createAccount("bbbb", "bbbb", "bbbb@3sp.com", "BBBB"); 272 User user6 = createAccount("xaax", "xaax", "xaax@3sp.com", "XAAX"); 273 assertEquals("There should be the seven users.", getDefaultUserDatabase().listAllUsers("*").length, currentNumberOfUsers + 6); 274 assertEquals("There should be the four users.", getDefaultUserDatabase().listAllUsers("a*").length, 4); 275 assertEquals("There should be the three users.", getDefaultUserDatabase().listAllUsers("aa*").length, 3); 276 assertEquals("There should be the two users.", getDefaultUserDatabase().listAllUsers("aaa*").length, 2); 277 assertEquals("There should be the three users.", getDefaultUserDatabase().listAllUsers("*ab*").length, 3); 278 assertEquals("There should be the zero users.", getDefaultUserDatabase().listAllUsers("*z*").length, 0); 279 assertEquals("There should be the one users.", getDefaultUserDatabase().listAllUsers("xa*").length, 1); 280 deleteAccount(user1, user2, user3, user4, user5, user6); 281 assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1); 282 } 283 284 287 @Test 288 public void listingRoles() throws Exception { 289 Role[] currentRoles = getDefaultUserDatabase().listAllRoles("*"); 290 int currentNumberOfRoles = currentRoles.length; 291 assertEquals("There should only be the 1 role 'Users'.", getDefaultUserDatabase().listAllRoles("*").length, 1); 292 Role role1 = createRole("aaaa"); 294 Role role2 = createRole("abbb"); 295 Role role3 = createRole("aabb"); 296 Role role4 = createRole("aaab"); 297 Role role5 = createRole("bbbb"); 298 Role role6 = createRole("xaax"); 299 assertEquals("There should be the seven roles.", getDefaultUserDatabase().listAllRoles("*").length, currentNumberOfRoles + 6); 300 assertEquals("There should be the five roles.", getDefaultUserDatabase().listAllRoles("a*").length, 4); 301 assertEquals("There should be the three roles.", getDefaultUserDatabase().listAllRoles("aa*").length, 3); 302 assertEquals("There should be the two roles.", getDefaultUserDatabase().listAllRoles("aaa*").length, 2); 303 assertEquals("There should be the three roles.", getDefaultUserDatabase().listAllRoles("*ab*").length, 3); 304 assertEquals("There should be the zero roles.", getDefaultUserDatabase().listAllRoles("*z*").length, 0); 305 assertEquals("There should be the one roles.", getDefaultUserDatabase().listAllRoles("xa*").length, 1); 306 deleteRole(role1, role2, role3, role4, role5, role6); 307 assertEquals("There should only be the 1 role users.", getDefaultUserDatabase().listAllRoles("*").length, 1); 308 } 309 } | Popular Tags |