Skip to content

Commit

Permalink
chore: Do not send data to Analytics on custom backend (WPB-10020) 🍒 (#…
Browse files Browse the repository at this point in the history
…3295)

Signed-off-by: alexandreferris <[email protected]>
Co-authored-by: Alexandre Ferris <[email protected]>
  • Loading branch information
github-actions[bot] and alexandreferris authored Aug 7, 2024
1 parent 59c23d9 commit 18ae978
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/src/main/kotlin/com/wire/android/WireApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ class WireApplication : BaseApp() {
analyticsIdentifierManagerProvider = {
coreLogic.get().getSessionScope(it).analyticsIdentifierManager
},
userDataStoreProvider = userDataStoreProvider.get()
userDataStoreProvider = userDataStoreProvider.get(),
currentBackend = {
coreLogic.get().getSessionScope(it).users.serverLinks()
}
).invoke()

AnonymousAnalyticsManagerImpl.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package com.wire.android.analytics

import com.wire.android.datastore.UserDataStoreProvider
import com.wire.android.feature.analytics.model.AnalyticsResult
import com.wire.kalium.logic.configuration.server.ServerConfig
import com.wire.kalium.logic.data.analytics.AnalyticsIdentifierResult
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.analytics.AnalyticsIdentifierManager
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.user.SelfServerConfigUseCase
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
Expand All @@ -42,13 +44,14 @@ interface ObserveCurrentSessionAnalyticsUseCase {
operator fun invoke(): Flow<AnalyticsResult<AnalyticsIdentifierManager>>
}

@Suppress("FunctionNaming")
@Suppress("FunctionNaming", "LongParameterList")
fun ObserveCurrentSessionAnalyticsUseCase(
currentSessionFlow: Flow<CurrentSessionResult>,
isUserTeamMember: suspend (UserId) -> Boolean,
observeAnalyticsTrackingIdentifierStatusFlow: suspend (UserId) -> Flow<AnalyticsIdentifierResult>,
analyticsIdentifierManagerProvider: (UserId) -> AnalyticsIdentifierManager,
userDataStoreProvider: UserDataStoreProvider
userDataStoreProvider: UserDataStoreProvider,
currentBackend: suspend (UserId) -> SelfServerConfigUseCase.Result
) = object : ObserveCurrentSessionAnalyticsUseCase {

private var previousAnalyticsResult: AnalyticsIdentifierResult? = null
Expand All @@ -74,7 +77,12 @@ fun ObserveCurrentSessionAnalyticsUseCase(
) { identifierResult, enabled ->
previousAnalyticsResult = identifierResult

if (enabled) {
val isProdBackend = when (val serverConfig = currentBackend(userId)) {
is SelfServerConfigUseCase.Result.Success -> serverConfig.serverLinks.links.api == ServerConfig.PRODUCTION.api
is SelfServerConfigUseCase.Result.Failure -> false
}

if (enabled && isProdBackend) {
AnalyticsResult(
identifierResult = identifierResult,
isTeamMember = isTeamMember,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ import com.wire.android.assertIs
import com.wire.android.datastore.UserDataStore
import com.wire.android.datastore.UserDataStoreProvider
import com.wire.android.framework.TestUser
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.configuration.server.CommonApiVersionType
import com.wire.kalium.logic.configuration.server.ServerConfig
import com.wire.kalium.logic.data.analytics.AnalyticsIdentifierResult
import com.wire.kalium.logic.data.auth.AccountInfo
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.analytics.AnalyticsIdentifierManager
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.user.SelfServerConfigUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.every
Expand Down Expand Up @@ -66,6 +70,7 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {
setCurrentSession(CurrentSessionResult.Success(AccountInfo.Valid(TestUser.SELF_USER.id)))
setIsTeamMember(TestUser.SELF_USER.id)
setObservingTrackingIdentifierStatus(AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.CURRENT_TRACKING_IDENTIFIER))
setSelfServerConfig(Arrangement.SEVER_CONFIG_PRODUCTION)
}.arrange()

// when
Expand All @@ -89,6 +94,61 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {
setObservingTrackingIdentifierStatus(
AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.CURRENT_TRACKING_IDENTIFIER)
)
setSelfServerConfig(Arrangement.SEVER_CONFIG_PRODUCTION)
}.arrange()

// when
useCase.invoke().test {
// then
val item = awaitItem()
assertIs<AnalyticsIdentifierResult.Disabled>(item.identifierResult)
assertEquals(true, item.isTeamMember)
assertEquals(true, item.manager != null)
}
}

@Test
fun givenThereIsAValidSessionButCustomBackend_whenObservingCurrentSessionAnalytics_thenDisabledAnalyticsResultIsReturned() =
runTest {
// given
val (_, useCase) = Arrangement()
.withIsAnonymousUsageDataEnabled(true)
.apply {
setCurrentSession(CurrentSessionResult.Success(AccountInfo.Valid(TestUser.SELF_USER.id)))
setIsTeamMember(TestUser.SELF_USER.id)
setObservingTrackingIdentifierStatus(
AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.CURRENT_TRACKING_IDENTIFIER)
)
setSelfServerConfig(
Arrangement.SEVER_CONFIG_PRODUCTION.copy(
serverLinks = Arrangement.SEVER_CONFIG_PRODUCTION.serverLinks.copy(links = ServerConfig.STAGING)
)
)
}.arrange()

// when
useCase.invoke().test {
// then
val item = awaitItem()
assertIs<AnalyticsIdentifierResult.Disabled>(item.identifierResult)
assertEquals(true, item.isTeamMember)
assertEquals(true, item.manager != null)
}
}

@Test
fun givenThereIsAValidSessionButFailureOnCustomBackend_whenObservingCurrentSessionAnalytics_thenDisabledAnalyticsResultIsReturned() =
runTest {
// given
val (_, useCase) = Arrangement()
.withIsAnonymousUsageDataEnabled(true)
.apply {
setCurrentSession(CurrentSessionResult.Success(AccountInfo.Valid(TestUser.SELF_USER.id)))
setIsTeamMember(TestUser.SELF_USER.id)
setObservingTrackingIdentifierStatus(
AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.CURRENT_TRACKING_IDENTIFIER)
)
setSelfServerConfig(SelfServerConfigUseCase.Result.Failure(CoreFailure.Unknown(null)))
}.arrange()

// when
Expand All @@ -110,6 +170,7 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {
setCurrentSession(CurrentSessionResult.Success(AccountInfo.Valid(TestUser.SELF_USER.id)))
setIsTeamMember(TestUser.SELF_USER.id)
setObservingTrackingIdentifierStatus(AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.CURRENT_TRACKING_IDENTIFIER))
setSelfServerConfig(Arrangement.SEVER_CONFIG_PRODUCTION)
}.arrange()

