spring-projects / spring-projects/spring-security
Support empty cotext ReactorContextTestExecutionListener pass Security
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
I would like to have option to pass SecurityContextHolder.createEmptyContext() into my reactor test
Expected Behavior
Test passed
@ExtendWith(SpringExtension::class)
@TestExecutionListeners(
WithSecurityContextTestExecutionListener::class,
ReactorContextTestExecutionListener::class
)
internal class SpringSecurityIdentityProviderTest {
@Test
@WithEmptySecurityContext
fun `when security context is empty then context should be passed to test`() {
Mono.defer {
ReactiveSecurityContextHolder.getContext()
.onEmptyError { IllegalStateException("Require security context") }
.filter { it.authentication == null }
}.onEmptyError { IllegalStateException("Require security context with Empty auth") }
.block()
}
}
class EmptySecurityContextFactory : WithSecurityContextFactory<WithEmptySecurityContext> {
override fun createSecurityContext(annotation: WithEmptySecurityContext?): SecurityContext {
return SecurityContextHolder.createEmptyContext()
}
}
@WithSecurityContext(factory = EmptySecurityContextFactory::class)
annotation class WithEmptySecurityContext
Current Behavior
test failed
Context
Sometime we receive empty SecurityContext in our logic and would like to have option to test these cases.
But ReactorContextTestExecutionListener do not pass empty SecurityContext to tests.
Workaround is to modify stream context like this:
Mono.defer {
ReactiveSecurityContextHolder.getContext()
.onEmptyError { IllegalStateException("Require security context") }
.filter { it.authentication == null }
}.onEmptyError { IllegalStateException("Require security context with Empty auth") }
.contextWrite({
val withSecurityContext = ReactiveSecurityContextHolder.withSecurityContext(
Mono.just(SecurityContextHolder.createEmptyContext())
)
withSecurityContext.putAll(it.readOnly())
withSecurityContext
})
.block()
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with ReactorContextTestExecutionListener and WithSecurityContextTestExecutionListener, using the reported @WithEmptySecurityContext example as the reproduction. Trace how SecurityContextHolder.createEmptyContext() is handled for reactor tests and run a focused test based on the example. Done means the empty context is available through ReactiveSecurityContextHolder without the stream workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- security, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100