diff options
author | jbion <joffrey.bion@amadeus.com> | 2017-05-27 23:01:11 +0200 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2017-05-27 23:01:11 +0200 |
commit | cfb287f6d29c87e5a74fadeb2e027ece403485b0 (patch) | |
tree | 6d98c4f60cbe56763e8d361a54b34fead7d025f1 /backend | |
parent | Improve forbidden subscription exception message (diff) | |
download | seven-wonders-cfb287f6d29c87e5a74fadeb2e027ece403485b0.tar.gz seven-wonders-cfb287f6d29c87e5a74fadeb2e027ece403485b0.tar.bz2 seven-wonders-cfb287f6d29c87e5a74fadeb2e027ece403485b0.zip |
Remove ForbiddenSubscriptionException
The exception was not processed by the ExceptionHandler anyway, so
nothing was sent to the user. We need to find a way to send an error to
the user. In the meantime, an error log will suffice.
Diffstat (limited to 'backend')
3 files changed, 49 insertions, 16 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/config/TopicSubscriptionInterceptor.java b/backend/src/main/java/org/luxons/sevenwonders/config/TopicSubscriptionInterceptor.java index 27f3bd24..855b7800 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/config/TopicSubscriptionInterceptor.java +++ b/backend/src/main/java/org/luxons/sevenwonders/config/TopicSubscriptionInterceptor.java @@ -1,7 +1,8 @@ package org.luxons.sevenwonders.config; -import org.luxons.sevenwonders.errors.ApiMisuseException; import org.luxons.sevenwonders.validation.DestinationAccessValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @@ -13,6 +14,8 @@ import org.springframework.stereotype.Component; @Component public class TopicSubscriptionInterceptor extends ChannelInterceptorAdapter { + private static final Logger logger = LoggerFactory.getLogger(TopicSubscriptionInterceptor.class); + private final DestinationAccessValidator destinationAccessValidator; @Autowired @@ -27,16 +30,14 @@ public class TopicSubscriptionInterceptor extends ChannelInterceptorAdapter { String username = headerAccessor.getUser().getName(); String destination = headerAccessor.getDestination(); if (!destinationAccessValidator.hasAccess(username, destination)) { - throw new ForbiddenSubscriptionException(username, destination); + sendForbiddenSubscriptionError(username, destination); + return null; } } return message; } - private static class ForbiddenSubscriptionException extends ApiMisuseException { - - ForbiddenSubscriptionException(String username, String destination) { - super(String.format("Player '%s' is not allowed to access %s", username, destination)); - } + private void sendForbiddenSubscriptionError(String username, String destination) { + logger.error(String.format("Player '%s' is not allowed to access %s", username, destination)); } } diff --git a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java index d6415376..fe0c4d83 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java @@ -1,6 +1,7 @@ package org.luxons.sevenwonders; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.After; @@ -14,6 +15,7 @@ import org.luxons.sevenwonders.test.api.ApiPlayerTurnInfo; import org.luxons.sevenwonders.test.api.SevenWondersClient; import org.luxons.sevenwonders.test.api.SevenWondersSession; import org.luxons.sevenwonders.test.client.Channel; +import org.luxons.sevenwonders.test.client.JackstompSession; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -21,6 +23,7 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @@ -61,10 +64,37 @@ public class SevenWondersTest { session.disconnect(); } + private SevenWondersSession newPlayer(String name) throws InterruptedException, TimeoutException, + ExecutionException { + SevenWondersSession otherSession = client.connect(serverUrl); + otherSession.chooseName(name); + return otherSession; + } + + @Test + public void lobbySubscription_ignoredForOutsiders() throws InterruptedException, ExecutionException, + TimeoutException { + SevenWondersSession ownerSession = newPlayer("GameOwner"); + SevenWondersSession session1 = newPlayer("Player1"); + SevenWondersSession session2 = newPlayer( "Player2"); + String gameName = "Test Game"; + ApiLobby lobby = ownerSession.createGame(gameName); + session1.joinGame(lobby.getId()); + session2.joinGame(lobby.getId()); + + SevenWondersSession outsiderSession = newPlayer("Outsider"); + JackstompSession session = outsiderSession.getJackstompSession(); + Channel<Object> started = session.subscribeEmptyMsgs("/topic/lobby/" + lobby.getId() + "/started"); + + ownerSession.startGame(lobby.getId()); + Object nothing = started.next(1, TimeUnit.SECONDS); + assertNull(nothing); + disconnect(ownerSession, session1, session2, outsiderSession); + } + @Test public void createGame_success() throws InterruptedException, ExecutionException, TimeoutException { - SevenWondersSession ownerSession = client.connect(serverUrl); - ownerSession.chooseName("GameOwner"); + SevenWondersSession ownerSession = newPlayer("GameOwner"); String gameName = "Test Game"; ApiLobby lobby = ownerSession.createGame(gameName); @@ -74,13 +104,6 @@ public class SevenWondersTest { disconnect(ownerSession); } - private SevenWondersSession newPlayer(String name) throws InterruptedException, TimeoutException, - ExecutionException { - SevenWondersSession otherSession = client.connect(serverUrl); - otherSession.chooseName(name); - return otherSession; - } - @Test public void createGame_seenByConnectedPlayers() throws InterruptedException, ExecutionException, TimeoutException { SevenWondersSession otherSession = newPlayer("OtherPlayer"); diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java index 093d391a..3f76a54b 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java +++ b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.test.api; import org.luxons.sevenwonders.actions.ChooseNameAction; import org.luxons.sevenwonders.actions.CreateGameAction; import org.luxons.sevenwonders.actions.JoinGameAction; +import org.luxons.sevenwonders.errors.UIError; import org.luxons.sevenwonders.test.client.Channel; import org.luxons.sevenwonders.test.client.JackstompSession; @@ -18,10 +19,18 @@ public class SevenWondersSession { this.session = session; } + public JackstompSession getJackstompSession() { + return session; + } + public void disconnect() { session.disconnect(); } + public Channel<UIError> watchErrors() { + return session.subscribe("/user/queue/errors", UIError.class); + } + public ApiPlayer chooseName(String displayName) throws InterruptedException { ChooseNameAction action = new ChooseNameAction(); action.setPlayerName(displayName); |