Skip to content

Commit aa27f6f

Browse files
committed
test fixes #355
1 parent 2bf8d12 commit aa27f6f

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/ReactiveGreeterGrpcService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope;
1111
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
1212
import org.springframework.security.access.annotation.Secured;
13+
import org.springframework.security.core.context.SecurityContextHolder;
1314
import org.springframework.transaction.annotation.Transactional;
1415
import reactor.core.publisher.Flux;
1516
import reactor.core.publisher.Mono;
1617

18+
import java.util.Optional;
1719
import java.util.stream.IntStream;
1820

1921

@@ -31,6 +33,8 @@ public ReactiveGreeterGrpcService(ReactiveGreeterService reactiveGreeterService)
3133
@Override
3234
@Secured({})
3335
public Mono<ReactiveHelloResponse> greet(Mono<ReactiveHelloRequest> request) {
36+
Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
37+
.orElseThrow();
3438
return reactiveGreeterService.greet(request);
3539

3640
}

grpc-spring-boot-starter-demo/src/reactiveTest/java/org/lognet/springboot/grpc/reactive/ReactiveDemoTest.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import org.junit.Test;
1313
import org.junit.runner.RunWith;
1414
import org.lognet.springboot.grpc.auth.JwtAuthBaseTest;
15+
import org.lognet.springboot.grpc.auth.JwtRoleTest;
1516
import org.lognet.springboot.grpc.demo.DemoApp;
1617
import org.lognet.springboot.grpc.security.GrpcSecurity;
1718
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
1819
import org.springframework.boot.test.context.SpringBootTest;
1920
import org.springframework.boot.test.context.TestConfiguration;
21+
import org.springframework.context.annotation.Import;
2022
import org.springframework.test.annotation.DirtiesContext;
2123
import org.springframework.test.context.ActiveProfiles;
2224
import org.springframework.test.context.junit4.SpringRunner;
@@ -34,27 +36,15 @@
3436

3537
@Slf4j
3638
@RunWith(SpringRunner.class)
37-
@SpringBootTest(classes = DemoApp.class, webEnvironment = NONE)
39+
@SpringBootTest(classes = DemoApp.class)
3840
@ActiveProfiles({"keycloack-test", "r2dbc-test"})
3941
@DirtiesContext
4042
public class ReactiveDemoTest extends JwtAuthBaseTest {
4143

42-
@TestConfiguration
43-
static class TestCfg {
44-
private static class DemoGrpcSecurityAdapter extends GrpcSecurityConfigurerAdapter {
45-
@Override
46-
public void configure(GrpcSecurity builder) throws Exception {
47-
builder.authorizeRequests()
48-
.withSecuredAnnotation();
49-
50-
}
51-
}
52-
}
53-
5444
@Test
5545
public void grpcGreetTest() {
5646
String shrek = "Shrek";
57-
String message = ReactiveGreeterGrpc.newBlockingStub(channel)
47+
String message = ReactiveGreeterGrpc.newBlockingStub(getChannel())
5848
.greet(ReactiveHelloRequest.newBuilder().setName(shrek).build())
5949
.getMessage();
6050
assertThat(message, containsString(shrek));
@@ -64,7 +54,7 @@ public void grpcGreetTest() {
6454
@Test
6555
public void reactorGreetTest() {
6656
String shrek = "Shrek";
67-
ReactiveHelloResponse helloResponse = ReactorReactiveGreeterGrpc.newReactorStub(channel)
57+
ReactiveHelloResponse helloResponse = ReactorReactiveGreeterGrpc.newReactorStub(getChannel())
6858
.greet(simpleRequest(shrek))
6959
.block(Duration.ofSeconds(10));
7060
assertThat(helloResponse, notNullValue());
@@ -78,7 +68,7 @@ public void reactorGreetFailureTest() {
7868
String shrek = "Wolf";
7969
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
8070

81-
ReactorReactiveGreeterGrpc.newReactorStub(channel)
71+
ReactorReactiveGreeterGrpc.newReactorStub(getChannel())
8272
.greet(simpleRequest(shrek))
8373
.block(Duration.ofSeconds(10));
8474
});
@@ -91,7 +81,7 @@ public void reactorGreetFailureTest() {
9181
@Test
9282
public void reactorMultiGreerTest() {
9383
String shrek = "Shrek";
94-
List<ReactiveHelloResponse> greets = ReactorReactiveGreeterGrpc.newReactorStub(channel)
84+
List<ReactiveHelloResponse> greets = ReactorReactiveGreeterGrpc.newReactorStub(getChannel())
9585
.multiGreet(simpleRequest(shrek))
9686
.collectList()
9787
.block(Duration.ofSeconds(10));
@@ -111,7 +101,7 @@ public void reactorBidiGreerTest() {
111101
"Robin",
112102
"Christopher"
113103
};
114-
List<ReactiveHelloResponse> greets = ReactorReactiveGreeterGrpc.newReactorStub(channel)
104+
List<ReactiveHelloResponse> greets = ReactorReactiveGreeterGrpc.newReactorStub(getChannel())
115105
.streamGreet(
116106
Flux.fromStream(Arrays.stream(names).map(this::simpleRequest))
117107
)

0 commit comments

Comments
 (0)