1 64 65 package com.jcorporate.expresso.services.dbobj.tests; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 69 import com.jcorporate.expresso.core.security.User; 70 import com.jcorporate.expresso.services.dbobj.DBObjSecurity; 71 import com.jcorporate.expresso.services.dbobj.GroupMembers; 72 import com.jcorporate.expresso.services.dbobj.UserGroup; 73 import com.jcorporate.expresso.services.dbobj.UserPreference; 74 import com.jcorporate.expresso.services.test.ExpressoTestCase; 75 import com.jcorporate.expresso.services.test.TestSystemInitializer; 76 import junit.framework.TestSuite; 77 78 import java.util.ArrayList ; 79 import java.util.Iterator ; 80 81 82 93 public class DBObjSecurityTests 94 extends ExpressoTestCase { 95 private static final String TEST_GROUP = "TestGroup"; 96 private static final String TEST_LOGIN = "testUser"; 97 private int TEST_UID = 0; 98 99 public DBObjSecurityTests(String name) 100 throws Exception { 101 super(name); 102 } 103 104 public static void main(String [] args) 105 throws java.lang.Exception { 106 107 junit.textui.TestRunner.run(suite()); 109 } 110 111 115 public static junit.framework.Test suite() 116 throws Exception { 117 return new TestSuite(DBObjSecurityTests.class); 118 } 119 120 123 public void testDBObjSecurity() 124 throws Exception { 125 126 127 DBObjSecurity oneSec = null; 128 DBObjSecurity secList = new DBObjSecurity(); 129 secList.setDataContext(TestSystemInitializer.getTestContext()); 130 secList.setField("GroupName", TEST_GROUP); 131 132 for (Iterator e1 = secList.searchAndRetrieveList().iterator(); 133 e1.hasNext();) { 134 oneSec = (DBObjSecurity) e1.next(); 135 oneSec.delete(); 136 } 137 138 139 UserPreference testPref = new UserPreference(this.TEST_UID); 140 testPref.setDataContext(TestSystemInitializer.getTestContext()); 141 142 if (testPref.checkAllowed("A")) { 143 fail("Test user was allowed add permission to " + 144 "UserPreference and should not have been"); 145 } 146 if (testPref.checkAllowed("U")) { 147 fail("Test user was allowed update permission to " + 148 "UserPreference and should not have been"); 149 } 150 if (testPref.checkAllowed("D")) { 151 fail("Test user was allowed delete permission to " + 152 "UserPreference and should not have been"); 153 } 154 if (testPref.checkAllowed("S")) { 155 fail("Test user was allowed search permission to " + 156 "UserPreference and should not have been"); 157 } 158 159 String testPrefDB = testPref.getDataContext(); 160 161 162 DBObjSecurity newSec = new DBObjSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 163 newSec.setDataContext(TestSystemInitializer.getTestContext()); 164 newSec.setField("GroupName", "TestGroup"); 165 newSec.setField("MethodCode", "A"); 166 newSec.setField("DBObjectName", 167 "com.jcorporate.expresso.services.dbobj.UserPreference"); 168 newSec.add(); 169 testPrefDB = testPref.getDataContext(); 170 if (!testPref.checkAllowed("A")) { 171 fail("Test user was not allowed add permission to " + 172 "UserPreference and should have been - add of permissions " + 173 "did not work"); 174 } 175 if (testPref.checkAllowed("U")) { 176 fail("Test user was allowed update permission to " + 177 "UserPreference and should not have been"); 178 } 179 if (testPref.checkAllowed("D")) { 180 fail("Test user was allowed delete permission to " + 181 "UserPreference and should not have been"); 182 } 183 if (testPref.checkAllowed("S")) { 184 fail("Test user was allowed search permission to " + 185 "UserPreference and should not have been"); 186 } 187 188 189 newSec.setField("GroupName", TEST_GROUP); 190 newSec.setField("MethodCode", "A"); 191 newSec.setField("DBObjectName", 192 com.jcorporate.expresso.services.dbobj.UserPreference.class.getName()); 193 newSec.delete(); 194 195 if (testPref.checkAllowed("A")) { 196 fail("Test user was allowed add permission to " + 197 "UserPreference and should not have been - delete of " + 198 "permissions did not work"); 199 } 200 if (testPref.checkAllowed("U")) { 201 fail("Test user was allowed update permission " + 202 "to UserPreference and should not have been"); 203 } 204 if (testPref.checkAllowed("D")) { 205 fail("Test user was allowed delete permission to " + 206 "UserPreference and should not have been"); 207 } 208 if (testPref.checkAllowed("S")) { 209 fail("Test user was allowed search permission " + 210 "to UserPreference and should not have been"); 211 } 212 } 213 214 protected void setUp() 215 throws java.lang.Exception { 216 User testUser = new User(); 217 testUser.setDataContext(TestSystemInitializer.getTestContext()); 218 testUser.setLoginName(TEST_LOGIN); 219 220 if (!testUser.find()) { 221 testUser.setEmail("testme@example.org"); 222 testUser.setLoginName(TEST_LOGIN); 223 testUser.setDisplayName("User for DBObjSecurity unit test - remove"); 224 testUser.add(); 225 } 226 if (testUser.find()) { 227 TEST_UID = testUser.getUid(); 228 } else { 229 fail("Unable to create Test User"); 230 } 231 232 UserGroup testGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 233 testGroup.setDataContext(TestSystemInitializer.getTestContext()); 234 testGroup.setField("GroupName", TEST_GROUP); 235 236 if (!testGroup.find()) { 237 testGroup.setField("Descrip", 238 "Group for DBObjSecurity unit test - remove"); 239 testGroup.add(); 240 } 241 242 GroupMembers testGroupMembers = new GroupMembers(SecuredDBObject.SYSTEM_ACCOUNT); 243 testGroupMembers.setDataContext(TestSystemInitializer.getTestContext()); 244 testGroupMembers.setField("GroupName", TEST_GROUP); 245 testGroupMembers.setField("ExpUid", TEST_UID); 246 247 if (!testGroupMembers.find()) { 248 testGroupMembers.add(); 249 } 250 251 252 super.setUp(); 253 } 254 255 259 protected void tearDown() 260 throws java.lang.Exception { 261 try { 263 GroupMembers testGroupMembers = new GroupMembers(SecuredDBObject.SYSTEM_ACCOUNT); 264 testGroupMembers.setDataContext(TestSystemInitializer.getTestContext()); 265 testGroupMembers.setField("GroupName", TEST_GROUP); 266 testGroupMembers.setField("ExpUid", TEST_UID); 267 ArrayList al = testGroupMembers.searchAndRetrieveList(); 268 269 for (Iterator i = al.iterator(); i.hasNext();) { 270 GroupMembers oneMember = (GroupMembers) i.next(); 271 oneMember.delete(); 272 } 273 } catch (DBException dbe) { 274 dbe.printStackTrace(); 275 System.out.println("Error Deleting Test Group Members: " + 276 dbe.getMessage()); 277 } 278 279 try { 281 DBObjSecurity oneSec = null; 282 DBObjSecurity secList = new DBObjSecurity(); 283 secList.setDataContext(TestSystemInitializer.getTestContext()); 284 secList.setField("GroupName", TEST_GROUP); 285 286 for (Iterator e1 = secList.searchAndRetrieveList().iterator(); 287 e1.hasNext();) { 288 oneSec = (DBObjSecurity) e1.next(); 289 oneSec.delete(); 290 } 291 } catch (DBException dbe) { 292 dbe.printStackTrace(); 293 System.out.println("DBObject Security Entries: " + 294 dbe.getMessage()); 295 } 296 297 try { 299 UserGroup testGroup = new UserGroup(); 300 testGroup.setDataContext(TestSystemInitializer.getTestContext()); 301 testGroup.setField("GroupName", TEST_GROUP); 302 303 if (testGroup.find()) { 304 testGroup.delete(); 305 } 306 } catch (DBException dbe) { 307 dbe.printStackTrace(); 308 System.out.println("Error Deleting Test Group: " + 309 dbe.getMessage()); 310 } 311 312 try { 314 User testUser = new User(); 315 testUser.setDataContext(TestSystemInitializer.getTestContext()); 316 testUser.setLoginName(TEST_LOGIN); 317 318 if (testUser.find()) { 319 testUser.delete(); 320 } 321 } catch (DBException dbe) { 322 dbe.printStackTrace(); 323 System.out.println("Error Deleting Test User: " + 324 dbe.getMessage()); 325 } 326 327 super.tearDown(); 328 } 329 } | Popular Tags |