Skip to content

Commit

Permalink
fix(mls-client): skip MLSClient registration if registered client is …
Browse files Browse the repository at this point in the history
…mlsCapable (WPB-10723) 🍒 (#2988) (#3032)

* fix(mls-client): skip MLSClient registration if registered client is mlsCapable (#2987)

* test: update to Mockative 2.0 syntax

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Mojtaba Chenani <[email protected]>
Co-authored-by: Alexandre Ferris <[email protected]>
Co-authored-by: Mohamad Jaara <[email protected]>
Co-authored-by: Vitor Hugo Schwaab <[email protected]>
  • Loading branch information
5 people authored Sep 25, 2024
1 parent d170dab commit 9bfeb80
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import com.wire.kalium.logic.data.client.ClientRepository
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.logic.functional.getOrElse
import com.wire.kalium.logic.functional.map
import com.wire.kalium.util.DelicateKaliumApi

/**
Expand All @@ -52,36 +50,22 @@ internal class VerifyExistingClientUseCaseImpl @OptIn(DelicateKaliumApi::class)
return clientRepository.selfListOfClients()
.fold({
VerifyExistingClientResult.Failure.Generic(it)
}, { listOfClients ->
val client = listOfClients.firstOrNull { it.id == clientId }
}, { listOfRegisteredClients ->
val registeredClient = listOfRegisteredClients.firstOrNull { it.id == clientId }
when {
(client == null) -> VerifyExistingClientResult.Failure.ClientNotRegistered
registeredClient == null -> VerifyExistingClientResult.Failure.ClientNotRegistered

isAllowedToRegisterMLSClient() -> {
registerMLSClientUseCase.invoke(clientId = client.id).map {
if (it is RegisterMLSClientResult.E2EICertificateRequired)
VerifyExistingClientResult.Failure.E2EICertificateRequired(client, selfUserId)
else VerifyExistingClientResult.Success(client)
}.getOrElse { VerifyExistingClientResult.Failure.Generic(it) }
}

else -> VerifyExistingClientResult.Success(client)
}

if (client != null) {
if (isAllowedToRegisterMLSClient()) {
registerMLSClientUseCase.invoke(clientId = client.id).fold({
!registeredClient.isMLSCapable && isAllowedToRegisterMLSClient() -> {
registerMLSClientUseCase.invoke(clientId = registeredClient.id).fold({
VerifyExistingClientResult.Failure.Generic(it)
}) {
}, {
if (it is RegisterMLSClientResult.E2EICertificateRequired)
VerifyExistingClientResult.Failure.E2EICertificateRequired(client, selfUserId)
else VerifyExistingClientResult.Success(client)
}
} else {
VerifyExistingClientResult.Success(client)
VerifyExistingClientResult.Failure.E2EICertificateRequired(registeredClient, selfUserId)
else VerifyExistingClientResult.Success(registeredClient)
})
}
} else {
VerifyExistingClientResult.Failure.ClientNotRegistered

else -> VerifyExistingClientResult.Success(registeredClient)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ class VerifyExistingClientUseCaseTest {
assertIs<VerifyExistingClientResult.Failure.E2EICertificateRequired>(result)
}

@Test
fun givenRegisteredClientIdAndMLSAllowedAndExistClientIsMLSCapable_whenRegisterMLSSucceed_thenReturnSuccessAndSkipMLSRegistration() =
runTest {
val clientId = ClientId("clientId")
val client = TestClient.CLIENT.copy(id = clientId, isMLSCapable = true)
val (arrangement, useCase) = arrange {
withSelfClientsResult(Either.Right(listOf(client)))
withIsAllowedToRegisterMLSClient(true)
}
val result = useCase.invoke(clientId)
assertIs<VerifyExistingClientResult.Success>(result)
coVerify {
arrangement.registerMLSClientUseCase(any())
}.wasNotInvoked()
assertEquals(client, result.client)
}

@Test
fun givenRegisteredClientIdAndMLSAllowed_whenRegisterMLSSucceed_thenReturnSuccess() = runTest {
val clientId = ClientId("clientId")
Expand Down

0 comments on commit 9bfeb80

Please sign in to comment.