Populate test security context with authentication

Add setAuthentication method on TestSecurityContextHolder.
This commit is contained in:
Tadaya Tsuyukubo 2018-09-22 23:23:56 -07:00 committed by Rob Winch
parent 44ee15cb4d
commit 12050404ad
3 changed files with 36 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@ package org.springframework.security.test.context;
import javax.servlet.FilterChain;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
@ -51,6 +52,7 @@ import org.springframework.util.Assert;
* </ul>
*
* @author Rob Winch
* @author Tadaya Tsuyukubo
* @since 4.0
*
*/
@ -94,6 +96,21 @@ public final class TestSecurityContextHolder {
SecurityContextHolder.setContext(context);
}
/**
* Creates a new {@link SecurityContext} with the given {@link Authentication}.
* The {@link SecurityContext} is set on {@link TestSecurityContextHolder} and
* {@link SecurityContextHolder}.
*
* @param authentication the {@link Authentication} to use
* @since 5.1.1
*/
public static void setAuthentication(Authentication authentication) {
Assert.notNull(authentication, "Only non-null Authentication instances are permitted");
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
setContext(context);
}
/**
* Gets the default {@link SecurityContext} by delegating to the
* {@link SecurityContextHolder}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,10 +16,12 @@
package org.springframework.security.test.context;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
@ -61,4 +63,13 @@ public class TestSecurityContextHolderTests {
assertThat(TestSecurityContextHolder.getContext()).isSameAs(context);
assertThat(SecurityContextHolder.getContext()).isSameAs(context);
}
}
@Test
public void setContextWithAuthentication() {
Authentication authentication = mock(Authentication.class);
TestSecurityContextHolder.setAuthentication(authentication);
assertThat(TestSecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -85,9 +85,7 @@ public class ReactorContextTestExecutionListenerTests {
@Test
public void beforeTestMethodWhenAuthenticationThenReactorContextHasAuthentication() throws Exception {
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
SecurityContextImpl context = new SecurityContextImpl();
context.setAuthentication(expectedAuthentication);
TestSecurityContextHolder.setContext(context);
TestSecurityContextHolder.setAuthentication(expectedAuthentication);
this.listener.beforeTestMethod(this.testContext);
@ -127,7 +125,7 @@ public class ReactorContextTestExecutionListenerTests {
public void beforeTestMethodWhenExistingAuthenticationThenReactorContextHasOriginalAuthentication() throws Exception {
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder));
TestSecurityContextHolder.setAuthentication(contextHolder);
this.listener.beforeTestMethod(this.testContext);
@ -146,7 +144,7 @@ public class ReactorContextTestExecutionListenerTests {
public void beforeTestMethodWhenClearThenReactorContextDoesNotOverride() throws Exception {
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder));
TestSecurityContextHolder.setAuthentication(contextHolder);
this.listener.beforeTestMethod(this.testContext);
@ -187,7 +185,7 @@ public class ReactorContextTestExecutionListenerTests {
@Test
public void checkSecurityContextResolutionWhenSubscribedContextCalledOnTheDifferentThreadThanWithSecurityContextTestExecutionListener() throws Exception {
TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder));
TestSecurityContextHolder.setAuthentication(contextHolder);
this.listener.beforeTestMethod(this.testContext);