Unit tests for GetCurrentSessionCrossSigningInfoUseCase

This commit is contained in:
Maxime NATUREL 2022-09-01 14:40:40 +02:00
parent 3f30cf0bee
commit 38a04c3655
4 changed files with 99 additions and 2 deletions

View file

@ -19,7 +19,6 @@ package im.vector.app.features.settings.devices
import im.vector.app.core.di.ActiveSessionHolder
import javax.inject.Inject
// TODO add unit tests
class GetCurrentSessionCrossSigningInfoUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {

View file

@ -0,0 +1,62 @@
/*
* 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
import im.vector.app.test.fakes.FakeActiveSessionHolder
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.auth.data.SessionParams
private const val A_DEVICE_ID = "device-id"
class GetCurrentSessionCrossSigningInfoUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val getCurrentSessionCrossSigningInfoUseCase = GetCurrentSessionCrossSigningInfoUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance
)
@Test
fun `given the active session when getting cross signing info then the result is correct`() = runTest {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns A_DEVICE_ID
fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams)
val isCrossSigningInitialized = true
fakeActiveSessionHolder.fakeSession
.fakeCryptoService
.fakeCrossSigningService
.givenIsCrossSigningInitializedReturns(isCrossSigningInitialized)
val isCrossSigningVerified = true
fakeActiveSessionHolder.fakeSession
.fakeCryptoService
.fakeCrossSigningService
.givenIsCrossSigningVerifiedReturns(isCrossSigningVerified)
val expectedResult = CurrentSessionCrossSigningInfo(
deviceId = A_DEVICE_ID,
isCrossSigningInitialized = isCrossSigningInitialized,
isCrossSigningVerified = isCrossSigningVerified
)
val result = getCurrentSessionCrossSigningInfoUseCase.execute()
result shouldBeEqualTo expectedResult
}
}

View file

@ -0,0 +1,32 @@
/*
* 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 io.mockk.every
import io.mockk.mockk
import org.matrix.android.sdk.api.session.crypto.crosssigning.CrossSigningService
class FakeCrossSigningService : CrossSigningService by mockk() {
fun givenIsCrossSigningInitializedReturns(isInitialized: Boolean) {
every { isCrossSigningInitialized() } returns isInitialized
}
fun givenIsCrossSigningVerifiedReturns(isVerified: Boolean) {
every { isCrossSigningVerified() } returns isVerified
}
}

View file

@ -23,13 +23,17 @@ 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.util.Optional
class FakeCryptoService : CryptoService by mockk() {
class FakeCryptoService(
val fakeCrossSigningService: FakeCrossSigningService = FakeCrossSigningService()
) : CryptoService by mockk() {
var roomKeysExport = ByteArray(size = 1)
var cryptoDeviceInfos = mutableMapOf<String, CryptoDeviceInfo>()
var cryptoDeviceInfoWithIdLiveData: MutableLiveData<Optional<CryptoDeviceInfo>> = MutableLiveData()
var myDevicesInfoWithIdLiveData: MutableLiveData<Optional<DeviceInfo>> = MutableLiveData()
override fun crossSigningService() = fakeCrossSigningService
override suspend fun exportRoomKeys(password: String) = roomKeysExport
override fun getLiveCryptoDeviceInfo() = MutableLiveData(cryptoDeviceInfos.values.toList())