diff --git a/app/src/main/kotlin/com/wire/android/WireApplication.kt b/app/src/main/kotlin/com/wire/android/WireApplication.kt index 82b98bec81e..889c35720a4 100644 --- a/app/src/main/kotlin/com/wire/android/WireApplication.kt +++ b/app/src/main/kotlin/com/wire/android/WireApplication.kt @@ -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( diff --git a/app/src/main/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCase.kt b/app/src/main/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCase.kt index d90cdad547c..c0c7aa79c28 100644 --- a/app/src/main/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCase.kt +++ b/app/src/main/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCase.kt @@ -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 @@ -42,13 +44,14 @@ interface ObserveCurrentSessionAnalyticsUseCase { operator fun invoke(): Flow> } -@Suppress("FunctionNaming") +@Suppress("FunctionNaming", "LongParameterList") fun ObserveCurrentSessionAnalyticsUseCase( currentSessionFlow: Flow, isUserTeamMember: suspend (UserId) -> Boolean, observeAnalyticsTrackingIdentifierStatusFlow: suspend (UserId) -> Flow, analyticsIdentifierManagerProvider: (UserId) -> AnalyticsIdentifierManager, - userDataStoreProvider: UserDataStoreProvider + userDataStoreProvider: UserDataStoreProvider, + currentBackend: suspend (UserId) -> SelfServerConfigUseCase.Result ) = object : ObserveCurrentSessionAnalyticsUseCase { private var previousAnalyticsResult: AnalyticsIdentifierResult? = null @@ -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, diff --git a/app/src/test/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCaseTest.kt b/app/src/test/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCaseTest.kt index 070f9c30515..50e963991b8 100644 --- a/app/src/test/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCaseTest.kt +++ b/app/src/test/kotlin/com/wire/android/analytics/ObserveCurrentSessionAnalyticsUseCaseTest.kt @@ -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 @@ -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 @@ -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(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(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 @@ -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 @@ -124,6 +185,7 @@ class ObserveCurrentSessionAnalyticsUseCaseTest { arrangement.setObservingTrackingIdentifierStatus( AnalyticsIdentifierResult.ExistingIdentifier(Arrangement.OTHER_TRACKING_IDENTIFIER) ) + arrangement.setSelfServerConfig(Arrangement.SEVER_CONFIG_PRODUCTION) arrangement.withIsAnonymousUsageDataEnabled(true) // then @@ -148,6 +210,8 @@ class ObserveCurrentSessionAnalyticsUseCaseTest { private val analyticsTrackingIdentifierStatusChannel = Channel(Channel.UNLIMITED) + private val selfServerConfigChannel = Channel(Channel.UNLIMITED) + private val teamMembers = mutableSetOf() private val isTeamMember: (UserId) -> Boolean = { teamMembers.contains(it) } @@ -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) @@ -183,7 +251,10 @@ class ObserveCurrentSessionAnalyticsUseCaseTest { analyticsIdentifierManagerProvider = { analyticsIdentifierManager }, - userDataStoreProvider = userDataStoreProvider + userDataStoreProvider = userDataStoreProvider, + currentBackend = { + selfServerConfigChannel.receive() + } ) fun arrange() = this to useCase @@ -191,6 +262,18 @@ class ObserveCurrentSessionAnalyticsUseCaseTest { 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 + ) + ) + ) } } }