Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SecurityContext creation for TEST_EXECUTION #8738

Closed
wants to merge 1 commit into from

Conversation

MGabr
Copy link
Contributor

@MGabr MGabr commented Jun 21, 2020

Hi,

Currently, there is support for setting up a SecurityContext after @Before by
using TestExecutionEvent.TEST_EXECUTION. The current implementation, however,
already creates the SecurityContext in @Before and just does not set it yet.
This leads to issues like #6591. For the case of @WithUserDetails, the
creation of the SecurityContext already looks up a user from the repository.
If the user was inserted in @Before, the user is not found despite using
TestExecutionEvent.TEST_EXECUTION. This can be especially problematic with
data driven test frameworks like mongoUnit which initialize the database in @Before
and drop the database after each test. In this case workarounds like described in
#6591 do not work.

This commit changes the creation of the SecurityContext to happen after @Before
if using TestExecutionEvent.TEST_EXECUTION.

Closes gh-6591.

I have submitted the CLA.

Currently, there is support for setting up a SecurityContext after @before by
using TestExecutionEvent.TEST_EXECUTION. The current implementation, however,
already creates the SecurityContext in @before and just does not set it yet.
This leads to issues like spring-projects#6591. For the case of @WithUserDetails, the
creation of the SecurityContext already looks up a user from the repository.
If the user was inserted in @before, the user is not found despite using
TestExecutionEvent.TEST_EXECUTION. This commit changes the creation of the
SecurityContext to happen after @before if using
TestExecutionEvent.TEST_EXECUTION.

Closes spring-projectsgh-6591
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 21, 2020
@rwinch rwinch self-assigned this Jun 24, 2020
@rwinch rwinch added in: test An issue in spring-security-test type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 24, 2020
@rwinch rwinch added this to the 5.4.0-M2 milestone Jun 24, 2020
@rwinch
Copy link
Member

rwinch commented Jun 24, 2020

Thanks for the PR! This is merged into master via 97ee6d6 I also added a test to validate that the creation of the SecurityContext was delayed via 97ee6d6

@rwinch rwinch closed this Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test An issue in spring-security-test type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@WithUserDetails setupBefore does not work with JUnit 5
3 participants