Skip to content

Commit

Permalink
Fixed callback issue with set email after logout
Browse files Browse the repository at this point in the history
  • Loading branch information
jkasten2 committed Feb 17, 2018
1 parent 713ead3 commit cb81e74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ protected void scheduleSyncToServer() {
void setEmail(String email, String emailAuthHash) {
JSONObject syncValues = getUserStateForModification().syncValues;

if (email.equals(syncValues.optString("identifier")) && syncValues.optString("email_auth_hash").equals(emailAuthHash == null ? "" : emailAuthHash)) {
boolean noChange = email.equals(syncValues.optString("identifier")) &&
syncValues.optString("email_auth_hash").equals(emailAuthHash == null ? "" : emailAuthHash);
if (noChange) {
OneSignal.fireEmailUpdateSuccess();
return;
}

String existingEmail = syncValues.optString("identifier", null);
if (existingEmail == null)
setSyncAsNewSession();

try {
JSONObject emailJSON = new JSONObject();
emailJSON.put("identifier", email);
Expand All @@ -69,7 +75,6 @@ void setEmail(String email, String emailAuthHash) {
emailJSON.put("email_auth_hash", emailAuthHash);

if (emailAuthHash == null) {
String existingEmail = syncValues.optString("identifier", null);
if (existingEmail != null && !existingEmail.equals(email)) {
OneSignal.saveEmailId("");
resetCurrentState();
Expand All @@ -78,6 +83,7 @@ void setEmail(String email, String emailAuthHash) {
}

generateJsonDiff(syncValues, emailJSON, syncValues, null);
scheduleSyncToServer();
}
catch (JSONException e) {
e.printStackTrace();
Expand Down Expand Up @@ -107,8 +113,13 @@ protected void addOnSessionOrCreateExtras(JSONObject jsonBody) {
@Override
void logoutEmail() {
OneSignal.saveEmailId("");

resetCurrentState();
nextSyncIsSession = false;
toSyncUserState.syncValues.remove("identifier");
toSyncUserState.syncValues.remove("email_auth_hash");
toSyncUserState.syncValues.remove("device_player_id");
toSyncUserState.persistState();

OneSignal.getPermissionSubscriptionState().emailSubscriptionStatus.clearEmailAndId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ public void shouldFireOnSuccessOfEmailUpdate() throws Exception {
OneSignalInit();
TestEmailUpdateHandler testEmailUpdateHandler = new TestEmailUpdateHandler();
OneSignal.setEmail("[email protected]", testEmailUpdateHandler);
Assert.assertFalse(testEmailUpdateHandler.emailFiredSuccess);
threadAndTaskWait();

Assert.assertTrue(testEmailUpdateHandler.emailFiredSuccess);
Expand Down Expand Up @@ -1005,6 +1006,19 @@ public void shouldFireOnFailureOfEmailUpdateOnNetworkFailure() throws Exception
Assert.assertEquals(OneSignal.EmailErrorType.NETWORK, testEmailUpdateHandler.emailFiredFailure.getType());
}

@Test
public void shouldFireOnSuccessOnlyAfterNetworkCallAfterLogout() throws Exception {
OneSignalInit();
emailSetThenLogout();
TestEmailUpdateHandler testEmailUpdateHandler = new TestEmailUpdateHandler();
OneSignal.setEmail("[email protected]", testEmailUpdateHandler);
Assert.assertFalse(testEmailUpdateHandler.emailFiredSuccess);
threadAndTaskWait();

Assert.assertTrue(testEmailUpdateHandler.emailFiredSuccess);
Assert.assertNull(testEmailUpdateHandler.emailFiredFailure);
}

// Should create a new email instead of updating existing player record when no auth hash
@Test
public void shouldDoPostOnEmailChange() throws Exception {
Expand Down

0 comments on commit cb81e74

Please sign in to comment.