BIT-2235: Add support for exporting vault data w/o passcode (#1281)

This commit is contained in:
Caleb Derosier
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 9648f720be
commit dae98111e6
4 changed files with 281 additions and 55 deletions
@@ -247,12 +247,55 @@ class ExportVaultScreenTest : BaseComposeTest() {
}
}
@Test
fun `send code click should send SendCodeClick action`() {
mutableStateFlow.update {
it.copy(showSendCodeButton = true)
}
composeTestRule.onNodeWithText("Send code").performClick()
verify {
viewModel.trySendAction(ExportVaultAction.SendCodeClick)
}
}
@Test
fun `verification code input change should send PasswordInputChange action`() {
val input = "123"
mutableStateFlow.update {
it.copy(showSendCodeButton = true)
}
composeTestRule.onNodeWithText("Verification code").performTextInput(input)
verify {
viewModel.trySendAction(ExportVaultAction.PasswordInputChanged(input))
}
}
@Test
fun `send code button and verification input field should appear based on state`() {
composeTestRule.onNodeWithText("Send code").assertIsNotDisplayed()
composeTestRule.onNodeWithText("Verification code").assertIsNotDisplayed()
mutableStateFlow.update {
it.copy(showSendCodeButton = true)
}
composeTestRule.onNodeWithText("Send code").assertIsDisplayed()
composeTestRule.onNodeWithText("Verification code").assertIsDisplayed()
}
@Test
fun `master password field should appear based on state`() {
composeTestRule.onNodeWithText("Master password").assertIsDisplayed()
mutableStateFlow.update {
it.copy(showSendCodeButton = true)
}
composeTestRule.onNodeWithText("Master password").assertIsNotDisplayed()
}
@Test
fun `password input change should send PasswordInputChange action`() {
val input = "Test123"
composeTestRule.onNodeWithText("Master password").performTextInput(input)
verify {
viewModel.trySendAction(ExportVaultAction.PasswordInputChanged("Test123"))
viewModel.trySendAction(ExportVaultAction.PasswordInputChanged(input))
}
}
}
@@ -267,4 +310,5 @@ private val DEFAULT_STATE = ExportVaultState(
exportData = "",
passwordStrengthState = PasswordStrengthState.NONE,
policyPreventsExport = false,
showSendCodeButton = false,
)
@@ -8,8 +8,10 @@ import com.x8bit.bitwarden.R
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.model.PasswordStrengthResult
import com.x8bit.bitwarden.data.auth.repository.model.RequestOtpResult
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.auth.repository.model.ValidatePasswordResult
import com.x8bit.bitwarden.data.auth.repository.model.VerifyOtpResult
import com.x8bit.bitwarden.data.platform.manager.PolicyManager
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.vault.datasource.network.model.PolicyTypeJson
@@ -105,6 +107,66 @@ class ExportVaultViewModelTest : BaseViewModelTest() {
}
}
@Test
fun `ConfirmExportVaultClicked with verified code should call exportVaultDataToString`() {
val passcode = "1234"
val initialState = DEFAULT_STATE.copy(
passwordInput = passcode,
showSendCodeButton = true,
)
coEvery {
authRepository.verifyOneTimePasscode(
oneTimePasscode = passcode,
)
} returns VerifyOtpResult.Verified
val viewModel = createViewModel(initialState)
viewModel.trySendAction(ExportVaultAction.ConfirmExportVaultClicked)
assertEquals(
initialState.copy(
exportData = "data",
passwordInput = "",
),
viewModel.stateFlow.value,
)
coVerify {
vaultRepository.exportVaultDataToString(any())
}
}
@Test
fun `ConfirmExportVaultClicked with invalid code should call exportVaultDataToString`() {
val passcode = "1234"
val initialState = DEFAULT_STATE.copy(
passwordInput = passcode,
showSendCodeButton = true,
)
coEvery {
authRepository.verifyOneTimePasscode(
oneTimePasscode = passcode,
)
} returns VerifyOtpResult.NotVerified("Wrong")
val viewModel = createViewModel(initialState)
viewModel.trySendAction(ExportVaultAction.ConfirmExportVaultClicked)
assertEquals(
initialState.copy(
dialogState = ExportVaultState.DialogState.Error(
title = R.string.an_error_has_occurred.asText(),
message = R.string.generic_error_message.asText(),
),
),
viewModel.stateFlow.value,
)
coVerify(exactly = 0) {
vaultRepository.exportVaultDataToString(any())
}
}
@Suppress("MaxLineLength")
@Test
fun `ConfirmExportVaultClicked should show success with valid input when export type is JSON_ENCRYPTED`() {
@@ -415,6 +477,19 @@ class ExportVaultViewModelTest : BaseViewModelTest() {
)
}
@Test
fun `SendCodeClick should call requestOneTimePasscode`() {
val viewModel = createViewModel()
coEvery { authRepository.requestOneTimePasscode() } returns RequestOtpResult.Success
viewModel.trySendAction(ExportVaultAction.SendCodeClick)
assertEquals(
DEFAULT_STATE,
viewModel.stateFlow.value,
)
coVerify { authRepository.requestOneTimePasscode() }
}
@Test
fun `ReceiveExportVaultDataToStringResult should update state to error if result is error`() {
val viewModel = createViewModel()
@@ -659,4 +734,5 @@ private val DEFAULT_STATE = ExportVaultState(
exportData = null,
passwordStrengthState = PasswordStrengthState.NONE,
policyPreventsExport = false,
showSendCodeButton = false,
)