11package com.auth0.android.authentication.storage
22
3+ import com.auth0.android.NetworkErrorException
34import com.auth0.android.authentication.AuthenticationAPIClient
45import com.auth0.android.authentication.AuthenticationException
56import com.auth0.android.callback.Callback
@@ -788,7 +789,7 @@ public class CredentialsManagerTest {
788789 }
789790
790791 @Test
791- public fun shouldGetAndFailToRenewExpiredCredentials () {
792+ public fun shouldGetAndFailToRenewExpiredCredentialsWhenRefreshTokenExpired () {
792793 Mockito .`when `(storage.retrieveString(" com.auth0.id_token" )).thenReturn(" idToken" )
793794 Mockito .`when `(storage.retrieveString(" com.auth0.access_token" )).thenReturn(" accessToken" )
794795 Mockito .`when `(storage.retrieveString(" com.auth0.refresh_token" )).thenReturn(" refreshToken" )
@@ -802,8 +803,9 @@ public class CredentialsManagerTest {
802803 client.renewAuth(" refreshToken" )
803804 ).thenReturn(request)
804805 // Trigger failure
805- val authenticationException = Mockito .mock(
806- AuthenticationException ::class .java
806+ val authenticationException = AuthenticationException (
807+ " invalid_grant" ,
808+ " Unknown or invalid refresh token."
807809 )
808810 Mockito .`when `(request.execute()).thenThrow(authenticationException)
809811 manager.getCredentials(callback)
@@ -828,6 +830,130 @@ public class CredentialsManagerTest {
828830 )
829831 }
830832
833+ @Test
834+ public fun shouldGetAndFailToRenewExpiredCredentialsWhenUserIsDeleted () {
835+ Mockito .`when `(storage.retrieveString(" com.auth0.id_token" )).thenReturn(" idToken" )
836+ Mockito .`when `(storage.retrieveString(" com.auth0.access_token" )).thenReturn(" accessToken" )
837+ Mockito .`when `(storage.retrieveString(" com.auth0.refresh_token" )).thenReturn(" refreshToken" )
838+ Mockito .`when `(storage.retrieveString(" com.auth0.token_type" )).thenReturn(" type" )
839+ val expirationTime = CredentialsMock .CURRENT_TIME_MS // Same as current time --> expired
840+ Mockito .`when `(storage.retrieveLong(" com.auth0.expires_at" )).thenReturn(expirationTime)
841+ Mockito .`when `(storage.retrieveLong(" com.auth0.cache_expires_at" ))
842+ .thenReturn(expirationTime)
843+ Mockito .`when `(storage.retrieveString(" com.auth0.scope" )).thenReturn(" scope" )
844+ Mockito .`when `(
845+ client.renewAuth(" refreshToken" )
846+ ).thenReturn(request)
847+ // Trigger failure
848+ val authenticationException = AuthenticationException (
849+ mapOf (
850+ " error" to " invalid_grant" ,
851+ " error_description" to " The refresh_token was generated for a user who doesn't exist anymore."
852+ ), 403
853+ )
854+ Mockito .`when `(request.execute()).thenThrow(authenticationException)
855+ manager.getCredentials(callback)
856+ verify(storage, never())
857+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyInt())
858+ verify(storage, never())
859+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyLong())
860+ verify(storage, never())
861+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyString())
862+ verify(storage, never())
863+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyBoolean())
864+ verify(storage, never()).remove(ArgumentMatchers .anyString())
865+ verify(callback).onFailure(
866+ exceptionCaptor.capture()
867+ )
868+ val exception = exceptionCaptor.firstValue
869+ MatcherAssert .assertThat(exception, Is .`is `(Matchers .notNullValue()))
870+ MatcherAssert .assertThat(exception.cause, Is .`is `(authenticationException))
871+ MatcherAssert .assertThat(
872+ exception.message,
873+ Is .`is `(" An error occurred while trying to use the Refresh Token to renew the Credentials." )
874+ )
875+ }
876+
877+ @Test
878+ public fun shouldGetAndFailToRenewExpiredCredentialsWhenNetworkIsNotAvailable () {
879+ Mockito .`when `(storage.retrieveString(" com.auth0.id_token" )).thenReturn(" idToken" )
880+ Mockito .`when `(storage.retrieveString(" com.auth0.access_token" )).thenReturn(" accessToken" )
881+ Mockito .`when `(storage.retrieveString(" com.auth0.refresh_token" )).thenReturn(" refreshToken" )
882+ Mockito .`when `(storage.retrieveString(" com.auth0.token_type" )).thenReturn(" type" )
883+ val expirationTime = CredentialsMock .CURRENT_TIME_MS // Same as current time --> expired
884+ Mockito .`when `(storage.retrieveLong(" com.auth0.expires_at" )).thenReturn(expirationTime)
885+ Mockito .`when `(storage.retrieveLong(" com.auth0.cache_expires_at" ))
886+ .thenReturn(expirationTime)
887+ Mockito .`when `(storage.retrieveString(" com.auth0.scope" )).thenReturn(" scope" )
888+ Mockito .`when `(
889+ client.renewAuth(" refreshToken" )
890+ ).thenReturn(request)
891+ // Trigger failure
892+ val authenticationException = AuthenticationException (
893+ " Failed to execute the network request" , NetworkErrorException (mock())
894+ )
895+ Mockito .`when `(request.execute()).thenThrow(authenticationException)
896+ manager.getCredentials(callback)
897+ verify(storage, never())
898+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyInt())
899+ verify(storage, never())
900+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyLong())
901+ verify(storage, never())
902+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyString())
903+ verify(storage, never())
904+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyBoolean())
905+ verify(storage, never()).remove(ArgumentMatchers .anyString())
906+ verify(callback).onFailure(
907+ exceptionCaptor.capture()
908+ )
909+ val exception = exceptionCaptor.firstValue
910+ MatcherAssert .assertThat(exception, Is .`is `(Matchers .notNullValue()))
911+ MatcherAssert .assertThat(exception.cause, Is .`is `(authenticationException))
912+ MatcherAssert .assertThat(
913+ exception.message,
914+ Is .`is `(" Failed to execute the network request." )
915+ )
916+ }
917+
918+ @Test
919+ public fun shouldGetAndFailToRenewExpiredCredentialsWhenApiErrorOccurs () {
920+ Mockito .`when `(storage.retrieveString(" com.auth0.id_token" )).thenReturn(" idToken" )
921+ Mockito .`when `(storage.retrieveString(" com.auth0.access_token" )).thenReturn(" accessToken" )
922+ Mockito .`when `(storage.retrieveString(" com.auth0.refresh_token" )).thenReturn(" refreshToken" )
923+ Mockito .`when `(storage.retrieveString(" com.auth0.token_type" )).thenReturn(" type" )
924+ val expirationTime = CredentialsMock .CURRENT_TIME_MS // Same as current time --> expired
925+ Mockito .`when `(storage.retrieveLong(" com.auth0.expires_at" )).thenReturn(expirationTime)
926+ Mockito .`when `(storage.retrieveLong(" com.auth0.cache_expires_at" ))
927+ .thenReturn(expirationTime)
928+ Mockito .`when `(storage.retrieveString(" com.auth0.scope" )).thenReturn(" scope" )
929+ Mockito .`when `(
930+ client.renewAuth(" refreshToken" )
931+ ).thenReturn(request)
932+ // Trigger failure
933+ val authenticationException = AuthenticationException (" Something went wrong" , mock<Exception >())
934+ Mockito .`when `(request.execute()).thenThrow(authenticationException)
935+ manager.getCredentials(callback)
936+ verify(storage, never())
937+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyInt())
938+ verify(storage, never())
939+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyLong())
940+ verify(storage, never())
941+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyString())
942+ verify(storage, never())
943+ .store(ArgumentMatchers .anyString(), ArgumentMatchers .anyBoolean())
944+ verify(storage, never()).remove(ArgumentMatchers .anyString())
945+ verify(callback).onFailure(
946+ exceptionCaptor.capture()
947+ )
948+ val exception = exceptionCaptor.firstValue
949+ MatcherAssert .assertThat(exception, Is .`is `(Matchers .notNullValue()))
950+ MatcherAssert .assertThat(exception.cause, Is .`is `(authenticationException))
951+ MatcherAssert .assertThat(
952+ exception.message,
953+ Is .`is `(" An error occurred while processing the request." )
954+ )
955+ }
956+
831957 @Test
832958 public fun shouldClearCredentials () {
833959 manager.clearCredentials()
0 commit comments