소스 검색

Populate test security context with authentication

Add setAuthentication method on TestSecurityContextHolder.
Tadaya Tsuyukubo 7 년 전
부모
커밋
12050404ad

+ 18 - 1
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");
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with 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 javax.servlet.FilterChain;
 
 
+import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContext;
 import org.springframework.security.core.context.SecurityContext;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
 import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
@@ -51,6 +52,7 @@ import org.springframework.util.Assert;
  * </ul>
  * </ul>
  *
  *
  * @author Rob Winch
  * @author Rob Winch
+ * @author Tadaya Tsuyukubo
  * @since 4.0
  * @since 4.0
  *
  *
  */
  */
@@ -94,6 +96,21 @@ public final class TestSecurityContextHolder {
 		SecurityContextHolder.setContext(context);
 		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
 	 * Gets the default {@link SecurityContext} by delegating to the
 	 * {@link SecurityContextHolder}
 	 * {@link SecurityContextHolder}

+ 13 - 2
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");
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * you may not use this file except in compliance with the License.
@@ -16,10 +16,12 @@
 package org.springframework.security.test.context;
 package org.springframework.security.test.context;
 
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
 
 
 import org.junit.After;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.Test;
+import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContext;
 import org.springframework.security.core.context.SecurityContext;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.context.SecurityContextHolder;
 
 
@@ -61,4 +63,13 @@ public class TestSecurityContextHolderTests {
 		assertThat(TestSecurityContextHolder.getContext()).isSameAs(context);
 		assertThat(TestSecurityContextHolder.getContext()).isSameAs(context);
 		assertThat(SecurityContextHolder.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);
+	}
+}

+ 5 - 7
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");
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * you may not use this file except in compliance with the License.
@@ -85,9 +85,7 @@ public class ReactorContextTestExecutionListenerTests {
 	@Test
 	@Test
 	public void beforeTestMethodWhenAuthenticationThenReactorContextHasAuthentication() throws Exception {
 	public void beforeTestMethodWhenAuthenticationThenReactorContextHasAuthentication() throws Exception {
 		TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
 		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);
 		this.listener.beforeTestMethod(this.testContext);
 
 
@@ -127,7 +125,7 @@ public class ReactorContextTestExecutionListenerTests {
 	public void beforeTestMethodWhenExistingAuthenticationThenReactorContextHasOriginalAuthentication() throws Exception {
 	public void beforeTestMethodWhenExistingAuthenticationThenReactorContextHasOriginalAuthentication() throws Exception {
 		TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
 		TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
 		TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
 		TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
-		TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder));
+		TestSecurityContextHolder.setAuthentication(contextHolder);
 
 
 		this.listener.beforeTestMethod(this.testContext);
 		this.listener.beforeTestMethod(this.testContext);
 
 
@@ -146,7 +144,7 @@ public class ReactorContextTestExecutionListenerTests {
 	public void beforeTestMethodWhenClearThenReactorContextDoesNotOverride() throws Exception {
 	public void beforeTestMethodWhenClearThenReactorContextDoesNotOverride() throws Exception {
 		TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
 		TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
 		TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
 		TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
-		TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder));
+		TestSecurityContextHolder.setAuthentication(contextHolder);
 
 
 		this.listener.beforeTestMethod(this.testContext);
 		this.listener.beforeTestMethod(this.testContext);
 
 
@@ -187,7 +185,7 @@ public class ReactorContextTestExecutionListenerTests {
 	@Test
 	@Test
 	public void checkSecurityContextResolutionWhenSubscribedContextCalledOnTheDifferentThreadThanWithSecurityContextTestExecutionListener() throws Exception {
 	public void checkSecurityContextResolutionWhenSubscribedContextCalledOnTheDifferentThreadThanWithSecurityContextTestExecutionListener() throws Exception {
 		TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
 		TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER");
-		TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder));
+		TestSecurityContextHolder.setAuthentication(contextHolder);
 
 
 		this.listener.beforeTestMethod(this.testContext);
 		this.listener.beforeTestMethod(this.testContext);