浏览代码

Add Test<DomainObject>s For OAuth2

Fixes: gh-5699
Rob Winch 7 年之前
父节点
当前提交
3a7083c7e9

+ 1 - 0
oauth2/oauth2-client/spring-security-oauth2-client.gradle

@@ -11,6 +11,7 @@ dependencies {
 	optional 'io.projectreactor:reactor-core'
 	optional 'org.springframework:spring-webflux'
 
+	testCompile project(path: ':spring-security-oauth2-core', configuration: 'tests')
 	testCompile powerMock2Dependencies
 	testCompile 'com.squareup.okhttp3:mockwebserver'
 	testCompile 'com.fasterxml.jackson.core:jackson-databind'

+ 47 - 0
oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/TestOAuth2AuthorizationCodeAuthenticationTokens.java

@@ -0,0 +1,47 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.client.authentication;
+
+import org.springframework.security.oauth2.client.registration.ClientRegistration;
+import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
+import org.springframework.security.oauth2.core.OAuth2AccessToken;
+import org.springframework.security.oauth2.core.OAuth2RefreshToken;
+import org.springframework.security.oauth2.core.TestOAuth2AccessTokens;
+import org.springframework.security.oauth2.core.TestOAuth2RefreshTokens;
+import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange;
+import org.springframework.security.oauth2.core.endpoint.TestOAuth2AuthorizationExchanges;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2AuthorizationCodeAuthenticationTokens {
+
+	public static OAuth2AuthorizationCodeAuthenticationToken unauthenticated() {
+		ClientRegistration registration = TestClientRegistrations.clientRegistration().build();
+		OAuth2AuthorizationExchange exchange = TestOAuth2AuthorizationExchanges.success();
+		return new OAuth2AuthorizationCodeAuthenticationToken(registration, exchange);
+	}
+
+	public static OAuth2AuthorizationCodeAuthenticationToken authenticated() {
+		ClientRegistration registration = TestClientRegistrations.clientRegistration().build();
+		OAuth2AuthorizationExchange exchange = TestOAuth2AuthorizationExchanges.success();
+		OAuth2AccessToken accessToken = TestOAuth2AccessTokens.noScopes();
+		OAuth2RefreshToken refreshToken = TestOAuth2RefreshTokens.refreshToken();
+		return new OAuth2AuthorizationCodeAuthenticationToken(registration, exchange, accessToken, refreshToken);
+	}
+}

+ 33 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/TestOAuth2AccessTokens.java

@@ -0,0 +1,33 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.core;
+
+import java.time.Duration;
+import java.time.Instant;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2AccessTokens {
+	public static OAuth2AccessToken noScopes() {
+		return new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
+				"no-scopes",
+				Instant.now(),
+				Instant.now().plus(Duration.ofDays(1)));
+	}
+}

+ 31 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/TestOAuth2RefreshTokens.java

@@ -0,0 +1,31 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.core;
+
+import java.time.Duration;
+import java.time.Instant;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2RefreshTokens {
+	public static OAuth2RefreshToken refreshToken() {
+		return new OAuth2RefreshToken("refresh-token", Instant.now(),
+				Instant.now().plus(Duration.ofDays(1)));
+	}
+}

+ 30 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AccessTokenResponses.java

@@ -0,0 +1,30 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.core.endpoint;
+
+import org.springframework.security.oauth2.core.OAuth2AccessToken;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2AccessTokenResponses {
+	public static OAuth2AccessTokenResponse.Builder accessTokenResponse() {
+		return OAuth2AccessTokenResponse.withToken("token")
+				.tokenType(OAuth2AccessToken.TokenType.BEARER);
+	}
+}

+ 30 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationExchanges.java

@@ -0,0 +1,30 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.core.endpoint;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2AuthorizationExchanges {
+
+	public static OAuth2AuthorizationExchange success() {
+		OAuth2AuthorizationRequest request = TestOAuth2AuthorizationRequests.request().build();
+		OAuth2AuthorizationResponse response = TestOAuth2AuthorizationResponses.success().build();
+		return new OAuth2AuthorizationExchange(request, response);
+	}
+}

+ 39 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java

@@ -0,0 +1,39 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.core.endpoint;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2AuthorizationRequests {
+	public static OAuth2AuthorizationRequest.Builder request() {
+		String registrationId = "registration-id";
+		String clientId = "client-id";
+		Map<String, Object> additionalParameters = new HashMap<>();
+		additionalParameters.put(OAuth2ParameterNames.REGISTRATION_ID, registrationId);
+		return OAuth2AuthorizationRequest.authorizationCode()
+				.authorizationUri("https://example.com/login/oauth/authorize")
+				.clientId(clientId)
+				.redirectUri("https://example.com/authorize/oauth2/code/registration-id")
+				.state("state")
+				.additionalParameters(additionalParameters);
+	}
+}

+ 36 - 0
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationResponses.java

@@ -0,0 +1,36 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.oauth2.core.endpoint;
+
+/**
+ * @author Rob Winch
+ * @since 5.1
+ */
+public class TestOAuth2AuthorizationResponses {
+
+	public static OAuth2AuthorizationResponse.Builder success() {
+		return OAuth2AuthorizationResponse.success("authorization-code")
+				.state("state")
+				.redirectUri("https://example.com/authorize/oauth2/code/registration-id");
+	}
+
+	public static OAuth2AuthorizationResponse.Builder error() {
+		return OAuth2AuthorizationResponse.error("error")
+				.redirectUri("https://example.com/authorize/oauth2/code/registration-id")
+				.errorUri("https://example.com/error");
+	}
+}