[PM-10065] Use appropriate back behavior depending on how you are take to auth approval screen (#3695)

This commit is contained in:
Dave Severns 2024-08-08 11:37:20 -04:00 committed by GitHub
parent f17289a104
commit a57a7e099c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 86 additions and 8 deletions

View file

@ -108,7 +108,7 @@ class LoginApprovalViewModel @Inject constructor(
} }
private fun handleCloseClicked() { private fun handleCloseClicked() {
closeScreen() sendClosingEvent()
} }
private fun handleDeclineRequestClicked() { private fun handleDeclineRequestClicked() {
@ -138,7 +138,7 @@ class LoginApprovalViewModel @Inject constructor(
when (action.result) { when (action.result) {
is AuthRequestResult.Success -> { is AuthRequestResult.Success -> {
sendEvent(LoginApprovalEvent.ShowToast(R.string.login_approved.asText())) sendEvent(LoginApprovalEvent.ShowToast(R.string.login_approved.asText()))
sendEvent(LoginApprovalEvent.NavigateBack) sendClosingEvent()
} }
is AuthRequestResult.Error -> { is AuthRequestResult.Error -> {
@ -184,7 +184,7 @@ class LoginApprovalViewModel @Inject constructor(
AuthRequestUpdatesResult.Declined, AuthRequestUpdatesResult.Declined,
AuthRequestUpdatesResult.Expired, AuthRequestUpdatesResult.Expired,
-> { -> {
closeScreen() sendClosingEvent()
} }
} }
} }
@ -195,7 +195,7 @@ class LoginApprovalViewModel @Inject constructor(
when (action.result) { when (action.result) {
is AuthRequestResult.Success -> { is AuthRequestResult.Success -> {
sendEvent(LoginApprovalEvent.ShowToast(R.string.log_in_denied.asText())) sendEvent(LoginApprovalEvent.ShowToast(R.string.log_in_denied.asText()))
sendEvent(LoginApprovalEvent.NavigateBack) sendClosingEvent()
} }
is AuthRequestResult.Error -> { is AuthRequestResult.Error -> {
@ -206,12 +206,14 @@ class LoginApprovalViewModel @Inject constructor(
} }
} }
private fun closeScreen() { private fun sendClosingEvent() {
if (state.specialCircumstance?.shouldFinishWhenComplete == true) { val event = if (state.specialCircumstance?.shouldFinishWhenComplete == true) {
sendEvent(LoginApprovalEvent.ExitApp) LoginApprovalEvent.ExitApp
} else { } else {
sendEvent(LoginApprovalEvent.NavigateBack) LoginApprovalEvent.NavigateBack
} }
sendEvent(event)
} }
} }

View file

@ -189,6 +189,44 @@ class LoginApprovalViewModelTest : BaseViewModelTest() {
} }
} }
@Test
@Suppress("MaxLineLength")
fun `When approval request is successful, should emit ExitApp when shouldFinishWhenComplete is true`() =
runTest {
val specialCircumstance = SpecialCircumstance.PasswordlessRequest(
passwordlessRequestData = PasswordlessRequestData(
loginRequestId = REQUEST_ID,
userId = USER_ID,
),
shouldFinishWhenComplete = true,
)
every {
mockSpecialCircumstanceManager.specialCircumstance
} returns specialCircumstance
val viewModel = createViewModel(
state = DEFAULT_STATE.copy(
specialCircumstance = specialCircumstance,
),
)
coEvery {
mockAuthRepository.updateAuthRequest(
requestId = REQUEST_ID,
masterPasswordHash = PASSWORD_HASH,
publicKey = PUBLIC_KEY,
isApproved = true,
)
} returns AuthRequestResult.Success(AUTH_REQUEST)
viewModel.eventFlow.test {
viewModel.trySendAction(LoginApprovalAction.ApproveRequestClick)
assertEquals(
LoginApprovalEvent.ShowToast(R.string.login_approved.asText()),
awaitItem(),
)
assertEquals(LoginApprovalEvent.ExitApp, awaitItem())
}
}
@Test @Test
fun `on DeclineRequestClick should deny auth request`() = runTest { fun `on DeclineRequestClick should deny auth request`() = runTest {
val viewModel = createViewModel() val viewModel = createViewModel()
@ -220,6 +258,44 @@ class LoginApprovalViewModelTest : BaseViewModelTest() {
} }
} }
@Test
@Suppress("MaxLineLength")
fun `When deny request is successful, should emit ExitApp when shouldFinishWhenComplete is true`() =
runTest {
val specialCircumstance = SpecialCircumstance.PasswordlessRequest(
passwordlessRequestData = PasswordlessRequestData(
loginRequestId = REQUEST_ID,
userId = USER_ID,
),
shouldFinishWhenComplete = true,
)
every {
mockSpecialCircumstanceManager.specialCircumstance
} returns specialCircumstance
val viewModel = createViewModel(
state = DEFAULT_STATE.copy(
specialCircumstance = specialCircumstance,
),
)
coEvery {
mockAuthRepository.updateAuthRequest(
requestId = REQUEST_ID,
masterPasswordHash = PASSWORD_HASH,
publicKey = PUBLIC_KEY,
isApproved = false,
)
} returns AuthRequestResult.Success(AUTH_REQUEST)
viewModel.eventFlow.test {
viewModel.trySendAction(LoginApprovalAction.DeclineRequestClick)
assertEquals(
LoginApprovalEvent.ShowToast(R.string.log_in_denied.asText()),
awaitItem(),
)
assertEquals(LoginApprovalEvent.ExitApp, awaitItem())
}
}
@Test @Test
fun `on ErrorDialogDismiss should update state`() = runTest { fun `on ErrorDialogDismiss should update state`() = runTest {
val viewModel = createViewModel() val viewModel = createViewModel()