Skip to content

Commit

Permalink
fix: wrong failType for MLS federation errors [WPB-9087] (#2757)
Browse files Browse the repository at this point in the history
* fix: wrong failType for MLS federation errors [WPB-9087]

* detekt
  • Loading branch information
saleniuk authored May 15, 2024
1 parent 4184893 commit 8c6ee23
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ internal class ConversationGroupRepositoryImpl(
Either.Right(Unit)
} else {
newGroupConversationSystemMessagesCreator.value.conversationFailedToAddMembers(
conversationEntity.id.toModel(), protocolSpecificAdditionFailures.toList(), FailedToAdd.Type.Unknown
conversationId = conversationEntity.id.toModel(),
userIdList = protocolSpecificAdditionFailures.toList(),
type = FailedToAdd.Type.Federation
)
}
}.flatMap {
Expand Down Expand Up @@ -427,7 +429,7 @@ internal class ConversationGroupRepositoryImpl(
}
}
} else {
val failType = (lastUsersAttempt as? LastUsersAttempt.Failed)?.failType ?: FailedToAdd.Type.Unknown
val failType = apiResult.value.toFailedToAddType()
newGroupConversationSystemMessagesCreator.value.conversationFailedToAddMembers(
conversationId, userIdList + lastUsersAttempt.failedUsers, failType
).flatMap {
Expand Down Expand Up @@ -597,6 +599,12 @@ internal class ConversationGroupRepositoryImpl(
Either.Right(ValidToInvalidUsers(userIdList, emptyList(), FailedToAdd.Type.Unknown))
}

private fun CoreFailure.toFailedToAddType() = when {
this is NetworkFailure.FederatedBackendFailure -> FailedToAdd.Type.Federation
this.isMissingLegalHoldConsentError -> FailedToAdd.Type.LegalHold
else -> FailedToAdd.Type.Unknown
}

/**
* Filter the initial [userIdList] into valid and invalid users where valid users are only team members.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class ConversationGroupRepositoryTest {
arrangement.newGroupConversationSystemMessagesCreator.conversationFailedToAddMembers(
any(),
matches { it.containsAll(missingMembersFromMLSGroup) },
any()
eq(MessageContent.MemberChange.FailedToAdd.Type.Federation)
)
}.wasInvoked(once)
}
Expand Down Expand Up @@ -1162,6 +1162,43 @@ class ConversationGroupRepositoryTest {
}.wasInvoked(once)
}

@Test
fun givenAConversationFailsWithGeneralFederationError_whenAddingMembers_thenRetryIsNotExecutedAndCreateSysMessage() =
runTest {
// given
val (arrangement, conversationGroupRepository) = Arrangement()
.withConversationDetailsById(TestConversation.CONVERSATION)
.withProtocolInfoById(PROTEUS_PROTOCOL_INFO)
.withFetchUsersIfUnknownByIdsSuccessful()
.withAddMemberAPIFailsFirstWithUnreachableThenSucceed(arrayOf(FEDERATION_ERROR_GENERAL, FEDERATION_ERROR_GENERAL))
.withSuccessfulHandleMemberJoinEvent()
.withInsertFailedToAddSystemMessageSuccess()
.arrange()

// when
val expectedInitialUsersNotFromUnreachableInformed = listOf(TestConversation.USER_1)
conversationGroupRepository.addMembers(expectedInitialUsersNotFromUnreachableInformed, TestConversation.ID).shouldFail()

// then
coVerify {
arrangement.conversationApi.addMember(any(), any())
}.wasInvoked(exactly = once)

coVerify {
arrangement.memberJoinEventHandler.handle(any())
}.wasNotInvoked()

coVerify {
arrangement.newGroupConversationSystemMessagesCreator.conversationFailedToAddMembers(
conversationId = any(),
userIdList = matches {
it.containsAll(expectedInitialUsersNotFromUnreachableInformed)
},
type = eq(MessageContent.MemberChange.FailedToAdd.Type.Federation)
)
}.wasInvoked(once)
}

@Test
fun givenAValidConversation_whenCreating_thenConversationIsCreatedAndUnverifiedWarningSystemMessagePersisted() = runTest {
val (arrangement, conversationGroupRepository) = Arrangement()
Expand Down Expand Up @@ -1887,6 +1924,10 @@ class ConversationGroupRepositoryTest {
)
)

val FEDERATION_ERROR_GENERAL = NetworkResponse.Error(
KaliumException.FederationError(ErrorResponse(422, "", "federation-remote-error"))
)

val ERROR_MISSING_LEGALHOLD_CONSENT = NetworkResponse.Error(
KaliumException.InvalidRequestError(
ErrorResponse(
Expand Down

0 comments on commit 8c6ee23

Please sign in to comment.