-
Notifications
You must be signed in to change notification settings - Fork 99
fix: Take into account that a VaadinSession can be detached from the HTTP session #911
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8396b69
Take into account that a VaadinSession can be detached from the HTTP …
Artur- 39e2160
Test
Artur- be8aed7
Merge branch 'master' into fix-session-not-in-session
Artur- 32274bb
Merge branch 'master' into fix-session-not-in-session
Artur- 2bfc7e3
Use method ref
Artur- 4ae6714
Merge branch 'master' into fix-session-not-in-session
Artur- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...st/java/com/vaadin/flow/spring/security/VaadinAwareSecurityContextHolderStrategyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.vaadin.flow.spring.security; | ||
|
|
||
| import javax.servlet.http.HttpSession; | ||
|
|
||
| import com.vaadin.flow.internal.CurrentInstance; | ||
| import com.vaadin.flow.server.VaadinSession; | ||
| import com.vaadin.flow.server.WrappedHttpSession; | ||
| import com.vaadin.flow.server.WrappedSession; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
| import org.springframework.security.core.context.SecurityContext; | ||
| import org.springframework.security.web.context.HttpSessionSecurityContextRepository; | ||
|
|
||
| public class VaadinAwareSecurityContextHolderStrategyTest { | ||
|
|
||
| private VaadinAwareSecurityContextHolderStrategy vaadinAwareSecurityContextHolderStrategy; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| vaadinAwareSecurityContextHolderStrategy = new VaadinAwareSecurityContextHolderStrategy(); | ||
| CurrentInstance.clearAll(); | ||
| } | ||
|
|
||
| @After | ||
| public void teardown() { | ||
| CurrentInstance.clearAll(); | ||
| } | ||
|
|
||
| @Test | ||
| public void currentSessionOverrides() { | ||
| VaadinSession vaadinSession = Mockito.mock(VaadinSession.class); | ||
| HttpSession httpSession = Mockito.mock(HttpSession.class); | ||
| Mockito.when(vaadinSession.getSession()).thenReturn(new WrappedHttpSession(httpSession)); | ||
| SecurityContext securityContext = Mockito.mock(SecurityContext.class); | ||
| Mockito.when(httpSession.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY)) | ||
| .thenReturn(securityContext); | ||
| VaadinSession.setCurrent(vaadinSession); | ||
|
|
||
| vaadinAwareSecurityContextHolderStrategy.setContext(Mockito.mock(SecurityContext.class)); | ||
| Assert.assertEquals(securityContext, vaadinAwareSecurityContextHolderStrategy.getContext()); | ||
| } | ||
|
|
||
| @Test | ||
| public void detachedSessionWorks() { | ||
| VaadinSession vaadinSession = Mockito.mock(VaadinSession.class); | ||
| Mockito.when(vaadinSession.getSession()).thenReturn(null); | ||
| VaadinSession.setCurrent(vaadinSession); | ||
|
|
||
| SecurityContext explicit = Mockito.mock(SecurityContext.class); | ||
| vaadinAwareSecurityContextHolderStrategy.setContext(explicit); | ||
| Assert.assertEquals(explicit, vaadinAwareSecurityContextHolderStrategy.getContext()); | ||
| } | ||
|
|
||
| @Test | ||
| public void explicitUsedWhenNoSessionAvailable() { | ||
| SecurityContext explicit = Mockito.mock(SecurityContext.class); | ||
| vaadinAwareSecurityContextHolderStrategy.setContext(explicit); | ||
| Assert.assertEquals(explicit, vaadinAwareSecurityContextHolderStrategy.getContext()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests ?
This can be easily revert back by someone accidentally without a test and no-one notice this ever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added