BIT-1729: Re-load the options to apply policy values (#1000)

This commit is contained in:
David Perez 2024-02-13 12:53:12 -06:00 committed by Álison Fernandes
parent 6bfb9440b5
commit 5928987a9b
2 changed files with 75 additions and 6 deletions

View file

@ -101,10 +101,7 @@ class GeneratorViewModel @Inject constructor(
init { init {
stateFlow.onEach { savedStateHandle[KEY_STATE] = it }.launchIn(viewModelScope) stateFlow.onEach { savedStateHandle[KEY_STATE] = it }.launchIn(viewModelScope)
when (val selectedType = mutableStateFlow.value.selectedType) { loadOptions()
is Passcode -> loadPasscodeOptions(selectedType, usePolicyDefault = true)
is Username -> loadUsernameOptions(selectedType)
}
policyManager policyManager
.getActivePoliciesFlow<PolicyInformation.PasswordGenerator>() .getActivePoliciesFlow<PolicyInformation.PasswordGenerator>()
.map { GeneratorAction.Internal.PasswordGeneratorPolicyReceive(it) } .map { GeneratorAction.Internal.PasswordGeneratorPolicyReceive(it) }
@ -251,6 +248,13 @@ class GeneratorViewModel @Inject constructor(
//region Generation Handlers //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") @Suppress("CyclomaticComplexMethod")
private fun loadPasscodeOptions(selectedType: Passcode, usePolicyDefault: Boolean) { private fun loadPasscodeOptions(selectedType: Passcode, usePolicyDefault: Boolean) {
val passwordType = if (usePolicyDefault) { val passwordType = if (usePolicyDefault) {
@ -652,6 +656,7 @@ class GeneratorViewModel @Inject constructor(
action: GeneratorAction.Internal.PasswordGeneratorPolicyReceive, action: GeneratorAction.Internal.PasswordGeneratorPolicyReceive,
) { ) {
mutableStateFlow.update { it.copy(isUnderPolicy = action.policies.any()) } mutableStateFlow.update { it.copy(isUnderPolicy = action.policies.any()) }
loadOptions()
} }
//endregion Generated Field Handlers //endregion Generated Field Handlers

View file

@ -139,10 +139,74 @@ class GeneratorViewModelTest : BaseViewModelTest() {
viewModel.stateFlow.test { viewModel.stateFlow.test {
assertEquals(initialPasscodeState, awaitItem()) assertEquals(initialPasscodeState, awaitItem())
every {
policyManager.getActivePolicies(type = PolicyTypeJson.PASSWORD_GENERATOR)
} returns policies
mutablePolicyFlow.tryEmit(value = 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()) 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(),
)
} }
} }