// when
Expand All @@ -124,6 +185,7 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {
arrangement.setObservingTrackingIdentifierStatus(
AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.OTHER_TRACKING_IDENTIFIER)
)
arrangement.setSelfServerConfig(Arrangement.SEVER_CONFIG_PRODUCTION)
arrangement.withIsAnonymousUsageDataEnabled(true)

// then
Expand All @@ -148,6 +210,8 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {

private val analyticsTrackingIdentifierStatusChannel = Channel<AnalyticsIdentifierResult>(Channel.UNLIMITED)

private val selfServerConfigChannel = Channel<SelfServerConfigUseCase.Result>(Channel.UNLIMITED)

private val teamMembers = mutableSetOf<UserId>()

private val isTeamMember: (UserId) -> Boolean = { teamMembers.contains(it) }
Expand All @@ -169,6 +233,10 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {
analyticsTrackingIdentifierStatusChannel.send(result)
}

suspend fun setSelfServerConfig(result: SelfServerConfigUseCase.Result) {
selfServerConfigChannel.send(result)
}

fun withIsAnonymousUsageDataEnabled(result: Boolean): Arrangement = apply {
every { userDataStoreProvider.getOrCreate(any()) } returns userDataStore
coEvery { userDataStore.isAnonymousUsageDataEnabled() } returns flowOf(result)
Expand All @@ -183,14 +251,29 @@ class ObserveCurrentSessionAnalyticsUseCaseTest {
analyticsIdentifierManagerProvider = {
analyticsIdentifierManager
},
userDataStoreProvider = userDataStoreProvider
userDataStoreProvider = userDataStoreProvider,
currentBackend = {
selfServerConfigChannel.receive()
}
)

fun arrange() = this to useCase

companion object {
const val CURRENT_TRACKING_IDENTIFIER = "abcd-1234"
const val OTHER_TRACKING_IDENTIFIER = "aaaa-bbbb-1234"

val SEVER_CONFIG_PRODUCTION = SelfServerConfigUseCase.Result.Success(
serverLinks = ServerConfig(
id = "server_id",
links = ServerConfig.PRODUCTION,
metaData = ServerConfig.MetaData(
federation = false,
commonApiVersion = CommonApiVersionType.New,
domain = null
)
)
)
}
}
}

0 comments on commit 18ae978

Please sign in to comment.