PM-10628: Add pin unlock to SetupUnlockViewModel (#3709)

This commit is contained in:
David Perez 2024-08-09 12:09:52 -05:00 committed by GitHub
parent 9b808058f5
commit edb87202d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 73 additions and 1 deletions

View file

@ -103,7 +103,22 @@ class SetupUnlockViewModel @Inject constructor(
}
private fun handleUnlockWithPinToggle(action: SetupUnlockAction.UnlockWithPinToggle) {
// TODO: Handle pin unlocking logic PM-10628
mutableStateFlow.update {
it.copy(isUnlockWithPinEnabled = action.state.isUnlockWithPinEnabled)
}
when (val state = action.state) {
UnlockWithPinState.PendingEnabled -> Unit
UnlockWithPinState.Disabled -> settingsRepository.clearUnlockPin()
is UnlockWithPinState.Enabled -> {
settingsRepository.storeUnlockPin(
pin = state.pin,
shouldRequireMasterPasswordOnRestart =
state.shouldRequireMasterPasswordOnRestart,
)
}
}
}
private fun handleInternalActions(action: SetupUnlockAction.Internal) {

View file

@ -11,6 +11,7 @@ import com.x8bit.bitwarden.data.platform.repository.model.BiometricsKeyResult
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.components.toggle.UnlockWithPinState
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
@ -153,6 +154,62 @@ class SetupUnlockViewModelTest : BaseViewModelTest() {
}
}
@Suppress("MaxLineLength")
@Test
fun `on UnlockWithPinToggle Disabled should set pin unlock to false and clear the PIN in settings`() {
val initialState = DEFAULT_STATE.copy(isUnlockWithPinEnabled = true)
every { settingsRepository.clearUnlockPin() } just runs
val viewModel = createViewModel(state = initialState)
viewModel.trySendAction(SetupUnlockAction.UnlockWithPinToggle(UnlockWithPinState.Disabled))
assertEquals(
initialState.copy(isUnlockWithPinEnabled = false),
viewModel.stateFlow.value,
)
verify(exactly = 1) {
settingsRepository.clearUnlockPin()
}
}
@Test
fun `on UnlockWithPinToggle PendingEnabled should set pin unlock to true`() {
val initialState = DEFAULT_STATE.copy(isUnlockWithPinEnabled = false)
val viewModel = createViewModel(state = initialState)
viewModel.trySendAction(
SetupUnlockAction.UnlockWithPinToggle(UnlockWithPinState.PendingEnabled),
)
assertEquals(
initialState.copy(isUnlockWithPinEnabled = true),
viewModel.stateFlow.value,
)
}
@Suppress("MaxLineLength")
@Test
fun `on UnlockWithPinToggle Enabled should set pin unlock to true and set the PIN in settings`() {
val initialState = DEFAULT_STATE.copy(isUnlockWithPinEnabled = false)
every { settingsRepository.storeUnlockPin(any(), any()) } just runs
val viewModel = createViewModel(state = initialState)
viewModel.trySendAction(
SetupUnlockAction.UnlockWithPinToggle(
UnlockWithPinState.Enabled(
pin = "1234",
shouldRequireMasterPasswordOnRestart = true,
),
),
)
assertEquals(
initialState.copy(isUnlockWithPinEnabled = true),
viewModel.stateFlow.value,
)
verify {
settingsRepository.storeUnlockPin(
pin = "1234",
shouldRequireMasterPasswordOnRestart = true,
)
}
}
private fun createViewModel(
state: SetupUnlockState? = null,
): SetupUnlockViewModel =