BIT-778: Account recovery (#905)

This commit is contained in:
Shannon Draeker
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 10471a7ea6
commit 608779ba68
14 changed files with 308 additions and 95 deletions
@@ -243,6 +243,21 @@ class AccountsServiceTest : BaseServiceTest() {
assertTrue(result.isSuccess)
}
@Test
fun `resetPassword with empty response and null current password is success`() = runTest {
val response = MockResponse().setBody("")
server.enqueue(response)
val result = service.resetPassword(
body = ResetPasswordRequestJson(
currentPasswordHash = null,
newPasswordHash = "",
passwordHint = null,
key = "",
),
)
assertTrue(result.isSuccess)
}
companion object {
private const val EMAIL = "email"
private val registerRequestBody = RegisterRequestJson(
@@ -540,6 +540,25 @@ class AuthRepositoryTest {
assertTrue(repository.hasExportVaultPoliciesEnabled)
}
@Test
fun `passwordResetReason should pull from the user's profile in AuthDiskSource`() = runTest {
val updatedProfile = ACCOUNT_1.profile.copy(
forcePasswordResetReason = ForcePasswordResetReason.WEAK_MASTER_PASSWORD_ON_LOGIN,
)
fakeAuthDiskSource.userState = UserStateJson(
activeUserId = USER_ID_1,
accounts = mapOf(
USER_ID_1 to ACCOUNT_1.copy(
profile = updatedProfile,
),
),
)
assertEquals(
ForcePasswordResetReason.WEAK_MASTER_PASSWORD_ON_LOGIN,
repository.passwordResetReason,
)
}
@Test
fun `clear Pending Account Deletion should unblock userState updates`() = runTest {
val masterPassword = "hello world"
@@ -79,7 +79,7 @@ private val GET_TOKEN_RESPONSE_SUCCESS = GetTokenResponseJson.Success(
kdfMemory = 16,
kdfParallelism = 4,
privateKey = "privateKey",
shouldForcePasswordReset = true,
shouldForcePasswordReset = false,
shouldResetMasterPassword = true,
twoFactorToken = null,
masterPasswordPolicyOptions = null,
@@ -8,6 +8,7 @@ import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import com.x8bit.bitwarden.data.auth.datasource.disk.model.ForcePasswordResetReason
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.ui.auth.feature.resetpassword.ResetPasswordAction
import com.x8bit.bitwarden.ui.auth.feature.resetpassword.ResetPasswordEvent
@@ -109,10 +110,37 @@ class ResetPasswordScreenTest : BaseComposeTest() {
}
@Test
@Suppress("MaxLineLength")
fun `password instructions should update according to state`() {
val baseString =
"One or more organization policies require your master password to meet the following requirements:"
fun `instructions text should update according to state`() {
val weakPasswordString = "Your master password does not meet one or more " +
"of your organization policies. In order to access the vault, you must " +
"update your master password now. Proceeding will log you out of your " +
"current session, requiring you to log back in. Active sessions on other " +
"devices may continue to remain active for up to one hour."
composeTestRule
.onNodeWithText(weakPasswordString)
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(
resetReason = ForcePasswordResetReason.ADMIN_FORCE_PASSWORD_RESET,
)
}
val adminChangeString =
"Your master password was recently changed by an administrator in " +
"your organization. In order to access the vault, you must update your master " +
"password now. Proceeding will log you out of your current session, " +
"requiring you to log back in. Active sessions on other devices may continue " +
"to remain active for up to one hour."
composeTestRule
.onNodeWithText(adminChangeString)
.assertIsDisplayed()
}
@Test
fun `detailed instructions should update according to state`() {
val baseString = "One or more organization policies require your master password to " +
"meet the following requirements:"
composeTestRule
.onNodeWithText(baseString)
.assertIsDisplayed()
@@ -131,6 +159,16 @@ class ResetPasswordScreenTest : BaseComposeTest() {
composeTestRule
.onNodeWithText(updatedString)
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(
resetReason = ForcePasswordResetReason.ADMIN_FORCE_PASSWORD_RESET,
)
}
composeTestRule
.onNodeWithText(baseString)
.assertDoesNotExist()
}
@Test
@@ -142,6 +180,23 @@ class ResetPasswordScreenTest : BaseComposeTest() {
}
}
@Test
fun `current password field should update according to state`() {
composeTestRule
.onNodeWithText("Current master password")
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(
resetReason = ForcePasswordResetReason.ADMIN_FORCE_PASSWORD_RESET,
)
}
composeTestRule
.onNodeWithText("Current master password")
.assertDoesNotExist()
}
@Test
fun `password input change should send PasswordInputChange action`() {
val input = "Test123"
@@ -172,6 +227,7 @@ class ResetPasswordScreenTest : BaseComposeTest() {
private val DEFAULT_STATE = ResetPasswordState(
policies = emptyList(),
resetReason = ForcePasswordResetReason.WEAK_MASTER_PASSWORD_ON_LOGIN,
dialogState = null,
currentPasswordInput = "",
passwordInput = "",
@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.ui.auth.feature.resetPassword
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.disk.model.ForcePasswordResetReason
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.ResetPasswordResult
import com.x8bit.bitwarden.data.auth.repository.model.ValidatePasswordResult
@@ -23,8 +24,9 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class ResetPasswordViewModelTest : BaseViewModelTest() {
private val authRepository: AuthRepository = mockk() {
private val authRepository: AuthRepository = mockk {
every { passwordPolicies } returns emptyList()
every { passwordResetReason } returns ForcePasswordResetReason.WEAK_MASTER_PASSWORD_ON_LOGIN
}
private val savedStateHandle = SavedStateHandle()
@@ -82,11 +84,37 @@ class ResetPasswordViewModelTest : BaseViewModelTest() {
}
@Test
fun `SubmitClicked with invalid password shows error alert`() = runTest {
fun `SubmitClicked with invalid password shows error alert for weak password reason`() =
runTest {
val password = "Test123"
coEvery {
authRepository.validatePasswordAgainstPolicies(password)
} returns false
val viewModel = createViewModel()
viewModel.trySendAction(ResetPasswordAction.PasswordInputChanged(password))
viewModel.eventFlow.test {
viewModel.trySendAction(ResetPasswordAction.SubmitClick)
assertEquals(
DEFAULT_STATE.copy(
dialogState = ResetPasswordState.DialogState.Error(
title = R.string.master_password_policy_validation_title.asText(),
message = R.string.master_password_policy_validation_message.asText(),
),
passwordInput = password,
),
viewModel.stateFlow.value,
)
}
}
@Test
fun `SubmitClicked with invalid password shows error alert for admin reset reason`() = runTest {
val password = "Test123"
coEvery {
authRepository.validatePasswordAgainstPolicies(password)
} returns false
every {
authRepository.passwordResetReason
} returns ForcePasswordResetReason.ADMIN_FORCE_PASSWORD_RESET
val viewModel = createViewModel()
viewModel.trySendAction(ResetPasswordAction.PasswordInputChanged(password))
@@ -95,9 +123,11 @@ class ResetPasswordViewModelTest : BaseViewModelTest() {
assertEquals(
DEFAULT_STATE.copy(
resetReason = ForcePasswordResetReason.ADMIN_FORCE_PASSWORD_RESET,
dialogState = ResetPasswordState.DialogState.Error(
title = R.string.master_password_policy_validation_title.asText(),
message = R.string.master_password_policy_validation_message.asText(),
title = null,
message = R.string.master_password_length_val_message_x
.asText(MIN_PASSWORD_LENGTH),
),
passwordInput = password,
),
@@ -311,8 +341,10 @@ class ResetPasswordViewModelTest : BaseViewModelTest() {
)
}
private const val MIN_PASSWORD_LENGTH = 12
private val DEFAULT_STATE = ResetPasswordState(
policies = emptyList(),
resetReason = ForcePasswordResetReason.WEAK_MASTER_PASSWORD_ON_LOGIN,
dialogState = null,
currentPasswordInput = "",
passwordInput = "",