Getting the current session verification status to change verify button visibility
This commit is contained in:
parent
95d133e0e2
commit
921533e4b2
|
@ -26,9 +26,12 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||||
import im.vector.app.core.platform.VectorViewModel
|
import im.vector.app.core.platform.VectorViewModel
|
||||||
import im.vector.app.features.settings.devices.v2.IsCurrentSessionUseCase
|
import im.vector.app.features.settings.devices.v2.IsCurrentSessionUseCase
|
||||||
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
|
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
|
||||||
|
|
||||||
class SessionOverviewViewModel @AssistedInject constructor(
|
class SessionOverviewViewModel @AssistedInject constructor(
|
||||||
@Assisted val initialState: SessionOverviewViewState,
|
@Assisted val initialState: SessionOverviewViewState,
|
||||||
|
@ -49,6 +52,7 @@ class SessionOverviewViewModel @AssistedInject constructor(
|
||||||
copy(isCurrentSession = isCurrentSession(deviceId))
|
copy(isCurrentSession = isCurrentSession(deviceId))
|
||||||
}
|
}
|
||||||
observeSessionInfo(initialState.deviceId)
|
observeSessionInfo(initialState.deviceId)
|
||||||
|
observeCurrentSessionInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isCurrentSession(deviceId: String): Boolean {
|
private fun isCurrentSession(deviceId: String): Boolean {
|
||||||
|
@ -61,6 +65,19 @@ class SessionOverviewViewModel @AssistedInject constructor(
|
||||||
.launchIn(viewModelScope)
|
.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) {
|
override fun handle(action: SessionOverviewAction) {
|
||||||
when (action) {
|
when (action) {
|
||||||
is SessionOverviewAction.VerifySession -> handleVerifySessionAction()
|
is SessionOverviewAction.VerifySession -> handleVerifySessionAction()
|
||||||
|
|
|
@ -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.features.settings.devices.v2.verification.CurrentSessionCrossSigningInfo
|
||||||
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
||||||
import io.mockk.every
|
|
||||||
import io.mockk.mockk
|
|
||||||
import org.amshove.kluent.shouldBeEqualTo
|
import org.amshove.kluent.shouldBeEqualTo
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.matrix.android.sdk.api.auth.data.SessionParams
|
|
||||||
|
|
||||||
private const val A_DEVICE_ID = "device-id"
|
private const val A_DEVICE_ID = "device-id"
|
||||||
|
|
||||||
|
@ -36,9 +33,7 @@ class GetCurrentSessionCrossSigningInfoUseCaseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given the active session when getting cross signing info then the result is correct`() {
|
fun `given the active session when getting cross signing info then the result is correct`() {
|
||||||
val sessionParams = mockk<SessionParams>()
|
fakeActiveSessionHolder.fakeSession.givenSessionId(A_DEVICE_ID)
|
||||||
every { sessionParams.deviceId } returns A_DEVICE_ID
|
|
||||||
fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams)
|
|
||||||
val isCrossSigningInitialized = true
|
val isCrossSigningInitialized = true
|
||||||
fakeActiveSessionHolder.fakeSession
|
fakeActiveSessionHolder.fakeSession
|
||||||
.fakeCryptoService
|
.fakeCryptoService
|
||||||
|
|
|
@ -17,8 +17,6 @@
|
||||||
package im.vector.app.features.settings.devices.v2
|
package im.vector.app.features.settings.devices.v2
|
||||||
|
|
||||||
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
||||||
import io.mockk.every
|
|
||||||
import io.mockk.mockk
|
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
import org.amshove.kluent.shouldBe
|
import org.amshove.kluent.shouldBe
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
@ -73,10 +71,7 @@ class IsCurrentSessionUseCaseTest {
|
||||||
result shouldBe false
|
result shouldBe false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun givenIdForCurrentSession(deviceId: String): SessionParams {
|
private fun givenIdForCurrentSession(sessionId: String): SessionParams {
|
||||||
val sessionParams = mockk<SessionParams>()
|
return fakeActiveSessionHolder.fakeSession.givenSessionId(sessionId)
|
||||||
every { sessionParams.deviceId } returns deviceId
|
|
||||||
fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams)
|
|
||||||
return sessionParams
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,10 @@ import io.mockk.verify
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
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 {
|
class SessionOverviewViewModelTest {
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ class SessionOverviewViewModelTest {
|
||||||
val mvRxTestRule = MvRxTestRule(testDispatcher = testDispatcher)
|
val mvRxTestRule = MvRxTestRule(testDispatcher = testDispatcher)
|
||||||
|
|
||||||
private val args = SessionOverviewArgs(
|
private val args = SessionOverviewArgs(
|
||||||
deviceId = A_SESSION_ID
|
deviceId = A_SESSION_ID_1
|
||||||
)
|
)
|
||||||
private val isCurrentSessionUseCase = mockk<IsCurrentSessionUseCase>()
|
private val isCurrentSessionUseCase = mockk<IsCurrentSessionUseCase>()
|
||||||
private val getDeviceFullInfoUseCase = mockk<GetDeviceFullInfoUseCase>()
|
private val getDeviceFullInfoUseCase = mockk<GetDeviceFullInfoUseCase>()
|
||||||
|
@ -54,16 +56,18 @@ class SessionOverviewViewModelTest {
|
||||||
)
|
)
|
||||||
|
|
||||||
@Test
|
@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
|
// Given
|
||||||
val deviceFullInfo = mockk<DeviceFullInfo>()
|
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
|
val isCurrentSession = true
|
||||||
every { isCurrentSessionUseCase.execute(any()) } returns isCurrentSession
|
every { isCurrentSessionUseCase.execute(any()) } returns isCurrentSession
|
||||||
|
givenCurrentSessionIsTrusted()
|
||||||
val expectedState = SessionOverviewViewState(
|
val expectedState = SessionOverviewViewState(
|
||||||
deviceId = A_SESSION_ID,
|
deviceId = A_SESSION_ID_1,
|
||||||
isCurrentSession = isCurrentSession,
|
isCurrentSession = isCurrentSession,
|
||||||
deviceInfo = Success(deviceFullInfo)
|
deviceInfo = Success(deviceFullInfo),
|
||||||
|
isCurrentSessionTrusted = true,
|
||||||
)
|
)
|
||||||
|
|
||||||
// When
|
// When
|
||||||
|
@ -74,8 +78,8 @@ class SessionOverviewViewModelTest {
|
||||||
.assertLatestState { state -> state == expectedState }
|
.assertLatestState { state -> state == expectedState }
|
||||||
.finish()
|
.finish()
|
||||||
verify {
|
verify {
|
||||||
isCurrentSessionUseCase.execute(A_SESSION_ID)
|
isCurrentSessionUseCase.execute(A_SESSION_ID_1)
|
||||||
getDeviceFullInfoUseCase.execute(A_SESSION_ID)
|
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`() {
|
fun `given current session can be verified when handling verify current session action then self verification event is posted`() {
|
||||||
// Given
|
// Given
|
||||||
val deviceFullInfo = mockk<DeviceFullInfo>()
|
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
|
every { isCurrentSessionUseCase.execute(any()) } returns true
|
||||||
val verifySessionAction = SessionOverviewAction.VerifySession
|
val verifySessionAction = SessionOverviewAction.VerifySession
|
||||||
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns true
|
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns true
|
||||||
|
givenCurrentSessionIsTrusted()
|
||||||
|
|
||||||
// When
|
// When
|
||||||
val viewModel = createViewModel()
|
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`() {
|
fun `given current session cannot be verified when handling verify current session action then reset secrets event is posted`() {
|
||||||
// Given
|
// Given
|
||||||
val deviceFullInfo = mockk<DeviceFullInfo>()
|
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
|
every { isCurrentSessionUseCase.execute(any()) } returns true
|
||||||
val verifySessionAction = SessionOverviewAction.VerifySession
|
val verifySessionAction = SessionOverviewAction.VerifySession
|
||||||
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns false
|
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns false
|
||||||
|
givenCurrentSessionIsTrusted()
|
||||||
|
|
||||||
// When
|
// When
|
||||||
val viewModel = createViewModel()
|
val viewModel = createViewModel()
|
||||||
|
@ -129,9 +135,10 @@ class SessionOverviewViewModelTest {
|
||||||
fun `given another session when handling verify session action then verify session event is posted`() {
|
fun `given another session when handling verify session action then verify session event is posted`() {
|
||||||
// Given
|
// Given
|
||||||
val deviceFullInfo = mockk<DeviceFullInfo>()
|
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
|
every { isCurrentSessionUseCase.execute(any()) } returns false
|
||||||
val verifySessionAction = SessionOverviewAction.VerifySession(A_SESSION_ID)
|
val verifySessionAction = SessionOverviewAction.VerifySession
|
||||||
|
givenCurrentSessionIsTrusted()
|
||||||
|
|
||||||
// When
|
// When
|
||||||
val viewModel = createViewModel()
|
val viewModel = createViewModel()
|
||||||
|
@ -143,4 +150,11 @@ class SessionOverviewViewModelTest {
|
||||||
.assertEvent { it is SessionOverviewViewEvent.ShowVerifyOtherSession }
|
.assertEvent { it is SessionOverviewViewEvent.ShowVerifyOtherSession }
|
||||||
.finish()
|
.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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,11 +107,8 @@ class GetCurrentSessionCrossSigningInfoUseCaseTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun givenSession(deviceId: String): FakeSession {
|
private fun givenSession(deviceId: String): FakeSession {
|
||||||
val sessionParams = mockk<SessionParams>()
|
|
||||||
every { sessionParams.deviceId } returns deviceId
|
|
||||||
|
|
||||||
val fakeSession = fakeActiveSessionHolder.fakeSession
|
val fakeSession = fakeActiveSessionHolder.fakeSession
|
||||||
fakeSession.givenSessionParams(sessionParams)
|
fakeSession.givenSessionId(deviceId)
|
||||||
|
|
||||||
return fakeSession
|
return fakeSession
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,6 +78,13 @@ class FakeSession(
|
||||||
every { this@FakeSession.sessionParams } returns sessionParams
|
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.
|
* Do not forget to call mockkStatic("org.matrix.android.sdk.flow.FlowSessionKt") in the setup method of the tests.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue