BIT-2106: Export vault file with password (#1241)

This commit is contained in:
Caleb Derosier 2024-04-08 15:06:25 -06:00 committed by Álison Fernandes
parent ee12bd9da5
commit bf26db1d4f
2 changed files with 59 additions and 2 deletions

View file

@ -272,7 +272,13 @@ class ExportVaultViewModel @Inject constructor(
viewModelScope.launch { viewModelScope.launch {
val result = vaultRepository.exportVaultDataToString( val result = vaultRepository.exportVaultDataToString(
format = state.exportFormat.toExportFormat(state.passwordInput), format = state.exportFormat.toExportFormat(
password = if (state.exportFormat == ExportVaultFormat.JSON_ENCRYPTED) {
state.filePasswordInput
} else {
state.passwordInput
},
),
) )
sendAction( sendAction(
@ -308,8 +314,11 @@ class ExportVaultViewModel @Inject constructor(
mutableStateFlow.update { mutableStateFlow.update {
it.copy( it.copy(
confirmFilePasswordInput = "",
dialogState = null, dialogState = null,
filePasswordInput = "",
passwordInput = "", passwordInput = "",
passwordStrengthState = PasswordStrengthState.NONE,
exportData = result.vaultData, exportData = result.vaultData,
) )
} }

View file

@ -3,6 +3,7 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.exportvault
import android.net.Uri import android.net.Uri
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test import app.cash.turbine.test
import com.bitwarden.core.ExportFormat
import com.x8bit.bitwarden.R import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength
import com.x8bit.bitwarden.data.auth.repository.AuthRepository import com.x8bit.bitwarden.data.auth.repository.AuthRepository
@ -51,7 +52,7 @@ class ExportVaultViewModelTest : BaseViewModelTest() {
) )
private val vaultRepository: VaultRepository = mockk { private val vaultRepository: VaultRepository = mockk {
coEvery { exportVaultDataToString(any()) } returns mockk() coEvery { exportVaultDataToString(any()) } returns ExportVaultDataResult.Success("data")
} }
private val fileManager: FileManager = mockk() private val fileManager: FileManager = mockk()
@ -104,6 +105,53 @@ class ExportVaultViewModelTest : BaseViewModelTest() {
} }
} }
@Suppress("MaxLineLength")
@Test
fun `ConfirmExportVaultClicked should show success with valid input when export type is JSON_ENCRYPTED`() {
val filePassword = "filePassword"
val password = "password"
val initialState = DEFAULT_STATE.copy(
confirmFilePasswordInput = filePassword,
exportFormat = ExportVaultFormat.JSON_ENCRYPTED,
filePasswordInput = filePassword,
passwordInput = password,
passwordStrengthState = PasswordStrengthState.STRONG,
)
coEvery {
authRepository.getPasswordStrength(
email = EMAIL_ADDRESS,
password = password,
)
} returns PasswordStrengthResult.Success(
passwordStrength = PasswordStrength.LEVEL_4,
)
coEvery {
authRepository.validatePassword(
password = password,
)
} returns ValidatePasswordResult.Success(isValid = true)
val viewModel = createViewModel(
initialState = initialState,
)
viewModel.trySendAction(ExportVaultAction.ConfirmExportVaultClicked)
assertEquals(
initialState.copy(
confirmFilePasswordInput = "",
exportData = "data",
filePasswordInput = "",
passwordInput = "",
passwordStrengthState = PasswordStrengthState.NONE,
),
viewModel.stateFlow.value,
)
coVerify {
authRepository.validatePassword(password)
vaultRepository.exportVaultDataToString(
format = ExportFormat.EncryptedJson(filePassword),
)
}
}
@Test @Test
fun `ConfirmExportVaultClicked blank password should show an error`() { fun `ConfirmExportVaultClicked blank password should show an error`() {
val viewModel = createViewModel() val viewModel = createViewModel()