Skip to content

FID delete() implementation. #813

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

Merged
merged 3 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -163,6 +166,16 @@ public void setUp() throws FirebaseInstallationServiceException {
when(persistedFidReturnsError.readPersistedFidEntryValue()).thenReturn(null);
when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1);
when(mockClock.currentTimeMillis()).thenReturn(TEST_CREATION_TIMESTAMP_1);
// Mocks success on FIS deletion
doNothing()
.when(backendClientReturnsOk)
.deleteFirebaseInstallation(anyString(), anyString(), anyString(), anyString());
// Mocks server error on FIS deletion
doThrow(
new FirebaseInstallationServiceException(
"Server Error", FirebaseInstallationServiceException.Status.SERVER_ERROR))
.when(backendClientReturnsError)
.deleteFirebaseInstallation(anyString(), anyString(), anyString(), anyString());
}

@After
Expand Down Expand Up @@ -483,4 +496,77 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th
verify(backendClientReturnsOk, times(2))
.generateAuthToken(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN);
}

@Test
public void testDelete_registeredFID_successful() throws Exception {
// Update local storage with a registered fid entry
persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY);
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);

Tasks.await(firebaseInstallations.delete());

PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull();
verify(backendClientReturnsOk, times(1))
.deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN);
}

@Test
public void testDelete_unregisteredFID_successful() throws Exception {
// Update local storage with a unregistered fid entry
persistedFid.insertOrUpdatePersistedFidEntry(UNREGISTERED_FID_ENTRY);
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);

Tasks.await(firebaseInstallations.delete());

PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull();
verify(backendClientReturnsOk, never())
.deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN);
}

@Test
public void testDelete_emptyPersistedFidEntry_successful() throws Exception {
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(
mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);

Tasks.await(firebaseInstallations.delete());

PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull();
verify(backendClientReturnsOk, never())
.deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN);
}

@Test
public void testDelete_serverError_failure() throws Exception {
// Update local storage with a registered fid entry
persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY);
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(
mockClock, executor, firebaseApp, backendClientReturnsError, persistedFid, mockUtils);

// Expect exception
try {
Tasks.await(firebaseInstallations.delete());
fail("delete() failed due to Server Error.");
} catch (ExecutionException expected) {
assertWithMessage("Exception class doesn't match")
.that(expected)
.hasCauseThat()
.isInstanceOf(FirebaseInstallationsException.class);
assertWithMessage("Exception status doesn't match")
.that(((FirebaseInstallationsException) expected.getCause()).getStatus())
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR);
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid Entry doesn't match")
.that(entryValue)
.isEqualTo(REGISTERED_FID_ENTRY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public synchronized Task<InstallationTokenResult> getAuthToken(
@NonNull
@Override
public Task<Void> delete() {
return Tasks.forResult(null);
return Tasks.call(executor, this::deleteFirebaseInstallationId);
}

/** Returns the application id of the {@link FirebaseApp} of this {@link FirebaseInstallations} */
Expand Down Expand Up @@ -400,6 +400,34 @@ private boolean isAuthTokenExpired(PersistedFidEntry persistedFidEntry) {
private long currentTimeInSecs() {
return TimeUnit.MILLISECONDS.toSeconds(clock.currentTimeMillis());
}

/**
* Deletes the firebase installation id of the {@link FirebaseApp} from FIS servers and local
* storage.
*/
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException {

PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue();

if (isPersistedFidRegistered(persistedFidEntry)) {
// Call the FIS servers to delete this firebase installation id.
try {
serviceClient.deleteFirebaseInstallation(
firebaseApp.getOptions().getApiKey(),
persistedFidEntry.getFirebaseInstallationId(),
firebaseApp.getOptions().getProjectId(),
persistedFidEntry.getRefreshToken());

} catch (FirebaseInstallationServiceException exception) {
throw new FirebaseInstallationsException(
"Failed to delete a Firebase Installation.",
FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR);
}
}

persistedFid.clear();
return null;
}
}

interface Supplier<T> {
Expand Down