diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index 20f28ebc..aa7f4414 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -54,7 +54,14 @@ class RegisterManager @Inject constructor( val did = getUserSettingsValueUseCase.getDid() when { did.isEmpty() || needReInitialization -> initializeNewDevice() - else -> initializeSdk(null) + else -> { + initializeSdk(null) + // A known device asks the server too, on every launch as the iOS and React Native SDKs + // do: the /init answer is where the server hands out the popup for the visit. Only the + // popup is taken from it — the did and the session stay as stored, so nobody is + // re-identified on upgrade. + init(knownDevice = true) + } } } @@ -68,7 +75,7 @@ class RegisterManager @Inject constructor( init() } - private fun init() { + private fun init(knownDevice: Boolean = false) { if (isTestDevice) { BaseInfoError( tag = TAG, @@ -77,10 +84,10 @@ class RegisterManager @Inject constructor( return } - sendInitRequestWithRetry() + sendInitRequestWithRetry(knownDevice = knownDevice) } - private fun sendInitRequestWithRetry(attempt: Int = 0) { + private fun sendInitRequestWithRetry(attempt: Int = 0, knownDevice: Boolean = false) { try { val params = buildInitParams() sendNetworkMethodUseCase.get( @@ -88,11 +95,11 @@ class RegisterManager @Inject constructor( params = params, listener = object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { - handleInitSuccess(response) + handleInitSuccess(response, knownDevice) } override fun onError(code: Int, msg: String?) { - handleInitError(code, msg, attempt) + handleInitError(code, msg, attempt, knownDevice) } } ) @@ -108,7 +115,7 @@ class RegisterManager @Inject constructor( } } - private fun handleInitSuccess(response: JSONObject?) { + private fun handleInitSuccess(response: JSONObject?, knownDevice: Boolean) { val errorHandler = JsonResponseErrorHandler( tag = TAG, response = response @@ -123,6 +130,9 @@ class RegisterManager @Inject constructor( } } + // A known device is already set up from storage: its did and session stay as they are. + if (knownDevice) return + val did = errorHandler.getRequiredField(fieldName = "did") ?: return val seance = errorHandler.getRequiredField(fieldName = "seance") ?: return @@ -130,12 +140,12 @@ class RegisterManager @Inject constructor( initializeSdk(seance) } - private fun handleInitError(code: Int, msg: String?, attempt: Int) { + private fun handleInitError(code: Int, msg: String?, attempt: Int, knownDevice: Boolean) { if (code in 500..Int.MAX_VALUE || code <= 0) { if (attempt < MAX_INIT_RETRIES) { CoroutineScope(Dispatchers.Main).launch { delay(RETRY_DELAY_MILLISECONDS * (attempt + 1)) - sendInitRequestWithRetry(attempt + 1) + sendInitRequestWithRetry(attempt + 1, knownDevice) } } } else { diff --git a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt index 16a80292..23d73f47 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt @@ -31,8 +31,9 @@ import org.robolectric.annotation.Config /** * Guards the device-id policy in [RegisterManager]. The SDK must NOT seed the did with a hardware id * (Settings.Secure.ANDROID_ID): like the iOS, React Native and web SDKs, the first `/init` goes out - * with an empty did and the server-assigned did is persisted. An existing install (non-empty did) is - * left untouched — no re-init, so nobody is silently re-identified on upgrade. + * with an empty did and the server-assigned did is persisted. An existing install (non-empty did) + * keeps its did and session — nobody is silently re-identified on upgrade — but still sends /init on + * every launch, as iOS and React Native do, for the popup the server hands out with it. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [28]) @@ -79,14 +80,23 @@ class RegisterManagerDidTest { } @Test - fun `an existing install with a did is not re-initialized`() { + fun `an existing install sends init for the popup and keeps its did and session`() { every { get.getDid() } returns "EXISTING_DID" + every { get.getSid() } returns "STORED_SID" + every { get.getSidLastActTime() } returns System.currentTimeMillis() + val listener = slot() + every { network.get(eq("init"), any(), capture(listener)) } just Runs manager.initialize(context, contentResolver, autoSendPushToken = false) + verify(exactly = 1) { network.get(eq("init"), any(), any()) } - // No /init round-trip and no did write — the persisted did is reused as-is. - verify(exactly = 0) { network.get(eq("init"), any(), any()) } + // Whatever did and seance the server answers with, the stored ones stay; the popup is shown. + listener.captured.onSuccess(JSONObject(INIT_WITH_POPUP)) verify(exactly = 0) { update.updateDid(any()) } + verify(exactly = 0) { update.updateSid("S") } + verify(exactly = 1) { inApp.shopPopUp(match { it.id == 7 }) } + // The session was set up once, from storage, before the answer came. + verify(exactly = 1) { queue.invoke() } } @Test diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt index 9cc7c709..a65070b6 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt @@ -2,13 +2,18 @@ package com.personalization import android.content.Context import androidx.test.core.app.ApplicationProvider +import com.personalization.api.OnApiCallbackListener import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase import com.personalization.sdk.domain.usecases.userSettings.GetUserSettingsValueUseCase import com.personalization.sdk.domain.usecases.userSettings.UpdateUserSettingsValueUseCase +import io.mockk.Runs +import io.mockk.every +import io.mockk.just import io.mockk.mockk -import io.mockk.verify +import io.mockk.slot +import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -105,11 +110,15 @@ class SdkUpgradeMigrationTest { mockk(relaxed = true) // PushTokenManager ) + val listener = slot() + every { network.get(eq("init"), any(), capture(listener)) } just Runs + manager.initialize(context, context.contentResolver, autoSendPushToken = false) - // The migrated did is non-empty, so RegisterManager takes the existing-install branch: no /init - // round-trip fires and the did is left as-is — nobody is silently re-identified on upgrade. - verify(exactly = 0) { network.get(eq("init"), any(), any()) } + // The migrated did is non-empty, so RegisterManager takes the existing-install branch: /init goes + // out only for the popup, and even a server answer with another did leaves the stored one as-is — + // nobody is silently re-identified on upgrade. + listener.captured.onSuccess(JSONObject("""{"did":"server-assigned","seance":"S"}""")) assertEquals("device-123", get.getDid()) } }