mirror of
https://github.com/bitwarden/android.git
synced 2025-03-15 18:58:59 +03:00
BIT-467: Implement Lock Now functionality (#327)
This commit is contained in:
parent
a7dc5fe08f
commit
c72053f935
5 changed files with 50 additions and 7 deletions
|
@ -53,6 +53,11 @@ interface VaultRepository {
|
|||
*/
|
||||
fun getVaultFolderStateFlow(folderId: String): StateFlow<DataState<FolderView?>>
|
||||
|
||||
/**
|
||||
* Locks the vault for the current user if currently unlocked.
|
||||
*/
|
||||
fun lockVaultForCurrentUser()
|
||||
|
||||
/**
|
||||
* Locks the vault for the user with the given [userId] if necessary.
|
||||
*/
|
||||
|
|
|
@ -159,6 +159,12 @@ class VaultRepositoryImpl constructor(
|
|||
initialValue = DataState.Loading,
|
||||
)
|
||||
|
||||
override fun lockVaultForCurrentUser() {
|
||||
authDiskSource.userState?.activeUserId?.let {
|
||||
lockVaultIfNecessary(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun lockVaultIfNecessary(userId: String) {
|
||||
setVaultToLocked(userId = userId)
|
||||
}
|
||||
|
|
|
@ -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.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.Text
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
|
@ -24,6 +25,7 @@ private const val KEY_STATE = "state"
|
|||
@HiltViewModel
|
||||
class AccountSecurityViewModel @Inject constructor(
|
||||
private val authRepository: AuthRepository,
|
||||
private val vaultRepository: VaultRepository,
|
||||
savedStateHandle: SavedStateHandle,
|
||||
) : BaseViewModel<AccountSecurityState, AccountSecurityEvent, AccountSecurityAction>(
|
||||
initialState = savedStateHandle[KEY_STATE]
|
||||
|
@ -99,8 +101,7 @@ class AccountSecurityViewModel @Inject constructor(
|
|||
}
|
||||
|
||||
private fun handleLockNowClick() {
|
||||
// TODO BIT-467: Lock the app
|
||||
sendEvent(AccountSecurityEvent.ShowToast("Lock the app.".asText()))
|
||||
vaultRepository.lockVaultForCurrentUser()
|
||||
}
|
||||
|
||||
private fun handleLoginRequestToggle(action: AccountSecurityAction.LoginRequestToggle) {
|
||||
|
|
|
@ -469,6 +469,31 @@ class VaultRepositoryTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `lockVaultForCurrentUser should lock the vault for the current user if it is currently unlocked`() =
|
||||
runTest {
|
||||
fakeAuthDiskSource.userState = MOCK_USER_STATE
|
||||
val userId = "mockId-1"
|
||||
verifyUnlockedVault(userId = userId)
|
||||
|
||||
assertEquals(
|
||||
VaultState(
|
||||
unlockedVaultUserIds = setOf(userId),
|
||||
),
|
||||
vaultRepository.vaultStateFlow.value,
|
||||
)
|
||||
|
||||
vaultRepository.lockVaultForCurrentUser()
|
||||
|
||||
assertEquals(
|
||||
VaultState(
|
||||
unlockedVaultUserIds = emptySet(),
|
||||
),
|
||||
vaultRepository.vaultStateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `lockVaultIfNecessary should lock the given account if it is currently unlocked`() =
|
||||
runTest {
|
||||
|
|
|
@ -3,10 +3,13 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.accountsecurity
|
|||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
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
|
||||
|
@ -80,12 +83,13 @@ class AccountSecurityViewModelTest : BaseViewModelTest() {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `on LockNowClick should emit ShowToast`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(AccountSecurityAction.LockNowClick)
|
||||
assertEquals(AccountSecurityEvent.ShowToast("Lock the app.".asText()), awaitItem())
|
||||
fun `on LockNowClick should call lockVaultForCurrentUser`() {
|
||||
val vaultRepository = mockk<VaultRepository>(relaxed = true) {
|
||||
every { lockVaultForCurrentUser() } just runs
|
||||
}
|
||||
val viewModel = createViewModel(vaultRepository = vaultRepository)
|
||||
viewModel.trySendAction(AccountSecurityAction.LockNowClick)
|
||||
verify { vaultRepository.lockVaultForCurrentUser() }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -230,11 +234,13 @@ class AccountSecurityViewModelTest : BaseViewModelTest() {
|
|||
|
||||
private fun createViewModel(
|
||||
authRepository: AuthRepository = mockk(relaxed = true),
|
||||
vaultRepository: VaultRepository = mockk(relaxed = true),
|
||||
savedStateHandle: SavedStateHandle = SavedStateHandle().apply {
|
||||
set("state", DEFAULT_STATE)
|
||||
},
|
||||
): AccountSecurityViewModel = AccountSecurityViewModel(
|
||||
authRepository = authRepository,
|
||||
vaultRepository = vaultRepository,
|
||||
savedStateHandle = savedStateHandle,
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue