From 836e97aaa73e29c617f13ccf7b33ef26921c59a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= Date: Fri, 26 Jan 2024 13:55:34 +0100 Subject: [PATCH 1/2] feat: handle legal hold failure result when sending connection [WPB-4395] --- .../SendConnectionRequestUseCase.kt | 18 ++++++++++++- .../SendConnectionRequestUseCaseTest.kt | 26 +++++++++++++++++++ .../network/exceptions/KaliumException.kt | 2 ++ .../network/exceptions/NetworkErrorLabel.kt | 1 + 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt index 35179fa794b..644ab08db8c 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt @@ -26,6 +26,8 @@ import com.wire.kalium.logic.data.user.UserRepository import com.wire.kalium.logic.functional.flatMap import com.wire.kalium.logic.functional.fold import com.wire.kalium.logic.kaliumLogger +import com.wire.kalium.network.exceptions.KaliumException +import com.wire.kalium.network.exceptions.isMissingLegalHoldConsent /** * Use Case that allows a user send a connection request to connect with another User @@ -53,13 +55,26 @@ internal class SendConnectionRequestUseCaseImpl( when (coreFailure) { is NetworkFailure.FederatedBackendFailure.FederationDenied -> SendConnectionRequestResult.Failure.FederationDenied - + is NetworkFailure.ServerMiscommunication -> handleServerMissCommunicationError(coreFailure) else -> SendConnectionRequestResult.Failure.GenericFailure(coreFailure) } }, { SendConnectionRequestResult.Success }) } + + private fun handleServerMissCommunicationError(coreFailure: NetworkFailure.ServerMiscommunication): SendConnectionRequestResult.Failure = + when (coreFailure.kaliumException) { + is KaliumException.InvalidRequestError -> { + with(coreFailure.kaliumException) { + when { + isMissingLegalHoldConsent() -> SendConnectionRequestResult.Failure.MissingLegalHoldConsent + else -> SendConnectionRequestResult.Failure.GenericFailure(coreFailure) + } + } + } + else -> SendConnectionRequestResult.Failure.GenericFailure(coreFailure) + } } sealed class SendConnectionRequestResult { @@ -68,6 +83,7 @@ sealed class SendConnectionRequestResult { sealed class Failure : SendConnectionRequestResult() { class GenericFailure(val coreFailure: CoreFailure) : Failure() data object FederationDenied : Failure() + data object MissingLegalHoldConsent : Failure() } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCaseTest.kt index 60d86ccc909..2004cec21ba 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCaseTest.kt @@ -24,6 +24,8 @@ import com.wire.kalium.logic.data.connection.ConnectionRepository import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.data.user.UserRepository import com.wire.kalium.logic.functional.Either +import com.wire.kalium.network.api.base.model.ErrorResponse +import com.wire.kalium.network.exceptions.KaliumException import io.mockative.Mock import io.mockative.any import io.mockative.classOf @@ -123,6 +125,30 @@ class SendConnectionRequestUseCaseTest { .wasInvoked(once) } + @Test + fun givenAConnectionRequest_whenInvokingAndFailsByMissingLegalHoldConsent_thenShouldReturnsAMissingLegalHoldConsent() = runTest { + // given + val (arrangement, sendConnectionRequestUseCase) = Arrangement() + .withFetchUserInfoResult(Either.Right(Unit)) + .withCreateConnectionResult( + Either.Left( + NetworkFailure.ServerMiscommunication( + KaliumException.InvalidRequestError(ErrorResponse(403, "", "missing-legalhold-consent")) + ) + ) + ) + .arrange() + // when + val resultFailure = sendConnectionRequestUseCase(userId) + + // then + assertEquals(SendConnectionRequestResult.Failure.MissingLegalHoldConsent::class, resultFailure::class) + verify(arrangement.connectionRepository) + .suspendFunction(arrangement.connectionRepository::sendUserConnection) + .with(eq(userId)) + .wasInvoked(once) + } + private class Arrangement { @Mock val connectionRepository = mock(classOf()) diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/KaliumException.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/KaliumException.kt index f0047a81ccb..6425f499301 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/KaliumException.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/KaliumException.kt @@ -41,6 +41,7 @@ import com.wire.kalium.network.exceptions.NetworkErrorLabel.INVALID_EMAIL import com.wire.kalium.network.exceptions.NetworkErrorLabel.INVALID_HANDLE import com.wire.kalium.network.exceptions.NetworkErrorLabel.KEY_EXISTS import com.wire.kalium.network.exceptions.NetworkErrorLabel.MISSING_AUTH +import com.wire.kalium.network.exceptions.NetworkErrorLabel.MISSING_LEGALHOLD_CONSENT import com.wire.kalium.network.exceptions.NetworkErrorLabel.MLS_CLIENT_MISMATCH import com.wire.kalium.network.exceptions.NetworkErrorLabel.MLS_COMMIT_MISSING_REFERENCES import com.wire.kalium.network.exceptions.NetworkErrorLabel.MLS_MISSING_GROUP_INFO @@ -231,3 +232,4 @@ val KaliumException.InvalidRequestError.authenticationCodeFailure: Authenticatio fun KaliumException.FederationError.isFederationDenied() = errorResponse.label == FEDERATION_DENIED fun KaliumException.FederationError.isFederationNotEnabled() = errorResponse.label == FEDERATION_NOT_ENABLED +fun KaliumException.InvalidRequestError.isMissingLegalHoldConsent(): Boolean = errorResponse.label == MISSING_LEGALHOLD_CONSENT diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/NetworkErrorLabel.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/NetworkErrorLabel.kt index 508774019cc..40b5299406f 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/NetworkErrorLabel.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/exceptions/NetworkErrorLabel.kt @@ -49,6 +49,7 @@ internal object NetworkErrorLabel { const val ACCESS_DENIED = "access-denied" const val WRONG_CONVERSATION_PASSWORD = "invalid-conversation-password" const val NOT_FOUND = "not-found" + const val MISSING_LEGALHOLD_CONSENT = "missing-legalhold-consent" // Federation const val FEDERATION_FAILURE = "federation-remote-error" From 4104ac6df9b24695265ac069d96a452ec2146d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= Date: Fri, 26 Jan 2024 14:18:07 +0100 Subject: [PATCH 2/2] fix detekt --- .../feature/connection/SendConnectionRequestUseCase.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt index 644ab08db8c..26a5e76d784 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/connection/SendConnectionRequestUseCase.kt @@ -63,17 +63,17 @@ internal class SendConnectionRequestUseCaseImpl( }) } - private fun handleServerMissCommunicationError(coreFailure: NetworkFailure.ServerMiscommunication): SendConnectionRequestResult.Failure = - when (coreFailure.kaliumException) { + private fun handleServerMissCommunicationError(failure: NetworkFailure.ServerMiscommunication): SendConnectionRequestResult.Failure = + when (failure.kaliumException) { is KaliumException.InvalidRequestError -> { - with(coreFailure.kaliumException) { + with(failure.kaliumException) { when { isMissingLegalHoldConsent() -> SendConnectionRequestResult.Failure.MissingLegalHoldConsent - else -> SendConnectionRequestResult.Failure.GenericFailure(coreFailure) + else -> SendConnectionRequestResult.Failure.GenericFailure(failure) } } } - else -> SendConnectionRequestResult.Failure.GenericFailure(coreFailure) + else -> SendConnectionRequestResult.Failure.GenericFailure(failure) } }