-
Notifications
You must be signed in to change notification settings - Fork 167
feat: Add Storage.removeAll(), default minTTL of 60s, and fix @Ignore tests #918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v4_development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -75,4 +75,9 @@ public interface Storage { | |||||
| * @param name the name of the value to remove. | ||||||
| */ | ||||||
| public fun remove(name: String) | ||||||
|
|
||||||
| /** | ||||||
| * Removes all values from the storage. | ||||||
| */ | ||||||
| public fun removeAll() | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| public fun removeAll() | |
| public fun removeAll() { } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package com.auth0.android.authentication.storage | |
| import com.auth0.android.NetworkErrorException | ||
| import com.auth0.android.authentication.AuthenticationAPIClient | ||
| import com.auth0.android.authentication.AuthenticationException | ||
| import com.auth0.android.authentication.storage.BaseCredentialsManager.Companion.DEFAULT_MIN_TTL | ||
| import com.auth0.android.callback.Callback | ||
| import com.auth0.android.request.Request | ||
| import com.auth0.android.request.internal.GsonProvider | ||
|
|
@@ -672,7 +673,7 @@ public class CredentialsManagerTest { | |
| Mockito.`when`( | ||
| client.renewAuth("refresh_token", "audience") | ||
| ).thenReturn(request) | ||
| val newDate = Date(CredentialsMock.CURRENT_TIME_MS + 1 * 1000) | ||
| val newDate = Date(CredentialsMock.CURRENT_TIME_MS + (DEFAULT_MIN_TTL + 10) * 1000L) | ||
| val jwtMock = mock<Jwt>() | ||
| Mockito.`when`(jwtMock.expiresAt).thenReturn(newDate) | ||
| Mockito.`when`(jwtDecoder.decode("newId")).thenReturn(jwtMock) | ||
|
|
@@ -1770,6 +1771,103 @@ public class CredentialsManagerTest { | |
| MatcherAssert.assertThat(manager.hasValidCredentials(), Is.`is`(true)) | ||
| } | ||
|
|
||
| @Test | ||
| public fun shouldRenewCredentialsViaCallbackWhenTokenExpiresWithinDefaultMinTtl() { | ||
| // Token expires in 30 seconds, which is within DEFAULT_MIN_TTL (60s) | ||
| val expirationTime = CredentialsMock.CURRENT_TIME_MS + 30 * 1000 | ||
| Mockito.`when`(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.token_type")).thenReturn("type") | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.cache_expires_at")) | ||
| .thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveString("com.auth0.scope")).thenReturn("scope") | ||
| Mockito.`when`( | ||
| client.renewAuth("refreshToken") | ||
| ).thenReturn(request) | ||
| val newDate = Date(CredentialsMock.ONE_HOUR_AHEAD_MS) | ||
| val jwtMock = mock<Jwt>() | ||
| Mockito.`when`(jwtMock.expiresAt).thenReturn(newDate) | ||
| Mockito.`when`(jwtDecoder.decode("newId")).thenReturn(jwtMock) | ||
|
|
||
| val renewedCredentials = | ||
| Credentials("newId", "newAccess", "newType", "refreshToken", newDate, "newScope") | ||
| Mockito.`when`(request.execute()).thenReturn(renewedCredentials) | ||
| // Use no-arg getCredentials which now uses DEFAULT_MIN_TTL | ||
| manager.getCredentials(callback) | ||
| verify(callback).onSuccess( | ||
| credentialsCaptor.capture() | ||
| ) | ||
| // Verify renewal was triggered (client.renewAuth was called) | ||
| verify(client).renewAuth("refreshToken") | ||
| val retrievedCredentials = credentialsCaptor.firstValue | ||
| MatcherAssert.assertThat(retrievedCredentials, Is.`is`(Matchers.notNullValue())) | ||
| MatcherAssert.assertThat(retrievedCredentials.idToken, Is.`is`("newId")) | ||
| MatcherAssert.assertThat(retrievedCredentials.accessToken, Is.`is`("newAccess")) | ||
| } | ||
|
|
||
| @Test | ||
| @ExperimentalCoroutinesApi | ||
| public fun shouldAwaitRenewedCredentialsWhenTokenExpiresWithinDefaultMinTtl(): Unit = runTest { | ||
| // Token expires in 30 seconds, which is within DEFAULT_MIN_TTL (60s) | ||
| val expirationTime = CredentialsMock.CURRENT_TIME_MS + 30 * 1000 | ||
| Mockito.`when`(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.token_type")).thenReturn("type") | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.cache_expires_at")) | ||
| .thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveString("com.auth0.scope")).thenReturn("scope") | ||
| Mockito.`when`( | ||
| client.renewAuth("refreshToken") | ||
| ).thenReturn(request) | ||
| val newDate = Date(CredentialsMock.ONE_HOUR_AHEAD_MS) | ||
| val jwtMock = mock<Jwt>() | ||
| Mockito.`when`(jwtMock.expiresAt).thenReturn(newDate) | ||
| Mockito.`when`(jwtDecoder.decode("newId")).thenReturn(jwtMock) | ||
|
|
||
| val renewedCredentials = | ||
| Credentials("newId", "newAccess", "newType", "refreshToken", newDate, "newScope") | ||
| Mockito.`when`(request.execute()).thenReturn(renewedCredentials) | ||
| // Use no-arg awaitCredentials which now uses DEFAULT_MIN_TTL | ||
| val result = manager.awaitCredentials() | ||
| // Verify renewal was triggered | ||
| verify(client).renewAuth("refreshToken") | ||
| MatcherAssert.assertThat(result, Is.`is`(Matchers.notNullValue())) | ||
| MatcherAssert.assertThat(result.idToken, Is.`is`("newId")) | ||
| MatcherAssert.assertThat(result.accessToken, Is.`is`("newAccess")) | ||
| } | ||
|
|
||
| @Test | ||
| public fun shouldNotHaveValidCredentialsWhenTokenExpiresWithinDefaultMinTtlAndNoRefreshToken() { | ||
| // Token expires in 30 seconds, within DEFAULT_MIN_TTL (60s), and no refresh token | ||
| val expirationTime = CredentialsMock.CURRENT_TIME_MS + 30 * 1000 | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.cache_expires_at")) | ||
| .thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveString("com.auth0.refresh_token")).thenReturn(null) | ||
| Mockito.`when`(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken") | ||
| // No-arg hasValidCredentials now uses DEFAULT_MIN_TTL, so token expiring in 30s is invalid | ||
| Assert.assertFalse(manager.hasValidCredentials()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun shouldHaveValidCredentialsWhenTokenExpiresWithinDefaultMinTtlButRefreshTokenAvailable() { | ||
| // Token expires in 30 seconds, within DEFAULT_MIN_TTL (60s), but refresh token is available | ||
| val expirationTime = CredentialsMock.CURRENT_TIME_MS + 30 * 1000 | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveLong("com.auth0.cache_expires_at")) | ||
| .thenReturn(expirationTime) | ||
| Mockito.`when`(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken") | ||
| Mockito.`when`(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken") | ||
| // Even though token expires within DEFAULT_MIN_TTL, refresh token makes it valid | ||
| MatcherAssert.assertThat(manager.hasValidCredentials(), Is.`is`(true)) | ||
| } | ||
|
|
||
| @Test | ||
| public fun shouldNotHaveCredentialsWhenAccessTokenAndIdTokenAreMissing() { | ||
| Mockito.`when`(storage.retrieveString("com.auth0.id_token")).thenReturn(null) | ||
|
|
@@ -1812,7 +1910,7 @@ public class CredentialsManagerTest { | |
| //now, update the clock and retry | ||
| manager.setClock(object : Clock { | ||
| override fun getCurrentTimeMillis(): Long { | ||
| return CredentialsMock.CURRENT_TIME_MS - 1000 | ||
| return CredentialsMock.CURRENT_TIME_MS - (DEFAULT_MIN_TTL * 1000 + 1000) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this +1000 ? |
||
| } | ||
| }) | ||
| MatcherAssert.assertThat(manager.hasValidCredentials(), Is.`is`(true)) | ||
|
|
@@ -1829,7 +1927,6 @@ public class CredentialsManagerTest { | |
| }) | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public fun shouldAddParametersToRequest() { | ||
| Mockito.`when`(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the V4_Migration guide for this change. Refer the Swift one