1 19 20 package com.sslexplorer.profiles; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertTrue; 24 25 import java.util.Calendar ; 26 import java.util.List ; 27 28 import org.junit.AfterClass; 29 import org.junit.BeforeClass; 30 import org.junit.Test; 31 32 import com.sslexplorer.boot.PropertyList; 33 import com.sslexplorer.core.CoreServlet; 34 import com.sslexplorer.core.UserDatabaseManager; 35 import com.sslexplorer.jdbc.JDBCPropertyDatabase; 36 import com.sslexplorer.policyframework.AccessRights; 37 import com.sslexplorer.policyframework.Policy; 38 import com.sslexplorer.policyframework.PolicyConstants; 39 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 40 import com.sslexplorer.policyframework.ResourceType; 41 import com.sslexplorer.properties.DefaultPropertyProfile; 42 import com.sslexplorer.properties.PropertyProfile; 43 import com.sslexplorer.realms.Realm; 44 import com.sslexplorer.security.User; 45 import com.sslexplorer.testcontainer.policyframework.AbstractTestPolicyEnabledResource; 46 47 49 public class ProfilesTests extends AbstractTestPolicyEnabledResource<PropertyProfile> { 50 51 private static JDBCPropertyDatabase propertyDatabase; 52 53 56 @BeforeClass 57 public static void oneTimeSetUp() throws Exception { 58 setUp(""); 59 propertyDatabase = new JDBCPropertyDatabase(); 60 propertyDatabase.open(CoreServlet.getServlet()); 61 } 62 63 66 @AfterClass 67 public static void after() throws Exception { 68 propertyDatabase.close(); 69 } 70 71 @Override 72 public ResourceType getResourceType() throws Exception { 73 return PolicyConstants.PROFILE_RESOURCE_TYPE; 74 } 75 76 77 @Override 78 public PropertyProfile getEmptyResource() throws Exception { 79 Calendar calendar = Calendar.getInstance(); 80 return new DefaultPropertyProfile(-1, -1, "", "", "", calendar, calendar); 81 } 82 83 @Override 84 public PropertyProfile getNormalResource() throws Exception { 85 Calendar calendar = Calendar.getInstance(); 86 return new DefaultPropertyProfile(getDefaultRealm().getRealmID(), -1, null, "resourceName", "resourceDescription", calendar, calendar); 87 } 88 89 @Override 90 public PropertyProfile getNullResource() throws Exception { 91 Calendar calendar = Calendar.getInstance(); 92 return new DefaultPropertyProfile(-1, -1, null, null, null, calendar, calendar); 93 } 94 95 @SuppressWarnings ("unchecked") 96 @Override 97 public List <PropertyProfile> getAllResources() throws Exception { 98 return propertyDatabase.getPropertyProfiles(null, true, UserDatabaseManager.getInstance().getDefaultRealmID()); 99 } 100 101 @Override 102 public PropertyProfile createResource(PropertyProfile resource) throws Exception { 103 return propertyDatabase.createPropertyProfile(resource.getOwnerUsername(), resource.getResourceName(), resource.getResourceDescription(), 0, UserDatabaseManager.getInstance().getDefaultRealmID()); 104 } 105 106 @Override 107 public PropertyProfile deleteResource(PropertyProfile resource) throws Exception { 108 return propertyDatabase.deletePropertyProfile(resource.getResourceId()); 109 } 110 111 @Override 112 public PropertyProfile getResource(PropertyProfile resource) throws Exception { 113 return propertyDatabase.getPropertyProfile(resource.getResourceId()); 114 } 115 116 @Override 117 public PropertyProfile updateResource(PropertyProfile resource) throws Exception { 118 propertyDatabase.updatePropertyProfile(resource.getResourceId(), resource.getResourceName(), resource.getResourceDescription()); 119 return getResource(resource); 120 } 121 122 @Test 123 public void checkResourceAccessRights() throws Exception { 124 Realm realm = getDefaultRealm(); 125 Policy policy = createPolicy(realm); 126 User user = createAccount(); 127 getPolicyService().grantPolicyToPrincipal(policy, user); 128 129 AccessRights accessRights = createAssignAccessRights(getResourceType().getPermissionClass()); 130 PropertyList selectedPolicies = PropertyList.createFromArray(new int[] {policy.getResourceId()}); 131 PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(accessRights, selectedPolicies, getSessionInfo()); 132 PropertyProfile resource = createResource(); 133 assertTrue("Should be, as one already exists.", getPolicyService().isPrincipalGrantedResourcesOfType(user, resource.getResourceType(), null)); 134 assertEquals("Should be only one.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 1); 135 136 PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, selectedPolicies, getSessionInfo()); 137 assertEquals("Should be two.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 2); 138 139 PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(resource, getSessionInfo()); 140 assertEquals("Should be only one.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 1); 141 getPolicyService().revokePolicyFromPrincipal(policy, user); 142 deleteAccount(user); 143 144 PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(accessRights, getSessionInfo()); 145 deleteResource(resource); 146 getPolicyService().deleteAccessRights(accessRights.getResourceId()); 147 getPolicyService().deletePolicy(policy.getResourceId()); 148 } 149 150 154 @Test 155 public void createProfileWithoutUser() throws Exception { 156 DefaultPropertyProfile newProfile = new DefaultPropertyProfile(getDefaultRealm().getRealmID(), -1, "test3", "profile", "A profile", Calendar.getInstance(), Calendar.getInstance()); 157 assertEquals("There should not be any PropertyProfile", 0, propertyDatabase.getPropertyProfiles("test1", true, 1).size()); 158 PropertyProfile createdProfile = createResource(newProfile); 159 assertEquals("There should be two PropertyProfile", 1, propertyDatabase.getPropertyProfiles("test3", true, 1).size()); 160 deleteResource(createdProfile); 161 assertEquals("There should not be any PropertyProfile", 0, propertyDatabase.getPropertyProfiles("test1", true, 1).size()); 162 } 163 } | Popular Tags |