Providing REST like authentication
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 13 Sep 2009 16:48:54 +0200
changeset 829ac7acee7d9f
parent 81 1174c9dcab41
child 83 8dd8b041a3e1
Providing REST like authentication
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
     1.1 --- a/freemarkerdor/pom.xml	Sat Sep 12 10:02:07 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Sun Sep 13 16:48:54 2009 +0200
     1.3 @@ -15,7 +15,7 @@
     1.4      <dependency>
     1.5        <groupId>${project.groupId}</groupId>
     1.6        <artifactId>webidor</artifactId>
     1.7 -      <version>1.0</version>
     1.8 +      <version>1.1</version>
     1.9      </dependency>
    1.10      <dependency>
    1.11        <groupId>org.netbeans.modules</groupId>
    1.12 @@ -100,7 +100,7 @@
    1.13      </plugin>
    1.14    </plugins>
    1.15    </build>
    1.16 -    <version>1.4</version>
    1.17 +  <version>1.5</version>
    1.18  </project>
    1.19  
    1.20  
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Sep 12 10:02:07 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Sep 13 16:48:54 2009 +0200
     2.3 @@ -35,14 +35,11 @@
     2.4  import com.sun.jersey.api.view.Viewable;
     2.5  import com.sun.net.httpserver.HttpServer;
     2.6  import java.io.File;
     2.7 -import java.io.FileInputStream;
     2.8 -import java.io.IOException;
     2.9  import java.net.URI;
    2.10  import java.util.HashMap;
    2.11  import java.util.Locale;
    2.12  import java.util.Map;
    2.13  import java.util.MissingResourceException;
    2.14 -import java.util.Properties;
    2.15  import java.util.ResourceBundle;
    2.16  import java.util.concurrent.Callable;
    2.17  import javax.ws.rs.DefaultValue;
    2.18 @@ -71,13 +68,16 @@
    2.19      @Context
    2.20      private HttpHeaders headers;
    2.21      private String user;
    2.22 +    private String uuid;
    2.23  
    2.24      public UI() {
    2.25      }
    2.26  
    2.27      private Viewable checkLogin() {
    2.28          if (headers.getCookies().containsKey("login")) {
    2.29 -            user = headers.getCookies().get("login").getValue();
    2.30 +            uuid = headers.getCookies().get("login").getValue();
    2.31 +            user = base.path("login").queryParam("id", uuid).
    2.32 +                accept(MediaType.TEXT_PLAIN).get(String.class);
    2.33              return null;
    2.34          }
    2.35          return viewable("login.fmt", null);
    2.36 @@ -89,16 +89,11 @@
    2.37      public Response login(
    2.38          @FormParam("name") String name, @FormParam("password") String password
    2.39      ) throws Exception {
    2.40 -        File f = new File(new File(System.getProperty("quoridor.dir")), "passwd");
    2.41 -        Properties p = new Properties();
    2.42 -        try {
    2.43 -            p.load(new FileInputStream(f));
    2.44 -        } catch (IOException ex) {
    2.45 -            ex.printStackTrace();
    2.46 -        }
    2.47 -        if (name != null && password.equals(p.getProperty(name))) {
    2.48 +        uuid = base.path("login").queryParam("name", name).queryParam("password", password).
    2.49 +            accept(MediaType.TEXT_PLAIN).put(String.class);
    2.50 +        if (uuid != null) {
    2.51              user = name;
    2.52 -            return Response.ok().cookie(new NewCookie("login", name)).entity(viewable("login.fmt", null)).build();
    2.53 +            return Response.ok().cookie(new NewCookie("login", uuid)).entity(viewable("login.fmt", null)).build();
    2.54          } else {
    2.55              Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
    2.56              return Response.status(1).entity(v).build();
    2.57 @@ -145,7 +140,9 @@
    2.58          if (v != null) {
    2.59              return v;
    2.60          }
    2.61 -        WebResource wr = base.path("games").path(id).queryParam("player", user);
    2.62 +        WebResource wr = base.path("games").path(id).
    2.63 +            queryParam("loginID", uuid).
    2.64 +            queryParam("player", user);
    2.65          try {
    2.66              if (type.equals("resign")) {
    2.67                  wr.queryParam("move", "RESIGN").put();
    2.68 @@ -179,7 +176,9 @@
    2.69  
    2.70          if (user.equals(white) || user.equals(black)) {
    2.71              Object obj =
    2.72 -                base.path("games").queryParam("white", white).
    2.73 +                base.path("games").
    2.74 +                queryParam("loginID", uuid).
    2.75 +                queryParam("white", white).
    2.76                  queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
    2.77              return Response.ok(welcomeImpl()).build();
    2.78          } else {
     3.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sat Sep 12 10:02:07 2009 +0200
     3.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Sep 13 16:48:54 2009 +0200
     3.3 @@ -38,6 +38,7 @@
     3.4  import java.util.Locale;
     3.5  import java.util.concurrent.Callable;
     3.6  import javax.ws.rs.core.Cookie;
     3.7 +import javax.ws.rs.core.MediaType;
     3.8  import javax.ws.rs.core.MultivaluedMap;
     3.9  import org.junit.AfterClass;
    3.10  import org.junit.Before;
    3.11 @@ -68,7 +69,7 @@
    3.12  
    3.13          File passwd = new File(dir, "passwd");
    3.14          FileOutputStream os = new FileOutputStream(passwd);
    3.15 -        os.write("test=pes\n".getBytes("UTF-8"));
    3.16 +        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
    3.17          os.close();
    3.18      }
    3.19  
    3.20 @@ -111,6 +112,13 @@
    3.21      }
    3.22  
    3.23      @Test public void testGetIndexPage() throws Exception {
    3.24 +        String logJarda = webResource.path("api/login").
    3.25 +            queryParam("name", "Jarda").
    3.26 +            queryParam("password", "darda").
    3.27 +            accept(MediaType.TEXT_PLAIN).
    3.28 +            put(String.class);
    3.29 +        assertNotNull("Logged in OK", logJarda);
    3.30 +
    3.31          String res = webResource.accept("text/html").get(String.class);
    3.32          if (res.indexOf("Quoridor") == -1) {
    3.33              fail("Wrong index.html:\n" + res);
    3.34 @@ -124,7 +132,7 @@
    3.35          if (res.toLowerCase().indexOf("error") != -1) {
    3.36              fail("There was an error:\n" + res);
    3.37          }
    3.38 -        res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class);
    3.39 +        res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
    3.40          if (res.indexOf("action=\"/games/create\"") == -1) {
    3.41              fail(res);
    3.42          }
    3.43 @@ -146,10 +154,15 @@
    3.44  
    3.45  
    3.46      @Test public void testCreateGameWrongUsers() throws Exception {
    3.47 +        String logTest = webResource.path("api/login").
    3.48 +            queryParam("name", "test").
    3.49 +            queryParam("password", "pes").
    3.50 +            accept(MediaType.TEXT_PLAIN).
    3.51 +            put(String.class);
    3.52          ClientResponse res = webResource.path("games/create").
    3.53              queryParam("white", "unknown1").
    3.54              queryParam("black", "unknown2").
    3.55 -            cookie(Cookie.valueOf("login=test")).
    3.56 +            cookie(Cookie.valueOf("login=" + logTest)).
    3.57              accept("text/html").
    3.58              get(ClientResponse.class);
    3.59          final String txt = res.getEntity(String.class);
    3.60 @@ -160,10 +173,15 @@
    3.61      }
    3.62  
    3.63      @Test public void testCreateGameOK() throws Exception {
    3.64 +        String logTest = webResource.path("api/login").
    3.65 +            queryParam("name", "test").
    3.66 +            queryParam("password", "pes").
    3.67 +            accept(MediaType.TEXT_PLAIN).
    3.68 +            put(String.class);
    3.69          ClientResponse res = webResource.path("games/create").
    3.70              queryParam("white", "unknown1").
    3.71              queryParam("black", "test").
    3.72 -            cookie(Cookie.valueOf("login=test")).
    3.73 +            cookie(Cookie.valueOf("login=" + logTest)).
    3.74              accept("text/html").
    3.75              get(ClientResponse.class);
    3.76  
     4.1 --- a/webidor/pom.xml	Sat Sep 12 10:02:07 2009 +0200
     4.2 +++ b/webidor/pom.xml	Sun Sep 13 16:48:54 2009 +0200
     4.3 @@ -9,7 +9,7 @@
     4.4    <groupId>org.apidesign</groupId>
     4.5    <artifactId>webidor</artifactId>
     4.6    <packaging>jar</packaging>
     4.7 -  <version>1.0</version>
     4.8 +  <version>1.1</version>
     4.9    <name>webidor server</name>
    4.10    <url>http://maven.apache.org</url>
    4.11    <repositories>
     5.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sat Sep 12 10:02:07 2009 +0200
     5.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Sep 13 16:48:54 2009 +0200
     5.3 @@ -48,19 +48,23 @@
     5.4  import javax.ws.rs.PathParam;
     5.5  import javax.ws.rs.Produces;
     5.6  import javax.ws.rs.QueryParam;
     5.7 +import javax.ws.rs.WebApplicationException;
     5.8  import javax.ws.rs.core.MediaType;
     5.9 +import javax.ws.rs.core.Response.Status;
    5.10  
    5.11  /**
    5.12   *
    5.13   * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.14   */
    5.15  public final class Games {
    5.16 +    private final Quoridor quoridor;
    5.17      private final List<Game> games = new ArrayList<Game>();
    5.18      private final File dir;
    5.19      private static final Logger LOG = Logger.getLogger(Games.class.getName());
    5.20  
    5.21 -    public Games(File dir) {
    5.22 +    public Games(File dir, Quoridor quoridor) {
    5.23          this.dir = dir;
    5.24 +        this.quoridor = quoridor;
    5.25          File[] arr = dir.listFiles();
    5.26          if (arr != null) {
    5.27              for (File f : arr) {
    5.28 @@ -77,15 +81,24 @@
    5.29      @POST
    5.30      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    5.31      public GameId createGame(
    5.32 +        @QueryParam("loginID") String id,
    5.33          @QueryParam("white") String white,
    5.34          @QueryParam("black") String black
    5.35      ) throws IOException {
    5.36 +        String logUser = quoridor.isLoggedIn(id);
    5.37 +        if (logUser == null) {
    5.38 +            throw new WebApplicationException(Status.UNAUTHORIZED);
    5.39 +        }
    5.40          if (white == null) {
    5.41 -            throw new IllegalArgumentException("Must specify white");
    5.42 +            throw new WebApplicationException(Status.PRECONDITION_FAILED);
    5.43          }
    5.44          if (black == null) {
    5.45 -            throw new IllegalArgumentException("Must specify black");
    5.46 +            throw new WebApplicationException(Status.PRECONDITION_FAILED);
    5.47          }
    5.48 +        if (!logUser.equals(white) && !logUser.equals(black)) {
    5.49 +            throw new WebApplicationException(Status.PRECONDITION_FAILED);
    5.50 +        }
    5.51 +
    5.52          Game g = new Game(white, black);
    5.53          storeGame(g);
    5.54          games.add(g);
    5.55 @@ -118,10 +131,19 @@
    5.56      @Path("{id}")
    5.57      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    5.58      public GameId applyMove(
    5.59 +        @QueryParam("loginID") String loginId,
    5.60          @PathParam("id") String id,
    5.61          @QueryParam("player") String player,
    5.62          @QueryParam("move") String move
    5.63      ) throws IllegalPositionException {
    5.64 +        String logUser = quoridor.isLoggedIn(loginId);
    5.65 +        if (logUser == null) {
    5.66 +            throw new WebApplicationException(Status.UNAUTHORIZED);
    5.67 +        }
    5.68 +        if (!logUser.equals(player)) {
    5.69 +            throw new WebApplicationException(Status.UNAUTHORIZED);
    5.70 +        }
    5.71 +
    5.72          Game g = findGame(id);
    5.73          if (g == null) {
    5.74              throw new IllegalArgumentException("Unknown game " + id);
     6.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sat Sep 12 10:02:07 2009 +0200
     6.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Sep 13 16:48:54 2009 +0200
     6.3 @@ -32,9 +32,19 @@
     6.4  import com.sun.jersey.spi.resource.Singleton;
     6.5  import com.sun.net.httpserver.HttpServer;
     6.6  import java.io.File;
     6.7 +import java.io.FileInputStream;
     6.8  import java.io.IOException;
     6.9  import java.net.ServerSocket;
    6.10 +import java.util.HashMap;
    6.11 +import java.util.Map;
    6.12 +import java.util.Properties;
    6.13 +import java.util.UUID;
    6.14 +import javax.ws.rs.GET;
    6.15 +import javax.ws.rs.PUT;
    6.16  import javax.ws.rs.Path;
    6.17 +import javax.ws.rs.Produces;
    6.18 +import javax.ws.rs.QueryParam;
    6.19 +import javax.ws.rs.core.MediaType;
    6.20  
    6.21  /**
    6.22   *
    6.23 @@ -45,6 +55,7 @@
    6.24  public final class Quoridor {
    6.25      private final File path;
    6.26      private Games games;
    6.27 +    private final Map<UUID,String> loggedIn;
    6.28  
    6.29      public Quoridor() {
    6.30          final String prop = System.getProperty("quoridor.dir"); // NOI18N
    6.31 @@ -53,16 +64,50 @@
    6.32          }
    6.33          path = new File(prop);
    6.34          path.mkdirs();
    6.35 +        loggedIn = new HashMap<UUID, String>();
    6.36      }
    6.37  
    6.38      @Path("games")
    6.39      public Games getGames() {
    6.40          if (games == null) {
    6.41 -            games = new Games(new File(path, "games"));
    6.42 +            games = new Games(new File(path, "games"), this); // NOI18N
    6.43          }
    6.44          return games;
    6.45      }
    6.46  
    6.47 +    @Path("login")
    6.48 +    @PUT
    6.49 +    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    6.50 +    public String login(
    6.51 +        @QueryParam("name") String name,
    6.52 +        @QueryParam("password") String password
    6.53 +    ) {
    6.54 +        File f = new File(path, "passwd"); // NOI18Nt
    6.55 +        Properties p = new Properties();
    6.56 +        try {
    6.57 +            p.load(new FileInputStream(f));
    6.58 +        } catch (IOException ex) {
    6.59 +            ex.printStackTrace();
    6.60 +        }
    6.61 +        if (name != null && password.equals(p.getProperty(name))) {
    6.62 +            UUID uuid = UUID.randomUUID();
    6.63 +            loggedIn.put(uuid, name);
    6.64 +            return uuid.toString();
    6.65 +        } else {
    6.66 +            return null;
    6.67 +        }
    6.68 +        
    6.69 +    }
    6.70 +
    6.71 +    @Path("login")
    6.72 +    @GET
    6.73 +    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    6.74 +    public String isLoggedIn(
    6.75 +        @QueryParam("id") String id
    6.76 +    ) {
    6.77 +        return id == null ? null : loggedIn.get(UUID.fromString(id));
    6.78 +    }
    6.79 +
    6.80      //
    6.81      // start the server
    6.82      //
     7.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Sat Sep 12 10:02:07 2009 +0200
     7.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Sun Sep 13 16:48:54 2009 +0200
     7.3 @@ -28,6 +28,7 @@
     7.4  
     7.5  import com.sun.jersey.test.framework.JerseyTest;
     7.6  import java.io.File;
     7.7 +import java.io.FileOutputStream;
     7.8  import java.io.IOException;
     7.9  import javax.ws.rs.core.MediaType;
    7.10  import org.junit.Test;
    7.11 @@ -49,6 +50,11 @@
    7.12          dir = File.createTempFile("quoridor", ".dir");
    7.13          dir.delete();
    7.14          System.setProperty("quoridor.dir", dir.getPath());
    7.15 +        dir.mkdirs();
    7.16 +        File passwd = new File(dir, "passwd");
    7.17 +        FileOutputStream os = new FileOutputStream(passwd);
    7.18 +        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    7.19 +        os.close();
    7.20          super.setUp();
    7.21      }
    7.22  
    7.23 @@ -73,20 +79,44 @@
    7.24  
    7.25      @Test public void testCreateAGame() throws Exception {
    7.26          webResource = webResource.path("api");
    7.27 +        String logJarda = webResource.path("login").
    7.28 +            queryParam("name", "Jarda").
    7.29 +            queryParam("password", "heslo").
    7.30 +            accept(MediaType.TEXT_PLAIN).
    7.31 +            put(String.class);
    7.32 +        String logJirka = webResource.path("login").
    7.33 +            queryParam("name", "Jirka").
    7.34 +            queryParam("password", "pesko").
    7.35 +            accept(MediaType.TEXT_PLAIN).
    7.36 +            put(String.class);
    7.37 +        assertNotNull("Logged in ok", logJarda);
    7.38          GameId s = webResource.path("games").queryParam("white", "Jarda")
    7.39 +                .queryParam("loginID", logJarda)
    7.40                  .queryParam("black", "Jirka").post(GameId.class);
    7.41  
    7.42          for (int i = 0; i < 3; i++) {
    7.43 -            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    7.44 -            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    7.45 +            webResource.path("games/" + s.getId())
    7.46 +                .queryParam("loginID", logJarda)
    7.47 +                .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    7.48 +            webResource.path("games/" + s.getId())
    7.49 +                .queryParam("loginID", logJirka)
    7.50 +                .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    7.51          }
    7.52  
    7.53 -        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    7.54 -        webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
    7.55 +        webResource.path("games/" + s.getId())
    7.56 +            .queryParam("loginID", logJarda)
    7.57 +            .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    7.58 +        webResource.path("games/" + s.getId())
    7.59 +            .queryParam("loginID", logJirka)
    7.60 +            .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
    7.61  
    7.62          for (int i = 0; i < 3; i++) {
    7.63 -            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    7.64 -            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    7.65 +            webResource.path("games/" + s.getId())
    7.66 +                .queryParam("loginID", logJarda)
    7.67 +                .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    7.68 +            webResource.path("games/" + s.getId())
    7.69 +                .queryParam("loginID", logJirka)
    7.70 +                .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    7.71          }
    7.72  
    7.73          Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    7.74 @@ -95,10 +125,19 @@
    7.75  
    7.76      @Test public void testResignAGame() throws Exception {
    7.77          webResource = webResource.path("api");
    7.78 +        String logJarda = webResource.path("login").
    7.79 +            queryParam("name", "Jarda").
    7.80 +            queryParam("password", "heslo").
    7.81 +            accept(MediaType.TEXT_PLAIN).
    7.82 +            put(String.class);
    7.83          GameId s = webResource.path("games").queryParam("white", "Jarda")
    7.84 +                .queryParam("loginID", logJarda)
    7.85                  .queryParam("black", "Jirka").post(GameId.class);
    7.86  
    7.87 -        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "RESIGN").put(GameId.class);
    7.88 +        webResource.path("games/" + s.getId()).
    7.89 +            queryParam("loginID", logJarda).
    7.90 +            queryParam("player", "Jarda").
    7.91 +            queryParam("move", "RESIGN").put(GameId.class);
    7.92          Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    7.93          assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
    7.94      }
     8.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Sat Sep 12 10:02:07 2009 +0200
     8.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Sun Sep 13 16:48:54 2009 +0200
     8.3 @@ -27,6 +27,7 @@
     8.4  package cz.xelfi.quoridor.webidor;
     8.5  
     8.6  import cz.xelfi.quoridor.webidor.resources.Games;
     8.7 +import cz.xelfi.quoridor.webidor.resources.Quoridor;
     8.8  import java.io.File;
     8.9  import java.io.FileOutputStream;
    8.10  import java.io.IOException;
    8.11 @@ -80,7 +81,7 @@
    8.12  
    8.13          Thread.sleep(1000);
    8.14  
    8.15 -        Games games = new Games(dir);
    8.16 +        Games games = new Games(dir, new Quoridor());
    8.17          Game g = games.getBoardInfo("x");
    8.18          assertNotNull("Game found", g);
    8.19          assertNotNull("Board found", g.getBoard());
     9.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sat Sep 12 10:02:07 2009 +0200
     9.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sun Sep 13 16:48:54 2009 +0200
     9.3 @@ -33,7 +33,9 @@
     9.4  import cz.xelfi.quoridor.Board;
     9.5  import cz.xelfi.quoridor.Move;
     9.6  import cz.xelfi.quoridor.webidor.resources.Games;
     9.7 +import cz.xelfi.quoridor.webidor.resources.Quoridor;
     9.8  import java.io.File;
     9.9 +import java.io.FileOutputStream;
    9.10  import java.io.FileReader;
    9.11  import java.io.IOException;
    9.12  import java.util.List;
    9.13 @@ -59,6 +61,11 @@
    9.14          dir = File.createTempFile("quoridor", ".dir");
    9.15          dir.delete();
    9.16          System.setProperty("quoridor.dir", dir.getPath());
    9.17 +        dir.mkdirs();
    9.18 +        File passwd = new File(dir, "passwd");
    9.19 +        FileOutputStream os = new FileOutputStream(passwd);
    9.20 +        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    9.21 +        os.close();
    9.22          super.setUp();
    9.23      }
    9.24  
    9.25 @@ -93,7 +100,18 @@
    9.26  
    9.27      @Test public void testCreateAGame() throws Exception {
    9.28          webResource = webResource.path("api");
    9.29 -        GameId s = webResource.path("games").queryParam("white", "Jarda")
    9.30 +        String logJarda = webResource.path("login").
    9.31 +            queryParam("name", "Jarda").
    9.32 +            queryParam("password", "heslo").
    9.33 +            accept(MediaType.TEXT_PLAIN).
    9.34 +            put(String.class);
    9.35 +        String logJirka = webResource.path("login").
    9.36 +            queryParam("name", "Jirka").
    9.37 +            queryParam("password", "pesko").
    9.38 +            accept(MediaType.TEXT_PLAIN).
    9.39 +            put(String.class);
    9.40 +    GameId s = webResource.path("games").queryParam("loginID", logJarda).
    9.41 +                queryParam("white", "Jarda")
    9.42                  .queryParam("black", "Jirka").post(GameId.class);
    9.43  
    9.44          Thread.sleep(100);
    9.45 @@ -117,20 +135,28 @@
    9.46          assertEquals("Same white", "Jarda", games.get(0).getWhite());
    9.47          assertEquals("Same black", "Jirka", games.get(0).getBlack());
    9.48  
    9.49 -        GameId s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    9.50 +        GameId s1 = webResource.path("games/" + s.getId()).
    9.51 +            queryParam("loginID", logJarda).
    9.52 +            queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    9.53          try {
    9.54 -            GameId s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    9.55 +            GameId s2 = webResource.path("games/" + s.getId()).
    9.56 +                queryParam("loginID", logJarda).
    9.57 +                queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    9.58              fail("Not Jarda's turn, previous call shall fail");
    9.59          } catch (UniformInterfaceException ex) {
    9.60              // OK
    9.61          }
    9.62          try {
    9.63 -            GameId s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
    9.64 +            GameId s2 = webResource.path("games/" + s.getId()).
    9.65 +                queryParam("loginID", logJirka).
    9.66 +                queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
    9.67              fail("Invalid move");
    9.68          } catch (UniformInterfaceException ex) {
    9.69              // OK
    9.70          }
    9.71 -        GameId s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    9.72 +        GameId s2 = webResource.path("games/" + s.getId()).
    9.73 +            queryParam("loginID", logJirka).
    9.74 +            queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    9.75          assertNotNull("Successful move", s2);
    9.76          if (s2.getModified() <= now) {
    9.77              fail("The game is newly modified");
    9.78 @@ -154,7 +180,7 @@
    9.79              fail(content);
    9.80          }
    9.81  
    9.82 -        Games read = new Games(new File(dir, "games"));
    9.83 +        Games read = new Games(new File(dir, "games"), new Quoridor());
    9.84          List<Game> readGames = read.getGames();
    9.85          assertEquals("One game read", 1, readGames.size());
    9.86          Board board = readGames.get(0).getBoard();