001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.business;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.List;
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.TestUtils;
029: import org.apache.roller.business.RollerFactory;
030: import org.apache.roller.business.UserManager;
031: import org.apache.roller.pojos.PermissionsData;
032: import org.apache.roller.pojos.UserData;
033: import org.apache.roller.pojos.WebsiteData;
034:
035: /**
036: * Test User/Weblog Permissions related business operations.
037: */
038: public class PermissionTest extends TestCase {
039:
040: public static Log log = LogFactory.getLog(PermissionTest.class);
041:
042: UserData testUser = null;
043: WebsiteData testWeblog = null;
044:
045: public PermissionTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: return new TestSuite(PermissionTest.class);
051: }
052:
053: /**
054: * All tests in this suite require a user and a weblog.
055: */
056: public void setUp() throws Exception {
057: try {
058: testUser = TestUtils.setupUser("permsTestUser");
059: testWeblog = TestUtils.setupWeblog("permsTestWeblog",
060: testUser);
061: TestUtils.endSession(true);
062: } catch (Exception ex) {
063: log.error(ex);
064: throw new Exception("Test setup failed", ex);
065: }
066: }
067:
068: public void tearDown() throws Exception {
069: try {
070: TestUtils.teardownWeblog(testWeblog.getId());
071: TestUtils.teardownUser(testUser.getId());
072: TestUtils.endSession(true);
073: } catch (Exception ex) {
074: log.error(ex);
075: throw new Exception("Test teardown failed", ex);
076: }
077: }
078:
079: /**
080: * Test basic persistence operations ... Create, Update, Delete.
081: */
082: public void testPermissionsCRUD() throws Exception {
083:
084: UserManager mgr = RollerFactory.getRoller().getUserManager();
085: PermissionsData perm = null;
086:
087: // delete permissions
088: perm = mgr.getPermissions(testWeblog, testUser);
089: assertNotNull(perm);
090: mgr.removePermissions(perm);
091: TestUtils.endSession(true);
092:
093: // check that delete was successful
094: perm = null;
095: perm = mgr.getPermissions(testWeblog, testUser);
096: assertNull(perm);
097:
098: // create permissions
099: perm = new PermissionsData();
100: perm.setUser(testUser);
101: perm.setWebsite(testWeblog);
102: perm.setPending(false);
103: perm.setPermissionMask(PermissionsData.ADMIN);
104: mgr.savePermissions(perm);
105: TestUtils.endSession(true);
106:
107: // check that create was successful
108: perm = null;
109: perm = mgr.getPermissions(testWeblog, testUser);
110: assertNotNull(perm);
111: assertEquals(PermissionsData.ADMIN, perm.getPermissionMask());
112:
113: // update permissions
114: perm.setPermissionMask(PermissionsData.LIMITED);
115: mgr.savePermissions(perm);
116: TestUtils.endSession(true);
117:
118: // check that update was successful
119: perm = null;
120: perm = mgr.getPermissions(testWeblog, testUser);
121: assertNotNull(perm);
122: assertEquals(PermissionsData.LIMITED, perm.getPermissionMask());
123: }
124:
125: /**
126: * Test lookup mechanisms.
127: */
128: public void testPermissionsLookups() throws Exception {
129:
130: // we need a second user for this test
131: UserData user = TestUtils.setupUser("foofoo");
132: TestUtils.endSession(true);
133:
134: UserManager mgr = RollerFactory.getRoller().getUserManager();
135: PermissionsData perm = null;
136: List perms = null;
137:
138: // get all permissions for a user
139: perms = mgr.getAllPermissions(user);
140: assertEquals(0, perms.size());
141: perms = mgr.getAllPermissions(testUser);
142: assertEquals(1, perms.size());
143:
144: // get all permissions for a weblog
145: perms = mgr.getAllPermissions(testWeblog);
146: assertEquals(1, perms.size());
147:
148: perm = new PermissionsData();
149: perm.setUser(user);
150: perm.setWebsite(testWeblog);
151: perm.setPending(true);
152: perm.setPermissionMask(PermissionsData.AUTHOR);
153: mgr.savePermissions(perm);
154:
155: // get pending permissions for a user
156: perms = mgr.getPendingPermissions(testUser);
157: assertEquals(0, perms.size());
158: perms = mgr.getPendingPermissions(user);
159: assertEquals(1, perms.size());
160:
161: // get pending permissions for a weblog
162: perms = mgr.getPendingPermissions(testWeblog);
163: assertEquals(1, perms.size());
164:
165: // get permissions by id
166: String id = perm.getId();
167: perm = null;
168: perm = mgr.getPermissions(id);
169: assertNotNull(perm);
170: assertEquals(id, perm.getId());
171:
172: // get permissions for a specific user/weblog
173: perm = null;
174: perm = mgr.getPermissions(testWeblog, testUser);
175: assertNotNull(perm);
176: assertEquals(PermissionsData.ADMIN, perm.getPermissionMask());
177: perm = null;
178: perm = mgr.getPermissions(testWeblog, user);
179: assertNotNull(perm);
180: assertEquals(PermissionsData.AUTHOR, perm.getPermissionMask());
181: assertEquals(true, perm.isPending());
182:
183: // cleanup the extra test user
184: TestUtils.teardownUser(user.getId());
185: TestUtils.endSession(true);
186: }
187:
188: /**
189: * Tests weblog invitation process.
190: */
191: public void testInvitations() throws Exception {
192:
193: // we need a second user for this test
194: UserData user = TestUtils.setupUser("foobee");
195: TestUtils.endSession(true);
196:
197: UserManager mgr = RollerFactory.getRoller().getUserManager();
198: PermissionsData perm = null;
199: List perms = null;
200:
201: // invite user to weblog
202: perm = mgr
203: .inviteUser(testWeblog, user, PermissionsData.LIMITED);
204: String id = perm.getId();
205: TestUtils.endSession(true);
206:
207: // accept invitation
208: perm = mgr.getPermissions(testWeblog, user);
209: perm.setPending(false);
210: mgr.savePermissions(perm);
211: TestUtils.endSession(true);
212:
213: // re-query now that we have changed things
214: user = mgr.getUserByUserName(user.getUserName());
215: testWeblog = mgr.getWebsiteByHandle(testWeblog.getHandle());
216:
217: // assert that invitation list is empty
218: assertTrue(mgr.getPendingPermissions(user).isEmpty());
219: assertTrue(mgr.getPendingPermissions(testWeblog).isEmpty());
220:
221: // assert that user is member of weblog
222: assertFalse(mgr.getPermissions(testWeblog, user).isPending());
223: List weblogs = mgr.getWebsites(user, null, null, null, null, 0,
224: -1);
225: assertEquals(1, weblogs.size());
226: assertEquals(testWeblog.getId(), ((WebsiteData) weblogs.get(0))
227: .getId());
228:
229: // assert that website has user
230: List users = mgr.getUsers(testWeblog, null, 0, -1);
231: assertEquals(2, users.size());
232:
233: // test user can be retired from website
234: mgr.retireUser(testWeblog, user);
235: TestUtils.endSession(true);
236:
237: user = mgr.getUser(user.getId());
238: weblogs = mgr.getWebsites(user, null, null, null, null, 0, -1);
239: assertEquals(0, weblogs.size());
240:
241: // cleanup the extra test user
242: TestUtils.teardownUser(user.getId());
243: TestUtils.endSession(true);
244: }
245:
246: }
|