Skip to content

Commit

Permalink
Success case reproducer for invalid credentials error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianbrandt committed Apr 4, 2023
1 parent 676b8c1 commit a56fcf2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
8 changes: 2 additions & 6 deletions reactive/rsocket/hello-security/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
plugins {
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.6.11'
id 'io.spring.dependency-management' version '1.1.0'
id "nebula.integtest" version "8.2.0"
id 'java'
}

repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/snapshot" }
}

ext["micrometer.version"] = "1.10.0-SNAPSHOT"

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-rsocket'
Expand Down
2 changes: 0 additions & 2 deletions reactive/rsocket/hello-security/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
version=6.0.0-SNAPSHOT
spring-security.version=6.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
*/
package example;

import io.rsocket.exceptions.RejectedSetupException;
import io.rsocket.metadata.WellKnownMimeType;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.rsocket.server.LocalRSocketServerPort;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.security.rsocket.metadata.SimpleAuthenticationEncoder;
import org.springframework.security.rsocket.metadata.UsernamePasswordMetadata;
import org.springframework.test.context.TestPropertySource;
import org.springframework.util.MimeTypeUtils;
import reactor.core.publisher.Mono;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* Integration tests for the rsocket application.
Expand All @@ -40,36 +40,63 @@
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = "spring.rsocket.server.port=0")
public class HelloRSocketApplicationITests {
class HelloRSocketApplicationITests {

@Autowired
RSocketRequester.Builder requester;

@LocalRSocketServerPort
// @LocalRSocketServerPort
@Value("${local.rsocket.server.port}")
int port;

@Test
void messageWhenAuthenticatedThenSuccess() {
UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
// @formatter:off
UsernamePasswordMetadata credentials =
new UsernamePasswordMetadata("user", "password");

RSocketRequester requester = this.requester
.rsocketStrategies((builder) -> builder.encoder(new SimpleAuthenticationEncoder()))
.setupMetadata(credentials, MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString()))
.connectTcp("localhost", this.port)
.block();
// @formatter:on
.rsocketStrategies(builder ->
builder.encoder(new SimpleAuthenticationEncoder())
)
.setupMetadata(
credentials,
MimeTypeUtils.parseMimeType(
WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString()
)
)
.tcp("localhost", this.port);

String message = requester.route("message").data(Mono.empty()).retrieveMono(String.class).block();
String message = requester
.route("message")
.data(Mono.empty())
.retrieveMono(String.class)
.block();

assertThat(message).isEqualTo("Hello");
}

@Test
void messageWhenNotAuthenticatedThenError() {
RSocketRequester requester = this.requester.connectTcp("localhost", this.port).block();
UsernamePasswordMetadata invalidCredentials =
new UsernamePasswordMetadata("invalid user", "bad password");

assertThatThrownBy(() -> requester.route("message").data(Mono.empty()).retrieveMono(String.class).block())
.isNotNull();
}
RSocketRequester requester = this.requester
.rsocketStrategies(builder ->
builder.encoder(new SimpleAuthenticationEncoder())
)
.setupMetadata(
invalidCredentials,
MimeTypeUtils.parseMimeType(
WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString()
)
)
.tcp("localhost", this.port);

assertThatExceptionOfType(RejectedSetupException.class).isThrownBy(() ->
requester.route("message")
.data(Mono.empty())
.retrieveMono(String.class)
.block()
).withMessageContaining("Invalid Credentials");
}
}

0 comments on commit a56fcf2

Please sign in to comment.