BIT-2234: Delete Account Confirmation Screen (functionality) (#1290)

This commit is contained in:
Ramsey Smith
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent d64457aa0f
commit 7b08e1abb8
13 changed files with 567 additions and 41 deletions
@@ -47,10 +47,11 @@ class AccountsServiceTest : BaseServiceTest() {
@Test
fun `deleteAccount with empty response is success`() = runTest {
val masterPasswordHash = "37y4d8r379r4789nt387r39k3dr87nr93"
val oneTimePassword = null
val json = ""
val response = MockResponse().setBody(json)
server.enqueue(response)
assertTrue(service.deleteAccount(masterPasswordHash).isSuccess)
assertTrue(service.deleteAccount(masterPasswordHash, oneTimePassword).isSuccess)
}
@Test
@@ -595,7 +595,10 @@ class AuthRepositoryTest {
authSdkSource.hashPassword(EMAIL, masterPassword, kdf, HashPurpose.SERVER_AUTHORIZATION)
} returns hashedMasterPassword.asSuccess()
coEvery {
accountsService.deleteAccount(hashedMasterPassword)
accountsService.deleteAccount(
masterPasswordHash = hashedMasterPassword,
oneTimePassword = null,
)
} returns Unit.asSuccess()
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
@@ -603,7 +606,7 @@ class AuthRepositoryTest {
assertEquals(originalUserState, awaitItem())
// Deleting the account sets the pending deletion flag
repository.deleteAccount(password = masterPassword)
repository.deleteAccountWithMasterPassword(masterPassword = masterPassword)
// Update the account. No changes are emitted because
// the pending deletion blocks the update.
@@ -619,7 +622,7 @@ class AuthRepositoryTest {
@Test
fun `delete account fails if not logged in`() = runTest {
val masterPassword = "hello world"
val result = repository.deleteAccount(password = masterPassword)
val result = repository.deleteAccountWithMasterPassword(masterPassword = masterPassword)
assertEquals(DeleteAccountResult.Error, result)
}
@@ -632,7 +635,7 @@ class AuthRepositoryTest {
authSdkSource.hashPassword(EMAIL, masterPassword, kdf, HashPurpose.SERVER_AUTHORIZATION)
} returns Throwable("Fail").asFailure()
val result = repository.deleteAccount(password = masterPassword)
val result = repository.deleteAccountWithMasterPassword(masterPassword = masterPassword)
assertEquals(DeleteAccountResult.Error, result)
coVerify {
@@ -650,20 +653,26 @@ class AuthRepositoryTest {
authSdkSource.hashPassword(EMAIL, masterPassword, kdf, HashPurpose.SERVER_AUTHORIZATION)
} returns hashedMasterPassword.asSuccess()
coEvery {
accountsService.deleteAccount(hashedMasterPassword)
accountsService.deleteAccount(
masterPasswordHash = hashedMasterPassword,
oneTimePassword = null,
)
} returns Throwable("Fail").asFailure()
val result = repository.deleteAccount(password = masterPassword)
val result = repository.deleteAccountWithMasterPassword(masterPassword = masterPassword)
assertEquals(DeleteAccountResult.Error, result)
coVerify {
authSdkSource.hashPassword(EMAIL, masterPassword, kdf, HashPurpose.SERVER_AUTHORIZATION)
accountsService.deleteAccount(hashedMasterPassword)
accountsService.deleteAccount(
masterPasswordHash = hashedMasterPassword,
oneTimePassword = null,
)
}
}
@Test
fun `delete account succeeds`() = runTest {
fun `deleteAccountWithMasterPassword succeeds`() = runTest {
val masterPassword = "hello world"
val hashedMasterPassword = "dlrow olleh"
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
@@ -672,15 +681,45 @@ class AuthRepositoryTest {
authSdkSource.hashPassword(EMAIL, masterPassword, kdf, HashPurpose.SERVER_AUTHORIZATION)
} returns hashedMasterPassword.asSuccess()
coEvery {
accountsService.deleteAccount(hashedMasterPassword)
accountsService.deleteAccount(
masterPasswordHash = hashedMasterPassword,
oneTimePassword = null,
)
} returns Unit.asSuccess()
val result = repository.deleteAccount(password = masterPassword)
val result = repository.deleteAccountWithMasterPassword(masterPassword = masterPassword)
assertEquals(DeleteAccountResult.Success, result)
coVerify {
authSdkSource.hashPassword(EMAIL, masterPassword, kdf, HashPurpose.SERVER_AUTHORIZATION)
accountsService.deleteAccount(hashedMasterPassword)
accountsService.deleteAccount(
masterPasswordHash = hashedMasterPassword,
oneTimePassword = null,
)
}
}
@Test
fun `deleteAccountWithOneTimePassword succeeds`() = runTest {
val oneTimePassword = "123456"
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
coEvery {
accountsService.deleteAccount(
masterPasswordHash = null,
oneTimePassword = oneTimePassword,
)
} returns Unit.asSuccess()
val result = repository.deleteAccountWithOneTimePassword(
oneTimePassword = oneTimePassword,
)
assertEquals(DeleteAccountResult.Success, result)
coVerify {
accountsService.deleteAccount(
masterPasswordHash = null,
oneTimePassword = oneTimePassword,
)
}
}
@@ -81,7 +81,9 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
runTest {
val viewModel = createViewModel()
val masterPassword = "ckasb kcs ja"
coEvery { authRepo.deleteAccount(masterPassword) } returns DeleteAccountResult.Success
coEvery {
authRepo.deleteAccountWithMasterPassword(masterPassword)
} returns DeleteAccountResult.Success
viewModel.trySendAction(
DeleteAccountAction.DeleteAccountConfirmDialogClick(
@@ -95,7 +97,7 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
)
coVerify {
authRepo.deleteAccount(masterPassword)
authRepo.deleteAccountWithMasterPassword(masterPassword)
}
}
@@ -122,7 +124,9 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
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
coEvery {
authRepo.deleteAccountWithMasterPassword(masterPassword)
} returns DeleteAccountResult.Error
viewModel.trySendAction(DeleteAccountAction.DeleteAccountConfirmDialogClick(masterPassword))
@@ -136,7 +140,7 @@ class DeleteAccountViewModelTest : BaseViewModelTest() {
)
coVerify {
authRepo.deleteAccount(masterPassword)
authRepo.deleteAccountWithMasterPassword(masterPassword)
}
}
@@ -4,7 +4,10 @@ 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.onFirst
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
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
@@ -123,7 +126,52 @@ class DeleteAccountConfirmationScreenTest : BaseComposeTest() {
viewModel.trySendAction(DeleteAccountConfirmationAction.DeleteAccountAcknowledge)
}
}
@Test
fun `Delete account button click should emit DeleteAccountClick`() {
mutableStateFlow.update {
DEFAULT_STATE.copy(
verificationCode = "123456",
)
}
composeTestRule
.onNodeWithText("Delete account")
.performClick()
verify {
viewModel.trySendAction(DeleteAccountConfirmationAction.DeleteAccountClick)
}
}
@Test
fun `Resend code button click should emit ResendCodeClick`() {
composeTestRule
.onNodeWithText("Resend code")
.performClick()
verify {
viewModel.trySendAction(DeleteAccountConfirmationAction.ResendCodeClick)
}
}
@Test
fun `Verification code text input should emit VerificationCodeTextChange`() {
composeTestRule
.onAllNodesWithText("Verification code")
.onFirst()
.performTextInput("123456")
verify {
viewModel.trySendAction(
DeleteAccountConfirmationAction.VerificationCodeTextChange("123456"),
)
}
}
}
private val DEFAULT_STATE: DeleteAccountConfirmationState =
DeleteAccountConfirmationState(dialog = null)
DeleteAccountConfirmationState(
dialog = null,
verificationCode = "",
)
@@ -2,9 +2,14 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.accountsecurity.deletea
import androidx.lifecycle.SavedStateHandle
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.RequestOtpResult
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
@@ -80,6 +85,129 @@ class DeleteAccountConfirmationViewModelTest : BaseViewModelTest() {
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
}
@Test
@Suppress("MaxLineLength")
fun `on DeleteAccountClick with DeleteAccountResult Success should set dialog to Success`() =
runTest {
coEvery {
authRepo.deleteAccountWithOneTimePassword("123456")
} returns DeleteAccountResult.Success
val initialState = DEFAULT_STATE.copy(
verificationCode = "123456",
)
val viewModel = createViewModel(state = initialState)
viewModel.stateFlow.test {
assertEquals(initialState, awaitItem())
viewModel.trySendAction(
DeleteAccountConfirmationAction.DeleteAccountClick,
)
assertEquals(
initialState.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Loading(),
),
awaitItem(),
)
assertEquals(
initialState.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.DeleteSuccess(),
),
awaitItem(),
)
}
coVerify { authRepo.deleteAccountWithOneTimePassword("123456") }
}
@Test
@Suppress("MaxLineLength")
fun `on DeleteAccountClick with DeleteAccountResult Error should set dialog to Error`() =
runTest {
coEvery {
authRepo.deleteAccountWithOneTimePassword("123456")
} returns DeleteAccountResult.Error
val initialState = DEFAULT_STATE.copy(
verificationCode = "123456",
)
val viewModel = createViewModel(
state = initialState,
)
viewModel.stateFlow.test {
assertEquals(initialState, awaitItem())
viewModel.trySendAction(
DeleteAccountConfirmationAction.DeleteAccountClick,
)
assertEquals(
initialState.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Loading(),
),
awaitItem(),
)
assertEquals(
initialState.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Error(
message = R.string.generic_error_message.asText(),
),
),
awaitItem(),
)
}
coVerify { authRepo.deleteAccountWithOneTimePassword("123456") }
}
@Test
@Suppress("MaxLineLength")
fun `on ResendCodeClick with requestOneTimePasscode Success should set dialog to null`() =
runTest {
coEvery {
authRepo.requestOneTimePasscode()
} returns RequestOtpResult.Success
val viewModel = createViewModel(state = DEFAULT_STATE)
viewModel.stateFlow.test {
assertEquals(DEFAULT_STATE, awaitItem())
viewModel.trySendAction(
DeleteAccountConfirmationAction.ResendCodeClick,
)
assertEquals(
DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Loading(),
),
awaitItem(),
)
assertEquals(DEFAULT_STATE, awaitItem())
}
coVerify { authRepo.requestOneTimePasscode() }
}
@Test
@Suppress("MaxLineLength")
fun `on ResendCodeClick with requestOneTimePasscode Success should set dialog to Error`() =
runTest {
coEvery {
authRepo.requestOneTimePasscode()
} returns RequestOtpResult.Error(message = "Error")
val viewModel = createViewModel(state = DEFAULT_STATE)
viewModel.stateFlow.test {
assertEquals(DEFAULT_STATE, awaitItem())
viewModel.trySendAction(
DeleteAccountConfirmationAction.ResendCodeClick,
)
assertEquals(
DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Loading(),
),
awaitItem(),
)
assertEquals(
DEFAULT_STATE.copy(
dialog = DeleteAccountConfirmationState.DeleteAccountConfirmationDialog.Error(
message = R.string.generic_error_message.asText(),
),
),
awaitItem(),
)
}
coVerify { authRepo.requestOneTimePasscode() }
}
private fun createViewModel(
authenticationRepository: AuthRepository = authRepo,
state: DeleteAccountConfirmationState? = null,
@@ -90,4 +218,7 @@ class DeleteAccountConfirmationViewModelTest : BaseViewModelTest() {
}
private val DEFAULT_STATE: DeleteAccountConfirmationState =
DeleteAccountConfirmationState(dialog = null)
DeleteAccountConfirmationState(
dialog = null,
verificationCode = "",
)