Only active games are visible for regular users. Need a bit more work to allow them to see history of active games. strict-games-access
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 08 Dec 2009 15:34:50 +0100
branchstrict-games-access
changeset 162c1bfbe98152b
parent 160 9e862a62de85
child 163 2a870f6d560e
Only active games are visible for regular users. Need a bit more work to allow them to see history of active games.
webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java	Tue Dec 01 09:40:54 2009 +0100
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java	Tue Dec 08 15:34:50 2009 +0100
     1.3 @@ -52,4 +52,10 @@
     1.4              return board.getCurrentPlayer() == board.getPlayers().get(0) ? GameStatus.whiteMove : GameStatus.blackMove;
     1.5          }
     1.6      }
     1.7 +
     1.8 +    /** @return true if the game is in progress
     1.9 +     */
    1.10 +    public boolean isInProgress() {
    1.11 +        return this == whiteMove || this == blackMove;
    1.12 +    }
    1.13  }
     2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Tue Dec 01 09:40:54 2009 +0100
     2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Tue Dec 08 15:34:50 2009 +0100
     2.3 @@ -131,10 +131,25 @@
     2.4      @Path("{id}")
     2.5      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
     2.6      public Game getBoardInfo(
     2.7 +        @QueryParam("loginID") @DefaultValue("") String loginId,
     2.8          @PathParam("id") String id,
     2.9          @QueryParam("move") @DefaultValue("-1") int move
    2.10      ) {
    2.11 -        return findGame(id, move);
    2.12 +        Game g = findGame(id, move);
    2.13 +        if (g.getId().getStatus().isInProgress()) {
    2.14 +            return g;
    2.15 +        }
    2.16 +        String logUser = quoridor.isLoggedIn(loginId);
    2.17 +        if (logUser == null) {
    2.18 +            throw new WebApplicationException(Status.UNAUTHORIZED);
    2.19 +        }
    2.20 +        if (logUser.equals(g.getId().getWhite())) {
    2.21 +            return g;
    2.22 +        }
    2.23 +        if (logUser.equals(g.getId().getBlack())) {
    2.24 +            return g;
    2.25 +        }
    2.26 +        throw new WebApplicationException(Status.UNAUTHORIZED);
    2.27      }
    2.28  
    2.29      @PUT
     3.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Tue Dec 01 09:40:54 2009 +0100
     3.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Tue Dec 08 15:34:50 2009 +0100
     3.3 @@ -27,6 +27,7 @@
     3.4  package cz.xelfi.quoridor.webidor;
     3.5  
     3.6  import com.sun.jersey.api.client.GenericType;
     3.7 +import com.sun.jersey.api.client.UniformInterfaceException;
     3.8  import com.sun.jersey.test.framework.JerseyTest;
     3.9  import java.io.File;
    3.10  import java.io.FileOutputStream;
    3.11 @@ -135,7 +136,13 @@
    3.12          }
    3.13  
    3.14  
    3.15 -        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    3.16 +        try {
    3.17 +            Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    3.18 +            fail("If the game is finished, one cannot get its status without login");
    3.19 +        } catch (UniformInterfaceException ex) {
    3.20 +            // OK
    3.21 +        }
    3.22 +        Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
    3.23          assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
    3.24  
    3.25          assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
    3.26 @@ -156,13 +163,23 @@
    3.27                  .queryParam("loginID", logJarda)
    3.28                  .queryParam("black", "Jirka").post(GameId.class);
    3.29  
    3.30 +        assertTrue("In progress", s.getStatus().isInProgress());
    3.31 +
    3.32          webResource.path("games/" + s.getId()).
    3.33              queryParam("loginID", logJarda).
    3.34              queryParam("player", "Jarda").
    3.35              queryParam("move", "RESIGN").put(GameId.class);
    3.36 -        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    3.37 +        try {
    3.38 +            Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    3.39 +            fail("Should not be able to get game when finished");
    3.40 +        } catch (UniformInterfaceException ex) {
    3.41 +            // OK
    3.42 +        }
    3.43 +        Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
    3.44          assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
    3.45          assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
    3.46 +
    3.47 +        assertFalse("is finished", end.getId().getStatus().isInProgress());
    3.48      }
    3.49  
    3.50  }
     4.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Tue Dec 01 09:40:54 2009 +0100
     4.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Tue Dec 08 15:34:50 2009 +0100
     4.3 @@ -86,7 +86,7 @@
     4.4          Thread.sleep(1000);
     4.5  
     4.6          Games games = new Games(dir, new Quoridor());
     4.7 -        Game g = games.getBoardInfo("x", -1);
     4.8 +        Game g = games.getBoardInfo("", "x", -1);
     4.9          assertNotNull("Game found", g);
    4.10          assertNotNull("Board found", g.getBoard());
    4.11          assertEquals("List of moves has two", 2, g.getMoves().size());
    4.12 @@ -108,7 +108,7 @@
    4.13          Thread.sleep(1000);
    4.14  
    4.15          Games games = new Games(dir, new Quoridor());
    4.16 -        Game g = games.getBoardInfo("x", -1);
    4.17 +        Game g = games.getBoardInfo("", "x", -1);
    4.18          assertNotNull("Game found", g);
    4.19          assertNotNull("Board found", g.getBoard());
    4.20          assertEquals("List of moves has two", 2, g.getMoves().size());
    4.21 @@ -133,7 +133,7 @@
    4.22          Thread.sleep(1000);
    4.23  
    4.24          Games games = new Games(dir, new Quoridor());
    4.25 -        Game g = games.getBoardInfo("x", -1);
    4.26 +        Game g = games.getBoardInfo("", "x", -1);
    4.27          assertNotNull("Game found", g);
    4.28          assertNotNull("Board found", g.getBoard());
    4.29          assertEquals("List of moves has two", 2, g.getMoves().size());