Skip to content

Commit 86dc0f7

Browse files
authored
fix: sending a connection request to already connected user (#3296)
1 parent 040b38c commit 86dc0f7

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

logic/src/commonMain/kotlin/com/wire/kalium/logic/data/connection/ConnectionRepository.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ internal class ConnectionDataSource(
124124
return wrapApiRequest {
125125
connectionApi.createConnection(userId.toApi())
126126
}.flatMap { connection ->
127-
val connectionSent = connection.copy(status = ConnectionStateDTO.SENT)
128-
handleUserConnectionStatusPersistence(connectionMapper.fromApiToModel(connectionSent))
127+
handleUserConnectionStatusPersistence(connectionMapper.fromApiToModel(connection))
129128
}.map { }
130129
}
131130

logic/src/commonTest/kotlin/com/wire/kalium/logic/data/connection/ConnectionRepositoryTest.kt

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ import com.wire.kalium.persistence.dao.UserDAO
5555
import com.wire.kalium.persistence.dao.conversation.ConversationDAO
5656
import com.wire.kalium.persistence.dao.member.MemberEntity
5757
import com.wire.kalium.util.time.UNIX_FIRST_DATE
58+
import io.mockative.Matchers
5859
import io.mockative.Mock
5960
import io.mockative.any
6061
import io.mockative.coEvery
6162
import io.mockative.coVerify
6263
import io.mockative.eq
6364
import io.mockative.fake.valueOf
6465
import io.mockative.matchers.AnyMatcher
66+
import io.mockative.matchers.EqualsMatcher
67+
import io.mockative.matchers.Matcher
6568
import io.mockative.matchers.PredicateMatcher
69+
import io.mockative.matches
6670
import io.mockative.mock
6771
import io.mockative.once
6872
import io.mockative.twice
@@ -126,7 +130,9 @@ class ConnectionRepositoryTest {
126130
arrangement
127131
.withSuccessfulFetchSelfUserConnectionsResponse(arrangement.stubUserProfileDTO)
128132
.withSuccessfulGetConversationById(arrangement.stubConversationID1)
129-
.withSuccessfulCreateConnectionResponse(userId)
133+
.withSuccessfulCreateConnectionResponse(
134+
userId = EqualsMatcher(userId)
135+
)
130136
.withSelfUserTeamId(Either.Right(TestUser.SELF.teamId))
131137
.withFetchSentConversationSucceed()
132138

@@ -170,11 +176,15 @@ class ConnectionRepositoryTest {
170176
fun givenAConnectionRequest_WhenSendingAConnectionAndPersistingReturnsAnError_thenTheConnectionShouldNotBePersisted() = runTest {
171177
// given
172178
val userId = NetworkUserId("user_id", "domain_id")
179+
val expectedConnection = Arrangement.stubConnectionOne.copy(status = ConnectionStateDTO.SENT)
173180
val (arrangement, connectionRepository) = Arrangement().arrange()
174181
arrangement
175182
.withSuccessfulFetchSelfUserConnectionsResponse(arrangement.stubUserProfileDTO)
176183
.withSuccessfulGetUserById(arrangement.stubUserEntity.id)
177-
.withSuccessfulCreateConnectionResponse(userId)
184+
.withSuccessfulCreateConnectionResponse(
185+
result = expectedConnection,
186+
userId = EqualsMatcher(userId)
187+
)
178188
.withSuccessfulGetConversationById(arrangement.stubConversationID1)
179189
.withErrorOnPersistingConnectionResponse(userId)
180190
.withSelfUserTeamId(Either.Right(TestUser.SELF.teamId))
@@ -205,7 +215,7 @@ class ConnectionRepositoryTest {
205215

206216
// when
207217
val result = connectionRepository.updateConnectionStatus(UserId(userId.value, userId.domain), ConnectionState.ACCEPTED)
208-
result.shouldSucceed { arrangement.stubConnectionOne }
218+
result.shouldSucceed { Arrangement.stubConnectionOne }
209219

210220
// then
211221
coVerify {
@@ -328,7 +338,7 @@ class ConnectionRepositoryTest {
328338

329339
// when
330340
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
331-
result.shouldSucceed { arrangement.stubConnectionOne }
341+
result.shouldSucceed { Arrangement.stubConnectionOne }
332342

333343
// then
334344
coVerify {
@@ -368,7 +378,7 @@ class ConnectionRepositoryTest {
368378

369379
// when
370380
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
371-
result.shouldSucceed { arrangement.stubConnectionOne }
381+
result.shouldSucceed { Arrangement.stubConnectionOne }
372382

373383
// then
374384
coVerify {
@@ -388,7 +398,7 @@ class ConnectionRepositoryTest {
388398

389399
// when
390400
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
391-
result.shouldFail { arrangement.stubConnectionOne }
401+
result.shouldFail { Arrangement.stubConnectionOne }
392402

393403
// then
394404
coVerify {
@@ -428,15 +438,7 @@ class ConnectionRepositoryTest {
428438
conversationRepository = conversationRepository
429439
)
430440

431-
val stubConnectionOne = ConnectionDTO(
432-
conversationId = "conversationId1",
433-
from = "fromId",
434-
lastUpdate = Instant.UNIX_FIRST_DATE,
435-
qualifiedConversationId = ConversationId("conversationId1", "domain"),
436-
qualifiedToId = NetworkUserId("connectionId1", "domain"),
437-
status = ConnectionStateDTO.ACCEPTED,
438-
toId = "connectionId1"
439-
)
441+
440442
val stubConnectionTwo = ConnectionDTO(
441443
conversationId = "conversationId2",
442444
from = "fromId",
@@ -503,10 +505,13 @@ class ConnectionRepositoryTest {
503505
return this
504506
}
505507

506-
suspend fun withSuccessfulCreateConnectionResponse(userId: NetworkUserId): Arrangement {
508+
suspend fun withSuccessfulCreateConnectionResponse(
509+
result: ConnectionDTO = stubConnectionOne,
510+
userId: Matcher<NetworkUserId>
511+
): Arrangement {
507512
coEvery {
508-
connectionApi.createConnection(eq(userId))
509-
}.returns(NetworkResponse.Success(stubConnectionOne, mapOf(), 200))
513+
connectionApi.createConnection(matches { userId.matches(it) })
514+
}.returns(NetworkResponse.Success(result, mapOf(), 200))
510515

511516
return this
512517
}
@@ -623,5 +628,17 @@ class ConnectionRepositoryTest {
623628
}
624629

625630
fun arrange() = this to connectionRepository
631+
632+
companion object {
633+
val stubConnectionOne = ConnectionDTO(
634+
conversationId = "conversationId1",
635+
from = "fromId",
636+
lastUpdate = Instant.UNIX_FIRST_DATE,
637+
qualifiedConversationId = ConversationId("conversationId1", "domain"),
638+
qualifiedToId = NetworkUserId("connectionId1", "domain"),
639+
status = ConnectionStateDTO.ACCEPTED,
640+
toId = "connectionId1"
641+
)
642+
}
626643
}
627644
}

0 commit comments

Comments
 (0)