-
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 approve legal hold [WPB-4393] (#2239)
* feat: add use case to approve legal hold [WPB-4393] * add doc comments * trigger build * update jvm tests * revert jvm tests --------- Co-authored-by: Yamil Medina <[email protected]>
- Loading branch information
1 parent
abb96d1
commit a950ad7
Showing
8 changed files
with
330 additions
and
0 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
84 changes: 84 additions & 0 deletions
84
.../src/commonMain/kotlin/com/wire/kalium/logic/feature/legalhold/ApproveLegalHoldUseCase.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,84 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.legalhold | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.NetworkFailure | ||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.data.id.SelfTeamIdProvider | ||
import com.wire.kalium.logic.data.team.TeamRepository | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.flatMap | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.network.exceptions.KaliumException | ||
import com.wire.kalium.network.exceptions.isAccessDenied | ||
import com.wire.kalium.network.exceptions.isBadRequest | ||
import io.ktor.http.HttpStatusCode | ||
|
||
/** | ||
* Use Case that allows the user to accept a requested legal hold. | ||
*/ | ||
interface ApproveLegalHoldUseCase { | ||
|
||
/** | ||
* Use case [ApproveLegalHoldUseCase] operation | ||
* | ||
* @param password password for the user account to confirm the action, can be empty for sso users | ||
* @return a [ApproveLegalHoldUseCase.Result] indicating the operation result | ||
*/ | ||
suspend operator fun invoke(password: String?): Result | ||
|
||
sealed class Result { | ||
data object Success : Result() | ||
sealed class Failure : Result() { | ||
data class GenericFailure(val coreFailure: CoreFailure) : Failure() | ||
data object InvalidPassword : Failure() | ||
data object PasswordRequired : Failure() | ||
} | ||
} | ||
} | ||
|
||
class ApproveLegalHoldUseCaseImpl internal constructor( | ||
private val teamRepository: TeamRepository, | ||
private val selfTeamIdProvider: SelfTeamIdProvider, | ||
) : ApproveLegalHoldUseCase { | ||
override suspend fun invoke(password: String?): ApproveLegalHoldUseCase.Result { | ||
return selfTeamIdProvider() | ||
.flatMap { | ||
if (it == null) Either.Left(StorageFailure.DataNotFound) | ||
else Either.Right(it) | ||
} | ||
.flatMap { teamId -> | ||
teamRepository.approveLegalHold(teamId, password) | ||
} | ||
.fold({ handleError(it) }, { ApproveLegalHoldUseCase.Result.Success }) | ||
} | ||
|
||
private fun handleError(failure: CoreFailure): ApproveLegalHoldUseCase.Result.Failure = | ||
if (failure is NetworkFailure.ServerMiscommunication && failure.kaliumException is KaliumException.InvalidRequestError) | ||
failure.kaliumException.let { error: KaliumException.InvalidRequestError -> | ||
when { | ||
error.errorResponse.code == HttpStatusCode.BadRequest.value && error.isBadRequest() -> | ||
ApproveLegalHoldUseCase.Result.Failure.InvalidPassword | ||
error.errorResponse.code == HttpStatusCode.Forbidden.value && error.isAccessDenied() -> | ||
ApproveLegalHoldUseCase.Result.Failure.PasswordRequired | ||
else -> ApproveLegalHoldUseCase.Result.Failure.GenericFailure(failure) | ||
} | ||
} | ||
else ApproveLegalHoldUseCase.Result.Failure.GenericFailure(failure) | ||
} |
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
165 changes: 165 additions & 0 deletions
165
.../commonTest/kotlin/com/wire/kalium/logic/feature/legalhold/ApproveLegalHoldUseCaseTest.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,165 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.legalhold | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.NetworkFailure | ||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.data.id.SelfTeamIdProvider | ||
import com.wire.kalium.logic.data.id.TeamId | ||
import com.wire.kalium.logic.data.team.TeamRepository | ||
import com.wire.kalium.logic.framework.TestTeam | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.test_util.TestNetworkException | ||
import com.wire.kalium.network.exceptions.KaliumException | ||
import io.ktor.utils.io.errors.IOException | ||
import io.mockative.Mock | ||
import io.mockative.anything | ||
import io.mockative.eq | ||
import io.mockative.given | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import io.mockative.verify | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertSame | ||
|
||
class ApproveLegalHoldUseCaseTest { | ||
|
||
@Test | ||
fun givenApproveLegalHoldParams_whenApproving_thenTheRepositoryShouldBeCalledWithCorrectParameters() = runTest { | ||
// given | ||
val selfTeamId = TeamId(TestTeam.TEAM.id) | ||
val password = "password" | ||
val (arrangement, useCase) = Arrangement() | ||
.withGetSelfTeamResult(Either.Right(selfTeamId)) | ||
.withApproveLegalHoldResult(Either.Right(Unit)) | ||
.arrange() | ||
// when | ||
useCase.invoke(password) | ||
// then | ||
verify(arrangement.teamRepository) | ||
.suspendFunction(arrangement.teamRepository::approveLegalHold) | ||
.with(eq(selfTeamId), eq(password)) | ||
.wasInvoked(once) | ||
} | ||
|
||
@Test | ||
fun givenGetSelfTeamIdFails_whenApproving_thenDataNotFoundErrorShouldBeReturned() = runTest { | ||
// given | ||
val password = "password" | ||
val (_, useCase) = Arrangement() | ||
.withGetSelfTeamResult(Either.Left(StorageFailure.DataNotFound)) | ||
.arrange() | ||
// when | ||
val result = useCase.invoke(password) | ||
// then | ||
assertIs<ApproveLegalHoldUseCase.Result.Failure.GenericFailure>(result) | ||
assertSame(StorageFailure.DataNotFound, result.coreFailure) | ||
} | ||
|
||
@Test | ||
fun givenApproveFailsDueToGenericError_whenApproving_thenGenericErrorShouldBeReturned() = runTest { | ||
// given | ||
val selfTeamId = TeamId(TestTeam.TEAM.id) | ||
val password = "password" | ||
val failure = NetworkFailure.ServerMiscommunication(KaliumException.GenericError(IOException("no internet"))) | ||
val (_, useCase) = Arrangement() | ||
.withGetSelfTeamResult(Either.Right(selfTeamId)) | ||
.withApproveLegalHoldResult(Either.Left(failure)) | ||
.arrange() | ||
// when | ||
val result = useCase(password) | ||
// then | ||
assertIs<ApproveLegalHoldUseCase.Result.Failure.GenericFailure>(result) | ||
assertSame(failure, result.coreFailure) | ||
} | ||
|
||
@Test | ||
fun givenApproveFailsDueToBadRequest_whenApproving_thenInvalidPasswordErrorShouldBeReturned() = runTest { | ||
// given | ||
val selfTeamId = TeamId(TestTeam.TEAM.id) | ||
val password = "password" | ||
val failure = NetworkFailure.ServerMiscommunication(TestNetworkException.badRequest) | ||
val (_, useCase) = Arrangement() | ||
.withGetSelfTeamResult(Either.Right(selfTeamId)) | ||
.withApproveLegalHoldResult(Either.Left(failure)) | ||
.arrange() | ||
// when | ||
val result = useCase(password) | ||
// then | ||
assertIs<ApproveLegalHoldUseCase.Result.Failure.InvalidPassword>(result) | ||
} | ||
|
||
@Test | ||
fun givenApproveFailsDueToAccessDenied_whenDeleting_thenPasswordRequiredErrorShouldBeReturned() = runTest { | ||
// given | ||
val selfTeamId = TeamId(TestTeam.TEAM.id) | ||
val failure = NetworkFailure.ServerMiscommunication(TestNetworkException.accessDenied) | ||
val (_, useCase) = Arrangement() | ||
.withGetSelfTeamResult(Either.Right(selfTeamId)) | ||
.withApproveLegalHoldResult(Either.Left(failure)) | ||
.arrange() | ||
// when | ||
val result = useCase(null) | ||
// then | ||
assertIs<ApproveLegalHoldUseCase.Result.Failure.PasswordRequired>(result) | ||
} | ||
|
||
@Test | ||
fun givenApproveSucceeds_whenApproving_thenSuccessShouldBeReturned() = runTest { | ||
// given | ||
val selfTeamId = TeamId(TestTeam.TEAM.id) | ||
val password = "password" | ||
val (_, useCase) = Arrangement() | ||
.withGetSelfTeamResult(Either.Right(selfTeamId)) | ||
.withApproveLegalHoldResult(Either.Right(Unit)) | ||
.arrange() | ||
// when | ||
val result = useCase(password) | ||
// then | ||
assertIs<ApproveLegalHoldUseCase.Result.Success>(result) | ||
} | ||
|
||
private class Arrangement { | ||
|
||
@Mock | ||
val teamRepository: TeamRepository = mock(TeamRepository::class) | ||
@Mock | ||
val selfTeamIdProvider: SelfTeamIdProvider = mock(SelfTeamIdProvider::class) | ||
|
||
val useCase: ApproveLegalHoldUseCase by lazy { ApproveLegalHoldUseCaseImpl(teamRepository, selfTeamIdProvider) } | ||
|
||
fun arrange() = this to useCase | ||
|
||
fun withGetSelfTeamResult(result: Either<CoreFailure, TeamId?>) = apply { | ||
given(selfTeamIdProvider) | ||
.suspendFunction(selfTeamIdProvider::invoke) | ||
.whenInvoked() | ||
.thenReturn(result) | ||
} | ||
|
||
fun withApproveLegalHoldResult(result: Either<CoreFailure, Unit>) = apply { | ||
given(teamRepository) | ||
.suspendFunction(teamRepository::approveLegalHold) | ||
.whenInvokedWith(anything(), anything()) | ||
.thenReturn(result) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.