mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-617: Vault Password History (#935)
This commit is contained in:
committed by
Álison Fernandes
parent
46bc489f1f
commit
8156e306f5
+57
-15
@@ -1,16 +1,23 @@
|
||||
package com.x8bit.bitwarden.ui.tools.feature.generator.passwordhistory
|
||||
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotDisplayed
|
||||
import androidx.compose.ui.test.filterToOne
|
||||
import androidx.compose.ui.test.hasAnyAncestor
|
||||
import androidx.compose.ui.test.isPopup
|
||||
import androidx.compose.ui.test.onAllNodesWithText
|
||||
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
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 com.x8bit.bitwarden.ui.tools.feature.generator.model.GeneratorPasswordHistoryMode
|
||||
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.Before
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
@@ -21,7 +28,10 @@ class PasswordHistoryScreenTest : BaseComposeTest() {
|
||||
private val mutableEventFlow = bufferedMutableSharedFlow<PasswordHistoryEvent>()
|
||||
|
||||
private val mutableStateFlow = MutableStateFlow(
|
||||
PasswordHistoryState(PasswordHistoryState.ViewState.Loading),
|
||||
PasswordHistoryState(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Default,
|
||||
viewState = PasswordHistoryState.ViewState.Loading,
|
||||
),
|
||||
)
|
||||
|
||||
private val viewModel = mockk<PasswordHistoryViewModel>(relaxed = true) {
|
||||
@@ -41,16 +51,22 @@ class PasswordHistoryScreenTest : BaseComposeTest() {
|
||||
|
||||
@Test
|
||||
fun `Empty state should display no passwords message`() {
|
||||
updateState(PasswordHistoryState(PasswordHistoryState.ViewState.Empty))
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = PasswordHistoryState.ViewState.Empty,
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithText("No passwords to list.").assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Error state should display error message`() {
|
||||
val errorMessage = "Error occurred"
|
||||
updateState(
|
||||
PasswordHistoryState(PasswordHistoryState.ViewState.Error(errorMessage.asText())),
|
||||
)
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = PasswordHistoryState.ViewState.Error(errorMessage.asText()),
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithText(errorMessage).assertIsDisplayed()
|
||||
}
|
||||
|
||||
@@ -74,13 +90,13 @@ class PasswordHistoryScreenTest : BaseComposeTest() {
|
||||
@Test
|
||||
fun `clicking the Copy button should send PasswordCopyClick action`() {
|
||||
val password = PasswordHistoryState.GeneratedPassword(password = "Password", date = "Date")
|
||||
updateState(
|
||||
PasswordHistoryState(
|
||||
PasswordHistoryState.ViewState.Content(
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = PasswordHistoryState.ViewState.Content(
|
||||
passwords = listOf(password),
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(password.password).assertIsDisplayed()
|
||||
composeTestRule.onNodeWithContentDescription("Copy").performClick()
|
||||
@@ -109,18 +125,44 @@ class PasswordHistoryScreenTest : BaseComposeTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Clear button should depend on state`() {
|
||||
composeTestRule
|
||||
.onNodeWithContentDescription(label = "More")
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Clear")
|
||||
.filterToOne(hasAnyAncestor(isPopup()))
|
||||
.assertIsDisplayed()
|
||||
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
)
|
||||
}
|
||||
composeTestRule
|
||||
.onNodeWithContentDescription(label = "More")
|
||||
.assertIsNotDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Clear")
|
||||
.filterToOne(hasAnyAncestor(isPopup()))
|
||||
.assertIsNotDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Content state should display list of passwords`() {
|
||||
val passwords =
|
||||
listOf(PasswordHistoryState.GeneratedPassword(password = "Password1", date = "Date1"))
|
||||
|
||||
updateState(
|
||||
PasswordHistoryState(
|
||||
PasswordHistoryState.ViewState.Content(
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = PasswordHistoryState.ViewState.Content(
|
||||
passwords = passwords,
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText("Password1").assertIsDisplayed()
|
||||
}
|
||||
|
||||
+136
-7
@@ -1,19 +1,26 @@
|
||||
package com.x8bit.bitwarden.ui.tools.feature.generator.passwordhistory
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.bitwarden.core.CipherView
|
||||
import com.bitwarden.core.PasswordHistoryView
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardManager
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.LocalDataState
|
||||
import com.x8bit.bitwarden.data.tools.generator.repository.util.FakeGeneratorRepository
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.util.toFormattedPattern
|
||||
import com.x8bit.bitwarden.ui.tools.feature.generator.model.GeneratorPasswordHistoryMode
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.runs
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
@@ -22,10 +29,14 @@ import java.time.Instant
|
||||
|
||||
class PasswordHistoryViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val initialState = PasswordHistoryState(PasswordHistoryState.ViewState.Loading)
|
||||
private val initialState = createPasswordHistoryState()
|
||||
|
||||
private val clipboardManager: BitwardenClipboardManager = mockk()
|
||||
private val fakeGeneratorRepository = FakeGeneratorRepository()
|
||||
private val mutableVaultItemFlow = MutableStateFlow<DataState<CipherView?>>(DataState.Loading)
|
||||
private val fakeVaultRepository: VaultRepository = mockk {
|
||||
every { getVaultItemStateFlow("mockId-1") } returns mutableVaultItemFlow
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should be correct`() = runTest {
|
||||
@@ -41,7 +52,7 @@ class PasswordHistoryViewModelTest : BaseViewModelTest() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = PasswordHistoryState(PasswordHistoryState.ViewState.Loading)
|
||||
val expectedState = createPasswordHistoryState()
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
}
|
||||
@@ -55,8 +66,10 @@ class PasswordHistoryViewModelTest : BaseViewModelTest() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = PasswordHistoryState(
|
||||
PasswordHistoryState.ViewState.Error(R.string.an_error_has_occurred.asText()),
|
||||
val expectedState = createPasswordHistoryState(
|
||||
viewState = PasswordHistoryState.ViewState.Error(
|
||||
message = R.string.an_error_has_occurred.asText(),
|
||||
),
|
||||
)
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
@@ -69,7 +82,92 @@ class PasswordHistoryViewModelTest : BaseViewModelTest() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = PasswordHistoryState(PasswordHistoryState.ViewState.Empty)
|
||||
val expectedState = createPasswordHistoryState(
|
||||
viewState = PasswordHistoryState.ViewState.Empty,
|
||||
)
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when VaultRepository emits Loading state the state updates correctly`() = runTest {
|
||||
mutableVaultItemFlow.value = DataState.Loading
|
||||
val viewModel = createViewModel(
|
||||
initialState = createPasswordHistoryState(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
),
|
||||
)
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = createPasswordHistoryState(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
)
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when VaultRepository emits Error state the state updates correctly`() = runTest {
|
||||
mutableVaultItemFlow.value = DataState.Error(error = IllegalStateException())
|
||||
val viewModel = createViewModel(
|
||||
initialState = createPasswordHistoryState(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
),
|
||||
)
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = createPasswordHistoryState(
|
||||
viewState = PasswordHistoryState.ViewState.Error(
|
||||
message = R.string.an_error_has_occurred.asText(),
|
||||
),
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
)
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when VaultRepository emits Empty state the state updates correctly`() = runTest {
|
||||
mutableVaultItemFlow.value = DataState.Loaded(null)
|
||||
val viewModel = createViewModel(
|
||||
initialState = createPasswordHistoryState(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
),
|
||||
)
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = createPasswordHistoryState(
|
||||
viewState = PasswordHistoryState.ViewState.Empty,
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
)
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when VaultRepository emits Pending state the state updates correctly`() = runTest {
|
||||
mutableVaultItemFlow.value = DataState.Pending(createMockCipherView(1))
|
||||
val viewModel = createViewModel(
|
||||
initialState = createPasswordHistoryState(
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
),
|
||||
)
|
||||
|
||||
viewModel.stateFlow.test {
|
||||
val expectedState = createPasswordHistoryState(
|
||||
viewState = PasswordHistoryState.ViewState.Content(
|
||||
passwords = listOf(
|
||||
PasswordHistoryState.GeneratedPassword(
|
||||
password = "mockPassword-1",
|
||||
date = "10/27/23 6:00 AM",
|
||||
),
|
||||
),
|
||||
),
|
||||
passwordHistoryMode = GeneratorPasswordHistoryMode.Item(itemId = "mockId-1"),
|
||||
)
|
||||
val actualState = awaitItem()
|
||||
assertEquals(expectedState, actualState)
|
||||
}
|
||||
@@ -82,7 +180,7 @@ class PasswordHistoryViewModelTest : BaseViewModelTest() {
|
||||
val passwordHistoryView = PasswordHistoryView("password", Instant.now())
|
||||
fakeGeneratorRepository.storePasswordHistory(passwordHistoryView)
|
||||
|
||||
val expectedState = PasswordHistoryState(
|
||||
val expectedState = createPasswordHistoryState(
|
||||
viewState = PasswordHistoryState.ViewState.Content(
|
||||
passwords = listOf(
|
||||
PasswordHistoryState.GeneratedPassword(
|
||||
@@ -146,10 +244,41 @@ class PasswordHistoryViewModelTest : BaseViewModelTest() {
|
||||
|
||||
//region Helper Functions
|
||||
|
||||
private fun createViewModel(): PasswordHistoryViewModel = PasswordHistoryViewModel(
|
||||
private fun createViewModel(
|
||||
initialState: PasswordHistoryState = createPasswordHistoryState(),
|
||||
): PasswordHistoryViewModel = PasswordHistoryViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(state = initialState),
|
||||
clipboardManager = clipboardManager,
|
||||
generatorRepository = fakeGeneratorRepository,
|
||||
vaultRepository = fakeVaultRepository,
|
||||
)
|
||||
|
||||
private fun createPasswordHistoryState(
|
||||
viewState: PasswordHistoryState.ViewState = PasswordHistoryState.ViewState.Loading,
|
||||
passwordHistoryMode: GeneratorPasswordHistoryMode = GeneratorPasswordHistoryMode.Default,
|
||||
): PasswordHistoryState =
|
||||
PasswordHistoryState(
|
||||
viewState = viewState,
|
||||
passwordHistoryMode = passwordHistoryMode,
|
||||
)
|
||||
|
||||
private fun createSavedStateHandleWithState(
|
||||
state: PasswordHistoryState? = createPasswordHistoryState(),
|
||||
passwordHistoryMode: GeneratorPasswordHistoryMode = GeneratorPasswordHistoryMode.Default,
|
||||
) = SavedStateHandle().apply {
|
||||
set("state", state)
|
||||
set(
|
||||
"password_history_mode",
|
||||
when (passwordHistoryMode) {
|
||||
is GeneratorPasswordHistoryMode.Default -> "default"
|
||||
is GeneratorPasswordHistoryMode.Item -> "item"
|
||||
},
|
||||
)
|
||||
set(
|
||||
"password_history_id",
|
||||
(passwordHistoryMode as? GeneratorPasswordHistoryMode.Item)?.itemId,
|
||||
)
|
||||
}
|
||||
|
||||
//endregion Helper Functions
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ class VaultItemScreenTest : BaseComposeTest() {
|
||||
private var onNavigateToVaultEditItemId: String? = null
|
||||
private var onNavigateToMoveToOrganizationItemId: String? = null
|
||||
private var onNavigateToAttachmentsId: String? = null
|
||||
private var onNavigateToPasswordHistoryId: String? = null
|
||||
|
||||
private val intentManager = mockk<IntentManager>(relaxed = true)
|
||||
|
||||
@@ -80,6 +81,7 @@ class VaultItemScreenTest : BaseComposeTest() {
|
||||
onNavigateToMoveToOrganizationItemId = id
|
||||
},
|
||||
onNavigateToAttachments = { onNavigateToAttachmentsId = it },
|
||||
onNavigateToPasswordHistory = { onNavigateToPasswordHistoryId = it },
|
||||
intentManager = intentManager,
|
||||
)
|
||||
}
|
||||
@@ -107,6 +109,13 @@ class VaultItemScreenTest : BaseComposeTest() {
|
||||
assertEquals(id, onNavigateToAttachmentsId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToPasswordHistory event should invoke onNavigateToPasswordHistory`() {
|
||||
val id = "id1234"
|
||||
mutableEventFlow.tryEmit(VaultItemEvent.NavigateToPasswordHistory(itemId = id))
|
||||
assertEquals(id, onNavigateToPasswordHistoryId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on close click should send CloseClick`() {
|
||||
composeTestRule.onNodeWithContentDescription(label = "Close").performClick()
|
||||
|
||||
+13
-7
@@ -104,6 +104,8 @@ class VaultAddItemStateExtensionsTest {
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `toCipherView should transform Login ItemType to CipherView with original cipher`() {
|
||||
mockkStatic(Instant::class)
|
||||
every { Instant.now() } returns Instant.MIN
|
||||
val cipherView = DEFAULT_LOGIN_CIPHER_VIEW
|
||||
val viewState = VaultAddEditState.ViewState.Content(
|
||||
common = VaultAddEditState.ViewState.Content.Common(
|
||||
@@ -147,7 +149,7 @@ class VaultAddItemStateExtensionsTest {
|
||||
login = LoginView(
|
||||
username = "mockUsername-1",
|
||||
password = "mockPassword-1",
|
||||
passwordRevisionDate = Instant.ofEpochSecond(1_000L),
|
||||
passwordRevisionDate = Instant.MIN,
|
||||
uris = listOf(
|
||||
LoginUriView(
|
||||
uri = "mockUri-1",
|
||||
@@ -186,9 +188,13 @@ class VaultAddItemStateExtensionsTest {
|
||||
),
|
||||
),
|
||||
passwordHistory = listOf(
|
||||
PasswordHistoryView(
|
||||
password = "password",
|
||||
lastUsedDate = Instant.MIN,
|
||||
),
|
||||
PasswordHistoryView(
|
||||
password = "old_password",
|
||||
lastUsedDate = Instant.ofEpochSecond(1_000L),
|
||||
lastUsedDate = Instant.MIN,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -500,7 +506,7 @@ class VaultAddItemStateExtensionsTest {
|
||||
passwordHistory = listOf(
|
||||
PasswordHistoryView(
|
||||
password = "old_password",
|
||||
lastUsedDate = Instant.ofEpochSecond(1_000L),
|
||||
lastUsedDate = Instant.MIN,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -564,12 +570,12 @@ private val DEFAULT_BASE_CIPHER_VIEW: CipherView = CipherView(
|
||||
passwordHistory = listOf(
|
||||
PasswordHistoryView(
|
||||
password = "old_password",
|
||||
lastUsedDate = Instant.ofEpochSecond(1_000L),
|
||||
lastUsedDate = Instant.MIN,
|
||||
),
|
||||
),
|
||||
creationDate = Instant.ofEpochSecond(1_000L),
|
||||
creationDate = Instant.MIN,
|
||||
deletedDate = null,
|
||||
revisionDate = Instant.ofEpochSecond(1_000L),
|
||||
revisionDate = Instant.MIN,
|
||||
)
|
||||
|
||||
private val DEFAULT_LOGIN_CIPHER_VIEW: CipherView = DEFAULT_BASE_CIPHER_VIEW.copy(
|
||||
@@ -577,7 +583,7 @@ private val DEFAULT_LOGIN_CIPHER_VIEW: CipherView = DEFAULT_BASE_CIPHER_VIEW.cop
|
||||
login = LoginView(
|
||||
username = "username",
|
||||
password = "password",
|
||||
passwordRevisionDate = Instant.ofEpochSecond(1_000L),
|
||||
passwordRevisionDate = Instant.MIN,
|
||||
uris = listOf(
|
||||
LoginUriView(
|
||||
uri = "www.example.com",
|
||||
|
||||
Reference in New Issue
Block a user