BIT-2234: Delete Account Confirmation Screen (Navigation) (#1278)

This commit is contained in:
Ramsey Smith
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 4ac9d05036
commit 9648f720be
11 changed files with 702 additions and 18 deletions
@@ -26,6 +26,7 @@ import org.junit.Test
class DeleteAccountScreenTest : BaseComposeTest() {
private var onNavigateBackCalled = false
private var onNavigateToDeleteAccountConfirmationScreenCalled = false
private val mutableEventFlow = bufferedMutableSharedFlow<DeleteAccountEvent>()
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
@@ -39,6 +40,9 @@ class DeleteAccountScreenTest : BaseComposeTest() {
composeTestRule.setContent {
DeleteAccountScreen(
onNavigateBack = { onNavigateBackCalled = true },
onNavigateToDeleteAccountConfirmation = {
onNavigateToDeleteAccountConfirmationScreenCalled = true
},
viewModel = viewModel,
)
}
@@ -50,6 +54,13 @@ class DeleteAccountScreenTest : BaseComposeTest() {
assertTrue(onNavigateBackCalled)
}
@Test
@Suppress("MaxLineLength")
fun `NavigateToDeleteAccountConfirmationScreen should call onNavigateToDeleteAccountConfirmationScreenCalled`() {
mutableEventFlow.tryEmit(DeleteAccountEvent.NavigateToDeleteAccountConfirmationScreen)
assertTrue(onNavigateToDeleteAccountConfirmationScreenCalled)
}
@Test
fun `cancel click should emit CancelClick`() {
composeTestRule.onNodeWithText("Cancel").performScrollTo().performClick()
@@ -195,11 +206,16 @@ class DeleteAccountScreenTest : BaseComposeTest() {
.assertDoesNotExist()
verify {
viewModel.trySendAction(DeleteAccountAction.DeleteAccountClick(password))
viewModel.trySendAction(
DeleteAccountAction.DeleteAccountConfirmDialogClick(
password,
),
)
}
}
}
private val DEFAULT_STATE: DeleteAccountState = DeleteAccountState(
dialog = null,
isUnlockWithPasswordEnabled = true,
)
@@ -5,6 +5,8 @@ import app.cash.turbine.test
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.DeleteAccountResult
import com.x8bit.bitwarden.data.auth.repository.model.UserState
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 io.mockk.coEvery
@@ -14,18 +16,36 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class DeleteAccountViewModelTest : BaseViewModelTest() {
private val authRepo: AuthRepository = mockk(relaxed = true)
private val mutableUserStateFlow = MutableStateFlow(DEFAULT_USER_STATE)
private val authRepo: AuthRepository = mockk {
every { userStateFlow } returns mutableUserStateFlow
}
@Test
fun `initial state should be correct when not set`() {
mutableUserStateFlow.update { currentState ->
currentState.copy(
accounts = currentState.accounts.map { account ->
account.copy(
trustedDevice = account.trustedDevice?.copy(
hasMasterPassword = false,
),
)
},
)
}
val viewModel = createViewModel(state = null)
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
assertEquals(
DEFAULT_STATE.copy(isUnlockWithPasswordEnabled = false),
viewModel.stateFlow.value,
)
}
@Test
@@ -56,13 +76,18 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
}
@Test
fun `on DeleteAccountClick should update dialog state when delete account succeeds`() =
@Suppress("MaxLineLength")
fun `on DeleteAccountConfirmDialogClick should update dialog state when delete account succeeds`() =
runTest {
val viewModel = createViewModel()
val masterPassword = "ckasb kcs ja"
coEvery { authRepo.deleteAccount(masterPassword) } returns DeleteAccountResult.Success
viewModel.trySendAction(DeleteAccountAction.DeleteAccountClick(masterPassword))
viewModel.trySendAction(
DeleteAccountAction.DeleteAccountConfirmDialogClick(
masterPassword,
),
)
assertEquals(
DEFAULT_STATE.copy(dialog = DeleteAccountState.DeleteAccountDialog.DeleteSuccess),
@@ -74,13 +99,32 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
}
}
@Test
@Suppress("MaxLineLength")
fun `on DeleteAccountClick should emit NavigateToDeleteAccountConfirmationScreen`() =
runTest {
val viewModel = createViewModel(
state = DEFAULT_STATE.copy(
isUnlockWithPasswordEnabled = false,
),
)
viewModel.eventFlow.test {
viewModel.trySendAction(DeleteAccountAction.DeleteAccountClick)
assertEquals(
DeleteAccountEvent.NavigateToDeleteAccountConfirmationScreen,
awaitItem(),
)
}
}
@Test
fun `on DeleteAccountClick should update dialog state when deleteAccount fails`() = runTest {
val viewModel = createViewModel()
val masterPassword = "ckasb kcs ja"
coEvery { authRepo.deleteAccount(masterPassword) } returns DeleteAccountResult.Error
viewModel.trySendAction(DeleteAccountAction.DeleteAccountClick(masterPassword))
viewModel.trySendAction(DeleteAccountAction.DeleteAccountConfirmDialogClick(masterPassword))
assertEquals(
DEFAULT_STATE.copy(
@@ -135,6 +179,34 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
)
}
private val DEFAULT_USER_STATE: UserState = UserState(
activeUserId = "activeUserId",
accounts = listOf(
UserState.Account(
userId = "activeUserId",
name = "name",
email = "email",
avatarColorHex = "avatarColorHex",
environment = Environment.Us,
isPremium = true,
isLoggedIn = true,
isVaultUnlocked = true,
needsPasswordReset = false,
isBiometricsEnabled = false,
organizations = emptyList(),
needsMasterPassword = false,
trustedDevice = UserState.TrustedDevice(
isDeviceTrusted = true,
hasMasterPassword = true,
hasAdminApproval = true,
hasLoginApprovingDevice = true,
hasResetPasswordPermission = true,
),
),
),
)
private val DEFAULT_STATE: DeleteAccountState = DeleteAccountState(
dialog = null,
isUnlockWithPasswordEnabled = true,
)
@@ -0,0 +1,129 @@
package com.x8bit.bitwarden.ui.platform.feature.settings.accountsecurity.deleteaccountconfirmation
import androidx.compose.ui.test.filterToOne
import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.performClick
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
class DeleteAccountConfirmationScreenTest : BaseComposeTest() {
private var onNavigateBackCalled = false
private val mutableEventFlow = bufferedMutableSharedFlow<DeleteAccountConfirmationEvent>()
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
private val viewModel = mockk<DeleteAccountConfirmationViewModel>(relaxed = true) {
every { eventFlow } returns mutableEventFlow
every { stateFlow } returns mutableStateFlow
}
@Before
fun setUp() {
composeTestRule.setContent {
DeleteAccountConfirmationScreen(
onNavigateBack = { onNavigateBackCalled = true },
viewModel = viewModel,
)
}
}
@Test
fun `on NavigateBack should call onNavigateBack`() {
mutableEventFlow.tryEmit(DeleteAccountConfirmationEvent.NavigateBack)
assertTrue(onNavigateBackCalled)
}
@Test
fun `loading dialog presence should update with dialog state`() {
composeTestRule
.onAllNodesWithText("Loading")
.filterToOne(hasAnyAncestor(isDialog()))
.assertDoesNotExist()
mutableStateFlow.update {
DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Loading(),
)
}
composeTestRule
.onAllNodesWithText("Loading")
.filterToOne(hasAnyAncestor(isDialog()))
.assertExists()
}
@Test
fun `error dialog presence should update with dialog state`() {
val message = "hello world"
composeTestRule
.onAllNodesWithText(message)
.filterToOne(hasAnyAncestor(isDialog()))
.assertDoesNotExist()
mutableStateFlow.update {
DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Error(
message = message.asText(),
),
)
}
composeTestRule
.onAllNodesWithText(message)
.filterToOne(hasAnyAncestor(isDialog()))
.assertExists()
}
@Test
fun `delete success dialog presence should update with dialog state`() {
val message = "Your account has been permanently deleted"
composeTestRule
.onAllNodesWithText(message)
.filterToOne(hasAnyAncestor(isDialog()))
.assertDoesNotExist()
mutableStateFlow.update {
DEFAULT_STATE.copy(
dialog =
DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.DeleteSuccess(),
)
}
composeTestRule
.onAllNodesWithText(message)
.filterToOne(hasAnyAncestor(isDialog()))
.assertExists()
}
@Test
fun `delete success dialog dismiss should emit DeleteAccountAction`() {
mutableStateFlow.update {
DEFAULT_STATE.copy(
dialog =
DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.DeleteSuccess(),
)
}
composeTestRule
.onAllNodesWithText("Ok")
.filterToOne(hasAnyAncestor(isDialog()))
.performClick()
verify {
viewModel.trySendAction(DeleteAccountConfirmationAction.DeleteAccountAcknowledge)
}
}
}
private val DEFAULT_STATE: DeleteAccountConfirmationState =
DeleteAccountConfirmationState(dialog = null)
@@ -0,0 +1,93 @@
package com.x8bit.bitwarden.ui.platform.feature.settings.accountsecurity.deleteaccountconfirmation
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class DeleteAccountConfirmationViewModelTest : BaseViewModelTest() {
private val authRepo: AuthRepository = mockk(relaxed = true)
@Test
fun `initial state should be correct when not set`() {
val viewModel = createViewModel(state = null)
assertEquals(
DEFAULT_STATE,
viewModel.stateFlow.value,
)
}
@Test
fun `initial state should be correct when set`() {
val state = DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Error(
message = "Hello".asText(),
),
)
val viewModel = createViewModel(state = state)
assertEquals(state, viewModel.stateFlow.value)
}
@Test
fun `on CloseClick should emit NavigateBack`() = runTest {
val viewModel = createViewModel()
viewModel.eventFlow.test {
viewModel.trySendAction(DeleteAccountConfirmationAction.CloseClick)
assertEquals(DeleteAccountConfirmationEvent.NavigateBack, awaitItem())
}
}
@Test
fun `DeleteAccountAcknowledge should clear dialog and call clearPendingAccountDeletion`() =
runTest {
every { authRepo.clearPendingAccountDeletion() } just runs
val state = DEFAULT_STATE.copy(
dialog =
DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.DeleteSuccess(),
)
val viewModel = createViewModel(state = state)
viewModel.trySendAction(DeleteAccountConfirmationAction.DeleteAccountAcknowledge)
assertEquals(
DEFAULT_STATE,
viewModel.stateFlow.value,
)
verify {
authRepo.clearPendingAccountDeletion()
}
}
@Test
fun `on DismissDialog should clear dialog state`() = runTest {
val state = DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Error(
message = "Hello".asText(),
),
)
val viewModel = createViewModel(state = state)
viewModel.trySendAction(DeleteAccountConfirmationAction.DismissDialog)
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
}
private fun createViewModel(
authenticationRepository: AuthRepository = authRepo,
state: DeleteAccountConfirmationState? = null,
): DeleteAccountConfirmationViewModel = DeleteAccountConfirmationViewModel(
authRepository = authenticationRepository,
savedStateHandle = SavedStateHandle().apply { set("state", state) },
)
}
private val DEFAULT_STATE: DeleteAccountConfirmationState =
DeleteAccountConfirmationState(dialog = null)