# HG changeset patch # User Jaroslav Tulach # Date 1252416555 -7200 # Node ID b3165f3a9ad75903a4e4f2c52d4ea0255174aaeb # Parent 5f081edc85026153cc871c02b34b1995c10386aa More tests to check behaviour of "UI", querying for text/xml in create new game page diff -r 5f081edc8502 -r b3165f3a9ad7 freemarkerdor/pom.xml --- a/freemarkerdor/pom.xml Tue Sep 08 07:54:12 2009 +0200 +++ b/freemarkerdor/pom.xml Tue Sep 08 15:29:15 2009 +0200 @@ -100,17 +100,6 @@ - 1.0 + 1.1 - - - - - - - - - - - diff -r 5f081edc8502 -r b3165f3a9ad7 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Tue Sep 08 07:54:12 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Tue Sep 08 15:29:15 2009 +0200 @@ -90,7 +90,7 @@ public Response login( @FormParam("name") String name, @FormParam("password") String password ) throws Exception { - File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd"); + File f = new File(new File(System.getProperty("quoridor.dir")), "passwd"); Properties p = new Properties(); try { p.load(new FileInputStream(f)); @@ -165,22 +165,23 @@ @GET @Path("games/create") @Produces(MediaType.TEXT_HTML) - public Viewable create( + public Response create( @QueryParam("white") String white, @QueryParam("black") String black ) { Viewable v = checkLogin(); if (v != null) { - return v; + return Response.status(Response.Status.FORBIDDEN).entity(v).build(); } if (user.equals(white) || user.equals(black)) { Object obj = base.path("games").queryParam("white", white). - queryParam("black", black).post(Document.class); - return welcomeImpl(); + queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class); + return Response.ok(welcomeImpl()).build(); } else { - return welcomeImpl("message", "You (" + user + ") must be white or black!"); + return Response.status(Response.Status.NOT_FOUND). + entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build(); } } @@ -215,10 +216,12 @@ } static Callable startServers(int port) throws Exception { - File home = new File(System.getProperty("user.home")); - File quoridor = new File(home, ".quoridor"); - System.setProperty("quoridor.dir", quoridor.getPath()); + if (System.getProperty("quoridor.dir") == null) { + File home = new File(System.getProperty("user.home")); + File quoridor = new File(home, ".quoridor"); + System.setProperty("quoridor.dir", quoridor.getPath()); + } ResourceConfig rc = new PackagesResourceConfig( "cz.xelfi.quoridor.webidor", diff -r 5f081edc8502 -r b3165f3a9ad7 freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Tue Sep 08 07:54:12 2009 +0200 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Tue Sep 08 15:29:15 2009 +0200 @@ -27,15 +27,19 @@ package cz.xelfi.quoridor.freemarkerdor; import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.core.header.MediaTypes; +import com.sun.jersey.core.util.MultivaluedMapImpl; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import java.util.Locale; import java.util.concurrent.Callable; import javax.ws.rs.core.Cookie; -import org.junit.After; +import javax.ws.rs.core.MultivaluedMap; +import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -46,31 +50,37 @@ * @author Jaroslav Tulach */ public class UITest extends Object { - private File dir; - private Callable stop; + private static File dir; + private static Callable stop; private WebResource webResource; public UITest() throws Exception { } @BeforeClass - public static void localeEnglish() { + public static void localeEnglish() throws Exception { Locale.setDefault(Locale.ENGLISH); + dir = File.createTempFile("quoridor", ".dir"); + dir.delete(); + dir.mkdirs(); + System.setProperty("quoridor.dir", dir.getPath()); + stop = UI.startServers(9991); + + File passwd = new File(dir, "passwd"); + FileOutputStream os = new FileOutputStream(passwd); + os.write("test=pes\n".getBytes("UTF-8")); + os.close(); } @Before public void setUp() throws Exception { - dir = File.createTempFile("quoridor", ".dir"); - dir.delete(); - System.setProperty("quoridor.dir", dir.getPath()); - stop = UI.startServers(9991); Client client = new Client(); webResource = client.resource(new URI("http://localhost:9991/")); } - @After - public void tearDown() throws Exception { + @AfterClass + public static void cleanUpAll() throws Exception { deleteRec(dir); if (stop != null) { stop.call(); @@ -120,4 +130,52 @@ } } + @Test public void testLogin() throws Exception { + MultivaluedMap formData = new MultivaluedMapImpl(); + formData.add("name", "test"); + formData.add("password", "pes"); + ClientResponse response = webResource.path("login"). + accept("text/html").type("application/x-www-form-urlencoded"). + post(ClientResponse.class, formData); + final String res = response.getEntity(String.class); + assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus()); + if (res.indexOf("You are logged as test") == -1) { + fail("res: " + res); + } + } + + + @Test public void testCreateGameWrongUsers() throws Exception { + ClientResponse res = webResource.path("games/create"). + queryParam("white", "unknown1"). + queryParam("black", "unknown2"). + cookie(Cookie.valueOf("login=test")). + accept("text/html"). + get(ClientResponse.class); + final String txt = res.getEntity(String.class); + assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus()); + if (txt.indexOf("You (test) must be") == -1) { + fail(txt); + } + } + + @Test public void testCreateGameOK() throws Exception { + ClientResponse res = webResource.path("games/create"). + queryParam("white", "unknown1"). + queryParam("black", "test"). + cookie(Cookie.valueOf("login=test")). + accept("text/html"). + get(ClientResponse.class); + + final String txt = res.getEntity(String.class); + assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus()); + + String[] games = new File(dir, "games").list(); + assertEquals("One game exists", 1, games.length); + + if (txt.indexOf(games[0]) == -1) { + fail(games[0] + " expected inside of:\n" + txt); + } + } + }