1 18 package org.apache.roller.business; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.List ; 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.roller.TestUtils; 29 import org.apache.roller.model.RollerFactory; 30 import org.apache.roller.model.UserManager; 31 import org.apache.roller.pojos.PermissionsData; 32 import org.apache.roller.pojos.UserData; 33 import org.apache.roller.pojos.WebsiteData; 34 35 36 39 public class PermissionTest extends TestCase { 40 41 public static Log log = LogFactory.getLog(PermissionTest.class); 42 43 UserData testUser = null; 44 WebsiteData testWeblog = null; 45 46 47 public PermissionTest(String name) { 48 super(name); 49 } 50 51 52 public static Test suite() { 53 return new TestSuite(PermissionTest.class); 54 } 55 56 57 60 public void setUp() throws Exception { 61 try { 62 testUser = TestUtils.setupUser("permsTestUser"); 63 testWeblog = TestUtils.setupWeblog("permsTestWeblog", testUser); 64 TestUtils.endSession(true); 65 } catch (Exception ex) { 66 log.error(ex); 67 throw new Exception ("Test setup failed", ex); 68 } 69 } 70 71 public void tearDown() throws Exception { 72 try { 73 TestUtils.teardownWeblog(testWeblog.getId()); 74 TestUtils.teardownUser(testUser.getId()); 75 TestUtils.endSession(true); 76 } catch (Exception ex) { 77 log.error(ex); 78 throw new Exception ("Test teardown failed", ex); 79 } 80 } 81 82 83 86 public void testPermissionsCRUD() throws Exception { 87 88 UserManager mgr = RollerFactory.getRoller().getUserManager(); 89 PermissionsData perm = null; 90 91 perm = mgr.getPermissions(testWeblog, testUser); 93 assertNotNull(perm); 94 mgr.removePermissions(perm); 95 TestUtils.endSession(true); 96 97 perm = null; 99 perm = mgr.getPermissions(testWeblog, testUser); 100 assertNull(perm); 101 102 perm = new PermissionsData(); 104 perm.setUser(testUser); 105 perm.setWebsite(testWeblog); 106 perm.setPending(false); 107 perm.setPermissionMask(PermissionsData.ADMIN); 108 mgr.savePermissions(perm); 109 TestUtils.endSession(true); 110 111 perm = null; 113 perm = mgr.getPermissions(testWeblog, testUser); 114 assertNotNull(perm); 115 assertEquals(PermissionsData.ADMIN, perm.getPermissionMask()); 116 117 perm.setPermissionMask(PermissionsData.LIMITED); 119 mgr.savePermissions(perm); 120 TestUtils.endSession(true); 121 122 perm = null; 124 perm = mgr.getPermissions(testWeblog, testUser); 125 assertNotNull(perm); 126 assertEquals(PermissionsData.LIMITED, perm.getPermissionMask()); 127 } 128 129 130 133 public void testPermissionsLookups() throws Exception { 134 135 UserData user = TestUtils.setupUser("foofoo"); 137 TestUtils.endSession(true); 138 139 UserManager mgr = RollerFactory.getRoller().getUserManager(); 140 PermissionsData perm = null; 141 List perms = null; 142 143 perms = mgr.getAllPermissions(user); 145 assertEquals(0, perms.size()); 146 perms = mgr.getAllPermissions(testUser); 147 assertEquals(1, perms.size()); 148 149 perms = mgr.getAllPermissions(testWeblog); 151 assertEquals(1, perms.size()); 152 153 perm = new PermissionsData(); 154 perm.setUser(user); 155 perm.setWebsite(testWeblog); 156 perm.setPending(true); 157 perm.setPermissionMask(PermissionsData.AUTHOR); 158 mgr.savePermissions(perm); 159 160 perms = mgr.getPendingPermissions(testUser); 162 assertEquals(0, perms.size()); 163 perms = mgr.getPendingPermissions(user); 164 assertEquals(1, perms.size()); 165 166 perms = mgr.getPendingPermissions(testWeblog); 168 assertEquals(1, perms.size()); 169 170 String id = perm.getId(); 172 perm = null; 173 perm = mgr.getPermissions(id); 174 assertNotNull(perm); 175 assertEquals(id, perm.getId()); 176 177 perm = null; 179 perm = mgr.getPermissions(testWeblog, testUser); 180 assertNotNull(perm); 181 assertEquals(PermissionsData.ADMIN, perm.getPermissionMask()); 182 perm = null; 183 perm = mgr.getPermissions(testWeblog, user); 184 assertNotNull(perm); 185 assertEquals(PermissionsData.AUTHOR, perm.getPermissionMask()); 186 assertEquals(true, perm.isPending()); 187 188 TestUtils.teardownUser(user.getId()); 190 TestUtils.endSession(true); 191 } 192 193 194 197 public void testInvitations() throws Exception { 198 199 UserData user = TestUtils.setupUser("foobee"); 201 TestUtils.endSession(true); 202 203 UserManager mgr = RollerFactory.getRoller().getUserManager(); 204 PermissionsData perm = null; 205 List perms = null; 206 207 perm = mgr.inviteUser(testWeblog, user, PermissionsData.LIMITED); 209 String id = perm.getId(); 210 TestUtils.endSession(true); 211 212 perm = mgr.getPermissions(testWeblog, user); 214 perm.setPending(false); 215 mgr.savePermissions(perm); 216 TestUtils.endSession(true); 217 218 user = mgr.getUserByUserName(user.getUserName()); 220 testWeblog = mgr.getWebsiteByHandle(testWeblog.getHandle()); 221 222 assertTrue(mgr.getPendingPermissions(user).isEmpty()); 224 assertTrue(mgr.getPendingPermissions(testWeblog).isEmpty()); 225 226 assertFalse(mgr.getPermissions(testWeblog, user).isPending()); 228 List weblogs = mgr.getWebsites(user, null, null, null, null, 0, -1); 229 assertEquals(1, weblogs.size()); 230 assertEquals(testWeblog.getId(), ((WebsiteData)weblogs.get(0)).getId()); 231 232 List users = mgr.getUsers(testWeblog, null, 0, -1); 234 assertEquals(2, users.size()); 235 236 mgr.retireUser(testWeblog, user); 238 TestUtils.endSession(true); 239 240 user = mgr.getUser(user.getId()); 241 weblogs = mgr.getWebsites(user, null, null, null, null, 0, -1); 242 assertEquals(0, weblogs.size()); 243 244 TestUtils.teardownUser(user.getId()); 246 TestUtils.endSession(true); 247 } 248 249 } 250 251 | Popular Tags |