diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCase.kt index b86dab13cd..60ca8e91c6 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCase.kt @@ -21,7 +21,6 @@ import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor import org.matrix.android.sdk.api.util.awaitCallback import javax.inject.Inject -// TODO add unit tests class SignoutSessionUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, ) { @@ -31,7 +30,7 @@ class SignoutSessionUseCase @Inject constructor( } private suspend fun deleteDevice(deviceId: String, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor) = runCatching { - awaitCallback<Unit> { matrixCallback -> + awaitCallback { matrixCallback -> activeSessionHolder.getActiveSession() .cryptoService() .deleteDevice(deviceId, userInteractiveAuthInterceptor, matrixCallback) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCaseTest.kt new file mode 100644 index 0000000000..5af91c16ce --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/signout/SignoutSessionUseCaseTest.kt @@ -0,0 +1,79 @@ +/* + * 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.features.settings.devices.v2.signout + +import im.vector.app.test.fakes.FakeActiveSessionHolder +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBe +import org.junit.Test +import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor + +private const val A_DEVICE_ID = "device-id" + +class SignoutSessionUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + + private val signoutSessionUseCase = SignoutSessionUseCase( + activeSessionHolder = fakeActiveSessionHolder.instance + ) + + @Test + fun `given a device id when signing out with success then success result is returned`() = runTest { + // Given + val interceptor = givenAuthInterceptor() + fakeActiveSessionHolder.fakeSession + .fakeCryptoService + .givenDeleteDeviceSucceeds(A_DEVICE_ID) + + // When + val result = signoutSessionUseCase.execute(A_DEVICE_ID, interceptor) + + // Then + result.isSuccess shouldBe true + every { + fakeActiveSessionHolder.fakeSession + .fakeCryptoService + .deleteDevice(A_DEVICE_ID, interceptor, any()) + } + } + + @Test + fun `given a device id when signing out with error then failure result is returned`() = runTest { + // Given + val interceptor = givenAuthInterceptor() + val error = mockk<Exception>() + fakeActiveSessionHolder.fakeSession + .fakeCryptoService + .givenDeleteDeviceFailsWithError(A_DEVICE_ID, error) + + // When + val result = signoutSessionUseCase.execute(A_DEVICE_ID, interceptor) + + // Then + result.isFailure shouldBe true + every { + fakeActiveSessionHolder.fakeSession + .fakeCryptoService + .deleteDevice(A_DEVICE_ID, interceptor, any()) + } + } + + private fun givenAuthInterceptor() = mockk<UserInteractiveAuthInterceptor>() +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeCryptoService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeCryptoService.kt index a83da8cb9d..3b1b4df7d1 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeCryptoService.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeCryptoService.kt @@ -67,4 +67,18 @@ class FakeCryptoService( thirdArg<MatrixCallback<Unit>>().onFailure(error) } } + + fun givenDeleteDeviceSucceeds(deviceId: String) { + val matrixCallback = slot<MatrixCallback<Unit>>() + every { deleteDevice(deviceId, any(), capture(matrixCallback)) } answers { + thirdArg<MatrixCallback<Unit>>().onSuccess(Unit) + } + } + + fun givenDeleteDeviceFailsWithError(deviceId: String, error: Exception) { + val matrixCallback = slot<MatrixCallback<Unit>>() + every { deleteDevice(deviceId, any(), capture(matrixCallback)) } answers { + thirdArg<MatrixCallback<Unit>>().onFailure(error) + } + } }