From 31c908c873c0e85b058e7375485353df5b705f69 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Sep 2022 14:20:58 +0200 Subject: [PATCH] Updating existing unit tests --- .../v2/overview/GetDeviceFullInfoUseCase.kt | 1 - .../overview/GetDeviceFullInfoUseCaseTest.kt | 41 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt index 51252de34a..3cde519385 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt @@ -28,7 +28,6 @@ import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.toOptional import javax.inject.Inject -// TODO update unit test class GetDeviceFullInfoUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, private val getCurrentSessionCrossSigningInfoUseCase: GetCurrentSessionCrossSigningInfoUseCase, diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCaseTest.kt index 32d7b6edfe..3d56f4ff11 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCaseTest.kt @@ -18,10 +18,15 @@ package im.vector.app.features.settings.devices.v2.overview import androidx.lifecycle.MutableLiveData import androidx.lifecycle.asFlow +import im.vector.app.features.settings.devices.CurrentSessionCrossSigningInfo import im.vector.app.features.settings.devices.DeviceFullInfo +import im.vector.app.features.settings.devices.GetCurrentSessionCrossSigningInfoUseCase +import im.vector.app.features.settings.devices.GetEncryptionTrustLevelForDeviceUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeFlowLiveDataConversions import im.vector.app.test.fakes.givenAsFlow +import io.mockk.every +import io.mockk.mockk import io.mockk.unmockkAll import io.mockk.verify import kotlinx.coroutines.flow.firstOrNull @@ -32,6 +37,7 @@ import org.junit.Before import org.junit.Test import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo +import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel import org.matrix.android.sdk.api.util.Optional private const val A_DEVICE_ID = "device-id" @@ -39,10 +45,14 @@ private const val A_DEVICE_ID = "device-id" class GetDeviceFullInfoUseCaseTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() + private val getCurrentSessionCrossSigningInfoUseCase = mockk() + private val getEncryptionTrustLevelForDeviceUseCase = mockk() private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions() private val getDeviceFullInfoUseCase = GetDeviceFullInfoUseCase( - activeSessionHolder = fakeActiveSessionHolder.instance + activeSessionHolder = fakeActiveSessionHolder.instance, + getCurrentSessionCrossSigningInfoUseCase = getCurrentSessionCrossSigningInfoUseCase, + getEncryptionTrustLevelForDeviceUseCase = getEncryptionTrustLevelForDeviceUseCase ) @Before @@ -57,23 +67,34 @@ class GetDeviceFullInfoUseCaseTest { @Test fun `given an active session and info for device when getting device info then the result is correct`() = runTest { + val currentSessionCrossSigningInfo = givenCurrentSessionCrossSigningInfo() val deviceInfo = DeviceInfo() fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData = MutableLiveData(Optional(deviceInfo)) fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData.givenAsFlow() val cryptoDeviceInfo = CryptoDeviceInfo(deviceId = A_DEVICE_ID, userId = "") fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData = MutableLiveData(Optional(cryptoDeviceInfo)) fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData.givenAsFlow() + val trustLevel = givenTrustLevel(currentSessionCrossSigningInfo, cryptoDeviceInfo) val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull() - deviceFullInfo shouldBeEqualTo Optional(DeviceFullInfo(deviceInfo = deviceInfo, cryptoDeviceInfo = cryptoDeviceInfo)) + deviceFullInfo shouldBeEqualTo Optional( + DeviceFullInfo( + deviceInfo = deviceInfo, + cryptoDeviceInfo = cryptoDeviceInfo, + trustLevelForShield = trustLevel + ) + ) verify { fakeActiveSessionHolder.instance.getSafeActiveSession() } + verify { getCurrentSessionCrossSigningInfoUseCase.execute() } + verify { getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo) } verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getMyDevicesInfoLive(A_DEVICE_ID).asFlow() } verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getLiveCryptoDeviceInfoWithId(A_DEVICE_ID).asFlow() } } @Test fun `given an active session and no info for device when getting device info then the result is null`() = runTest { + givenCurrentSessionCrossSigningInfo() fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData = MutableLiveData(Optional(null)) fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData.givenAsFlow() fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData = MutableLiveData(Optional(null)) @@ -96,4 +117,20 @@ class GetDeviceFullInfoUseCaseTest { deviceFullInfo shouldBeEqualTo null verify { fakeActiveSessionHolder.instance.getSafeActiveSession() } } + + private fun givenCurrentSessionCrossSigningInfo(): CurrentSessionCrossSigningInfo { + val currentSessionCrossSigningInfo = CurrentSessionCrossSigningInfo( + deviceId = A_DEVICE_ID, + isCrossSigningInitialized = true, + isCrossSigningVerified = false + ) + every { getCurrentSessionCrossSigningInfoUseCase.execute() } returns currentSessionCrossSigningInfo + return currentSessionCrossSigningInfo + } + + private fun givenTrustLevel(currentSessionCrossSigningInfo: CurrentSessionCrossSigningInfo, cryptoDeviceInfo: CryptoDeviceInfo?): RoomEncryptionTrustLevel { + val trustLevel = RoomEncryptionTrustLevel.Trusted + every { getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo) } returns trustLevel + return trustLevel + } }