Add Test<DomainObject>s For OAuth2

Fixes: gh-5699
This commit is contained in:
Rob Winch 2018-08-07 21:49:19 -05:00
parent b02ce59188
commit 3a7083c7e9
8 changed files with 247 additions and 0 deletions

View File

@ -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'

View File

@ -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);
}
}

View File

@ -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)));
}
}

View File

@ -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)));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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");
}
}