-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add use case to check if backend supports personal to team migr…
…ation (WPB-12022) (#3114) * feat: add use case to check if backend supports personal to team migration * feat: detekt * chore: unit test * chore: address comment * chore: cleanup * chore: address comments * chore: address comments * chore: cleanup * chore: cleanup * chore: use selfTeamIdProvider
- Loading branch information
Showing
8 changed files
with
245 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...om/wire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly") | ||
|
||
package com.wire.kalium.logic.feature.personaltoteamaccount | ||
|
||
import com.wire.kalium.logic.configuration.server.ServerConfigRepository | ||
import com.wire.kalium.logic.data.id.SelfTeamIdProvider | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.network.session.SessionManager | ||
|
||
/** | ||
* Use case to check if the user can migrate from personal to team account. | ||
* The user can migrate if the user is not in a team and the server supports the migration. | ||
*/ | ||
interface CanMigrateFromPersonalToTeamUseCase { | ||
suspend operator fun invoke(): Boolean | ||
} | ||
|
||
internal class CanMigrateFromPersonalToTeamUseCaseImpl( | ||
val sessionManager: SessionManager, | ||
val serverConfigRepository: ServerConfigRepository, | ||
val selfTeamIdProvider: SelfTeamIdProvider | ||
) : CanMigrateFromPersonalToTeamUseCase { | ||
override suspend fun invoke(): Boolean { | ||
val commonApiVersion = sessionManager.serverConfig().metaData.commonApiVersion.version | ||
val minApi = serverConfigRepository.minimumApiVersionForPersonalToTeamAccountMigration | ||
return selfTeamIdProvider().fold( | ||
{ false }, | ||
{ teamId -> teamId == null && commonApiVersion >= minApi } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...ire/kalium/logic/feature/personaltoteamaccount/CanMigrateFromPersonalToTeamUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
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.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 | ||
import io.mockative.Mock | ||
import io.mockative.coEvery | ||
import io.mockative.every | ||
import io.mockative.mock | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class CanMigrateFromPersonalToTeamUseCaseTest { | ||
|
||
@Test | ||
fun givenAPIVersionBelowMinimumAndUserNotInATeam_whenInvoking_thenReturnsFalse() = runTest { | ||
// Given | ||
val (_, useCase) = Arrangement() | ||
.withRepositoryReturningMinimumApiVersion() | ||
.withTeamId(Either.Right(null)) | ||
.withServerConfig(6) | ||
.arrange() | ||
|
||
// When | ||
val result = useCase.invoke() | ||
|
||
// Then | ||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun givenAPIVersionEqualToMinimumAndUserNotInATeam_whenInvoking_thenReturnsTrue() = | ||
runTest { | ||
// Given | ||
val (_, useCase) = Arrangement() | ||
.withRepositoryReturningMinimumApiVersion() | ||
.withServerConfig(7) | ||
.withTeamId(Either.Right(null)) | ||
.arrange() | ||
|
||
// When | ||
val result = useCase.invoke() | ||
|
||
// Then | ||
assertTrue(result) | ||
} | ||
|
||
@Test | ||
fun givenAPIVersionAboveMinimumAndUserInATeam_whenInvoking_thenReturnsFalse() = runTest { | ||
// Given | ||
val (_, useCase) = Arrangement() | ||
.withRepositoryReturningMinimumApiVersion() | ||
.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() | ||
|
||
// When | ||
val result = useCase.invoke() | ||
|
||
// Then | ||
assertFalse(result) | ||
} | ||
|
||
private class Arrangement { | ||
|
||
@Mock | ||
val serverConfigRepository = mock(ServerConfigRepository::class) | ||
|
||
@Mock | ||
val sessionManager = mock(SessionManager::class) | ||
|
||
@Mock | ||
val selfTeamIdProvider = mock(SelfTeamIdProvider::class) | ||
|
||
suspend fun withTeamId(result: Either<CoreFailure, TeamId?>) = apply { | ||
coEvery { | ||
selfTeamIdProvider() | ||
}.returns(result) | ||
} | ||
|
||
fun withRepositoryReturningMinimumApiVersion() = apply { | ||
every { | ||
serverConfigRepository.minimumApiVersionForPersonalToTeamAccountMigration | ||
}.returns(MIN_API_VERSION) | ||
} | ||
|
||
fun withServerConfig(apiVersion: Int) = apply { | ||
val backendConfig = TEST_BACKEND_CONFIG.copy( | ||
metaData = TEST_BACKEND_CONFIG.metaData.copy( | ||
commonApiVersion = ApiVersionDTO.Valid(apiVersion) | ||
) | ||
) | ||
every { | ||
sessionManager.serverConfig() | ||
}.returns(backendConfig) | ||
} | ||
|
||
fun arrange() = this to CanMigrateFromPersonalToTeamUseCaseImpl( | ||
sessionManager = sessionManager, | ||
serverConfigRepository = serverConfigRepository, | ||
selfTeamIdProvider = selfTeamIdProvider | ||
) | ||
} | ||
|
||
companion object { | ||
private const val MIN_API_VERSION = 7 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters