From be593b95a8ab36acdd1e2c79c07e9f2d31fdd065 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Fri, 27 Oct 2017 09:59:13 -0500 Subject: [PATCH] Prefer existing Context in ReactorContextTestExecutionListener Issue gh-4718 --- .../ReactorContextTestExecutionListener.java | 2 +- ...ctorContextTestExecutionListenerTests.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java b/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java index 6c60560fc1..76985026b5 100644 --- a/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java +++ b/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java @@ -77,7 +77,7 @@ public class ReactorContextTestExecutionListener return context; } Context toMerge = ReactiveSecurityContextHolder.withAuthentication(authentication); - return context.putAll(toMerge); + return toMerge.putAll(context); } @Override diff --git a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java index 3e12c386b9..a2f9089f33 100644 --- a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java +++ b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java @@ -92,6 +92,25 @@ public class ReactorContextTestExecutionListenerTests { assertAuthentication(expectedAuthentication); } + @Test + public void beforeTestMethodWhenExistingAuthenticationThenReactorContextHasOriginalAuthentication() throws Exception { + TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER"); + TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER"); + TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder)); + + this.listener.beforeTestMethod(this.testContext); + + Mono authentication = Mono.just("any") + .flatMap(s -> ReactiveSecurityContextHolder.getContext() + .map(SecurityContext::getAuthentication) + ) + .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(expectedAuthentication)); + + StepVerifier.create(authentication) + .expectNext(expectedAuthentication) + .verifyComplete(); + } + @Test public void afterTestMethodWhenSecurityContextEmptyThenNoError() throws Exception { this.listener.beforeTestMethod(this.testContext);