diff --git a/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/AllMatrixUsersDataSourceTest.kt b/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/AllMatrixUsersDataSourceTest.kt index 1dbef27f97..f20e46d1ba 100644 --- a/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/AllMatrixUsersDataSourceTest.kt +++ b/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/AllMatrixUsersDataSourceTest.kt @@ -47,18 +47,7 @@ internal class AllMatrixUsersDataSourceTest { val dataSource = AllMatrixUsersDataSource(matrixClient) val results = dataSource.search("test") - Truth.assertThat(results).containsExactly( - MatrixUser( - userId = A_USER_ID, - displayName = A_USER_NAME, - avatarUrl = AN_AVATAR_URL - ), - MatrixUser( - userId = A_USER_ID_2, - displayName = A_USER_NAME, - avatarUrl = AN_AVATAR_URL - ), - ) + Truth.assertThat(results).containsExactly(aMatrixUserProfile(), aMatrixUserProfile(userId = A_USER_ID_2)) } @Test @@ -74,6 +63,32 @@ internal class AllMatrixUsersDataSourceTest { Truth.assertThat(results).isEmpty() } + @Test + fun `get profile - returns user on success`() = runTest { + val matrixClient = FakeMatrixClient() + matrixClient.givenGetProfileResult( + userId = A_USER_ID, + result = Result.success(aMatrixUserProfile()) + ) + val dataSource = AllMatrixUsersDataSource(matrixClient) + + val result = dataSource.getProfile(A_USER_ID) + Truth.assertThat(result).isEqualTo(aMatrixUserProfile()) + } + + @Test + fun `get profile - returns null on error`() = runTest { + val matrixClient = FakeMatrixClient() + matrixClient.givenGetProfileResult( + userId = A_USER_ID, + result = Result.failure(Throwable("Ruhroh")) + ) + val dataSource = AllMatrixUsersDataSource(matrixClient) + + val result = dataSource.getProfile(A_USER_ID) + Truth.assertThat(result).isNull() + } + private fun aMatrixUserProfile( userId: UserId = A_USER_ID, displayName: String = A_USER_NAME, diff --git a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt index 4dda15124a..9d3ce40da1 100644 --- a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt +++ b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt @@ -58,6 +58,7 @@ class FakeMatrixClient( private var logoutFailure: Throwable? = null private val getRoomResults = mutableMapOf() private val searchUserResults = mutableMapOf>() + private val getProfileResults = mutableMapOf>() override fun getRoom(roomId: RoomId): MatrixRoom? { return getRoomResults[roomId] @@ -87,7 +88,7 @@ class FakeMatrixClient( } override suspend fun getProfile(userId: UserId): Result { - return Result.success(MatrixUser(userId)) + return getProfileResults[userId] ?: Result.failure(IllegalStateException("No profile found for $userId")) } override fun startSync() = Unit @@ -174,4 +175,8 @@ class FakeMatrixClient( fun givenSearchUsersResult(searchTerm: String, result: Result) { searchUserResults[searchTerm] = result } + + fun givenGetProfileResult(userId: UserId, result: Result) { + getProfileResults[userId] = result + } }