Adding unit tests for reAuth needed case

This commit is contained in:
Maxime NATUREL 2022-09-27 14:48:00 +02:00
parent 68d9f672c5
commit e47bf2b200
3 changed files with 50 additions and 3 deletions

View File

@ -94,7 +94,6 @@ class SessionOverviewViewModel @AssistedInject constructor(
}
}
// TODO add unit tests
override fun handle(action: SessionOverviewAction) {
when (action) {
is SessionOverviewAction.VerifySession -> handleVerifySessionAction()
@ -128,7 +127,6 @@ class SessionOverviewViewModel @AssistedInject constructor(
_viewEvents.post(SessionOverviewViewEvent.ShowVerifyOtherSession(deviceId))
}
// TODO add unit tests
private fun handleSignoutSession() = withState { state ->
if (state.deviceInfo.invoke()?.isCurrentDevice.orFalse()) {
handleSignoutCurrentSession()

View File

@ -44,6 +44,7 @@ import io.mockk.unmockkAll
import io.mockk.verify
import io.mockk.verifyAll
import kotlinx.coroutines.flow.flowOf
import org.amshove.kluent.shouldBeEqualTo
import org.junit.After
import org.junit.Before
import org.junit.Rule
@ -53,6 +54,7 @@ import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor
import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
import org.matrix.android.sdk.api.session.uia.DefaultBaseAuth
import javax.net.ssl.HttpsURLConnection
import kotlin.coroutines.Continuation
@ -321,6 +323,31 @@ class SessionOverviewViewModelTest {
.finish()
}
@Test
fun `given another session and reAuth is needed during signout when handling signout action then requestReAuth is sent and pending auth is stored`() {
// Given
val deviceFullInfo = mockk<DeviceFullInfo>()
every { deviceFullInfo.isCurrentDevice } returns false
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID_1) } returns flowOf(deviceFullInfo)
val reAuthNeeded = givenSignoutReAuthNeeded(A_SESSION_ID_1)
val signoutAction = SessionOverviewAction.SignoutSession
givenCurrentSessionIsTrusted()
val expectedPendingAuth = DefaultBaseAuth(session = reAuthNeeded.flowResponse.session)
val expectedReAuthEvent = SessionOverviewViewEvent.RequestReAuth(reAuthNeeded.flowResponse, reAuthNeeded.errCode)
// When
val viewModel = createViewModel()
val viewModelTest = viewModel.test()
viewModel.handle(signoutAction)
// Then
viewModelTest
.assertEvent { it == expectedReAuthEvent }
.finish()
fakePendingAuthHandler.instance.pendingAuth shouldBeEqualTo expectedPendingAuth
fakePendingAuthHandler.instance.uiaContinuation shouldBeEqualTo reAuthNeeded.uiaContinuation
}
@Test
fun `given SSO auth has been done when handling ssoAuthDone action then corresponding method of pending auth handler is called`() {
// Given
@ -399,6 +426,27 @@ class SessionOverviewViewModelTest {
}
}
private fun givenSignoutReAuthNeeded(deviceId: String): SignoutSessionResult.ReAuthNeeded {
val interceptor = slot<UserInteractiveAuthInterceptor>()
val flowResponse = mockk<RegistrationFlowResponse>()
every { flowResponse.session } returns A_SESSION_ID_1
val errorCode = "errorCode"
val promise = mockk<Continuation<UIABaseAuth>>()
val reAuthNeeded = SignoutSessionResult.ReAuthNeeded(
pendingAuth = mockk(),
uiaContinuation = promise,
flowResponse = flowResponse,
errCode = errorCode,
)
every { interceptSignoutFlowResponseUseCase.execute(flowResponse, errorCode, promise) } returns reAuthNeeded
coEvery { signoutSessionUseCase.execute(deviceId, capture(interceptor)) } coAnswers {
secondArg<UserInteractiveAuthInterceptor>().performStage(flowResponse, errorCode, promise)
Result.success(Unit)
}
return reAuthNeeded
}
private fun givenSignoutError(deviceId: String, error: Throwable) {
coEvery { signoutSessionUseCase.execute(deviceId, any()) } returns Result.failure(error)
}

View File

@ -18,8 +18,9 @@ package im.vector.app.test.fakes
import im.vector.app.features.auth.PendingAuthHandler
import io.mockk.mockk
import io.mockk.spyk
class FakePendingAuthHandler {
val instance = mockk<PendingAuthHandler>()
val instance = spyk(mockk<PendingAuthHandler>())
}