-
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 1 commit
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 |
---|---|---|
|
@@ -50,18 +50,17 @@ | |
import com.google.firebase.installations.remote.InstallationResponse; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.SynchronousQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
import org.mockito.AdditionalAnswers; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.internal.stubbing.answers.AnswersWithDelay; | ||
import org.mockito.internal.stubbing.answers.Returns; | ||
|
||
/** | ||
|
@@ -126,7 +125,7 @@ public class FirebaseInstallationsInstrumentedTest { | |
public void setUp() throws FirebaseInstallationServiceException { | ||
MockitoAnnotations.initMocks(this); | ||
FirebaseApp.clearInstancesForTest(); | ||
executor = new ThreadPoolExecutor(0, 4, 10L, TimeUnit.SECONDS, new SynchronousQueue<>()); | ||
executor = Executors.newSingleThreadExecutor(); | ||
firebaseApp = | ||
FirebaseApp.initializeApp( | ||
ApplicationProvider.getApplicationContext(), | ||
|
@@ -268,13 +267,12 @@ public void testGetId_PersistedFidError_BackendOk() throws InterruptedException | |
Tasks.await(firebaseInstallations.getId()); | ||
fail("Could not update local storage."); | ||
} catch (ExecutionException expected) { | ||
Throwable cause = expected.getCause(); | ||
assertWithMessage("Exception class doesn't match") | ||
.that(expected) | ||
.hasCauseThat() | ||
.isInstanceOf(FirebaseInstallationsException.class); | ||
assertWithMessage("Exception status doesn't match") | ||
.that(((FirebaseInstallationsException) cause).getStatus()) | ||
.that(((FirebaseInstallationsException) expected.getCause()).getStatus()) | ||
.isEqualTo(FirebaseInstallationsException.Status.CLIENT_ERROR); | ||
} | ||
} | ||
|
@@ -310,13 +308,12 @@ public void testGetAuthToken_PersistedFidError_failure() throws Exception { | |
firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); | ||
fail("Could not update local storage."); | ||
} catch (ExecutionException expected) { | ||
Throwable cause = expected.getCause(); | ||
assertWithMessage("Exception class doesn't match") | ||
.that(expected) | ||
.hasCauseThat() | ||
.isInstanceOf(FirebaseInstallationsException.class); | ||
assertWithMessage("Exception status doesn't match") | ||
.that(((FirebaseInstallationsException) cause).getStatus()) | ||
.that(((FirebaseInstallationsException) expected.getCause()).getStatus()) | ||
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); | ||
} | ||
} | ||
|
@@ -395,22 +392,21 @@ public void testGetAuthToken_serverError_failure() throws Exception { | |
Tasks.await(firebaseInstallations.getAuthToken(FirebaseInstallationsApi.FORCE_REFRESH)); | ||
fail("getAuthToken() failed due to Server Error."); | ||
} catch (ExecutionException expected) { | ||
Throwable cause = expected.getCause(); | ||
assertWithMessage("Exception class doesn't match") | ||
.that(expected) | ||
.hasCauseThat() | ||
.isInstanceOf(FirebaseInstallationsException.class); | ||
assertWithMessage("Exception status doesn't match") | ||
.that(((FirebaseInstallationsException) cause).getStatus()) | ||
.that(((FirebaseInstallationsException) expected.getCause()).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 |
||
} | ||
} | ||
|
||
@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 | ||
// 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()) | ||
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. add a comment explaining what this means |
||
.thenReturn( | ||
|
@@ -444,19 +440,19 @@ public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce( | |
public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() throws Exception { | ||
when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY); | ||
// Use a mock ServiceClient for network calls with delay(1000ms) to ensure first task is not | ||
// completed before the second task starts. Hence, we can test multiple calls to getAUthToken() | ||
// completed before the second task starts. Hence, we can test multiple calls to getAuthToken() | ||
// and verify one task waits for another task to complete. | ||
|
||
doAnswer( | ||
new AnswersWithDelay( | ||
AdditionalAnswers.answersWithDelay( | ||
1000, | ||
new Returns( | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
InstallationTokenResult.builder() | ||
.setToken(TEST_AUTH_TOKEN_3) | ||
.setTokenExpirationInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.build()))) | ||
.doAnswer( | ||
new AnswersWithDelay( | ||
AdditionalAnswers.answersWithDelay( | ||
1000, | ||
new Returns( | ||
InstallationTokenResult.builder() | ||
|
@@ -480,10 +476,10 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th | |
// As we cannot ensure which task got executed first, verifying with both expected values | ||
assertWithMessage("Persisted Auth Token doesn't match") | ||
.that(task1.getResult().getToken()) | ||
.isAnyOf(TEST_AUTH_TOKEN_3, TEST_AUTH_TOKEN_4); | ||
.isEqualTo(TEST_AUTH_TOKEN_3); | ||
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. nice |
||
assertWithMessage("Persisted Auth Token doesn't match") | ||
.that(task2.getResult().getToken()) | ||
.isAnyOf(TEST_AUTH_TOKEN_4, TEST_AUTH_TOKEN_3); | ||
.isEqualTo(TEST_AUTH_TOKEN_4); | ||
verify(backendClientReturnsOk, times(2)) | ||
.generateAuthToken(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); | ||
} | ||
|
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.
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 comment
The 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 comment
The 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