From da3d3308b4dd7f1acb06cf03dc5a05491c6bc64d Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Mon, 26 Sep 2022 16:26:31 +0200 Subject: [PATCH] Fix unit tests in ViewModel --- .../devices/v2/DevicesViewModelTest.kt | 4 +- .../overview/SessionOverviewViewModelTest.kt | 45 ++++++++++++++----- .../app/test/fakes/FakePendingAuthHandler.kt | 25 +++++++++++ .../app/test/fakes/FakeVerificationService.kt | 14 +++++- 4 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/test/fakes/FakePendingAuthHandler.kt 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 44940dd2c3..a1b6205a77 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 @@ -217,8 +217,8 @@ class DevicesViewModelTest { .fakeSession .fakeCryptoService .fakeVerificationService - every { fakeVerificationService.addListener(any()) } just runs - every { fakeVerificationService.removeListener(any()) } just runs + fakeVerificationService.givenAddListenerSucceeds() + fakeVerificationService.givenRemoveListenerSucceeds() return fakeVerificationService } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index 857fa94ade..b9fc027a63 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -19,9 +19,14 @@ package im.vector.app.features.settings.devices.v2.overview import com.airbnb.mvrx.Success import com.airbnb.mvrx.test.MvRxTestRule import im.vector.app.features.settings.devices.v2.DeviceFullInfo -import im.vector.app.features.settings.devices.v2.IsCurrentSessionUseCase +import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase +import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase +import im.vector.app.features.settings.devices.v2.signout.SignoutSessionUseCase import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fakes.FakePendingAuthHandler +import im.vector.app.test.fakes.FakeStringProvider +import im.vector.app.test.fakes.FakeVerificationService import im.vector.app.test.test import im.vector.app.test.testDispatcher import io.mockk.coEvery @@ -46,29 +51,35 @@ class SessionOverviewViewModelTest { deviceId = A_SESSION_ID_1 ) private val fakeActiveSessionHolder = FakeActiveSessionHolder() - private val isCurrentSessionUseCase = mockk() + private val fakeStringProvider = FakeStringProvider() private val getDeviceFullInfoUseCase = mockk() private val checkIfCurrentSessionCanBeVerifiedUseCase = mockk() + private val signoutSessionUseCase = mockk() + private val interceptSignoutFlowResponseUseCase = mockk() + private val fakePendingAuthHandler = FakePendingAuthHandler() + private val refreshDevicesUseCase = mockk() private fun createViewModel() = SessionOverviewViewModel( initialState = SessionOverviewViewState(args), - activeSessionHolder = fakeActiveSessionHolder.instance, - isCurrentSessionUseCase = isCurrentSessionUseCase, + stringProvider = fakeStringProvider.instance, getDeviceFullInfoUseCase = getDeviceFullInfoUseCase, checkIfCurrentSessionCanBeVerifiedUseCase = checkIfCurrentSessionCanBeVerifiedUseCase, + signoutSessionUseCase = signoutSessionUseCase, + interceptSignoutFlowResponseUseCase = interceptSignoutFlowResponseUseCase, + pendingAuthHandler = fakePendingAuthHandler.instance, + activeSessionHolder = fakeActiveSessionHolder.instance, + refreshDevicesUseCase = refreshDevicesUseCase, ) @Test fun `given the viewModel has been initialized then viewState is updated with session info and current session verification status`() { // Given + givenVerificationService() val deviceFullInfo = mockk() 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_1, - isCurrentSession = isCurrentSession, deviceInfo = Success(deviceFullInfo), isCurrentSessionTrusted = true, ) @@ -81,7 +92,6 @@ class SessionOverviewViewModelTest { .assertLatestState { state -> state == expectedState } .finish() verify { - isCurrentSessionUseCase.execute(A_SESSION_ID_1) getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } } @@ -89,9 +99,10 @@ class SessionOverviewViewModelTest { @Test fun `given current session can be verified when handling verify current session action then self verification event is posted`() { // Given + givenVerificationService() val deviceFullInfo = mockk() + every { deviceFullInfo.isCurrentDevice } returns true 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() @@ -113,9 +124,10 @@ class SessionOverviewViewModelTest { @Test fun `given current session cannot be verified when handling verify current session action then reset secrets event is posted`() { // Given + givenVerificationService() val deviceFullInfo = mockk() + every { deviceFullInfo.isCurrentDevice } returns true 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() @@ -137,9 +149,10 @@ class SessionOverviewViewModelTest { @Test fun `given another session when handling verify session action then verify session event is posted`() { // Given + givenVerificationService() val deviceFullInfo = mockk() + every { deviceFullInfo.isCurrentDevice } returns false every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } returns flowOf(deviceFullInfo) - every { isCurrentSessionUseCase.execute(any()) } returns false val verifySessionAction = SessionOverviewAction.VerifySession givenCurrentSessionIsTrusted() @@ -154,6 +167,16 @@ class SessionOverviewViewModelTest { .finish() } + private fun givenVerificationService(): FakeVerificationService { + val fakeVerificationService = fakeActiveSessionHolder + .fakeSession + .fakeCryptoService + .fakeVerificationService + fakeVerificationService.givenAddListenerSucceeds() + fakeVerificationService.givenRemoveListenerSucceeds() + return fakeVerificationService + } + private fun givenCurrentSessionIsTrusted() { fakeActiveSessionHolder.fakeSession.givenSessionId(A_SESSION_ID_2) val deviceFullInfo = mockk() diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakePendingAuthHandler.kt b/vector/src/test/java/im/vector/app/test/fakes/FakePendingAuthHandler.kt new file mode 100644 index 0000000000..5d49dc3a77 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakePendingAuthHandler.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.test.fakes + +import im.vector.app.features.auth.PendingAuthHandler +import io.mockk.mockk + +class FakePendingAuthHandler { + + val instance = mockk() +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeVerificationService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeVerificationService.kt index 984a48b2c1..4b2dec9fc2 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeVerificationService.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeVerificationService.kt @@ -16,7 +16,19 @@ package im.vector.app.test.fakes +import io.mockk.every +import io.mockk.just import io.mockk.mockk +import io.mockk.runs import org.matrix.android.sdk.api.session.crypto.verification.VerificationService -class FakeVerificationService : VerificationService by mockk() +class FakeVerificationService : VerificationService by mockk() { + + fun givenAddListenerSucceeds() { + every { addListener(any()) } just runs + } + + fun givenRemoveListenerSucceeds() { + every { removeListener(any()) } just runs + } +}