Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sending a connection request to already connected user [WPB-16146] 🍒 #3298

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -391,7 +401,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 @@ -437,7 +447,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 @@ -479,15 +489,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 @@ -554,10 +556,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 @@ -680,5 +685,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"
)
}
}
}
Loading