1 19 20 package com.sslexplorer.testcontainer; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertTrue; 24 25 import java.io.File ; 26 import java.net.InetAddress ; 27 import java.util.Collection ; 28 import java.util.List ; 29 30 import org.junit.AfterClass; 31 import org.junit.Test; 32 33 import com.sslexplorer.boot.Context; 34 import com.sslexplorer.core.UserDatabaseManager; 35 import com.sslexplorer.policyframework.Policy; 36 import com.sslexplorer.policyframework.PolicyDatabase; 37 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 38 import com.sslexplorer.policyframework.PolicyUtil; 39 import com.sslexplorer.properties.Property; 40 import com.sslexplorer.properties.impl.realms.RealmKey; 41 import com.sslexplorer.realms.Realm; 42 import com.sslexplorer.security.Role; 43 import com.sslexplorer.security.SessionInfo; 44 import com.sslexplorer.security.User; 45 import com.sslexplorer.security.UserDatabase; 46 47 49 public abstract class AbstractTest{ 50 private static final String TEST_PROPERTY = "sslexplorer.testing"; 51 private static final String DEV_CONFIG_PROPERTY = "sslexplorer.useDevConfig"; 52 private static final String DEV_EXTENSIONS_PROPERTY = "sslexplorer.devExtensions"; 53 private static final String DATABASE_TYPE = "builtIn"; 54 protected static String USERNAME = "testAdministrator"; 55 protected static String PASSWORD = "newPassword"; 56 private static Context context_; 57 58 62 public static void setUp(String pluginName) throws Exception { 63 deleteDatabase(); 64 System.setProperty(TEST_PROPERTY, "true"); 65 System.setProperty(DEV_CONFIG_PROPERTY, "true"); 66 System.setProperty(DEV_EXTENSIONS_PROPERTY, pluginName); 67 context_ = TestContext.getTestContext(); 68 createSuperUser(); 69 assertPolicies(); 70 PolicyUtil.checkLogin(getAccount(USERNAME)); 72 } 73 74 private static void createSuperUser() throws Exception { 75 UserDatabase userDatabase = getUserService().createUserDatabase(DATABASE_TYPE, UserDatabaseManager.DEFAULT_REALM_NAME, UserDatabaseManager.DEFAULT_REALM_DESCRIPTION, true); 76 User user = userDatabase.createAccount(USERNAME, "", "", "", new Role[] {}); 77 userDatabase.changePassword(user.getPrincipalName(), "", PASSWORD, false); 78 79 Property.setProperty(new RealmKey("security.userDatabase", userDatabase.getRealm()), DATABASE_TYPE, getSessionInfo()); 80 Property.setProperty(new RealmKey("security.administrators", userDatabase.getRealm()), USERNAME, getSessionInfo()); 81 } 82 83 private static void assertPolicies() throws Exception { 84 List <Policy> policies = PolicyDatabaseFactory.getInstance().getPolicies(); 85 assertEquals("There should be only one policy", policies.size(), 1); 86 Policy policy = policies.get(0); 87 assertEquals("The policy should be called 'Everyone'", policy.getResourceName(), "Everyone"); 88 assertEquals("The policy id should be '0'", policy.getResourceId(), 0); 89 } 90 91 94 @AfterClass 95 public static void oneTimeTearDown() { 96 if (context_ != null){ 97 context_.shutdown(false); 98 } 99 deleteDatabase(); 100 } 101 102 105 private static void deleteDatabase() { 106 for (File file : TestContext.DB_DIR.listFiles()) { 107 file.delete(); 108 } 109 } 110 111 115 protected static SessionInfo getSessionInfo() throws Exception { 116 User account = getAccount(USERNAME); 117 return SessionInfo.nextSession(null, "testPolicyAdmin", account, InetAddress.getLocalHost(), SessionInfo.ALL_CONTEXTS, ""); 119 } 120 121 125 public static UserDatabase getDefaultUserDatabase() throws Exception { 126 return getUserService().getDefaultUserDatabase(); 127 } 128 129 133 public static Realm getDefaultRealm() throws Exception { 134 return getUserService().getDefaultRealm(); 135 } 136 137 141 public static UserDatabaseManager getUserService() throws Exception { 142 return UserDatabaseManager.getInstance(); 143 } 144 145 150 public static User getAccount(String username) throws Exception { 151 return getDefaultUserDatabase().getAccount(username); 152 } 153 154 158 public static User createAccount() throws Exception { 159 return createAccount("username", "password", "username@company.com", "Full Name"); 160 } 161 162 170 public static User createAccount(String username, String password, String email, String fullname) throws Exception { 171 return getDefaultUserDatabase().createAccount(username, password, email, fullname, new Role[] {}); 172 } 173 174 180 public static User updateAccountRoles (User user, Collection <Role> roles) throws Exception { 181 return updateAccount(user, user.getEmail(), user.getFullname(), roles.toArray(new Role[roles.size()])); 182 } 183 184 192 public static User updateAccount(User user, String email, String fullname, Role[] roles) throws Exception { 193 getDefaultUserDatabase().updateAccount(user, email, fullname, roles); 194 return getAccount(user.getPrincipalName()); 195 } 196 197 201 public static void deleteAccount(User... users) throws Exception { 202 for (User user : users) { 203 getDefaultUserDatabase().deleteAccount(user); 204 } 205 } 206 207 211 public static Role createRole() throws Exception { 212 return createRole("testRole"); 213 } 214 215 220 public static Role createRole(String rolename) throws Exception { 221 return getDefaultUserDatabase().createRole(rolename); 222 } 223 224 228 public static void deleteRole(Role...roles) throws Exception { 229 for (Role role : roles) { 230 getDefaultUserDatabase().deleteRole(role.getPrincipalName()); 231 } 232 } 233 234 238 public static PolicyDatabase getPolicyService() throws Exception { 239 return PolicyDatabaseFactory.getInstance(); 240 } 241 242 244 @Test 245 public void finalTestWhichFailsDueToBadTearDown() { 246 assertTrue(true); 247 } 248 } | Popular Tags |