From 35db958279e51ec2599bb84a7410995f00c5cd8c Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 14 Sep 2022 17:39:41 +0200 Subject: [PATCH] Adding unit tests for new action in DevicesViewModel --- .../settings/devices/v2/DevicesViewModel.kt | 1 - .../devices/v2/DevicesViewModelTest.kt | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt index 44dc599765..5667b1cc57 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt @@ -101,7 +101,6 @@ class DevicesViewModel @AssistedInject constructor( } } - // TODO add unit tests private fun handleVerifyCurrentSessionAction() { viewModelScope.launch { val currentSessionCanBeVerified = checkIfCurrentSessionCanBeVerifiedUseCase.execute() diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt index d9a64c82ef..18796191af 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt @@ -145,6 +145,54 @@ class DevicesViewModelTest { coVerify { refreshDevicesOnCryptoDevicesChangeUseCase.execute() } } + @Test + fun `given current session can be verified when handling verify current session action then self verification event is posted`() { + // Given + givenVerificationService() + givenCurrentSessionCrossSigningInfo() + givenDeviceFullInfoList() + givenRefreshDevicesOnCryptoDevicesChange() + val verifyCurrentSessionAction = DevicesAction.VerifyCurrentSession + coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns true + + // When + val viewModel = createViewModel() + val viewModelTest = viewModel.test() + viewModel.handle(verifyCurrentSessionAction) + + // Then + viewModelTest + .assertEvent { it is DevicesViewEvent.SelfVerification } + .finish() + coVerify { + checkIfCurrentSessionCanBeVerifiedUseCase.execute() + } + } + + @Test + fun `given current session cannot be verified when handling verify current session action then reset secrets event is posted`() { + // Given + givenVerificationService() + givenCurrentSessionCrossSigningInfo() + givenDeviceFullInfoList() + givenRefreshDevicesOnCryptoDevicesChange() + val verifyCurrentSessionAction = DevicesAction.VerifyCurrentSession + coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns false + + // When + val viewModel = createViewModel() + val viewModelTest = viewModel.test() + viewModel.handle(verifyCurrentSessionAction) + + // Then + viewModelTest + .assertEvent { it is DevicesViewEvent.PromptResetSecrets } + .finish() + coVerify { + checkIfCurrentSessionCanBeVerifiedUseCase.execute() + } + } + private fun givenVerificationService(): FakeVerificationService { val fakeVerificationService = fakeActiveSessionHolder .fakeSession