Improve rename session use case and add tests

This commit is contained in:
Maxime NATUREL 2022-09-20 14:48:37 +02:00
parent d7afea7b3a
commit 4a3966d4f5
3 changed files with 122 additions and 7 deletions

View file

@ -17,21 +17,28 @@
package im.vector.app.features.settings.devices.v2.rename package im.vector.app.features.settings.devices.v2.rename
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.extensions.andThen
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import org.matrix.android.sdk.api.util.awaitCallback import org.matrix.android.sdk.api.util.awaitCallback
import javax.inject.Inject import javax.inject.Inject
// TODO add unit tests
class RenameSessionUseCase @Inject constructor( class RenameSessionUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder, private val activeSessionHolder: ActiveSessionHolder,
private val refreshDevicesUseCase: RefreshDevicesUseCase,
) { ) {
suspend fun execute(deviceId: String, newName: String): Result<Unit> { suspend fun execute(deviceId: String, newName: String): Result<Unit> {
return runCatching { return renameDevice(deviceId, newName)
awaitCallback<Unit> { matrixCallback -> .andThen { refreshDevices() }
activeSessionHolder.getActiveSession() }
.cryptoService()
.setDeviceName(deviceId, newName, matrixCallback) private suspend fun renameDevice(deviceId: String, newName: String) = runCatching {
} awaitCallback<Unit> { matrixCallback ->
activeSessionHolder.getActiveSession()
.cryptoService()
.setDeviceName(deviceId, newName, matrixCallback)
} }
} }
private fun refreshDevices() = runCatching { refreshDevicesUseCase.execute() }
} }

View file

@ -0,0 +1,91 @@
/*
* 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.rename
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
private const val A_DEVICE_ID = "device-id"
private const val A_DEVICE_NAME = "device-name"
class RenameSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val refreshDevicesUseCase = mockk<RefreshDevicesUseCase>()
private val renameSessionUseCase = RenameSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
refreshDevicesUseCase = refreshDevicesUseCase
)
@Test
fun `given a device id and a new name when no error during rename then the device is renamed with success`() = runTest {
// Given
fakeActiveSessionHolder.fakeSession.fakeCryptoService.givenSetDeviceNameSucceeds()
every { refreshDevicesUseCase.execute() } just runs
// When
val result = renameSessionUseCase.execute(A_DEVICE_ID, A_DEVICE_NAME)
// Then
result.isSuccess shouldBe true
verify {
fakeActiveSessionHolder.fakeSession
.cryptoService()
.setDeviceName(A_DEVICE_ID, A_DEVICE_NAME, any())
refreshDevicesUseCase.execute()
}
}
@Test
fun `given a device id and a new name when an error occurs during rename then result is failure`() = runTest {
// Given
val error = Exception()
fakeActiveSessionHolder.fakeSession.fakeCryptoService.givenSetDeviceNameFailsWithError(error)
// When
val result = renameSessionUseCase.execute(A_DEVICE_ID, A_DEVICE_NAME)
// Then
result.isFailure shouldBe true
result.exceptionOrNull() shouldBeEqualTo error
}
@Test
fun `given a device id and a new name when an error occurs during devices refresh then result is failure`() = runTest {
// Given
val error = Exception()
fakeActiveSessionHolder.fakeSession.fakeCryptoService.givenSetDeviceNameSucceeds()
every { refreshDevicesUseCase.execute() } throws error
// When
val result = renameSessionUseCase.execute(A_DEVICE_ID, A_DEVICE_NAME)
// Then
result.isFailure shouldBe true
result.exceptionOrNull() shouldBeEqualTo error
}
}

View file

@ -17,7 +17,10 @@
package im.vector.app.test.fakes package im.vector.app.test.fakes
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.slot
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.crypto.CryptoService import org.matrix.android.sdk.api.session.crypto.CryptoService
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo 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.DeviceInfo
@ -50,4 +53,18 @@ class FakeCryptoService(
override fun getLiveCryptoDeviceInfoWithId(deviceId: String) = cryptoDeviceInfoWithIdLiveData override fun getLiveCryptoDeviceInfoWithId(deviceId: String) = cryptoDeviceInfoWithIdLiveData
override fun getMyDevicesInfoLive(deviceId: String) = myDevicesInfoWithIdLiveData override fun getMyDevicesInfoLive(deviceId: String) = myDevicesInfoWithIdLiveData
fun givenSetDeviceNameSucceeds() {
val matrixCallback = slot<MatrixCallback<Unit>>()
every { setDeviceName(any(), any(), capture(matrixCallback)) } answers {
thirdArg<MatrixCallback<Unit>>().onSuccess(Unit)
}
}
fun givenSetDeviceNameFailsWithError(error: Exception) {
val matrixCallback = slot<MatrixCallback<Unit>>()
every { setDeviceName(any(), any(), capture(matrixCallback)) } answers {
thirdArg<MatrixCallback<Unit>>().onFailure(error)
}
}
} }