From 5928987a9b4b4b293a870e41634fc592a4fedaf5 Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 13 Feb 2024 12:53:12 -0600 Subject: [PATCH] BIT-1729: Re-load the options to apply policy values (#1000) --- .../feature/generator/GeneratorViewModel.kt | 13 ++-- .../generator/GeneratorViewModelTest.kt | 68 ++++++++++++++++++- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt index ae3eea09d..2a06af5e0 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt @@ -101,10 +101,7 @@ class GeneratorViewModel @Inject constructor( init { stateFlow.onEach { savedStateHandle[KEY_STATE] = it }.launchIn(viewModelScope) - when (val selectedType = mutableStateFlow.value.selectedType) { - is Passcode -> loadPasscodeOptions(selectedType, usePolicyDefault = true) - is Username -> loadUsernameOptions(selectedType) - } + loadOptions() policyManager .getActivePoliciesFlow() .map { GeneratorAction.Internal.PasswordGeneratorPolicyReceive(it) } @@ -251,6 +248,13 @@ class GeneratorViewModel @Inject constructor( //region Generation Handlers + private fun loadOptions() { + when (val selectedType = state.selectedType) { + is Passcode -> loadPasscodeOptions(selectedType = selectedType, usePolicyDefault = true) + is Username -> loadUsernameOptions(selectedType = selectedType) + } + } + @Suppress("CyclomaticComplexMethod") private fun loadPasscodeOptions(selectedType: Passcode, usePolicyDefault: Boolean) { val passwordType = if (usePolicyDefault) { @@ -652,6 +656,7 @@ class GeneratorViewModel @Inject constructor( action: GeneratorAction.Internal.PasswordGeneratorPolicyReceive, ) { mutableStateFlow.update { it.copy(isUnderPolicy = action.policies.any()) } + loadOptions() } //endregion Generated Field Handlers diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt index 43a2845d0..ae2ec038b 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt @@ -139,10 +139,74 @@ class GeneratorViewModelTest : BaseViewModelTest() { viewModel.stateFlow.test { assertEquals(initialPasscodeState, awaitItem()) + every { + policyManager.getActivePolicies(type = PolicyTypeJson.PASSWORD_GENERATOR) + } returns policies mutablePolicyFlow.tryEmit(value = policies) - assertEquals(initialPasscodeState.copy(isUnderPolicy = true), awaitItem()) + assertEquals( + initialPasscodeState.copy( + selectedType = GeneratorState.MainType.Passcode( + selectedType = GeneratorState.MainType.Passcode.PasscodeType.Password( + length = 14, + minLength = 10, + maxLength = 128, + useCapitals = true, + capitalsEnabled = false, + useLowercase = true, + lowercaseEnabled = false, + useNumbers = true, + numbersEnabled = false, + useSpecialChars = true, + specialCharsEnabled = false, + minNumbers = 3, + minNumbersAllowed = 3, + maxNumbersAllowed = 5, + minSpecial = 3, + minSpecialAllowed = 3, + maxSpecialAllowed = 5, + avoidAmbiguousChars = false, + ambiguousCharsEnabled = true, + isUserInteracting = false, + ), + ), + isUnderPolicy = true, + ), + awaitItem(), + ) + every { + policyManager.getActivePolicies(type = PolicyTypeJson.PASSWORD_GENERATOR) + } returns emptyList() mutablePolicyFlow.tryEmit(value = emptyList()) - assertEquals(initialPasscodeState.copy(isUnderPolicy = false), awaitItem()) + assertEquals( + initialPasscodeState.copy( + selectedType = GeneratorState.MainType.Passcode( + selectedType = GeneratorState.MainType.Passcode.PasscodeType.Password( + length = 14, + minLength = 5, + maxLength = 128, + useCapitals = true, + capitalsEnabled = true, + useLowercase = true, + lowercaseEnabled = true, + useNumbers = true, + numbersEnabled = true, + useSpecialChars = true, + specialCharsEnabled = true, + minNumbers = 3, + minNumbersAllowed = 0, + maxNumbersAllowed = 5, + minSpecial = 3, + minSpecialAllowed = 0, + maxSpecialAllowed = 5, + avoidAmbiguousChars = false, + ambiguousCharsEnabled = true, + isUserInteracting = false, + ), + ), + isUnderPolicy = false, + ), + awaitItem(), + ) } }