|
From: <tre...@us...> - 2007-08-06 19:41:06
|
Revision: 236
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=236&view=rev
Author: trevorolio
Date: 2007-08-06 12:41:08 -0700 (Mon, 06 Aug 2007)
Log Message:
-----------
Starting down the path of using the newly refactored web API classes to do RMock based tests.
Modified Paths:
--------------
spaces/trunk/src/com/ogoglio/OgoglioTestSuite.java
spaces/trunk/src/com/ogoglio/client/WebAPIAuthenticatorFactory.java
spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
spaces/trunk/src/com/ogoglio/xml/AccountDocument.java
Added Paths:
-----------
spaces/trunk/src/com/ogoglio/client/WebAPITests.java
Modified: spaces/trunk/src/com/ogoglio/OgoglioTestSuite.java
===================================================================
--- spaces/trunk/src/com/ogoglio/OgoglioTestSuite.java 2007-08-06 17:34:13 UTC (rev 235)
+++ spaces/trunk/src/com/ogoglio/OgoglioTestSuite.java 2007-08-06 19:41:08 UTC (rev 236)
@@ -17,6 +17,7 @@
import junit.framework.TestSuite;
import com.ogoglio.client.ClientTests;
+import com.ogoglio.client.WebAPITests;
import com.ogoglio.persist.PersistTests;
import com.ogoglio.sim.script.ScriptTests;
import com.ogoglio.templatesync.TemplateSyncTestSuite;
@@ -28,7 +29,8 @@
TestSuite suite = new TestSuite();
suite.addTestSuite(ObjTest.class);
suite.addTestSuite(XMLTests.class);
- suite.addTestSuite(PersistTests.class);
+ //suite.addTestSuite(PersistTests.class);
+ suite.addTestSuite(WebAPITests.class);
suite.addTestSuite(ClientTests.class);
suite.addTestSuite(ScriptTests.class);
suite.addTest(TemplateSyncTestSuite.suite());
Modified: spaces/trunk/src/com/ogoglio/client/WebAPIAuthenticatorFactory.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/WebAPIAuthenticatorFactory.java 2007-08-06 17:34:13 UTC (rev 235)
+++ spaces/trunk/src/com/ogoglio/client/WebAPIAuthenticatorFactory.java 2007-08-06 19:41:08 UTC (rev 236)
@@ -5,13 +5,20 @@
public class WebAPIAuthenticatorFactory {
public WebAPIAuthenticator authenticate(WebAPIClientWire wire, WebAPIDescriptor descriptor, String username, String password) throws AuthenticationFailedException {
try {
- System.out.println("Twonk");
return new WebAPIAuthenticator(wire, descriptor, username, password);
} catch (IOException e) {
- throw new AuthenticationFailedException("Could not auth: " + e);
+ throw new AuthenticationFailedException("Could not auth via username (" + username + ") and pass: " + e);
}
}
+ public WebAPIAuthenticator authenticate(WebAPIClientWire wire, WebAPIDescriptor descriptor, String authCookie) throws AuthenticationFailedException {
+ try {
+ return new WebAPIAuthenticator(wire, descriptor, authCookie);
+ } catch (IOException e) {
+ throw new AuthenticationFailedException("Could not auth via cookie: " + e);
+ }
+ }
+
public WebAPIGuestAuthenticator authenticate(WebAPIDescriptor descriptor, String guestCookie) {
return new WebAPIGuestAuthenticator(descriptor, guestCookie);
}
Added: spaces/trunk/src/com/ogoglio/client/WebAPITests.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/WebAPITests.java (rev 0)
+++ spaces/trunk/src/com/ogoglio/client/WebAPITests.java 2007-08-06 19:41:08 UTC (rev 236)
@@ -0,0 +1,111 @@
+package com.ogoglio.client;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import com.ogoglio.persist.AccountRecord;
+import com.ogoglio.templatesync.OgoglioSpecBase;
+import com.ogoglio.xml.AccountDocument;
+import com.ogoglio.xml.AuthDocument;
+
+public class WebAPITests extends OgoglioSpecBase {
+
+ public static final String GOOD_COOKIE = "trGoodCookie";
+
+ private static final String BAD_COOKIE = "badBacCookie";
+
+ URI serviceURI = null;
+
+ WebAPIDescriptor descriptor = null;
+
+ WebAPIClientWire mockWire = null;
+
+ WebAPIAuthenticatorFactory mockAuthFactory = null;
+
+ AuthDocument notAuthedAuthDoc = null;
+
+ AuthDocument authedAuthDoc = null;
+
+ AccountDocument accountDoc = null;
+
+ public void setUp() {
+ try {
+ serviceURI = new URI("http://example.com/og/");
+ descriptor = new WebAPIDescriptor(serviceURI);
+
+ mockWire = (WebAPIClientWire) mock_ignoreEqualsAndToString(WebAPIClientWire.class);
+ mockAuthFactory = (WebAPIAuthenticatorFactory) mock_ignoreEqualsAndToString(WebAPIAuthenticatorFactory.class);
+
+ notAuthedAuthDoc = new AuthDocument("Ian", false);
+
+ authedAuthDoc = new AuthDocument("Ian", true);
+ accountDoc = new AccountDocument(authedAuthDoc.getUsername(), AccountRecord.ACCOUNT_LEVEL_ADVANCED, null, null, null, null, null, null, null, null, null, -1);
+ } catch (URISyntaxException e) {
+ fail("Bad URIL: " + e);
+ }
+ }
+
+ public void tearDown() {
+ }
+
+ public void testWebAPIAuthenticatorViaCookie() throws IOException, AuthenticationFailedException {
+ //mockWire should respond to good and bad cookies with good and bad auth docs
+ mockWire.fetchAuthenticatedXML(descriptor.getMeAuthURI(), BAD_COOKIE);
+ modify().returnValue(notAuthedAuthDoc.toElement());
+ mockWire.fetchAuthenticatedXML(descriptor.getMeAuthURI(), GOOD_COOKIE);
+ modify().returnValue(authedAuthDoc.toElement());
+
+ //mockWire should respond to good account requests with a good account doc
+ mockWire.fetchAuthenticatedXML(descriptor.getAccountURI(authedAuthDoc.getUsername()), GOOD_COOKIE);
+ modify().returnValue(accountDoc.toElement());
+
+ mockAuthFactory.authenticate(mockWire, descriptor, GOOD_COOKIE);
+ modify().args(is.AS_RECORDED, is.AS_RECORDED, is.ANYTHING);
+ modify().multiplicity(expect.atMost(2));
+ modify().forward();
+
+ expectThatExceptionThrown(is.instanceOf(AuthenticationFailedException.class));
+
+ //###################################
+ startVerification();
+
+ WebAPIAuthenticator auth = mockAuthFactory.authenticate(mockWire, descriptor, GOOD_COOKIE);
+ assertNotNull(auth);
+ assertEquals(GOOD_COOKIE, auth.getAuthCookie());
+ assertEquals(authedAuthDoc.getUsername(), auth.getUsername());
+ assertEquals(accountDoc, auth.getAccountDocument(true));
+
+ mockAuthFactory.authenticate(mockWire, descriptor, BAD_COOKIE);
+
+ }
+
+ public void testWebAPIAuthenticatorViaUsernameAndPass() throws AuthenticationFailedException, URISyntaxException, IOException {
+ mockWire.getAuthCookieViaPost(serviceURI, "username=Ian&password=farts", "text/plain");
+ modify().args(is.ANYTHING, is.AS_RECORDED, is.ANYTHING);
+ modify().returnValue(null);
+
+ mockWire.getAuthCookieViaPost(serviceURI, "username=Ian&password=goodPW", "text/plain");
+ modify().args(is.ANYTHING, is.AS_RECORDED, is.ANYTHING);
+ modify().returnValue(GOOD_COOKIE);
+
+ mockAuthFactory.authenticate(mockWire, descriptor, "Ian", "farts");
+ modify().forward();
+
+ mockAuthFactory.authenticate(mockWire, descriptor, "Ian", "goodPW");
+ modify().forward();
+
+ expectThatExceptionThrown(is.instanceOf(AuthenticationFailedException.class));
+
+ //###################################
+ startVerification();
+
+ //try good pw
+ WebAPIAuthenticator auth = mockAuthFactory.authenticate(mockWire, descriptor, "Ian", "goodPW");
+ assertThat(auth.getAuthCookie(), is.eq(GOOD_COOKIE));
+
+ //try bad pw
+ mockAuthFactory.authenticate(mockWire, descriptor, "Ian", "farts");
+ }
+
+}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-08-06 17:34:13 UTC (rev 235)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-08-06 19:41:08 UTC (rev 236)
@@ -110,37 +110,6 @@
checkWrongNumberOfArgs(2);
}
- public void testWebAPIAuthenticator() throws AuthenticationFailedException, URISyntaxException, IOException {
- WebAPIClientWire mockWire = (WebAPIClientWire) mock_ignoreEqualsAndToString(WebAPIClientWire.class, new Object[] {}, "mockWire");
- URI serviceURI = new URI("http://example.com/og/");
- mockWire.getAuthCookieViaPost(serviceURI, "username=Ian&password=farts", "text/plain");
- modify().args(is.ANYTHING, is.AS_RECORDED, is.ANYTHING);
- modify().returnValue(null);
-
- mockWire.getAuthCookieViaPost(serviceURI, "username=Ian&password=goodPW", "text/plain");
- modify().args(is.ANYTHING, is.AS_RECORDED, is.ANYTHING);
- modify().returnValue("cookieIsGood");
-
- WebAPIAuthenticatorFactory factory = (WebAPIAuthenticatorFactory) mock_ignoreEqualsAndToString(WebAPIAuthenticatorFactory.class, new Object[] {}, "mockAuthFactory");
- WebAPIDescriptor descriptor = new WebAPIDescriptor(serviceURI);
- factory.authenticate(mockWire, descriptor, "Ian", "farts");
- modify().forward();
-
- factory.authenticate(mockWire, descriptor, "Ian", "goodPW");
- modify().forward();
-
- expectThatExceptionThrown(is.instanceOf(AuthenticationFailedException.class));
-
- startVerification();
- //try good pw
- WebAPIAuthenticator auth = factory.authenticate(mockWire, descriptor, "Ian", "goodPW");
- assertThat(auth, is.NOT_NULL);
- assertThat(auth.getAuthCookie(), is.eq("cookieIsGood"));
-
- //try bad pw
- factory.authenticate(mockWire, descriptor, "Ian", "farts");
- }
-
//basically test syncing runs through add and delete
public void testStartupDoesAddDel() throws IOException, URISyntaxException {
File templateDir = new File("/some/path");
Modified: spaces/trunk/src/com/ogoglio/xml/AccountDocument.java
===================================================================
--- spaces/trunk/src/com/ogoglio/xml/AccountDocument.java 2007-08-06 17:34:13 UTC (rev 235)
+++ spaces/trunk/src/com/ogoglio/xml/AccountDocument.java 2007-08-06 19:41:08 UTC (rev 236)
@@ -182,4 +182,14 @@
}
}
+ public int hashCode() {
+ return getUsername().hashCode();
+ }
+
+ public boolean equals(Object obj) {
+ if(obj == null || !(obj instanceof AccountDocument)) {
+ return false;
+ }
+ return getUsername().equals(((AccountDocument)obj).getUsername());
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|