From 232ffe93cae755f7c2c82bb6dff2453d051848a6 Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 19 Mar 2024 16:38:10 -0500 Subject: [PATCH] Log user out when clicking 'not you' on trusted device screen (#1160) --- .../trusteddevice/TrustedDeviceViewModel.kt | 4 +++- .../TrustedDeviceViewModelTest.kt | 20 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModel.kt index 0e2b7fa23..b84c5be08 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModel.kt @@ -2,6 +2,7 @@ package com.x8bit.bitwarden.ui.auth.feature.trusteddevice import android.os.Parcelable import androidx.lifecycle.SavedStateHandle +import com.x8bit.bitwarden.data.auth.repository.AuthRepository import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository import com.x8bit.bitwarden.ui.platform.base.BaseViewModel import com.x8bit.bitwarden.ui.platform.base.util.Text @@ -20,6 +21,7 @@ private const val KEY_STATE = "state" class TrustedDeviceViewModel @Inject constructor( savedStateHandle: SavedStateHandle, environmentRepository: EnvironmentRepository, + private val authRepository: AuthRepository, ) : BaseViewModel( initialState = savedStateHandle[KEY_STATE] ?: run { @@ -71,7 +73,7 @@ class TrustedDeviceViewModel @Inject constructor( } private fun handleNotYouClick() { - sendEvent(TrustedDeviceEvent.ShowToast("Not yet implemented".asText())) + authRepository.logout() } } diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModelTest.kt index f55425d42..e027607aa 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/trusteddevice/TrustedDeviceViewModelTest.kt @@ -2,16 +2,25 @@ package com.x8bit.bitwarden.ui.auth.feature.trusteddevice import androidx.lifecycle.SavedStateHandle import app.cash.turbine.test +import com.x8bit.bitwarden.data.auth.repository.AuthRepository import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository import com.x8bit.bitwarden.data.platform.repository.util.FakeEnvironmentRepository import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest import com.x8bit.bitwarden.ui.platform.base.util.asText +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.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test class TrustedDeviceViewModelTest : BaseViewModelTest() { + private val authRepository: AuthRepository = mockk { + every { logout() } just runs + } private val environmentRepo: FakeEnvironmentRepository = FakeEnvironmentRepository() @Test @@ -78,18 +87,20 @@ class TrustedDeviceViewModelTest : BaseViewModelTest() { } @Test - fun `on NotYouClick emits ShowToast`() = runTest { + fun `on NotYouClick emits ShowToast`() { val viewModel = createViewModel() - viewModel.eventFlow.test { - viewModel.trySendAction(TrustedDeviceAction.NotYouClick) - assertEquals(TrustedDeviceEvent.ShowToast("Not yet implemented".asText()), awaitItem()) + viewModel.trySendAction(TrustedDeviceAction.NotYouClick) + + verify(exactly = 1) { + authRepository.logout() } } private fun createViewModel( state: TrustedDeviceState? = null, environmentRepository: EnvironmentRepository = environmentRepo, + authorizationRepository: AuthRepository = authRepository, ): TrustedDeviceViewModel = TrustedDeviceViewModel( savedStateHandle = SavedStateHandle().apply { @@ -97,6 +108,7 @@ class TrustedDeviceViewModelTest : BaseViewModelTest() { set("email_address", "email@bitwarden.com") }, environmentRepository = environmentRepository, + authRepository = authorizationRepository, ) }