spring-projects / spring-projects/spring-security

Support empty cotext ReactorContextTestExecutionListener pass Security

Open
#12,115 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: test status: waiting-for-triage type: enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.