Getting the current session verification status to change verify button visibility

This commit is contained in:
Maxime NATUREL 2022-09-16 15:03:14 +02:00
parent 95d133e0e2
commit 921533e4b2
6 changed files with 54 additions and 29 deletions

View File

@ -26,9 +26,12 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.settings.devices.v2.IsCurrentSessionUseCase
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
class SessionOverviewViewModel @AssistedInject constructor(
@Assisted val initialState: SessionOverviewViewState,
@ -49,6 +52,7 @@ class SessionOverviewViewModel @AssistedInject constructor(
copy(isCurrentSession = isCurrentSession(deviceId))
}
observeSessionInfo(initialState.deviceId)
observeCurrentSessionInfo()
}
private fun isCurrentSession(deviceId: String): Boolean {
@ -61,6 +65,19 @@ class SessionOverviewViewModel @AssistedInject constructor(
.launchIn(viewModelScope)
}
private fun observeCurrentSessionInfo() {
activeSessionHolder.getSafeActiveSession()
?.sessionParams
?.deviceId
?.let { deviceId ->
getDeviceFullInfoUseCase.execute(deviceId)
.map { it.roomEncryptionTrustLevel == RoomEncryptionTrustLevel.Trusted }
.distinctUntilChanged()
.onEach { setState { copy(isCurrentSessionTrusted = it) } }
.launchIn(viewModelScope)
}
}
override fun handle(action: SessionOverviewAction) {
when (action) {
is SessionOverviewAction.VerifySession -> handleVerifySessionAction()

View File

@ -18,11 +18,8 @@ package im.vector.app.features.settings.devices
import im.vector.app.features.settings.devices.v2.verification.CurrentSessionCrossSigningInfo
import im.vector.app.test.fakes.FakeActiveSessionHolder
import io.mockk.every
import io.mockk.mockk
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.auth.data.SessionParams
private const val A_DEVICE_ID = "device-id"
@ -36,9 +33,7 @@ class GetCurrentSessionCrossSigningInfoUseCaseTest {
@Test
fun `given the active session when getting cross signing info then the result is correct`() {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns A_DEVICE_ID
fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams)
fakeActiveSessionHolder.fakeSession.givenSessionId(A_DEVICE_ID)
val isCrossSigningInitialized = true
fakeActiveSessionHolder.fakeSession
.fakeCryptoService

View File

@ -17,8 +17,6 @@
package im.vector.app.features.settings.devices.v2
import im.vector.app.test.fakes.FakeActiveSessionHolder
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.amshove.kluent.shouldBe
import org.junit.Test
@ -73,10 +71,7 @@ class IsCurrentSessionUseCaseTest {
result shouldBe false
}
private fun givenIdForCurrentSession(deviceId: String): SessionParams {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns deviceId
fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams)
return sessionParams
private fun givenIdForCurrentSession(sessionId: String): SessionParams {
return fakeActiveSessionHolder.fakeSession.givenSessionId(sessionId)
}
}

View File

@ -31,8 +31,10 @@ import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
private const val A_SESSION_ID = "session-id"
private const val A_SESSION_ID_1 = "session-id-1"
private const val A_SESSION_ID_2 = "session-id-2"
class SessionOverviewViewModelTest {
@ -40,7 +42,7 @@ class SessionOverviewViewModelTest {
val mvRxTestRule = MvRxTestRule(testDispatcher = testDispatcher)
private val args = SessionOverviewArgs(
deviceId = A_SESSION_ID
deviceId = A_SESSION_ID_1
)
private val isCurrentSessionUseCase = mockk<IsCurrentSessionUseCase>()
private val getDeviceFullInfoUseCase = mockk<GetDeviceFullInfoUseCase>()
@ -54,16 +56,18 @@ class SessionOverviewViewModelTest {
)
@Test
fun `given the viewModel has been initialized then viewState is updated with session info`() {
fun `given the viewModel has been initialized then viewState is updated with session info and current session verification status`() {
// Given
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } returns flowOf(deviceFullInfo)
val isCurrentSession = true
every { isCurrentSessionUseCase.execute(any()) } returns isCurrentSession
givenCurrentSessionIsTrusted()
val expectedState = SessionOverviewViewState(
deviceId = A_SESSION_ID,
deviceId = A_SESSION_ID_1,
isCurrentSession = isCurrentSession,
deviceInfo = Success(deviceFullInfo)
deviceInfo = Success(deviceFullInfo),
isCurrentSessionTrusted = true,
)
// When
@ -74,8 +78,8 @@ class SessionOverviewViewModelTest {
.assertLatestState { state -> state == expectedState }
.finish()
verify {
isCurrentSessionUseCase.execute(A_SESSION_ID)
getDeviceFullInfoUseCase.execute(A_SESSION_ID)
isCurrentSessionUseCase.execute(A_SESSION_ID_1)
getDeviceFullInfoUseCase.execute(A_SESSION_ID_1)
}
}
@ -83,10 +87,11 @@ class SessionOverviewViewModelTest {
fun `given current session can be verified when handling verify current session action then self verification event is posted`() {
// Given
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } returns flowOf(deviceFullInfo)
every { isCurrentSessionUseCase.execute(any()) } returns true
val verifySessionAction = SessionOverviewAction.VerifySession
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns true
givenCurrentSessionIsTrusted()
// When
val viewModel = createViewModel()
@ -106,10 +111,11 @@ class SessionOverviewViewModelTest {
fun `given current session cannot be verified when handling verify current session action then reset secrets event is posted`() {
// Given
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } returns flowOf(deviceFullInfo)
every { isCurrentSessionUseCase.execute(any()) } returns true
val verifySessionAction = SessionOverviewAction.VerifySession
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns false
givenCurrentSessionIsTrusted()
// When
val viewModel = createViewModel()
@ -129,9 +135,10 @@ class SessionOverviewViewModelTest {
fun `given another session when handling verify session action then verify session event is posted`() {
// Given
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } returns flowOf(deviceFullInfo)
every { isCurrentSessionUseCase.execute(any()) } returns false
val verifySessionAction = SessionOverviewAction.VerifySession(A_SESSION_ID)
val verifySessionAction = SessionOverviewAction.VerifySession
givenCurrentSessionIsTrusted()
// When
val viewModel = createViewModel()
@ -143,4 +150,11 @@ class SessionOverviewViewModelTest {
.assertEvent { it is SessionOverviewViewEvent.ShowVerifyOtherSession }
.finish()
}
private fun givenCurrentSessionIsTrusted() {
fakeActiveSessionHolder.fakeSession.givenSessionId(A_SESSION_ID_2)
val deviceFullInfo = mockk<DeviceFullInfo>()
every { deviceFullInfo.roomEncryptionTrustLevel } returns RoomEncryptionTrustLevel.Trusted
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_2) } returns flowOf(deviceFullInfo)
}
}

View File

@ -107,11 +107,8 @@ class GetCurrentSessionCrossSigningInfoUseCaseTest {
}
private fun givenSession(deviceId: String): FakeSession {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns deviceId
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionParams(sessionParams)
fakeSession.givenSessionId(deviceId)
return fakeSession
}

View File

@ -78,6 +78,13 @@ class FakeSession(
every { this@FakeSession.sessionParams } returns sessionParams
}
fun givenSessionId(sessionId: String): SessionParams {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns sessionId
givenSessionParams(sessionParams)
return sessionParams
}
/**
* Do not forget to call mockkStatic("org.matrix.android.sdk.flow.FlowSessionKt") in the setup method of the tests.
*/