mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 07:05:35 +03:00
BIT-2236: Trusted Device screen logs out when unauthenticated (#1253)
This commit is contained in:
parent
0bddd70d2a
commit
b81b79e215
2 changed files with 28 additions and 1 deletions
|
@ -5,6 +5,7 @@ import androidx.lifecycle.SavedStateHandle
|
|||
import androidx.lifecycle.viewModelScope
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthState
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.NewSsoUserResult
|
||||
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
|
@ -30,9 +31,10 @@ class TrustedDeviceViewModel @Inject constructor(
|
|||
) : BaseViewModel<TrustedDeviceState, TrustedDeviceEvent, TrustedDeviceAction>(
|
||||
initialState = savedStateHandle[KEY_STATE]
|
||||
?: run {
|
||||
val isAuthenticated = authRepository.authStateFlow.value is AuthState.Authenticated
|
||||
val account = authRepository.userStateFlow.value?.activeAccount
|
||||
val trustedDevice = account?.trustedDevice
|
||||
if (trustedDevice == null) authRepository.logout()
|
||||
if (trustedDevice == null || !isAuthenticated) authRepository.logout()
|
||||
TrustedDeviceState(
|
||||
dialogState = null,
|
||||
emailAddress = account?.email.orEmpty(),
|
||||
|
|
|
@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
|
|||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthState
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.NewSsoUserResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
||||
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
|
||||
|
@ -25,13 +26,35 @@ import org.junit.jupiter.api.Test
|
|||
|
||||
class TrustedDeviceViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val mutableAuthStateFlow = MutableStateFlow(DEFAULT_AUTH_STATE)
|
||||
private val mutableUserStateFlow = MutableStateFlow<UserState?>(DEFAULT_USER_STATE)
|
||||
private val authRepository: AuthRepository = mockk {
|
||||
every { authStateFlow } returns mutableAuthStateFlow
|
||||
every { userStateFlow } returns mutableUserStateFlow
|
||||
every { logout() } just runs
|
||||
}
|
||||
private val environmentRepo: FakeEnvironmentRepository = FakeEnvironmentRepository()
|
||||
|
||||
@Test
|
||||
fun `on init should logout when Uninitialized`() {
|
||||
mutableAuthStateFlow.value = AuthState.Uninitialized
|
||||
createViewModel()
|
||||
|
||||
verify(exactly = 1) {
|
||||
authRepository.logout()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on init should logout when unauthenticated`() {
|
||||
mutableAuthStateFlow.value = AuthState.Unauthenticated
|
||||
createViewModel()
|
||||
|
||||
verify(exactly = 1) {
|
||||
authRepository.logout()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on init should logout when trusted device is not present`() {
|
||||
mutableUserStateFlow.value = DEFAULT_USER_STATE.copy(
|
||||
|
@ -222,6 +245,8 @@ private val DEFAULT_STATE: TrustedDeviceState = TrustedDeviceState(
|
|||
showMasterPasswordButton = false,
|
||||
)
|
||||
|
||||
private val DEFAULT_AUTH_STATE: AuthState = AuthState.Authenticated(accessToken = "accessToken")
|
||||
|
||||
private val TRUSTED_DEVICE = UserState.TrustedDevice(
|
||||
isDeviceTrusted = false,
|
||||
hasMasterPassword = false,
|
||||
|
|
Loading…
Reference in a new issue