Skip to content

Commit

Permalink
fix: sending a connection request to already connected user (#3296)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadJaara authored Feb 18, 2025
1 parent 040b38c commit 86dc0f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ internal class ConnectionDataSource(
return wrapApiRequest {
connectionApi.createConnection(userId.toApi())
}.flatMap { connection ->
val connectionSent = connection.copy(status = ConnectionStateDTO.SENT)
handleUserConnectionStatusPersistence(connectionMapper.fromApiToModel(connectionSent))
handleUserConnectionStatusPersistence(connectionMapper.fromApiToModel(connection))
}.map { }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ import com.wire.kalium.persistence.dao.UserDAO
import com.wire.kalium.persistence.dao.conversation.ConversationDAO
import com.wire.kalium.persistence.dao.member.MemberEntity
import com.wire.kalium.util.time.UNIX_FIRST_DATE
import io.mockative.Matchers
import io.mockative.Mock
import io.mockative.any
import io.mockative.coEvery
import io.mockative.coVerify
import io.mockative.eq
import io.mockative.fake.valueOf
import io.mockative.matchers.AnyMatcher
import io.mockative.matchers.EqualsMatcher
import io.mockative.matchers.Matcher
import io.mockative.matchers.PredicateMatcher
import io.mockative.matches
import io.mockative.mock
import io.mockative.once
import io.mockative.twice
Expand Down Expand Up @@ -126,7 +130,9 @@ class ConnectionRepositoryTest {
arrangement
.withSuccessfulFetchSelfUserConnectionsResponse(arrangement.stubUserProfileDTO)
.withSuccessfulGetConversationById(arrangement.stubConversationID1)
.withSuccessfulCreateConnectionResponse(userId)
.withSuccessfulCreateConnectionResponse(
userId = EqualsMatcher(userId)
)
.withSelfUserTeamId(Either.Right(TestUser.SELF.teamId))
.withFetchSentConversationSucceed()

Expand Down Expand Up @@ -170,11 +176,15 @@ class ConnectionRepositoryTest {
fun givenAConnectionRequest_WhenSendingAConnectionAndPersistingReturnsAnError_thenTheConnectionShouldNotBePersisted() = runTest {
// given
val userId = NetworkUserId("user_id", "domain_id")
val expectedConnection = Arrangement.stubConnectionOne.copy(status = ConnectionStateDTO.SENT)
val (arrangement, connectionRepository) = Arrangement().arrange()
arrangement
.withSuccessfulFetchSelfUserConnectionsResponse(arrangement.stubUserProfileDTO)
.withSuccessfulGetUserById(arrangement.stubUserEntity.id)
.withSuccessfulCreateConnectionResponse(userId)
.withSuccessfulCreateConnectionResponse(
result = expectedConnection,
userId = EqualsMatcher(userId)
)
.withSuccessfulGetConversationById(arrangement.stubConversationID1)
.withErrorOnPersistingConnectionResponse(userId)
.withSelfUserTeamId(Either.Right(TestUser.SELF.teamId))
Expand Down Expand Up @@ -205,7 +215,7 @@ class ConnectionRepositoryTest {

// when
val result = connectionRepository.updateConnectionStatus(UserId(userId.value, userId.domain), ConnectionState.ACCEPTED)
result.shouldSucceed { arrangement.stubConnectionOne }
result.shouldSucceed { Arrangement.stubConnectionOne }

// then
coVerify {
Expand Down Expand Up @@ -328,7 +338,7 @@ class ConnectionRepositoryTest {

// when
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
result.shouldSucceed { arrangement.stubConnectionOne }
result.shouldSucceed { Arrangement.stubConnectionOne }

// then
coVerify {
Expand Down Expand Up @@ -368,7 +378,7 @@ class ConnectionRepositoryTest {

// when
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
result.shouldSucceed { arrangement.stubConnectionOne }
result.shouldSucceed { Arrangement.stubConnectionOne }

// then
coVerify {
Expand All @@ -388,7 +398,7 @@ class ConnectionRepositoryTest {

// when
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
result.shouldFail { arrangement.stubConnectionOne }
result.shouldFail { Arrangement.stubConnectionOne }

// then
coVerify {
Expand Down Expand Up @@ -428,15 +438,7 @@ class ConnectionRepositoryTest {
conversationRepository = conversationRepository
)

val stubConnectionOne = ConnectionDTO(
conversationId = "conversationId1",
from = "fromId",
lastUpdate = Instant.UNIX_FIRST_DATE,
qualifiedConversationId = ConversationId("conversationId1", "domain"),
qualifiedToId = NetworkUserId("connectionId1", "domain"),
status = ConnectionStateDTO.ACCEPTED,
toId = "connectionId1"
)

val stubConnectionTwo = ConnectionDTO(
conversationId = "conversationId2",
from = "fromId",
Expand Down Expand Up @@ -503,10 +505,13 @@ class ConnectionRepositoryTest {
return this
}

suspend fun withSuccessfulCreateConnectionResponse(userId: NetworkUserId): Arrangement {
suspend fun withSuccessfulCreateConnectionResponse(
result: ConnectionDTO = stubConnectionOne,
userId: Matcher<NetworkUserId>
): Arrangement {
coEvery {
connectionApi.createConnection(eq(userId))
}.returns(NetworkResponse.Success(stubConnectionOne, mapOf(), 200))
connectionApi.createConnection(matches { userId.matches(it) })
}.returns(NetworkResponse.Success(result, mapOf(), 200))

return this
}
Expand Down Expand Up @@ -623,5 +628,17 @@ class ConnectionRepositoryTest {
}

fun arrange() = this to connectionRepository

companion object {
val stubConnectionOne = ConnectionDTO(
conversationId = "conversationId1",
from = "fromId",
lastUpdate = Instant.UNIX_FIRST_DATE,
qualifiedConversationId = ConversationId("conversationId1", "domain"),
qualifiedToId = NetworkUserId("connectionId1", "domain"),
status = ConnectionStateDTO.ACCEPTED,
toId = "connectionId1"
)
}
}
}

0 comments on commit 86dc0f7

Please sign in to comment.