diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt index 75811b8e482..451a14b6779 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt @@ -1890,6 +1890,7 @@ class UserSessionScope internal constructor( certificateRevocationListRepository, incrementalSyncRepository, sessionManager, + selfTeamId, checkRevocationList, syncFeatureConfigsUseCase, userScopedLogger diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCase.kt index b698bea111f..be386f4e509 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCase.kt @@ -21,7 +21,8 @@ package com.wire.kalium.logic.feature.personaltoteamaccount import com.wire.kalium.logic.configuration.server.ServerConfigRepository -import com.wire.kalium.logic.data.user.UserRepository +import com.wire.kalium.logic.data.id.SelfTeamIdProvider +import com.wire.kalium.logic.functional.fold import com.wire.kalium.network.session.SessionManager /** @@ -35,11 +36,14 @@ interface CanMigrateFromPersonalToTeamUseCase { internal class CanMigrateFromPersonalToTeamUseCaseImpl( val sessionManager: SessionManager, val serverConfigRepository: ServerConfigRepository, - val userRepository: UserRepository + val selfTeamIdProvider: SelfTeamIdProvider ) : CanMigrateFromPersonalToTeamUseCase { override suspend fun invoke(): Boolean { val commonApiVersion = sessionManager.serverConfig().metaData.commonApiVersion.version val minApi = serverConfigRepository.minimumApiVersionForPersonalToTeamAccountMigration - return userRepository.getSelfUser()?.teamId == null && commonApiVersion >= minApi + return selfTeamIdProvider().fold( + { false }, + { teamId -> teamId == null && commonApiVersion >= minApi } + ) } } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt index e7b03086a33..9bae507ae54 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt @@ -31,6 +31,7 @@ import com.wire.kalium.logic.data.e2ei.CertificateRevocationListRepository import com.wire.kalium.logic.data.e2ei.E2EIRepository import com.wire.kalium.logic.data.e2ei.RevocationListChecker import com.wire.kalium.logic.data.id.CurrentClientIdProvider +import com.wire.kalium.logic.data.id.SelfTeamIdProvider import com.wire.kalium.logic.data.properties.UserPropertyRepository import com.wire.kalium.logic.data.session.SessionRepository import com.wire.kalium.logic.data.sync.IncrementalSyncRepository @@ -113,6 +114,7 @@ class UserScope internal constructor( private val certificateRevocationListRepository: CertificateRevocationListRepository, private val incrementalSyncRepository: IncrementalSyncRepository, private val sessionManager: SessionManager, + private val selfTeamIdProvider: SelfTeamIdProvider, private val checkRevocationList: RevocationListChecker, private val syncFeatureConfigs: SyncFeatureConfigsUseCase, private val userScopedLogger: KaliumLogger @@ -232,7 +234,7 @@ class UserScope internal constructor( CanMigrateFromPersonalToTeamUseCaseImpl( sessionManager = sessionManager, serverConfigRepository = serverConfigRepository, - userRepository = userRepository + selfTeamIdProvider = selfTeamIdProvider ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCaseTest.kt index 72f45a154de..9a806ec5816 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCaseTest.kt @@ -17,10 +17,11 @@ */ package com.wire.kalium.logic.feature.personaltoteamaccount +import com.wire.kalium.logic.CoreFailure import com.wire.kalium.logic.configuration.server.ServerConfigRepository +import com.wire.kalium.logic.data.id.SelfTeamIdProvider import com.wire.kalium.logic.data.id.TeamId -import com.wire.kalium.logic.data.user.UserRepository -import com.wire.kalium.logic.framework.TestUser +import com.wire.kalium.logic.functional.Either import com.wire.kalium.network.api.unbound.configuration.ApiVersionDTO import com.wire.kalium.network.session.SessionManager import com.wire.kalium.network.utils.TestRequestHandler.Companion.TEST_BACKEND_CONFIG @@ -28,7 +29,6 @@ import io.mockative.Mock import io.mockative.coEvery import io.mockative.every import io.mockative.mock -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertFalse @@ -39,9 +39,9 @@ class CanMigrateFromPersonalToTeamUseCaseTest { @Test fun givenAPIVersionBelowMinimumAndUserNotInATeam_whenInvoking_thenReturnsFalse() = runTest { // Given - val (arrangement, useCase) = Arrangement() + val (_, useCase) = Arrangement() .withRepositoryReturningMinimumApiVersion() - .withTeamId(null) + .withTeamId(Either.Right(null)) .withServerConfig(6) .arrange() @@ -54,12 +54,12 @@ class CanMigrateFromPersonalToTeamUseCaseTest { @Test fun givenAPIVersionEqualToMinimumAndUserNotInATeam_whenInvoking_thenReturnsTrue() = - runBlocking { + runTest { // Given - val (arrangement, useCase) = Arrangement() + val (_, useCase) = Arrangement() .withRepositoryReturningMinimumApiVersion() .withServerConfig(7) - .withTeamId(null) + .withTeamId(Either.Right(null)) .arrange() // When @@ -70,11 +70,28 @@ class CanMigrateFromPersonalToTeamUseCaseTest { } @Test - fun givenAPIVersionAboveMinimumAndUserInATeam_whenInvoking_thenReturnsFalse() = runBlocking { + fun givenAPIVersionAboveMinimumAndUserInATeam_whenInvoking_thenReturnsFalse() = runTest { // Given - val (arrangement, useCase) = Arrangement() + val (_, useCase) = Arrangement() .withRepositoryReturningMinimumApiVersion() - .withTeamId(TeamId("teamId")) + .withTeamId(Either.Right(TeamId("teamId"))) + .withServerConfig(9) + .arrange() + + // When + val result = useCase.invoke() + + // Then + assertFalse(result) + } + + + @Test + fun givenSelfTeamIdProviderFailure_whenInvoking_thenReturnsFalse() = runTest { + // Given + val (_, useCase) = Arrangement() + .withRepositoryReturningMinimumApiVersion() + .withTeamId(Either.Left(CoreFailure.MissingClientRegistration)) .withServerConfig(9) .arrange() @@ -94,12 +111,12 @@ class CanMigrateFromPersonalToTeamUseCaseTest { val sessionManager = mock(SessionManager::class) @Mock - val userRepository = mock(UserRepository::class) + val selfTeamIdProvider = mock(SelfTeamIdProvider::class) - suspend fun withTeamId(result: TeamId?) = apply { + suspend fun withTeamId(result: Either) = apply { coEvery { - userRepository.getSelfUser() - }.returns(TestUser.SELF.copy(teamId = result)) + selfTeamIdProvider() + }.returns(result) } fun withRepositoryReturningMinimumApiVersion() = apply { @@ -122,7 +139,7 @@ class CanMigrateFromPersonalToTeamUseCaseTest { fun arrange() = this to CanMigrateFromPersonalToTeamUseCaseImpl( sessionManager = sessionManager, serverConfigRepository = serverConfigRepository, - userRepository = userRepository + selfTeamIdProvider = selfTeamIdProvider ) }