-
Notifications
You must be signed in to change notification settings - Fork 617
FIS getAuthToken implementation. #769
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
Changes from 20 commits
c54292e
b4b6ba8
b8bfc1a
7a2a112
42c20f6
46d936c
48fd7fe
82fb69e
fe04e15
e91cb7e
8569952
b74b163
58a30e3
96e30c2
9e3e34d
824cb63
90673c3
f5b4f8c
3e29941
75f2581
91e49bc
4578880
7d19b50
fe04884
f1d1d37
ec65040
396d273
81b4aeb
3932d3b
8d4d811
3f44b77
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 |
---|---|---|
|
@@ -17,11 +17,14 @@ | |
import static com.google.common.truth.Truth.assertWithMessage; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN_2; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_CREATION_TIMESTAMP_1; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_CREATION_TIMESTAMP_2; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_FID_1; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_PROJECT_ID; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_REFRESH_TOKEN; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP; | ||
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP_2; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
|
@@ -68,6 +71,37 @@ public class FirebaseInstallationsInstrumentedTest { | |
@Mock private PersistedFid persistedFidReturnsError; | ||
@Mock private Utils mockUtils; | ||
@Mock private Clock mockClock; | ||
@Mock private PersistedFid mockPersistedFid; | ||
|
||
private static final PersistedFidEntry REGISTERED_FID_ENTRY = | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken(TEST_AUTH_TOKEN) | ||
.setRefreshToken(TEST_REFRESH_TOKEN) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) | ||
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) | ||
.build(); | ||
|
||
private static final PersistedFidEntry EXPIRED_AUTH_TOKEN_ENTRY = | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken(TEST_AUTH_TOKEN) | ||
.setRefreshToken(TEST_REFRESH_TOKEN) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) | ||
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP_2) | ||
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) | ||
.build(); | ||
|
||
private static final PersistedFidEntry UNREGISTERED_FID_ENTRY = | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken("") | ||
.setRefreshToken("") | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) | ||
.setExpiresInSecs(0) | ||
.setRegistrationStatus(PersistedFid.RegistrationStatus.UNREGISTERED) | ||
.build(); | ||
|
||
@Before | ||
public void setUp() throws FirebaseInstallationServiceException { | ||
|
@@ -92,9 +126,16 @@ public void setUp() throws FirebaseInstallationServiceException { | |
.setAuthToken( | ||
InstallationTokenResult.builder() | ||
.setToken(TEST_AUTH_TOKEN) | ||
.setTokenExpirationTimestampMillis(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.setTokenExpirationInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.build()) | ||
.build()); | ||
when(backendClientReturnsOk.generateAuthToken( | ||
anyString(), anyString(), anyString(), anyString())) | ||
.thenReturn( | ||
InstallationTokenResult.builder() | ||
.setToken(TEST_AUTH_TOKEN_2) | ||
.setTokenExpirationInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.build()); | ||
when(backendClientReturnsError.createFirebaseInstallation( | ||
anyString(), anyString(), anyString(), anyString())) | ||
.thenThrow( | ||
|
@@ -217,4 +258,126 @@ public void testGetId_PersistedFidError_BackendOk() throws InterruptedException | |
.isEqualTo(FirebaseInstallationsException.Status.CLIENT_ERROR); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_fidDoesNotExist_successful() throws Exception { | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); | ||
|
||
Tasks.await(firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); | ||
|
||
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); | ||
assertWithMessage("Persisted Auth Token doesn't match") | ||
.that(entryValue.getAuthToken()) | ||
.isEqualTo(TEST_AUTH_TOKEN); | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_PersistedFidError_failure() throws Exception { | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, | ||
executor, | ||
firebaseApp, | ||
backendClientReturnsOk, | ||
persistedFidReturnsError, | ||
mockUtils); | ||
|
||
// Expect exception | ||
try { | ||
Tasks.await( | ||
firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); | ||
fail(); | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (ExecutionException expected) { | ||
Throwable cause = expected.getCause(); | ||
assertWithMessage("Exception class doesn't match") | ||
.that(cause) | ||
.isInstanceOf(FirebaseInstallationsException.class); | ||
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. Truth has built-in support for unpacking exceptions assertWithMessage("w/e").that(expected).hasCauseThat().isInstanceOf(FirebaseInstallationsException.class) 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. Done. Did you also want the status to be changed? I tried but dint find a easy way. 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. You'd have to create a custom truth subject for that and tbh I don't think it's worth it atm |
||
assertWithMessage("Exception status doesn't match") | ||
.that(((FirebaseInstallationsException) cause).getStatus()) | ||
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_fidExists_successful() throws Exception { | ||
when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY); | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, mockPersistedFid, mockUtils); | ||
|
||
InstallationTokenResult installationTokenResult = | ||
Tasks.await( | ||
firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); | ||
|
||
assertWithMessage("Persisted Auth Token doesn't match") | ||
.that(installationTokenResult.getToken()) | ||
.isEqualTo(TEST_AUTH_TOKEN); | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_expiredAuthToken_fetchedNewTokenFromFIS() throws Exception { | ||
when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(EXPIRED_AUTH_TOKEN_ENTRY); | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, mockPersistedFid, mockUtils); | ||
|
||
InstallationTokenResult installationTokenResult = | ||
Tasks.await( | ||
firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); | ||
|
||
assertWithMessage("Persisted Auth Token doesn't match") | ||
.that(installationTokenResult.getToken()) | ||
.isEqualTo(TEST_AUTH_TOKEN_2); | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_unregisteredFid_fetchedNewTokenFromFIS() throws Exception { | ||
when(mockPersistedFid.readPersistedFidEntryValue()) | ||
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. imo this test could do with some comments explaining what you're testing 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. Done. PTAL. |
||
.thenReturn(UNREGISTERED_FID_ENTRY, REGISTERED_FID_ENTRY); | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, executor, firebaseApp, backendClientReturnsOk, mockPersistedFid, mockUtils); | ||
|
||
InstallationTokenResult installationTokenResult = | ||
Tasks.await( | ||
firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); | ||
|
||
assertWithMessage("Persisted Auth Token doesn't match") | ||
.that(installationTokenResult.getToken()) | ||
.isEqualTo(TEST_AUTH_TOKEN); | ||
} | ||
|
||
@Test | ||
public void testGetAuthToken_serverError_failure() throws Exception { | ||
when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY); | ||
when(backendClientReturnsError.generateAuthToken( | ||
anyString(), anyString(), anyString(), anyString())) | ||
.thenThrow( | ||
new FirebaseInstallationServiceException( | ||
"Server Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)); | ||
FirebaseInstallations firebaseInstallations = | ||
new FirebaseInstallations( | ||
mockClock, | ||
executor, | ||
firebaseApp, | ||
backendClientReturnsError, | ||
mockPersistedFid, | ||
mockUtils); | ||
|
||
// Expect exception | ||
try { | ||
Tasks.await(firebaseInstallations.getAuthToken(FirebaseInstallationsApi.FORCE_REFRESH)); | ||
fail(); | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (ExecutionException expected) { | ||
Throwable cause = expected.getCause(); | ||
assertWithMessage("Exception class doesn't match") | ||
.that(cause) | ||
.isInstanceOf(FirebaseInstallationsException.class); | ||
assertWithMessage("Exception status doesn't match") | ||
.that(((FirebaseInstallationsException) cause).getStatus()) | ||
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); | ||
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. Truth |
||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.