-
Notifications
You must be signed in to change notification settings - Fork 616
Refresh Auth token if expired during getId call. #809
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
18cc029
Refresh Auth token if expired during getId call.
ankitaj224 c0580b1
Addressing Di's comments.
ankitaj224 8558f73
Refractoring the code to replace small methods with inline code.
ankitaj224 0fb2666
Addressing Ciaran's comments and refractored the code for better
ankitaj224 b727b35
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 60f4023
Adding isFidRegistered method.
ankitaj224 63da567
Fixing api-information check.
ankitaj224 c26adc7
Restructuring the synchronized block of registerFidIfNeccesary
ankitaj224 4afc7e5
Addressed ciaran'c comments.
ankitaj224 86e3728
Added helper methods for registration status.
ankitaj224 4f64c09
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 62098a7
Introducing new registration status - NOT_GENERATED as default to avo…
ankitaj224 f7457d3
Cleaning up the code for redudant checks.
ankitaj224 fe2577c
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package com.google.firebase.installations; | ||
|
||
import static com.google.common.truth.Truth.assertWithMessage; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.DEFAULT_PERSISTED_FID_ENTRY; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_API_KEY; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN; | ||
|
@@ -89,7 +90,7 @@ public class FirebaseInstallationsInstrumentedTest { | |
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken(TEST_AUTH_TOKEN) | ||
.setRefreshToken(TEST_REFRESH_TOKEN) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) | ||
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) | ||
.build(); | ||
|
@@ -99,7 +100,7 @@ public class FirebaseInstallationsInstrumentedTest { | |
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken(TEST_AUTH_TOKEN) | ||
.setRefreshToken(TEST_REFRESH_TOKEN) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) | ||
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP_2) | ||
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) | ||
.build(); | ||
|
@@ -109,7 +110,7 @@ public class FirebaseInstallationsInstrumentedTest { | |
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken("") | ||
.setRefreshToken("") | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) | ||
.setExpiresInSecs(0) | ||
.setRegistrationStatus(PersistedFid.RegistrationStatus.UNREGISTERED) | ||
.build(); | ||
|
@@ -163,7 +164,8 @@ public void setUp() throws FirebaseInstallationServiceException { | |
new FirebaseInstallationServiceException( | ||
"SDK Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)); | ||
when(persistedFidReturnsError.insertOrUpdatePersistedFidEntry(any())).thenReturn(false); | ||
when(persistedFidReturnsError.readPersistedFidEntryValue()).thenReturn(null); | ||
when(persistedFidReturnsError.readPersistedFidEntryValue()) | ||
.thenReturn(DEFAULT_PERSISTED_FID_ENTRY); | ||
when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1); | ||
when(mockClock.currentTimeMillis()).thenReturn(TEST_CREATION_TIMESTAMP_1); | ||
// Mocks success on FIS deletion | ||
|
@@ -190,7 +192,7 @@ public void testGetId_PersistedFidOk_BackendOk() throws Exception { | |
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); | ||
|
||
// No exception, means success. | ||
assertWithMessage("getId Task fails.") | ||
assertWithMessage("getId Task failed") | ||
.that(Tasks.await(firebaseInstallations.getId())) | ||
.isNotEmpty(); | ||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
|
@@ -217,7 +219,7 @@ public void testGetId_multipleCalls_sameFIDReturned() throws Exception { | |
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); | ||
|
||
// No exception, means success. | ||
assertWithMessage("getId Task fails.") | ||
assertWithMessage("getId Task failed") | ||
.that(Tasks.await(firebaseInstallations.getId())) | ||
.isNotEmpty(); | ||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
|
@@ -290,6 +292,36 @@ public void testGetId_PersistedFidError_BackendOk() throws InterruptedException | |
} | ||
} | ||
|
||
@Test | ||
public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception { | ||
// Update local storage with fid entry that has auth token expired. | ||
persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY); | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); | ||
|
||
assertWithMessage("getId Task failed") | ||
.that(Tasks.await(firebaseInstallations.getId())) | ||
.isNotEmpty(); | ||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
assertWithMessage("Persisted Fid doesn't match") | ||
.that(entryValue.getFirebaseInstallationId()) | ||
.isEqualTo(TEST_FID_1); | ||
|
||
// Waiting for Task that generates auth token with the FIS Servers | ||
executor.awaitTermination(500, TimeUnit.MILLISECONDS); | ||
|
||
// Validate that registration is complete with updated auth token | ||
PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); | ||
assertWithMessage("Persisted Fid doesn't match") | ||
.that(updatedFidEntry) | ||
.isEqualTo(UPDATED_AUTH_TOKEN_FID_ENTRY); | ||
verify(backendClientReturnsOk, never()) | ||
.createFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_APP_ID_1); | ||
verify(backendClientReturnsOk, times(1)) | ||
.generateAuthToken(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_fidDoesNotExist_successful() throws Exception { | ||
FirebaseInstallations firebaseInstallations = | ||
|
@@ -418,18 +450,13 @@ public void testGetAuthToken_serverError_failure() throws Exception { | |
@Test | ||
public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce() | ||
throws Exception { | ||
// Using mockPersistedFid to ensure the order of returning persistedFidEntry to 2 tasks | ||
// triggered simultaneously. Task2 waits for Task1 to complete. On Task1 completion, task2 reads | ||
// the UPDATED_AUTH_TOKEN_FID_ENTRY by Task1 on execution. | ||
when(mockPersistedFid.readPersistedFidEntryValue()) | ||
.thenReturn( | ||
EXPIRED_AUTH_TOKEN_ENTRY, | ||
EXPIRED_AUTH_TOKEN_ENTRY, | ||
EXPIRED_AUTH_TOKEN_ENTRY, | ||
UPDATED_AUTH_TOKEN_FID_ENTRY); | ||
// Update local storage with fid entry that has auth token expired. When 2 tasks are triggered | ||
// simultaneously, Task2 waits for Task1 to complete. On Task1 completion, task2 reads the | ||
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. nit: "reads the" has too many (two) spaces |
||
// UPDATED_AUTH_TOKEN_FID_ENTRY by Task1 on execution. | ||
persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY); | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, mockPersistedFid, mockUtils); | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); | ||
|
||
// Call getAuthToken multiple times with DO_NOT_FORCE_REFRESH option | ||
Task<InstallationTokenResult> task1 = | ||
|
@@ -508,7 +535,9 @@ public void testDelete_registeredFID_successful() throws Exception { | |
Tasks.await(firebaseInstallations.delete()); | ||
|
||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull(); | ||
assertWithMessage("Persisted Fid Entry is not null.") | ||
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. the "is not null" message isn't correct any more |
||
.that(entryValue) | ||
.isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); | ||
verify(backendClientReturnsOk, times(1)) | ||
.deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); | ||
} | ||
|
@@ -524,7 +553,9 @@ public void testDelete_unregisteredFID_successful() throws Exception { | |
Tasks.await(firebaseInstallations.delete()); | ||
|
||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull(); | ||
assertWithMessage("Persisted Fid Entry is not null.") | ||
.that(entryValue) | ||
.isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); | ||
verify(backendClientReturnsOk, never()) | ||
.deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); | ||
} | ||
|
@@ -538,7 +569,9 @@ public void testDelete_emptyPersistedFidEntry_successful() throws Exception { | |
Tasks.await(firebaseInstallations.delete()); | ||
|
||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull(); | ||
assertWithMessage("Persisted Fid Entry is not null.") | ||
.that(entryValue) | ||
.isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); | ||
verify(backendClientReturnsOk, never()) | ||
.deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.