Skip to content

Commit

Permalink
Add test cases for failed connection attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
CatoTH committed Mar 16, 2024
1 parent 902ee20 commit 26ee182
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 34 deletions.
68 changes: 68 additions & 0 deletions src/test/java/de/antragsgruen/live/AuthenticationErrorTests.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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);
}
}
}
34 changes: 0 additions & 34 deletions src/test/java/de/antragsgruen/live/SpeechUserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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<String> 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<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public void connectAndWait(String installation, String site, String consultation
}
}

public FutureTask<String> connectAndExpectError(String installation, String site, String consultation, String userId, @Nullable List<String> roles) {
this.connect(installation, site, consultation, userId, roles);
return this.onError;
}

public void subscribe(String topic) {
this.stompSession.subscribe(topic, new StompFrameHandler() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 26ee182

Please sign in to comment.