Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public SecurityContext getContext() {
* the current request.
*/
SecurityContext context = getFromVaadinSession()
.orElseGet(() -> contextHolder.get());
.orElseGet(contextHolder::get);
if (context == null) {
context = createEmptyContext();
contextHolder.set(context);
Expand All @@ -65,7 +65,7 @@ public SecurityContext getContext() {
@NonNull
private Optional<SecurityContext> getFromVaadinSession() {
VaadinSession session = VaadinSession.getCurrent();
if (session == null) {
if (session == null || session.getSession() == null) {

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

return Optional.empty();
}
Object securityContext = session.getSession().getAttribute(
Expand Down
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());
}
}