summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-20 15:24:25 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-20 19:39:49 +0100
commit7eec728142cc02305032a7e2f624f9d398b21e47 (patch)
tree34e70d62503c8af8d7696806038600df1ada441d /src/test
parentMerge pull request #1 from luxons/feature/websockets-sagas (diff)
downloadseven-wonders-7eec728142cc02305032a7e2f624f9d398b21e47.tar.gz
seven-wonders-7eec728142cc02305032a7e2f624f9d398b21e47.tar.bz2
seven-wonders-7eec728142cc02305032a7e2f624f9d398b21e47.zip
Add interceptor for invalid subscriptions
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java b/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java
new file mode 100644
index 00000000..1ae0b3fc
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java
@@ -0,0 +1,125 @@
+package org.luxons.sevenwonders.validation;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.luxons.sevenwonders.game.Game;
+import org.luxons.sevenwonders.game.Lobby;
+import org.luxons.sevenwonders.game.Player;
+import org.luxons.sevenwonders.game.Settings;
+import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
+import org.luxons.sevenwonders.repositories.GameRepository;
+import org.luxons.sevenwonders.repositories.LobbyRepository;
+import org.luxons.sevenwonders.repositories.LobbyRepository.LobbyNotFoundException;
+
+import static org.junit.Assert.*;
+
+public class DestinationAccessValidatorTest {
+
+ private LobbyRepository lobbyRepository;
+
+ private GameRepository gameRepository;
+
+ private DestinationAccessValidator destinationAccessValidator;
+
+ @Before
+ public void setup() {
+ gameRepository = new GameRepository();
+ lobbyRepository = new LobbyRepository(new GameDefinitionLoader());
+ destinationAccessValidator = new DestinationAccessValidator(lobbyRepository, gameRepository);
+ }
+
+ private Lobby createLobby(String gameName, String ownerUserName, String... otherPlayers) {
+ Player owner = new Player(ownerUserName, ownerUserName);
+ Lobby lobby = lobbyRepository.create(gameName, owner);
+ for (String playerName : otherPlayers) {
+ Player player = new Player(playerName, playerName);
+ lobby.addPlayer(player);
+ }
+ return lobby;
+ }
+
+ private Game createGame(String gameName, String ownerUserName, String... otherPlayers) {
+ Lobby lobby = createLobby(gameName, ownerUserName, otherPlayers);
+ Game game = lobby.startGame(new Settings());
+ gameRepository.add(game);
+ return game;
+ }
+
+ @Test
+ public void validate_successWhenNoReference() {
+ assertTrue(destinationAccessValidator.hasAccess("", ""));
+ assertTrue(destinationAccessValidator.hasAccess("", "test"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "test"));
+ }
+
+ @Test
+ public void validate_successWhenNoRefFollows() {
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/game/"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/lobby/"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "prefix/game/"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "prefix/lobby/"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/game//suffix"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/lobby//suffix"));
+ }
+
+ @Test
+ public void validate_successWhenRefIsNotANumber() {
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/game/notANumber"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/lobby/notANumber"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "prefix/game/notANumber"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "prefix/lobby/notANumber"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/game/notANumber/suffix"));
+ assertTrue(destinationAccessValidator.hasAccess("testUser", "/lobby/notANumber/suffix"));
+ }
+
+ @Test(expected = LobbyNotFoundException.class)
+ public void validate_failWhenNoLobbyExist() {
+ destinationAccessValidator.hasAccess("", "/lobby/0");
+ }
+
+ @Test(expected = LobbyNotFoundException.class)
+ public void validate_failWhenReferencedLobbyDoesNotExist() {
+ createLobby("Test Game", "ownerUser1");
+ createLobby("Test Game 2", "ownerUser2");
+ destinationAccessValidator.hasAccess("", "/lobby/3");
+ }
+
+ @Test
+ public void validate_failWhenUserIsNotPartOfReferencedLobby() {
+ createLobby("Test Game", "ownerUser");
+ destinationAccessValidator.hasAccess("", "/lobby/0");
+ }
+
+ @Test
+ public void validate_successWhenUserIsOwnerOfReferencedLobby() {
+ createLobby("Test Game 1", "user1");
+ assertTrue(destinationAccessValidator.hasAccess("user1", "/lobby/0"));
+ createLobby("Test Game 2", "user2");
+ assertTrue(destinationAccessValidator.hasAccess("user2", "/lobby/1"));
+ }
+
+ @Test
+ public void validate_successWhenUserIsMemberOfReferencedLobby() {
+ createLobby("Test Game 1", "user1", "user2");
+ assertTrue(destinationAccessValidator.hasAccess("user2", "/lobby/0"));
+ createLobby("Test Game 2", "user3", "user4");
+ assertTrue(destinationAccessValidator.hasAccess("user4", "/lobby/1"));
+ }
+
+ @Test
+ public void validate_successWhenUserIsMemberOfReferencedGame() {
+ createGame("Test Game 1", "user1", "user2", "user3");
+ assertTrue(destinationAccessValidator.hasAccess("user2", "/game/0"));
+ createGame("Test Game 2", "user4", "user5", "user6");
+ assertTrue(destinationAccessValidator.hasAccess("user6", "/game/1"));
+ }
+
+ @Test
+ public void validate_failsWhenUserPartOfReferencedGame() {
+// lobbyRepository.create("Test Game Name");
+// gameRepository.add();
+ assertTrue(destinationAccessValidator.hasAccess("", "/game/notAnId"));
+ assertTrue(destinationAccessValidator.hasAccess("", "/lobby/notAnId"));
+ }
+
+} \ No newline at end of file
bgstack15