001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.reg;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.auth.*;
031: import com.methodhead.sitecontext.*;
032: import com.methodhead.aikp.*;
033: import com.methodhead.transfer.*;
034: import com.methodhead.*;
035: import servletunit.struts.*;
036: import org.apache.struts.action.*;
037: import org.apache.cactus.*;
038:
039: public class RolesActionTest extends CactusStrutsTestCase {
040:
041: DynaActionForm form = null;
042: List list = null;
043: String[] strings = null;
044: User user = null;
045: Contact contact = null;
046: Role role = null;
047:
048: static {
049: TestUtils.initLogger();
050: TestUtils.initDb();
051: }
052:
053: public RolesActionTest(String name) {
054: super (name);
055: }
056:
057: public void setUp() {
058: try {
059: super .setUp();
060: ConnectionSingleton.runBatchUpdate(new FileReader(
061: "webapp/WEB-INF/db/transfer-reset.sql"));
062:
063: TestData.createUsers();
064: AuthUtil.setUser(request, TestData.user1);
065: } catch (Exception e) {
066: fail(e.getMessage());
067: }
068: }
069:
070: public void tearDown() throws Exception {
071: super .tearDown();
072: }
073:
074: public void testDoRolesForm() {
075:
076: setRequestPathInfo("/rolesForm");
077: actionPerform();
078:
079: verifyInputForward();
080:
081: form = (DynaActionForm) getActionForm();
082: assertEquals("", form.get("siteid"));
083: }
084:
085: public void testDoRolesSelect() {
086:
087: //
088: // select a site
089: //
090: setRequestPathInfo("/roles");
091: addRequestParameter("select", "Select");
092: addRequestParameter("userid", "1");
093: addRequestParameter("siteid", "1");
094: actionPerform();
095:
096: verifyInputForward();
097:
098: form = (DynaActionForm) getActionForm();
099: assertEquals("1", form.get("siteid"));
100: assertEquals("1", form.get("userid"));
101:
102: strings = (String[]) form.get("roles");
103: assertNotNull(strings);
104: assertEquals(1, strings.length);
105: assertEquals("ROLE_SYSADMIN", strings[0]);
106: }
107:
108: public void testDoRolesSelectNoRoles() {
109:
110: //
111: // select a site where the user has no roles
112: //
113: setRequestPathInfo("/roles");
114: addRequestParameter("select", "Select");
115: addRequestParameter("userid", "1");
116: addRequestParameter("siteid", "3");
117: addRequestParameter("roles", new String[] { "ROLE1" }); // this should be ignored
118: actionPerform();
119:
120: verifyInputForward();
121:
122: form = (DynaActionForm) getActionForm();
123: assertEquals("3", form.get("siteid"));
124: assertEquals("1", form.get("userid"));
125:
126: //
127: // roles should be reset
128: //
129: strings = (String[]) form.get("roles");
130: assertNotNull(strings);
131: assertEquals(0, strings.length);
132: }
133:
134: public void testDoRolesCancel() {
135:
136: //
137: // cancel the roles form
138: //
139: setRequestPathInfo("/roles");
140: addRequestParameter("cancel", "Cancel");
141: addRequestParameter("userid", "1");
142: actionPerform();
143:
144: verifyForwardPath("/user.do?action=edit&id=1");
145: }
146:
147: public void testDoRoles() {
148:
149: setRequestPathInfo("/roles");
150: addRequestParameter("userid", "1");
151: addRequestParameter("siteid", "1");
152: addRequestParameter("roles", new String[] {
153: DefaultTransferPolicy.ROLE_WEBMASTER,
154: DefaultTransferPolicy.ROLE_SITEADMIN });
155: actionPerform();
156:
157: verifyForwardPath("/user.do?action=edit&id=1");
158:
159: user = new User();
160: user.load(new IntKey(1));
161:
162: assertEquals(3, user.getRoles().size());
163:
164: role = (Role) user.getRoles().get(0);
165: assertEquals(SiteContext.getDefaultContext(), role
166: .getSiteContext());
167: assertEquals(DefaultTransferPolicy.ROLE_SYSADMIN, role
168: .getName());
169:
170: role = (Role) user.getRoles().get(1);
171: assertEquals(TestData.siteContext1, role.getSiteContext());
172: assertEquals(DefaultTransferPolicy.ROLE_WEBMASTER, role
173: .getName());
174:
175: role = (Role) user.getRoles().get(2);
176: assertEquals(TestData.siteContext1, role.getSiteContext());
177: assertEquals(DefaultTransferPolicy.ROLE_SITEADMIN, role
178: .getName());
179: }
180: }
|