Skip to content

Commit

Permalink
fix: search users not filtering correctly users connected when domain…
Browse files Browse the repository at this point in the history
… present (WPB-4793) (#2079)

* fix: sanitize search term before searching, filtering contacts

* fix: test cov

* fix: test cov

* fix: test cov
  • Loading branch information
yamilmedina authored Sep 21, 2023
1 parent 395c2c1 commit 38f5d6f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ internal class SearchKnownUsersUseCaseImpl(
val sanitizedSearchQuery = searchQuery.lowercase()
return if (isUserLookingForHandle(sanitizedSearchQuery)) {
searchUserRepository.searchKnownUsersByHandle(
handle = sanitizedSearchQuery.removePrefix("@"),
handle = sanitizedSearchQuery.sanitizeHandleSearchPattern(),
searchUsersOptions = searchUsersOptions
)
} else {
searchUserRepository.searchKnownUsersByNameOrHandleOrEmail(
searchQuery = if (sanitizedSearchQuery.matches(FEDERATION_REGEX))
searchQuery = if (sanitizedSearchQuery.matches(FEDERATION_REGEX)) {
sanitizedSearchQuery.run {
qualifiedIdMapper.fromStringToQualifiedID(this)
}.value
else sanitizedSearchQuery,
} else {
sanitizedSearchQuery.removeSuffix("@")
},
searchUsersOptions = searchUsersOptions
)
}
Expand All @@ -87,3 +89,5 @@ internal class SearchKnownUsersUseCaseImpl(
}

}

internal fun String.sanitizeHandleSearchPattern() = this.substringAfter("@").substringBefore("@")
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.wire.kalium.logic.data.user.type.UserType
import com.wire.kalium.logic.feature.publicuser.search.SearchKnownUsersUseCase
import com.wire.kalium.logic.feature.publicuser.search.SearchKnownUsersUseCaseImpl
import com.wire.kalium.logic.feature.publicuser.search.SearchUsersResult
import com.wire.kalium.logic.feature.publicuser.search.sanitizeHandleSearchPattern
import com.wire.kalium.logic.framework.TestUser
import io.mockative.Mock
import io.mockative.any
Expand Down Expand Up @@ -251,6 +252,31 @@ class SearchKnownUserUseCaseTest {

}

@Test
fun givenAnInputStartingWithAtSymbolAndDomainPresent_whenSearchingUsers_thenSearchBySanitizedHandle() = runTest {
// given
val handlePartOfQuery = "somehandle"
val handleSearchQuery = "@$handlePartOfQuery@bella.wire.link"
val (arrangement, searchKnownUsersUseCase) = Arrangement()
.withSuccessFullSelfUserRetrieve()
.withSearchByHandle(handleSearchQuery)
.arrange()

// when
searchKnownUsersUseCase(handleSearchQuery)

// then
verify(arrangement.searchUserRepository)
.suspendFunction(arrangement.searchUserRepository::searchKnownUsersByHandle)
.with(eq(handlePartOfQuery), anything())
.wasInvoked(exactly = once)

verify(arrangement.searchUserRepository)
.suspendFunction(arrangement.searchUserRepository::searchKnownUsersByNameOrHandleOrEmail)
.with(anything(), anything())
.wasNotInvoked()
}

private class Arrangement {

@Mock
Expand Down Expand Up @@ -292,7 +318,7 @@ class SearchKnownUserUseCaseTest {
given(searchUserRepository)
.suspendFunction(searchUserRepository::searchKnownUsersByHandle)
.whenInvokedWith(
if (searchQuery == null) any() else eq(searchQuery.removePrefix("@")),
if (searchQuery == null) any() else eq(searchQuery.sanitizeHandleSearchPattern()),
if (searchUsersOptions == null) any() else eq(searchUsersOptions)
)
.thenReturn(
Expand Down

0 comments on commit 38f5d6f

Please sign in to comment.