diff --git a/src/test/java/de/antragsgruen/live/AuthenticationErrorTests.java b/src/test/java/de/antragsgruen/live/AuthenticationErrorTests.java new file mode 100644 index 0000000..47fb401 --- /dev/null +++ b/src/test/java/de/antragsgruen/live/AuthenticationErrorTests.java @@ -0,0 +1,68 @@ +package de.antragsgruen.live; + +import de.antragsgruen.live.utils.StompRabbitMQTestHelper; +import de.antragsgruen.live.utils.StompTestConnection; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.context.ActiveProfiles; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +class AuthenticationErrorTests { + @Autowired + private StompRabbitMQTestHelper testHelper; + + @LocalServerPort + private int port; + + @Test + public void tryToConnectToIncorrectTopic1() { + StompTestConnection stompConnection = testHelper.getStompConnection(port); + + stompConnection.connectAndWait("test", "site", "con", "login-1", null); + FutureTask onError = stompConnection.subscribeAndExpectError("/user/site/othercon/login-1/speech"); + try { + String message = onError.get(5, TimeUnit.SECONDS); + assertThat(message).isEqualTo("Forbidden to subscribe to this destination"); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + throw new RuntimeException(e); + } + } + + @Test + public void tryToConnectToIncorrectTopic2() { + StompTestConnection stompConnection = testHelper.getStompConnection(port); + + stompConnection.connectAndWait("test", "site", "con", "login-2", null); + FutureTask onError = stompConnection.subscribeAndExpectError("/user/site/con/login-1/speech"); + try { + String message = onError.get(5, TimeUnit.SECONDS); + assertThat(message).isEqualTo("Forbidden to subscribe to this destination"); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + throw new RuntimeException(e); + } + } + + @Test + public void tryToConnectToIncorrectInstallation() { + StompTestConnection stompConnection = testHelper.getStompConnection(port); + + FutureTask onError = stompConnection.connectAndExpectError("other", "site", "con", "login-1", null); + try { + String message = onError.get(5, TimeUnit.SECONDS); + assertThat(message).isEqualTo("Could not authenticate JWT: Invalid installation id: other"); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/de/antragsgruen/live/SpeechUserTests.java b/src/test/java/de/antragsgruen/live/SpeechUserTests.java index 223e482..e3d26ce 100644 --- a/src/test/java/de/antragsgruen/live/SpeechUserTests.java +++ b/src/test/java/de/antragsgruen/live/SpeechUserTests.java @@ -9,12 +9,6 @@ import org.springframework.test.context.ActiveProfiles; import java.io.IOException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.FutureTask; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") @@ -57,32 +51,4 @@ public void sendAndConvertRabbitMQMessage_speech3() throws IOException { testHelper.sendFileContentToRabbitMQ("sendAndConvertRabbitMQMessage_speech3_in.json", "speech.site.con"); testHelper.expectStompToSendFileContent(stompConnection, "sendAndConvertRabbitMQMessage_speech3_user_out.json"); } - - @Test - public void tryToConnectToIncorrectTopic1() { - StompTestConnection stompConnection = testHelper.getStompConnection(port); - - stompConnection.connectAndWait("test", "site", "con", "login-1", null); - FutureTask onError = stompConnection.subscribeAndExpectError("/user/site/othercon/login-1/speech"); - try { - String message = onError.get(5, TimeUnit.SECONDS); - assertThat(message).isEqualTo("Forbidden to subscribe to this destination"); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - throw new RuntimeException(e); - } - } - - @Test - public void tryToConnectToIncorrectTopic2() { - StompTestConnection stompConnection = testHelper.getStompConnection(port); - - stompConnection.connectAndWait("test", "site", "con", "login-2", null); - FutureTask onError = stompConnection.subscribeAndExpectError("/user/site/con/login-1/speech"); - try { - String message = onError.get(5, TimeUnit.SECONDS); - assertThat(message).isEqualTo("Forbidden to subscribe to this destination"); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - throw new RuntimeException(e); - } - } } diff --git a/src/test/java/de/antragsgruen/live/utils/StompTestConnection.java b/src/test/java/de/antragsgruen/live/utils/StompTestConnection.java index b4032a7..f9c83b8 100644 --- a/src/test/java/de/antragsgruen/live/utils/StompTestConnection.java +++ b/src/test/java/de/antragsgruen/live/utils/StompTestConnection.java @@ -134,6 +134,11 @@ public void connectAndWait(String installation, String site, String consultation } } + public FutureTask connectAndExpectError(String installation, String site, String consultation, String userId, @Nullable List roles) { + this.connect(installation, site, consultation, userId, roles); + return this.onError; + } + public void subscribe(String topic) { this.stompSession.subscribe(topic, new StompFrameHandler() { @Override diff --git a/src/test/java/de/antragsgruen/live/utils/StompTestSessionHandler.java b/src/test/java/de/antragsgruen/live/utils/StompTestSessionHandler.java index d856537..dc02acd 100644 --- a/src/test/java/de/antragsgruen/live/utils/StompTestSessionHandler.java +++ b/src/test/java/de/antragsgruen/live/utils/StompTestSessionHandler.java @@ -46,6 +46,8 @@ public void handleException(StompSession session, @Nullable StompCommand command @Override public void handleTransportError(StompSession session, Throwable exception) { System.out.println("Transport error: " + exception.getMessage()); + this.lastStompError = exception.getMessage(); + this.onErrorFuture.run(); } @Override