Skip to content
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 @@ -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)
}
}
}

Expand All @@ -68,7 +75,7 @@ class RegisterManager @Inject constructor(
init()
}

private fun init() {
private fun init(knownDevice: Boolean = false) {
if (isTestDevice) {
BaseInfoError(
tag = TAG,
Expand All @@ -77,22 +84,22 @@ 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(
method = GET_INIT_METHOD,
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)
}
}
)
Expand All @@ -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
Expand All @@ -123,19 +130,22 @@ 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

updateUserSettingsValueUseCase.updateDid(did)
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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<OnApiCallbackListener>()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,11 +110,15 @@ class SdkUpgradeMigrationTest {
mockk(relaxed = true) // PushTokenManager
)

val listener = slot<OnApiCallbackListener>()
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())
}
}
Loading