Add ReactorContextTestExecutionListener
Fixes gh-4502
This commit is contained in:
+3
-1
@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.test.context.support.ReactorContextTestExecutionListener;
|
||||
import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
@@ -40,6 +41,7 @@ import org.springframework.test.context.TestExecutionListeners;
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@TestExecutionListeners(inheritListeners = false, listeners = WithSecurityContextTestExecutionListener.class)
|
||||
@TestExecutionListeners(inheritListeners = false, listeners = {WithSecurityContextTestExecutionListener.class,
|
||||
ReactorContextTestExecutionListener.class})
|
||||
public @interface SecurityTestExecutionListeners {
|
||||
}
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright 2002-2017 the original author or authors.
|
||||
* *
|
||||
* * Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* * you may not use this file except in compliance with the License.
|
||||
* * You may obtain a copy of the License at
|
||||
* *
|
||||
* * http://www.apache.org/licenses/LICENSE-2.0
|
||||
* *
|
||||
* * Unless required by applicable law or agreed to in writing, software
|
||||
* * distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* * See the License for the specific language governing permissions and
|
||||
* * limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.security.test.context.support;
|
||||
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
class DelegatingTestExecutionListener
|
||||
extends AbstractTestExecutionListener {
|
||||
|
||||
private final TestExecutionListener delegate;
|
||||
|
||||
public DelegatingTestExecutionListener(TestExecutionListener delegate) {
|
||||
Assert.notNull(delegate, "delegate cannot be null");
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(TestContext testContext) throws Exception {
|
||||
delegate.beforeTestClass(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
delegate.prepareTestInstance(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
delegate.beforeTestMethod(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestExecution(TestContext testContext) throws Exception {
|
||||
delegate.beforeTestExecution(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestExecution(TestContext testContext) throws Exception {
|
||||
delegate.afterTestExecution(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
delegate.afterTestMethod(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestClass(TestContext testContext) throws Exception {
|
||||
delegate.afterTestClass(testContext);
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright 2002-2017 the original author or authors.
|
||||
* *
|
||||
* * Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* * you may not use this file except in compliance with the License.
|
||||
* * You may obtain a copy of the License at
|
||||
* *
|
||||
* * http://www.apache.org/licenses/LICENSE-2.0
|
||||
* *
|
||||
* * Unless required by applicable law or agreed to in writing, software
|
||||
* * distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* * See the License for the specific language governing permissions and
|
||||
* * limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.security.test.context.support;
|
||||
|
||||
import org.reactivestreams.Subscription;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.test.context.TestSecurityContextHolder;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import reactor.core.CoreSubscriber;
|
||||
import reactor.core.publisher.Hooks;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.publisher.Operators;
|
||||
import reactor.util.context.Context;
|
||||
|
||||
/**
|
||||
* Sets up the Reactor Context with the Authentication from the TestSecurityContextHolder
|
||||
* and then clears the Reactor Context at the end of the tests.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ReactorContextTestExecutionListener
|
||||
extends DelegatingTestExecutionListener {
|
||||
|
||||
private static final String HOOKS_CLASS_NAME = "reactor.core.publisher.Hooks";
|
||||
|
||||
public ReactorContextTestExecutionListener() {
|
||||
super(createDelegate());
|
||||
}
|
||||
|
||||
private static TestExecutionListener createDelegate() {
|
||||
return ClassUtils.isPresent(HOOKS_CLASS_NAME, ReactorContextTestExecutionListener.class.getClassLoader()) ?
|
||||
new DelegateTestExecutionListener() :
|
||||
new AbstractTestExecutionListener() {};
|
||||
}
|
||||
|
||||
private static class DelegateTestExecutionListener extends AbstractTestExecutionListener {
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
Hooks.onLastOperator(Operators.lift((s, sub) -> new SecuritySubContext<>(sub)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
Hooks.resetOnLastOperator();
|
||||
}
|
||||
|
||||
private static class SecuritySubContext<T> implements CoreSubscriber<T> {
|
||||
private final CoreSubscriber<T> delegate;
|
||||
|
||||
SecuritySubContext(CoreSubscriber<T> delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context currentContext() {
|
||||
Context context = delegate.currentContext();
|
||||
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
return context;
|
||||
}
|
||||
return context.put(Authentication.class, Mono.just(authentication));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
delegate.onSubscribe(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
delegate.onNext(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
delegate.onError(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
delegate.onComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code 11000}.
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 11000;
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
org.springframework.test.context.TestExecutionListener = org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener
|
||||
org.springframework.test.context.TestExecutionListener = \
|
||||
org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener,\
|
||||
org.springframework.security.test.context.support.ReactorContextTestExecutionListener
|
||||
|
||||
Reference in New Issue
Block a user