2121import java .util .HashSet ;
2222import java .util .Set ;
2323
24+ import org .junit .Assert ;
2425import org .junit .Before ;
2526import org .junit .Test ;
2627import org .junit .runner .RunWith ;
5253
5354import com .google .common .collect .Sets ;
5455
56+ import static com .google .common .collect .Sets .newHashSet ;
5557import static org .hamcrest .CoreMatchers .equalTo ;
5658import static org .hamcrest .CoreMatchers .is ;
5759import static org .hamcrest .CoreMatchers .not ;
6062
6163import static org .mockito .Mockito .never ;
6264import static org .mockito .Mockito .when ;
63-
65+ import static org .junit .Assert .assertEquals ;
66+ import static org .junit .Assert .assertFalse ;
6467import static org .junit .Assert .assertThat ;
6568import static org .junit .Assert .assertTrue ;
6669import static org .junit .Assert .fail ;
@@ -83,7 +86,9 @@ public class TestDefaultOAuth2ProviderTokenService {
8386 private String badClientId = "bad_client" ;
8487 private Set <String > scope = Sets .newHashSet ("openid" , "profile" , "email" , "offline_access" );
8588 private OAuth2RefreshTokenEntity refreshToken ;
89+ private OAuth2AccessTokenEntity accessToken ;
8690 private String refreshTokenValue = "refresh_token_value" ;
91+ private String userSub = "6a50ac11786d402a9591d3e592ac770f" ;
8792 private TokenRequest tokenRequest ;
8893
8994 // for use when refreshing access tokens
@@ -142,6 +147,8 @@ public void prepare() {
142147 Mockito .when (tokenRepository .getRefreshTokenByValue (refreshTokenValue )).thenReturn (refreshToken );
143148 Mockito .when (refreshToken .getClient ()).thenReturn (client );
144149 Mockito .when (refreshToken .isExpired ()).thenReturn (false );
150+
151+ accessToken = Mockito .mock (OAuth2AccessTokenEntity .class );
145152
146153 tokenRequest = new TokenRequest (null , clientId , null , null );
147154
@@ -542,5 +549,22 @@ public void refreshAccessToken_expiration() {
542549
543550 assertTrue (token .getExpiration ().after (lowerBoundAccessTokens ) && token .getExpiration ().before (upperBoundAccessTokens ));
544551 }
545-
552+
553+ @ Test
554+ public void getAllAccessTokensForUser (){
555+ Mockito .when (tokenRepository .getAccessTokensBySub (userSub )).thenReturn (newHashSet (accessToken ));
556+
557+ Set <OAuth2AccessTokenEntity > tokens = service .getAllAccessTokensForUser (userSub );
558+ assertEquals (1 , tokens .size ());
559+ assertTrue (tokens .contains (accessToken ));
560+ }
561+
562+ @ Test
563+ public void getAllRefreshTokensForUser (){
564+ Mockito .when (tokenRepository .getRefreshTokensBySub (userSub )).thenReturn (newHashSet (refreshToken ));
565+
566+ Set <OAuth2RefreshTokenEntity > tokens = service .getAllRefreshTokensForUser (userSub );
567+ assertEquals (1 , tokens .size ());
568+ assertTrue (tokens .contains (refreshToken ));
569+ }
546570}
0 commit comments