BIT-1019, BIT-1190: Add "Never Lock" vault timeout implementation (#559)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 38cd8984e9
commit 125c304d12
16 changed files with 350 additions and 28 deletions
@@ -103,6 +103,13 @@ class FakeAuthDiskSource : AuthDiskSource {
assertEquals(privateKey, storedPrivateKeys[userId])
}
/**
* Assert that the [userAutoUnlockKey] was stored successfully using the [userId].
*/
fun assertUserAutoUnlockKey(userId: String, userAutoUnlockKey: String?) {
assertEquals(userAutoUnlockKey, storedUserAutoUnlockKeys[userId])
}
/**
* Assert the the [organizationKeys] was stored successfully using the [userId].
*/
@@ -7,6 +7,9 @@ import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
import com.x8bit.bitwarden.data.auth.datasource.disk.util.FakeAuthDiskSource
import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams
import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeout
import com.x8bit.bitwarden.data.platform.util.asFailure
import com.x8bit.bitwarden.data.platform.util.asSuccess
import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
@@ -22,23 +25,167 @@ import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
@Suppress("LargeClass")
class VaultLockManagerTest {
private val fakeAuthDiskSource = FakeAuthDiskSource()
private val vaultSdkSource: VaultSdkSource = mockk {
every { clearCrypto(userId = any()) } just runs
}
private val mutableVaultTimeoutStateFlow =
MutableStateFlow<VaultTimeout>(VaultTimeout.ThirtyMinutes)
private val settingsRepository: SettingsRepository = mockk {
every { getVaultTimeoutStateFlow(any()) } returns mutableVaultTimeoutStateFlow
}
private val vaultLockManager: VaultLockManager = VaultLockManagerImpl(
authDiskSource = fakeAuthDiskSource,
vaultSdkSource = vaultSdkSource,
settingsRepository = settingsRepository,
dispatcherManager = FakeDispatcherManager(),
)
@Test
fun `vaultTimeout updates to non-Never should clear the user's auto-unlock key`() = runTest {
val userId = "mockId-1"
val userAutoUnlockKey = "userAutoUnlockKey"
// Initialize Never state
coEvery {
vaultSdkSource.getUserEncryptionKey(userId = userId)
} returns userAutoUnlockKey.asSuccess()
verifyUnlockedVault(userId = userId)
fakeAuthDiskSource.userState = MOCK_USER_STATE
mutableVaultTimeoutStateFlow.value = VaultTimeout.Never
fakeAuthDiskSource.assertUserAutoUnlockKey(
userId = userId,
userAutoUnlockKey = userAutoUnlockKey,
)
mutableVaultTimeoutStateFlow.value = VaultTimeout.ThirtyMinutes
fakeAuthDiskSource.assertUserAutoUnlockKey(
userId = userId,
userAutoUnlockKey = null,
)
}
@Suppress("MaxLineLength")
@Test
fun `vaultTimeout update to Never for an unlocked account should store the user's encrypted key`() =
runTest {
val userId = "mockId-1"
val userAutoUnlockKey = "userAutoUnlockKey"
fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery {
vaultSdkSource.getUserEncryptionKey(userId = userId)
} returns userAutoUnlockKey.asSuccess()
verifyUnlockedVault(userId = userId)
fakeAuthDiskSource.assertUserAutoUnlockKey(
userId = userId,
userAutoUnlockKey = null,
)
mutableVaultTimeoutStateFlow.value = VaultTimeout.Never
fakeAuthDiskSource.assertUserAutoUnlockKey(
userId = userId,
userAutoUnlockKey = userAutoUnlockKey,
)
}
@Suppress("MaxLineLength")
@Test
fun `vaultTimeout update to Never for a locked account when there is no stored private key should do nothing`() {
val userId = "mockId-1"
fakeAuthDiskSource.userState = MOCK_USER_STATE
assertFalse(vaultLockManager.isVaultUnlocked(userId = userId))
mutableVaultTimeoutStateFlow.value = VaultTimeout.Never
assertFalse(vaultLockManager.isVaultUnlocked(userId = userId))
}
@Suppress("MaxLineLength")
@Test
fun `vaultTimeout update to Never for a locked account when there is no stored auto-unlock key should do nothing`() {
val userId = "mockId-1"
val privateKey = "privateKey"
fakeAuthDiskSource.apply {
userState = MOCK_USER_STATE
storePrivateKey(
userId = userId,
privateKey = privateKey,
)
}
assertFalse(vaultLockManager.isVaultUnlocked(userId = userId))
mutableVaultTimeoutStateFlow.value = VaultTimeout.Never
assertFalse(vaultLockManager.isVaultUnlocked(userId = userId))
}
@Suppress("MaxLineLength")
@Test
fun `vaultTimeout update to Never for a locked account when there is a stored auto-unlock key should unlock the vault`() {
val userId = "mockId-1"
val privateKey = "privateKey"
val userAutoUnlockKey = "userAutoUnlockKey"
fakeAuthDiskSource.apply {
userState = MOCK_USER_STATE
storePrivateKey(
userId = userId,
privateKey = privateKey,
)
storeUserAutoUnlockKey(
userId = userId,
userAutoUnlockKey = userAutoUnlockKey,
)
}
coEvery {
vaultSdkSource.initializeCrypto(
userId = userId,
request = InitUserCryptoRequest(
kdfParams = MOCK_PROFILE.toSdkParams(),
email = MOCK_PROFILE.email,
privateKey = privateKey,
method = InitUserCryptoMethod.DecryptedKey(
decryptedUserKey = userAutoUnlockKey,
),
),
)
} returns InitializeCryptoResult.Success.asSuccess()
assertFalse(vaultLockManager.isVaultUnlocked(userId = userId))
mutableVaultTimeoutStateFlow.value = VaultTimeout.Never
assertTrue(vaultLockManager.isVaultUnlocked(userId = userId))
coVerify {
vaultSdkSource.initializeCrypto(
userId = userId,
request = InitUserCryptoRequest(
kdfParams = MOCK_PROFILE.toSdkParams(),
email = MOCK_PROFILE.email,
privateKey = privateKey,
method = InitUserCryptoMethod.DecryptedKey(
decryptedUserKey = userAutoUnlockKey,
),
),
)
}
}
@Test
fun `isVaultUnlocked should return the correct value based on the vault lock state`() =
runTest {
@@ -29,7 +29,7 @@ class LandingViewModelTest : BaseViewModelTest() {
every { logout(any()) } just runs
}
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVaultIfNecessary(any()) } just runs
every { lockVault(any()) } just runs
}
private val fakeEnvironmentRepository = FakeEnvironmentRepository()
@@ -98,7 +98,7 @@ class LandingViewModelTest : BaseViewModelTest() {
}
@Test
fun `LockAccountClick should call lockVaultIfNecessary for the given account`() {
fun `LockAccountClick should call lockVault for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
@@ -107,7 +107,7 @@ class LandingViewModelTest : BaseViewModelTest() {
viewModel.trySendAction(LandingAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
verify { vaultRepository.lockVault(userId = accountUserId) }
}
@Test
@@ -50,7 +50,7 @@ class LoginViewModelTest : BaseViewModelTest() {
every { logout(any()) } just runs
}
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVaultIfNecessary(any()) } just runs
every { lockVault(any()) } just runs
}
private val fakeEnvironmentRepository = FakeEnvironmentRepository()
@@ -160,7 +160,7 @@ class LoginViewModelTest : BaseViewModelTest() {
}
@Test
fun `LockAccountClick should call lockVaultIfNecessary for the given account`() {
fun `LockAccountClick should call lockVault for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
@@ -169,7 +169,7 @@ class LoginViewModelTest : BaseViewModelTest() {
viewModel.trySendAction(LoginAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
verify { vaultRepository.lockVault(userId = accountUserId) }
}
@Test
@@ -40,7 +40,7 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
every { switchAccount(any()) } returns SwitchAccountResult.AccountSwitched
}
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVaultIfNecessary(any()) } just runs
every { lockVault(any()) } just runs
}
@Test
@@ -87,8 +87,9 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
)
}
@Suppress("MaxLineLength")
@Test
fun `UserState updates with a non-null value update the account information in the state`() {
fun `UserState updates with a non-null unlocked account should not update the state`() {
val viewModel = createViewModel()
assertEquals(
DEFAULT_STATE,
@@ -111,6 +112,37 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
),
)
assertEquals(
DEFAULT_STATE,
viewModel.stateFlow.value,
)
}
@Suppress("MaxLineLength")
@Test
fun `UserState updates with a non-null locked account should update the account information in the state`() {
val viewModel = createViewModel()
assertEquals(
DEFAULT_STATE,
viewModel.stateFlow.value,
)
mutableUserStateFlow.value =
DEFAULT_USER_STATE.copy(
accounts = listOf(
UserState.Account(
userId = "activeUserId",
name = "Other User",
email = "active+test@bitwarden.com",
avatarColorHex = "#00aaaa",
environment = Environment.Us,
isPremium = true,
isVaultUnlocked = false,
organizations = emptyList(),
),
),
)
assertEquals(
DEFAULT_STATE.copy(
avatarColorString = "#00aaaa",
@@ -124,7 +156,7 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
avatarColorHex = "#00aaaa",
environmentLabel = "bitwarden.com",
isActive = true,
isVaultUnlocked = true,
isVaultUnlocked = false,
),
),
),
@@ -172,7 +204,7 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
}
@Test
fun `on LockAccountClick should call lockVaultIfNecessary for the given account`() {
fun `on LockAccountClick should call lockVault for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
@@ -181,7 +213,7 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
viewModel.trySendAction(VaultUnlockAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
verify { vaultRepository.lockVault(userId = accountUserId) }
}
@Test
@@ -82,7 +82,7 @@ class RootNavScreenTest : BaseComposeTest() {
}
// Make sure navigating to vault unlocked works as expected:
rootNavStateFlow.value = RootNavState.VaultUnlocked
rootNavStateFlow.value = RootNavState.VaultUnlocked(activeUserId = "userId")
composeTestRule.runOnIdle {
fakeNavHostController.assertLastNavigation(
route = "vault_unlocked_graph",
@@ -43,7 +43,10 @@ class RootNavViewModelTest : BaseViewModelTest() {
),
)
val viewModel = createViewModel()
assertEquals(RootNavState.VaultUnlocked, viewModel.stateFlow.value)
assertEquals(
RootNavState.VaultUnlocked(activeUserId = "activeUserId"),
viewModel.stateFlow.value,
)
}
@Test
@@ -57,7 +57,7 @@ class VaultViewModelTest : BaseViewModelTest() {
every { vaultDataStateFlow } returns mutableVaultDataStateFlow
every { sync() } returns Unit
every { lockVaultForCurrentUser() } just runs
every { lockVaultIfNecessary(any()) } just runs
every { lockVault(any()) } just runs
}
@Test
@@ -174,7 +174,7 @@ class VaultViewModelTest : BaseViewModelTest() {
}
@Test
fun `on LockAccountClick should call lockVaultIfNecessary for the given account`() {
fun `on LockAccountClick should call lockVault for the given account`() {
val accountUserId = "userId"
val accountSummary = mockk<AccountSummary> {
every { userId } returns accountUserId
@@ -184,7 +184,7 @@ class VaultViewModelTest : BaseViewModelTest() {
viewModel.trySendAction(VaultAction.LockAccountClick(accountSummary))
verify { vaultRepository.lockVaultIfNecessary(userId = accountUserId) }
verify { vaultRepository.lockVault(userId = accountUserId) }
}
@Suppress("MaxLineLength")