From ed2223cae37a2f0977099e23e89dd617444324e1 Mon Sep 17 00:00:00 2001 From: William Kim Date: Fri, 5 Jul 2013 12:32:45 -0400 Subject: [PATCH] unit tests of exceptions cases for refreshing access tokens. --- ...TestDefaultOAuth2ProviderTokenService.java | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultOAuth2ProviderTokenService.java b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultOAuth2ProviderTokenService.java index 688c0f6c2..19f1486fe 100644 --- a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultOAuth2ProviderTokenService.java +++ b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultOAuth2ProviderTokenService.java @@ -38,6 +38,7 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; import org.springframework.security.oauth2.common.exceptions.InvalidClientException; +import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import org.springframework.security.oauth2.provider.AuthorizationRequest; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.token.TokenEnhancer; @@ -57,6 +58,11 @@ public class TestDefaultOAuth2ProviderTokenService { private String clientId = "test_client"; private Set scope = Sets.newHashSet("openid", "profile", "email", "offline_access"); + private OAuth2RefreshTokenEntity refreshToken; + private String refreshTokenValue = "refresh_token_value"; + + private AuthorizationRequest authRequest; + @Mock private OAuth2TokenRepository tokenRepository; @@ -92,6 +98,13 @@ public class TestDefaultOAuth2ProviderTokenService { // by default in tests, allow refresh tokens Mockito.when(client.isAllowRefresh()).thenReturn(true); + + refreshToken = Mockito.mock(OAuth2RefreshTokenEntity.class); + Mockito.when(tokenRepository.getRefreshTokenByValue(refreshTokenValue)).thenReturn(refreshToken); + Mockito.when(refreshToken.getClient()).thenReturn(client); + Mockito.when(refreshToken.isExpired()).thenReturn(false); + + authRequest = Mockito.mock(AuthorizationRequest.class); } /** @@ -214,13 +227,37 @@ public class TestDefaultOAuth2ProviderTokenService { AuthenticationHolderEntity authHolder = Mockito.mock(AuthenticationHolderEntity.class); Mockito.when(authHolder.getAuthentication()).thenReturn(authentication); - + Mockito.when(authenticationHolderRepository.save(Mockito.any(AuthenticationHolderEntity.class))).thenReturn(authHolder); - + OAuth2AccessTokenEntity token = service.createAccessToken(authentication); assertThat(token.getAuthenticationHolder().getAuthentication(), equalTo(authentication)); Mockito.verify(authenticationHolderRepository).save(Mockito.any(AuthenticationHolderEntity.class)); } + @Test(expected = InvalidTokenException.class) + public void refreshAccessToken_noRefreshToken() { + + Mockito.when(tokenRepository.getRefreshTokenByValue(Mockito.anyString())).thenReturn(null); + + service.refreshAccessToken(refreshTokenValue, authRequest); + } + + @Test(expected = InvalidClientException.class) + public void refreshAccessToken_notAllowRefresh() { + + Mockito.when(client.isAllowRefresh()).thenReturn(false); + + service.refreshAccessToken(refreshTokenValue, authRequest); + } + + @Test(expected = InvalidTokenException.class) + public void refreshAccessToken_expired() { + + Mockito.when(refreshToken.isExpired()).thenReturn(true); + + service.refreshAccessToken(refreshTokenValue, authRequest); + } + }