From 12050404adccfb80800f1071876d1c4fa42edc0e Mon Sep 17 00:00:00 2001 From: Tadaya Tsuyukubo Date: Sat, 22 Sep 2018 23:23:56 -0700 Subject: [PATCH] Populate test security context with authentication Add setAuthentication method on TestSecurityContextHolder. --- .../context/TestSecurityContextHolder.java | 19 ++++++++++++++++++- .../TestSecurityContextHolderTests.java | 15 +++++++++++++-- ...ctorContextTestExecutionListenerTests.java | 12 +++++------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/test/src/main/java/org/springframework/security/test/context/TestSecurityContextHolder.java b/test/src/main/java/org/springframework/security/test/context/TestSecurityContextHolder.java index 03c4b43b6a..c5af483aae 100644 --- a/test/src/main/java/org/springframework/security/test/context/TestSecurityContextHolder.java +++ b/test/src/main/java/org/springframework/security/test/context/TestSecurityContextHolder.java @@ -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; * * * @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} diff --git a/test/src/test/java/org/springframework/security/test/context/TestSecurityContextHolderTests.java b/test/src/test/java/org/springframework/security/test/context/TestSecurityContextHolderTests.java index 4b071d9ee1..90c1a8959c 100644 --- a/test/src/test/java/org/springframework/security/test/context/TestSecurityContextHolderTests.java +++ b/test/src/test/java/org/springframework/security/test/context/TestSecurityContextHolderTests.java @@ -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); } -} \ No newline at end of file + + @Test + public void setContextWithAuthentication() { + Authentication authentication = mock(Authentication.class); + + TestSecurityContextHolder.setAuthentication(authentication); + + assertThat(TestSecurityContextHolder.getContext().getAuthentication()).isSameAs(authentication); + } +} diff --git a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java index d71ab5737b..dd983f88d7 100644 --- a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java +++ b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java @@ -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);