001: package com.ecyrd.jspwiki.web;
002:
003: import java.io.*;
004: import java.net.URL;
005: import java.security.CodeSource;
006: import java.security.ProtectionDomain;
007: import java.util.Properties;
008:
009: import junit.framework.TestCase;
010:
011: import com.ecyrd.jspwiki.auth.login.CookieAssertionLoginModule;
012:
013: public abstract class CommonTests extends TestCase {
014: protected static final String TEST_PASSWORD = "myP@5sw0rd";
015: protected static final String TEST_LOGINNAME = "janne";
016: protected static final String TEST_FULLNAME = "Janne Jalkanen";
017: protected static final String TEST_WIKINAME = "JanneJalkanen";
018: protected final String m_baseURL;
019:
020: public CommonTests(String name, String uRL) {
021: super (name);
022: m_baseURL = getHostAndPort() + uRL;
023: newSession();
024: }
025:
026: public void setUp() {
027: newSession();
028: }
029:
030: public void testAclJanneEdit() {
031: // Log in as 'janne' and create page with him as editor
032: newSession();
033: login(TEST_LOGINNAME, TEST_PASSWORD);
034: String page = "AclEditOnly" + System.currentTimeMillis();
035:
036: String text = "[{ALLOW edit janne}]\n"
037: + "This page was created with an ACL by janne";
038:
039: // Anonymous viewing should NOT succeed
040: newSession();
041:
042: // Anonymous editing should fail
043:
044: // Now log in as janne again and view/edit it successfully
045: login(TEST_LOGINNAME, TEST_PASSWORD);
046:
047: }
048:
049: public void testAclJanneEditAllView() {
050: /** testCreatePage does all of the form validation tests */
051: // Log in as 'janne' and create page with him as editor
052: newSession();
053: login(TEST_LOGINNAME, TEST_PASSWORD);
054: String page = "AclViewAndEdit" + System.currentTimeMillis();
055:
056: String text = "[{ALLOW edit janne}]\n" + "[{ALLOW view All}]\n"
057: + "This page was created with an ACL by janne";
058:
059: // Anonymous viewing should succeed
060: newSession();
061:
062: // Anonymous editing should fail
063:
064: // Now log in as janne again and view/edit it successfully
065: login(TEST_LOGINNAME, TEST_PASSWORD);
066:
067: }
068:
069: public void testAnonymousCreateGroup() {
070: // Try to create a group; we should get redirected to login page
071:
072: }
073:
074: public void testAnonymousView() {
075: // Start at main, then navigate to About; verify user not logged in
076:
077: }
078:
079: public void testAnonymousViewImage() throws IOException {
080: // See if we can view the JSPWiki logo
081:
082: }
083:
084: public void testAssertedName() {
085: // Navigate to Prefs page; see the 'G'day message' for the anonymous user
086:
087: // Go to the UserPreferences page; see the set-cookie form, plus welcome text that invites user to set cookie
088:
089: // Set the cookie to our test user name
090:
091: // Now navigate back to the main page; see the 'G'day message' for the test user
092:
093: String cookie = getCookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME);
094: assertNotNull(cookie);
095: assertEquals("Don+Quixote", cookie);
096:
097: // Clear user cookie
098:
099: // Go back to the main page, and see the 'G'day message for the anonymous user again
100:
101: cookie = getCookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME);
102: assertEquals("", cookie);
103: }
104:
105: public void testAssertedPermissions() {
106: // Create new group with 'janne' and 'FredFlintstone' as members
107:
108: login(TEST_LOGINNAME, TEST_PASSWORD);
109: String group = "AssertedPermissions"
110: + String.valueOf(System.currentTimeMillis());
111: String members = TEST_LOGINNAME + " \n FredFlintstone";
112:
113: // First, create the group
114:
115: // Verify the group was created
116:
117: // Verifiy that anonymous users can't view the group
118: newSession();
119:
120: // Log in again and verify we can read it
121: login(TEST_LOGINNAME, TEST_PASSWORD);
122:
123: // Verify that asserted user 'Fred' can view the group but not edit
124: newSession();
125:
126: // Try to edit -- it should not be allowed
127:
128: }
129:
130: public void testCreateGroupFullName() {
131: createGroup(TEST_LOGINNAME, "Janne Jalkanen");
132: }
133:
134: public void testCreateGroupLoginName() {
135: createGroup(TEST_LOGINNAME, TEST_LOGINNAME);
136: }
137:
138: public void testCreateGroupWikiName() {
139: createGroup(TEST_LOGINNAME, "JanneJalkanen");
140: }
141:
142: public void testCreatePage() {
143: login(TEST_LOGINNAME, TEST_PASSWORD);
144: String page = "CreatePage" + System.currentTimeMillis();
145:
146: }
147:
148: public void testLogin() {
149: // Start at front page; try to log in
150:
151: }
152:
153: public void testLogout() {
154: // Start at front page; try to log in
155:
156: String cookie = getCookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME);
157: assertNotNull(cookie);
158: assertEquals(TEST_WIKINAME, cookie);
159:
160: // Log out; we should NOT see any asserted identities
161:
162: cookie = getCookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME);
163: assertEquals("", cookie);
164: }
165:
166: public void testRedirectPageAfterLogin() {
167: // Create new page
168: login(TEST_LOGINNAME, TEST_PASSWORD);
169: String page = "CreatePage" + System.currentTimeMillis();
170:
171: String redirectText = "We created this page to test redirects.";
172:
173: // Now, from an anonymous session, try to view it, fail, then login
174: newSession();
175:
176: // We should be able to see the page now
177:
178: }
179:
180: public void testRenameProfile() {
181: // Create a new user and group (and log in)
182: String loginName = createProfile("TestRenameProfileUser",
183: "TestRenameProfileUser");
184:
185: newSession();
186: String group = createGroup(loginName, loginName);
187:
188: // Create a page with a view ACL restricted to the new user
189: String page = "TestRenameProfilePage"
190: + System.currentTimeMillis();
191:
192: String text = "[{ALLOW edit " + loginName
193: + "}]\nThis page was created with an ACL by "
194: + loginName;
195:
196: // Anonymous editing should fail
197: newSession();
198:
199: // Now log in as the test user and view/edit it successfully
200: login(loginName, TEST_PASSWORD);
201:
202: // Verify that our ACL test is present (note the extra linebreak at the end of the text
203:
204: // OK -- now that we've got a user, a protected page and a group successfully set up, let's change the profile name
205:
206: String newLoginName = "Renamed" + loginName;
207:
208: // Now, the main page should show the new authenticated user name
209:
210: // When we navigate to the protected page, the ACL should have the NEW name in it
211:
212: // Also, when we navigate to the group page, the group member should be the NEW name (we will see this inside a <td> element)
213:
214: }
215:
216: protected String createGroup(String user, String members) {
217:
218: login(user, TEST_PASSWORD);
219:
220: String group = "Test"
221: + String.valueOf(System.currentTimeMillis());
222:
223: // First, name the group
224:
225: // Verify the group was created
226:
227: // Verifiy that anonymous users can't view the group
228: newSession();
229:
230: // Log in again and verify we can read it
231: login(user, TEST_PASSWORD);
232:
233: // Try to edit -- it should be allowed
234:
235: return group;
236: }
237:
238: protected String createProfile(String loginname, String fullname) {
239: return createProfile(loginname, fullname, true);
240: }
241:
242: protected String createProfile(String loginname, String fullname,
243: boolean withPassword) {
244: // Navigate to profile tab
245:
246: // Create user profile with generated user name
247: String suffix = generateSuffix();
248:
249: if (withPassword) {
250:
251: }
252: return loginname + suffix;
253: }
254:
255: protected String generateSuffix() {
256: return String.valueOf(System.currentTimeMillis());
257: }
258:
259: protected void login(String user, String password) {
260: // Start at front page; try to log in
261:
262: }
263:
264: protected String getCookie(String cookie) {
265: return "to-be-fixed";
266: }
267:
268: protected void newSession() {
269: }
270:
271: private String getHostAndPort() {
272: Properties p = new Properties();
273: String buildFile = "build.properties";
274: String host = "http://localhost";
275: String port = ":8080/";
276: BufferedReader in = null;
277:
278: try {
279: // search which properties file is being used by build.xml.
280: // build.xml is NOT loaded by the classloader, so we have to do some trickery
281: // in order to avoid instantiating a File object with a hard-coded, absolute
282: // path. Instead of doing that, we determine from where the class was loaded
283: // (cfr. http://www.exampledepot.com/egs/java.lang/ClassOrigin.html) and, from
284: // there, we set the relative location of build.xml
285:
286: Class cls = this .getClass();
287: ProtectionDomain pDomain = cls.getProtectionDomain();
288: CodeSource cSource = pDomain.getCodeSource();
289: URL loc = cSource.getLocation(); // ${JSPWiki}/classes
290: in = new BufferedReader(new FileReader(new File(loc
291: .getFile()
292: + "../build.xml")));
293:
294: String line;
295: while ((line = in.readLine()) != null) {
296: line = line.trim();
297: if (line
298: .startsWith("<property name=\"build.properties\" value=\"")) {
299: int beginsIn = "<property name=\"build.properties\" value=\""
300: .length();
301: int endsAt = line.lastIndexOf("\"");
302: buildFile = line.substring(beginsIn, endsAt);
303: }
304: }
305:
306: // buildFile is also NOT loaded by the classloader. Luckily it's path is relative
307: // to build.xml path, as it is defined inside it.
308: p.load(new FileInputStream(loc.getFile() + "../"
309: + buildFile));
310: if (p.getProperty("tomcat.host") != null) {
311: host = "http://" + p.getProperty("tomcat.host");
312: }
313: if (p.getProperty("tomcat.port") != null) {
314: port = ":" + p.getProperty("tomcat.port") + "/";
315: }
316: } catch (Exception e) {
317: e.printStackTrace();
318: System.out.println("Error loading build.properties");
319: }
320: return host + port;
321: }
322:
323: }
|